1 ! (c)2009 Joe Groff, Doug Coleman. bsd license
2 USING: accessors arrays combinators game.input game.loop
3 game.input.scancodes grouping kernel literals locals
4 math math.constants math.functions math.order
5 math.vectors opengl opengl.capabilities opengl.gl
6 opengl.shaders opengl.textures opengl.textures.private
7 sequences sequences.product specialized-arrays
8 terrain.generation terrain.shaders typed ui ui.gadgets
9 ui.gadgets.worlds ui.pixel-formats game.worlds method-chains
10 math.matrices.simd noise ui.gestures combinators.short-circuit
11 destructors grid-meshes math.vectors.simd ;
12 QUALIFIED-WITH: alien.c-types c
13 SPECIALIZED-ARRAY: c:float
17 CONSTANT: FOV $[ 2.0 sqrt 1 + ]
18 CONSTANT: NEAR-PLANE 1/1024.
19 CONSTANT: FAR-PLANE 2.0
20 CONSTANT: PLAYER-START-LOCATION float-4{ 0.5 0.51 0.5 1.0 }
21 CONSTANT: VELOCITY-MODIFIER-NORMAL float-4{ 1.0 1.0 1.0 0.0 }
22 CONSTANT: VELOCITY-MODIFIER-FAST float-4{ 2.0 1.0 2.0 0.0 }
23 CONSTANT: BOUNCE float-4{ 1.0 -0.2 1.0 1.0 }
24 CONSTANT: PLAYER-HEIGHT 1/256.
25 CONSTANT: GRAVITY float-4{ 0.0 -1/4096. 0.0 0.0 }
26 CONSTANT: JUMP 1/1024.
27 CONSTANT: MOUSE-SCALE 1/10.
28 CONSTANT: MOVEMENT-SPEED 1/16384.
29 CONSTANT: FRICTION float-4{ 0.95 0.99 0.95 1.0 }
30 CONSTANT: COMPONENT-SCALE float-4{ 0.5 0.01 0.0005 0.0 }
31 CONSTANT: SKY-PERIOD 1200
32 CONSTANT: SKY-SPEED 0.0005
34 CONSTANT: terrain-vertex-size { 512 512 }
41 { velocity-modifier float-4 }
44 TUPLE: terrain-world < game-world
46 sky-image sky-texture sky-program
47 terrain terrain-segment terrain-texture terrain-program
51 : <player> ( -- player )
53 PLAYER-START-LOCATION >>location
56 float-4{ 0.0 0.0 0.0 1.0 } >>velocity
57 VELOCITY-MODIFIER-NORMAL >>velocity-modifier ;
59 M: terrain-world tick-length
62 : frustum ( dim -- -x x -y y near far )
64 NEAR-PLANE FOV / v*n first2 [ [ neg ] keep ] bi@
65 NEAR-PLANE FAR-PLANE ;
67 : set-modelview-matrix ( gadget -- )
68 GL_DEPTH_BUFFER_BIT glClear
69 GL_MODELVIEW glMatrixMode
72 [ pitch>> 1.0 0.0 0.0 glRotatef ]
73 [ yaw>> 0.0 1.0 0.0 glRotatef ]
74 [ location>> vneg first3 glTranslatef ] tri ;
76 : degrees ( deg -- rad )
79 TYPED: eye-rotate ( yaw: float pitch: float v: float-4 -- v': float-4 )
80 [ float-4{ 0.0 -1.0 0.0 0.0 } swap degrees rotation-matrix4 ]
81 [ float-4{ -1.0 0.0 0.0 0.0 } swap degrees rotation-matrix4 m4. ]
82 [ m4.v ] tri* float-4{ t t t f } vand ;
84 : forward-vector ( player -- v )
86 float-4{ 0.0 0.0 $ MOVEMENT-SPEED 1.0 } vneg eye-rotate ; inline
87 : rightward-vector ( player -- v )
89 float-4{ $ MOVEMENT-SPEED 0.0 0.0 1.0 } eye-rotate ; inline
90 : clamp-pitch ( pitch -- pitch' )
91 -90.0 90.0 clamp ; inline
93 : walk-forward ( player -- )
94 dup forward-vector [ v+ ] curry change-velocity drop ; inline
95 : walk-backward ( player -- )
96 dup forward-vector [ v- ] curry change-velocity drop ; inline
97 : walk-leftward ( player -- )
98 dup rightward-vector [ v- ] curry change-velocity drop ; inline
99 : walk-rightward ( player -- )
100 dup rightward-vector [ v+ ] curry change-velocity drop ; inline
102 [ float-4{ 0.0 $ JUMP 0.0 0.0 } v+ ] change-velocity drop ; inline
103 : rotate-leftward ( player x -- )
104 [ - ] curry change-yaw drop ; inline
105 : rotate-rightward ( player x -- )
106 [ + ] curry change-yaw drop ; inline
107 : look-horizontally ( player x -- )
108 [ + ] curry change-yaw drop ; inline
109 : look-vertically ( player x -- )
110 [ + clamp-pitch ] curry change-pitch drop ; inline
113 : rotate-with-mouse ( player mouse -- )
114 [ dx>> MOUSE-SCALE * look-horizontally ]
115 [ dy>> MOUSE-SCALE * look-vertically ] 2bi ;
119 { T{ key-down { mods { A+ } } { sym "RET" } } [ toggle-fullscreen ] }
122 :: handle-input ( world -- )
123 world player>> :> player
124 read-keyboard keys>> :> keys
126 key-left-shift keys nth
127 VELOCITY-MODIFIER-FAST VELOCITY-MODIFIER-NORMAL ? player (>>velocity-modifier)
130 [ key-1 keys nth 1 f ? ]
131 [ key-2 keys nth 2 f ? ]
132 [ key-3 keys nth 3 f ? ]
133 [ key-4 keys nth 4 f ? ]
134 [ key-5 keys nth 10000 f ? ]
135 } 0|| player (>>reverse-time)
137 key-w keys nth [ player walk-forward ] when
138 key-s keys nth [ player walk-backward ] when
139 key-a keys nth [ player walk-leftward ] when
140 key-d keys nth [ player walk-rightward ] when
141 key-q keys nth [ player -1 look-horizontally ] when
142 key-e keys nth [ player 1 look-horizontally ] when
143 key-left-arrow keys nth [ player -1 look-horizontally ] when
144 key-right-arrow keys nth [ player 1 look-horizontally ] when
145 key-down-arrow keys nth [ player 1 look-vertically ] when
146 key-up-arrow keys nth [ player -1 look-vertically ] when
147 key-space keys nth [ player jump ] when
148 key-escape keys nth [ world close-window ] when
149 player read-mouse rotate-with-mouse
152 : apply-friction ( velocity -- velocity' )
155 : apply-gravity ( velocity -- velocity' )
158 : clamp-coords ( coords dim -- coords' )
159 [ { 0 0 } vmax ] dip { 2 2 } v- vmin ;
161 :: pixel-indices ( coords dim -- indices )
162 coords vfloor [ >integer ] map dim clamp-coords :> floor-coords
163 floor-coords first2 dim first * + :> base-index
164 base-index dim first + :> next-row-index
169 next-row-index 1 + 4array ;
171 :: terrain-height-at ( segment point -- height )
173 dim point v* :> pixel
174 pixel dup vfloor v- :> pixel-mantissa
175 segment bitmap>> 4 <groups> :> pixels
176 pixel dim pixel-indices :> indices
178 indices [ pixels nth COMPONENT-SCALE v. 255.0 / ] map
179 first4 pixel-mantissa bilerp ;
181 : (collide) ( segment location -- location' )
183 { 0 2 3 3 } vshuffle terrain-height-at PLAYER-HEIGHT +
184 -1/0. swap -1/0. -1/0. float-4-boa
187 :: collide ( world player -- )
188 world terrain-segment>> :> segment
189 player location>> :> location
190 segment location (collide) :> location'
192 location location' = not [
195 [ BOUNCE v* ] change-velocity
199 : scaled-velocity ( player -- velocity )
200 [ velocity>> ] [ velocity-modifier>> ] bi v* ;
202 : save-history ( world player -- )
203 clone swap history>> push ;
205 :: tick-player-reverse ( world player -- )
206 player reverse-time>> :> reverse-time
207 world history>> :> history
209 history length reverse-time 1 - - 1 max history set-length
210 history pop world (>>player)
213 : tick-player-forward ( world player -- )
215 [ apply-friction apply-gravity ] change-velocity
216 dup scaled-velocity [ v+ ] curry change-location
219 : tick-player ( world player -- )
221 [ tick-player-reverse ]
222 [ tick-player-forward ] if ;
224 M: terrain-world tick*
225 [ dup focused?>> [ handle-input ] [ drop ] if ]
226 [ dup player>> tick-player ] bi ;
228 : set-texture-parameters ( texture -- )
229 GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit
230 GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
231 GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
232 GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP_TO_EDGE glTexParameteri
233 GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE glTexParameteri ;
235 : sky-gradient ( world -- t )
236 game-loop>> tick-number>> SKY-PERIOD mod SKY-PERIOD /f ;
237 : sky-theta ( world -- theta )
238 game-loop>> tick-number>> SKY-SPEED * ;
240 BEFORE: terrain-world begin-world
241 "2.0" { "GL_ARB_vertex_buffer_object" "GL_ARB_shader_objects" }
242 require-gl-version-or-extensions
243 GL_DEPTH_TEST glEnable
244 GL_TEXTURE_2D glEnable
245 GL_VERTEX_ARRAY glEnableClientState
248 <perlin-noise-table> 0.01 float-4-with scale-matrix4 { 512 512 } perlin-noise-image
250 make-texture [ set-texture-parameters ] keep >>sky-texture
251 <terrain> [ >>terrain ] keep
252 float-4{ 0.0 0.0 0.0 1.0 } terrain-segment [ >>terrain-segment ] keep
253 make-texture [ set-texture-parameters ] keep >>terrain-texture
254 sky-vertex-shader sky-pixel-shader <simple-gl-program>
256 terrain-vertex-shader terrain-pixel-shader <simple-gl-program>
258 terrain-vertex-size <grid-mesh> >>terrain-mesh
261 AFTER: terrain-world end-world
263 [ terrain-mesh>> dispose ]
264 [ terrain-program>> delete-gl-program ]
265 [ terrain-texture>> delete-texture ]
266 [ sky-program>> delete-gl-program ]
267 [ sky-texture>> delete-texture ]
270 M: terrain-world resize-world
271 GL_PROJECTION glMatrixMode
273 dim>> [ [ 0 0 ] dip first2 glViewport ]
274 [ frustum glFrustum ] bi ;
276 M: terrain-world draw-world*
278 [ set-modelview-matrix ]
279 [ terrain-texture>> GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
280 [ sky-texture>> GL_TEXTURE_2D GL_TEXTURE1 bind-texture-unit ]
281 [ GL_DEPTH_TEST glDisable dup sky-program>> [
282 [ nip "sky" glGetUniformLocation 1 glUniform1i ]
283 [ "sky_gradient" glGetUniformLocation swap sky-gradient glUniform1f ]
284 [ "sky_theta" glGetUniformLocation swap sky-theta glUniform1f ] 2tri
285 { -1.0 -1.0 } { 2.0 2.0 } gl-fill-rect
287 [ GL_DEPTH_TEST glEnable dup terrain-program>> [
288 [ "heightmap" glGetUniformLocation 0 glUniform1i ]
289 [ "component_scale" glGetUniformLocation COMPONENT-SCALE first4 glUniform4f ] bi
290 terrain-mesh>> draw-grid-mesh
294 M: terrain-world pref-dim* drop { 1024 768 } ;
296 : terrain-window ( -- )
298 f T{ world-attributes
299 { world-class terrain-world }
301 { pixel-format-attributes {
304 T{ depth-bits { value 24 } }