]> gitweb.factorcode.org Git - factor.git/blob - extra/terrain/terrain.factor
scryfall: add more filter/reject words, better mtga parser
[factor.git] / extra / terrain / terrain.factor
1 ! Copyright (C) 2009 Joe Groff, Doug Coleman.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays combinators combinators.short-circuit
4 destructors game.input game.input.scancodes game.loop
5 game.worlds grid-meshes grouping kernel literals math
6 math.functions math.matrices.simd math.order
7 math.vectors math.vectors.simd noise opengl
8 opengl.capabilities opengl.gl opengl.shaders opengl.textures
9 sequences specialized-arrays terrain.generation terrain.shaders
10 typed ui ui.gadgets.worlds ui.gestures ui.pixel-formats ;
11 QUALIFIED-WITH: alien.c-types c
12 SPECIALIZED-ARRAY: c:float
13 IN: terrain
14
15 CONSTANT: FOV $[ 2.0 sqrt 1 + ]
16 CONSTANT: NEAR-PLANE 1/1024.
17 CONSTANT: FAR-PLANE 2.0
18 CONSTANT: PLAYER-START-LOCATION float-4{ 0.5 0.51 0.5 1.0 }
19 CONSTANT: VELOCITY-MODIFIER-NORMAL float-4{ 1.0 1.0 1.0 0.0 }
20 CONSTANT: VELOCITY-MODIFIER-FAST float-4{ 2.0 1.0 2.0 0.0 }
21 CONSTANT: BOUNCE float-4{ 1.0 -0.2 1.0 1.0 }
22 CONSTANT: PLAYER-HEIGHT 1/256.
23 CONSTANT: GRAVITY float-4{ 0.0 -1/8192. 0.0 0.0 }
24 CONSTANT: JUMP 1/2048.
25 CONSTANT: MOUSE-SCALE 1/20.
26 CONSTANT: MOVEMENT-SPEED 1/32768.
27 CONSTANT: FRICTION float-4{ 0.97 0.995 0.97 1.0 }
28 CONSTANT: COMPONENT-SCALE float-4{ 0.5 0.01 0.0005 0.0 }
29 CONSTANT: SKY-PERIOD 2400
30 CONSTANT: SKY-SPEED 0.00025
31
32 CONSTANT: terrain-vertex-size { 512 512 }
33
34 TUPLE: player
35     { location float-4 }
36     { yaw float }
37     { pitch float }
38     { velocity float-4 }
39     { velocity-modifier float-4 }
40     reverse-time ;
41
42 TUPLE: terrain-world < game-world
43     { player player }
44     sky-image sky-texture sky-program
45     terrain terrain-segment terrain-texture terrain-program
46     terrain-mesh
47     history ;
48
49 : <player> ( -- player )
50     player new
51         PLAYER-START-LOCATION >>location
52         0.0 >>yaw
53         0.0 >>pitch
54         float-4{ 0.0 0.0 0.0 1.0 } >>velocity
55         VELOCITY-MODIFIER-NORMAL >>velocity-modifier ;
56
57 : frustum ( dim -- -x x -y y near far )
58     dup first2 min v/n
59     NEAR-PLANE FOV / v*n first2 [ [ neg ] keep ] bi@
60     NEAR-PLANE FAR-PLANE ;
61
62 : set-modelview-matrix ( gadget -- )
63     GL_DEPTH_BUFFER_BIT glClear
64     GL_MODELVIEW glMatrixMode
65     glLoadIdentity
66     player>>
67     [ pitch>> 1.0 0.0 0.0 glRotatef ]
68     [ yaw>> 0.0 1.0 0.0 glRotatef ]
69     [ location>> vneg first3 glTranslatef ] tri ;
70
71 TYPED: eye-rotate ( yaw: float pitch: float v: float-4 -- v': float-4 )
72     [ float-4{  0.0 -1.0 0.0 0.0 } swap deg>rad rotation-matrix4 ]
73     [ float-4{ -1.0  0.0 0.0 0.0 } swap deg>rad rotation-matrix4 m4. ]
74     [ m4.v ] tri* float-4{ t t t f } vand ;
75
76 : forward-vector ( player -- v )
77     yaw>> 0.0
78     float-4{ 0.0 0.0 $ MOVEMENT-SPEED 1.0 } vneg eye-rotate ; inline
79 : rightward-vector ( player -- v )
80     yaw>> 0.0
81     float-4{ $ MOVEMENT-SPEED 0.0 0.0 1.0 } eye-rotate ; inline
82 : clamp-pitch ( pitch -- pitch' )
83     -90.0 90.0 clamp ; inline
84
85 : walk-forward ( player -- )
86     dup forward-vector [ v+ ] curry change-velocity drop ; inline
87 : walk-backward ( player -- )
88     dup forward-vector [ v- ] curry change-velocity drop ; inline
89 : walk-leftward ( player -- )
90     dup rightward-vector [ v- ] curry change-velocity drop ; inline
91 : walk-rightward ( player -- )
92     dup rightward-vector [ v+ ] curry change-velocity drop ; inline
93 : jump ( player -- )
94     [ float-4{ 0.0 $ JUMP 0.0 0.0 } v+ ] change-velocity drop ; inline
95 : rotate-leftward ( player x -- )
96     [ - ] curry change-yaw drop ; inline
97 : rotate-rightward ( player x -- )
98     [ + ] curry change-yaw drop ; inline
99 : look-horizontally ( player x -- )
100     [ + ] curry change-yaw drop ; inline
101 : look-vertically ( player x -- )
102     [ + clamp-pitch ] curry change-pitch drop ; inline
103
104
105 : rotate-with-mouse ( player mouse -- )
106     [ dx>> MOUSE-SCALE * look-horizontally ]
107     [ dy>> MOUSE-SCALE * look-vertically ] 2bi ;
108
109
110 terrain-world H{
111     { T{ key-down { mods { A+ } } { sym "RET" } } [ toggle-fullscreen ] }
112 } set-gestures
113
114 :: handle-input ( world -- )
115     world player>> :> player
116     read-keyboard keys>> :> keys
117
118     key-left-shift keys nth
119     VELOCITY-MODIFIER-FAST VELOCITY-MODIFIER-NORMAL ? player velocity-modifier<<
120
121     {
122         [ key-1 keys nth 1  f ? ]
123         [ key-2 keys nth 2  f ? ]
124         [ key-3 keys nth 3  f ? ]
125         [ key-4 keys nth 4  f ? ]
126         [ key-5 keys nth 10000 f ? ]
127     } 0|| player reverse-time<<
128
129     key-w keys nth [ player walk-forward ] when
130     key-s keys nth [ player walk-backward ] when
131     key-a keys nth [ player walk-leftward ] when
132     key-d keys nth [ player walk-rightward ] when
133     key-q keys nth [ player -1 look-horizontally ] when
134     key-e keys nth [ player 1 look-horizontally ] when
135     key-left-arrow keys nth [ player -1 look-horizontally ] when
136     key-right-arrow keys nth [ player 1 look-horizontally ] when
137     key-down-arrow keys nth [ player 1 look-vertically ] when
138     key-up-arrow keys nth [ player -1 look-vertically ] when
139     key-space keys nth [ player jump ] when
140     key-escape keys nth [ world close-window ] when
141     player read-mouse rotate-with-mouse
142     reset-mouse ;
143
144 : apply-friction ( velocity -- velocity' )
145     FRICTION v* ;
146
147 : apply-gravity ( velocity -- velocity' )
148     GRAVITY v+ ;
149
150 : clamp-coords ( coords dim -- coords' )
151     { 0 0 } swap { 2 2 } v- vclamp ;
152
153 :: pixel-indices ( coords dim -- indices )
154     coords vfloor v>integer dim clamp-coords :> floor-coords
155     floor-coords first2 dim first * + :> base-index
156     base-index dim first + :> next-row-index
157
158     base-index
159     base-index 1 +
160     next-row-index
161     next-row-index 1 + 4array ;
162
163 :: terrain-height-at ( segment point -- height )
164     segment dim>> :> dim
165     dim point v* :> pixel
166     pixel dup vfloor v- :> pixel-mantissa
167     segment bitmap>> 4 <groups> :> pixels
168     pixel dim pixel-indices :> indices
169
170     indices [ pixels nth COMPONENT-SCALE vdot 255.0 / ] map
171     first4 pixel-mantissa bilerp ;
172
173 : (collide) ( segment location -- location' )
174     [
175         { 0 2 3 3 } vshuffle terrain-height-at PLAYER-HEIGHT +
176         -1/0. swap -1/0. -1/0. float-4-boa
177     ] keep vmax ; inline
178
179 TYPED:: collide ( world: terrain-world player: player -- )
180     world terrain-segment>> :> segment
181     player location>> :> location
182     segment location (collide) :> location'
183
184     location location' = not [
185         player
186             location' >>location
187             [ BOUNCE v* ] change-velocity
188             drop
189     ] when ;
190
191 : scaled-velocity ( player -- velocity )
192     [ velocity>> ] [ velocity-modifier>> ] bi v* ;
193
194 : save-history ( world player -- )
195     clone swap history>> push ;
196
197 :: tick-player-reverse ( world player -- )
198     player reverse-time>> :> reverse-time
199     world history>> :> history
200     history length 0 > [
201         history length reverse-time 1 - - 1 max history set-length
202         history pop world player<<
203     ] when ;
204
205 : tick-player-forward ( world player -- )
206     2dup save-history
207     [ apply-friction apply-gravity ] change-velocity
208     dup scaled-velocity [ v+ ] curry change-location
209     collide ;
210
211 : tick-player ( world player -- )
212     dup reverse-time>>
213     [ tick-player-reverse ]
214     [ tick-player-forward ] if ;
215
216 M: terrain-world tick-game-world
217     [ dup focused?>> [ handle-input ] [ drop ] if ]
218     [ dup player>> tick-player ] bi ;
219
220 : set-texture-parameters ( texture -- )
221     GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit
222     GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
223     GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
224     GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP_TO_EDGE glTexParameteri
225     GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE glTexParameteri ;
226
227 : sky-gradient ( world -- t )
228     game-loop>> tick#>> SKY-PERIOD mod SKY-PERIOD /f ;
229 : sky-theta ( world -- theta )
230     game-loop>> tick#>> SKY-SPEED * ;
231
232 M: terrain-world begin-game-world
233     "2.0" { "GL_ARB_vertex_buffer_object" "GL_ARB_shader_objects" }
234     require-gl-version-or-extensions
235     GL_DEPTH_TEST glEnable
236     GL_TEXTURE_2D glEnable
237     GL_VERTEX_ARRAY glEnableClientState
238     <player> >>player
239     V{ } clone >>history
240     <perlin-noise-table> 0.01 float-4-with scale-matrix4 { 512 512 } perlin-noise-image
241     [ >>sky-image ] keep
242     make-texture [ set-texture-parameters ] keep >>sky-texture
243     <terrain> [ >>terrain ] keep
244     float-4{ 0.0 0.0 0.0 1.0 } terrain-segment [ >>terrain-segment ] keep
245     make-texture [ set-texture-parameters ] keep >>terrain-texture
246     sky-vertex-shader sky-pixel-shader <simple-gl-program>
247     >>sky-program
248     terrain-vertex-shader terrain-pixel-shader <simple-gl-program>
249     >>terrain-program
250     terrain-vertex-size <grid-mesh> >>terrain-mesh
251     drop ;
252
253 M: terrain-world end-game-world
254     {
255         [ terrain-mesh>> dispose ]
256         [ terrain-program>> delete-gl-program ]
257         [ terrain-texture>> delete-texture ]
258         [ sky-program>> delete-gl-program ]
259         [ sky-texture>> delete-texture ]
260     } cleave ;
261
262 M: terrain-world resize-world
263     GL_PROJECTION glMatrixMode
264     glLoadIdentity
265     dim>> [ [ { 0 0 } ] dip gl-viewport ]
266     [ frustum glFrustum ] bi ;
267
268 M: terrain-world draw-world*
269     {
270         [ set-modelview-matrix ]
271         [ terrain-texture>> GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
272         [ sky-texture>> GL_TEXTURE_2D GL_TEXTURE1 bind-texture-unit ]
273         [ GL_DEPTH_TEST glDisable dup sky-program>> [
274             [ nip "sky" glGetUniformLocation 1 glUniform1i ]
275             [ "sky_gradient" glGetUniformLocation swap sky-gradient glUniform1f ]
276             [ "sky_theta" glGetUniformLocation swap sky-theta glUniform1f ] 2tri
277             { -1.0 -1.0 } { 2.0 2.0 } gl-fill-rect
278         ] with-gl-program ]
279         [ GL_DEPTH_TEST glEnable dup terrain-program>> [
280             [ "heightmap" glGetUniformLocation 0 glUniform1i ]
281             [ "component_scale" glGetUniformLocation COMPONENT-SCALE first4 glUniform4f ] bi
282             terrain-mesh>> draw-grid-mesh
283         ] with-gl-program ]
284     } cleave gl-error ;
285
286 GAME: terrain-game {
287         { world-class terrain-world }
288         { title "Terrain" }
289         { pixel-format-attributes {
290             windowed
291             double-buffered
292             T{ depth-bits { value 24 } }
293         } }
294         { use-game-input? t }
295         { grab-input? t }
296         { pref-dim { 1024 768 } }
297         { tick-interval-nanos $[ 60 fps ] }
298     } ;