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
16 CONSTANT: FOV $[ 2.0 sqrt 1 + ]
17 CONSTANT: NEAR-PLANE 1/1024.
18 CONSTANT: FAR-PLANE 2.0
19 CONSTANT: PLAYER-START-LOCATION float-4{ 0.5 0.51 0.5 1.0 }
20 CONSTANT: VELOCITY-MODIFIER-NORMAL float-4{ 1.0 1.0 1.0 0.0 }
21 CONSTANT: VELOCITY-MODIFIER-FAST float-4{ 2.0 1.0 2.0 0.0 }
22 CONSTANT: BOUNCE float-4{ 1.0 -0.2 1.0 1.0 }
23 CONSTANT: PLAYER-HEIGHT 1/256.
24 CONSTANT: GRAVITY float-4{ 0.0 -1/4096. 0.0 0.0 }
25 CONSTANT: JUMP 1/1024.
26 CONSTANT: MOUSE-SCALE 1/10.
27 CONSTANT: MOVEMENT-SPEED 1/16384.
28 CONSTANT: FRICTION float-4{ 0.95 0.99 0.95 1.0 }
29 CONSTANT: COMPONENT-SCALE float-4{ 0.5 0.01 0.0005 0.0 }
30 CONSTANT: SKY-PERIOD 1200
31 CONSTANT: SKY-SPEED 0.0005
33 CONSTANT: terrain-vertex-size { 512 512 }
40 { velocity-modifier float-4 }
43 TUPLE: terrain-world < game-world
45 sky-image sky-texture sky-program
46 terrain terrain-segment terrain-texture terrain-program
50 : <player> ( -- player )
52 PLAYER-START-LOCATION >>location
55 float-4{ 0.0 0.0 0.0 1.0 } >>velocity
56 VELOCITY-MODIFIER-NORMAL >>velocity-modifier ;
58 M: terrain-world tick-length
61 : frustum ( dim -- -x x -y y near far )
63 NEAR-PLANE FOV / v*n first2 [ [ neg ] keep ] bi@
64 NEAR-PLANE FAR-PLANE ;
66 : set-modelview-matrix ( gadget -- )
67 GL_DEPTH_BUFFER_BIT glClear
68 GL_MODELVIEW glMatrixMode
71 [ pitch>> 1.0 0.0 0.0 glRotatef ]
72 [ yaw>> 0.0 1.0 0.0 glRotatef ]
73 [ location>> vneg first3 glTranslatef ] tri ;
75 : degrees ( deg -- rad )
78 TYPED: eye-rotate ( yaw: float pitch: float v: float-4 -- v': float-4 )
79 [ float-4{ 0.0 -1.0 0.0 0.0 } swap degrees rotation-matrix4 ]
80 [ float-4{ -1.0 0.0 0.0 0.0 } swap degrees rotation-matrix4 m4. ]
81 [ m4.v ] tri* float-4{ t t t f } vand ;
83 : forward-vector ( player -- v )
85 float-4{ 0.0 0.0 $ MOVEMENT-SPEED 1.0 } vneg eye-rotate ; inline
86 : rightward-vector ( player -- v )
88 float-4{ $ MOVEMENT-SPEED 0.0 0.0 1.0 } eye-rotate ; inline
89 : clamp-pitch ( pitch -- pitch' )
90 -90.0 90.0 clamp ; inline
92 : walk-forward ( player -- )
93 dup forward-vector [ v+ ] curry change-velocity drop ; inline
94 : walk-backward ( player -- )
95 dup forward-vector [ v- ] curry change-velocity drop ; inline
96 : walk-leftward ( player -- )
97 dup rightward-vector [ v- ] curry change-velocity drop ; inline
98 : walk-rightward ( player -- )
99 dup rightward-vector [ v+ ] curry change-velocity drop ; inline
101 [ float-4{ 0.0 $ JUMP 0.0 0.0 } v+ ] change-velocity drop ; inline
102 : rotate-leftward ( player x -- )
103 [ - ] curry change-yaw drop ; inline
104 : rotate-rightward ( player x -- )
105 [ + ] curry change-yaw drop ; inline
106 : look-horizontally ( player x -- )
107 [ + ] curry change-yaw drop ; inline
108 : look-vertically ( player x -- )
109 [ + clamp-pitch ] curry change-pitch drop ; inline
112 : rotate-with-mouse ( player mouse -- )
113 [ dx>> MOUSE-SCALE * look-horizontally ]
114 [ dy>> MOUSE-SCALE * look-vertically ] 2bi ;
118 { T{ key-down { mods { A+ } } { sym "RET" } } [ toggle-fullscreen ] }
121 :: handle-input ( world -- )
122 world player>> :> player
123 read-keyboard keys>> :> keys
125 key-left-shift keys nth
126 VELOCITY-MODIFIER-FAST VELOCITY-MODIFIER-NORMAL ? player (>>velocity-modifier)
129 [ key-1 keys nth 1 f ? ]
130 [ key-2 keys nth 2 f ? ]
131 [ key-3 keys nth 3 f ? ]
132 [ key-4 keys nth 4 f ? ]
133 [ key-5 keys nth 10000 f ? ]
134 } 0|| player (>>reverse-time)
136 key-w keys nth [ player walk-forward ] when
137 key-s keys nth [ player walk-backward ] when
138 key-a keys nth [ player walk-leftward ] when
139 key-d keys nth [ player walk-rightward ] when
140 key-q keys nth [ player -1 look-horizontally ] when
141 key-e keys nth [ player 1 look-horizontally ] when
142 key-left-arrow keys nth [ player -1 look-horizontally ] when
143 key-right-arrow keys nth [ player 1 look-horizontally ] when
144 key-down-arrow keys nth [ player 1 look-vertically ] when
145 key-up-arrow keys nth [ player -1 look-vertically ] when
146 key-space keys nth [ player jump ] when
147 key-escape keys nth [ world close-window ] when
148 player read-mouse rotate-with-mouse
151 : apply-friction ( velocity -- velocity' )
154 : apply-gravity ( velocity -- velocity' )
157 : clamp-coords ( coords dim -- coords' )
158 [ { 0 0 } vmax ] dip { 2 2 } v- vmin ;
160 :: pixel-indices ( coords dim -- indices )
161 coords vfloor [ >integer ] map dim clamp-coords :> floor-coords
162 floor-coords first2 dim first * + :> base-index
163 base-index dim first + :> next-row-index
168 next-row-index 1 + 4array ;
170 :: terrain-height-at ( segment point -- height )
172 dim point v* :> pixel
173 pixel dup vfloor v- :> pixel-mantissa
174 segment bitmap>> 4 <groups> :> pixels
175 pixel dim pixel-indices :> indices
177 indices [ pixels nth COMPONENT-SCALE v. 255.0 / ] map
178 first4 pixel-mantissa bilerp ;
180 : (collide) ( segment location -- location' )
182 { 0 2 3 3 } vshuffle terrain-height-at PLAYER-HEIGHT +
183 -1/0. swap -1/0. -1/0. float-4-boa
186 :: collide ( world player -- )
187 world terrain-segment>> :> segment
188 player location>> :> location
189 segment location (collide) :> location'
191 location location' = not [
194 [ BOUNCE v* ] change-velocity
198 : scaled-velocity ( player -- velocity )
199 [ velocity>> ] [ velocity-modifier>> ] bi v* ;
201 : save-history ( world player -- )
202 clone swap history>> push ;
204 :: tick-player-reverse ( world player -- )
205 player reverse-time>> :> reverse-time
206 world history>> :> history
208 history length reverse-time 1 - - 1 max history set-length
209 history pop world (>>player)
212 : tick-player-forward ( world player -- )
214 [ apply-friction apply-gravity ] change-velocity
215 dup scaled-velocity [ v+ ] curry change-location
218 : tick-player ( world player -- )
220 [ tick-player-reverse ]
221 [ tick-player-forward ] if ;
223 M: terrain-world tick*
224 [ dup focused?>> [ handle-input ] [ drop ] if ]
225 [ dup player>> tick-player ] bi ;
227 : set-texture-parameters ( texture -- )
228 GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit
229 GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
230 GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
231 GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP_TO_EDGE glTexParameteri
232 GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE glTexParameteri ;
234 : sky-gradient ( world -- t )
235 game-loop>> tick-number>> SKY-PERIOD mod SKY-PERIOD /f ;
236 : sky-theta ( world -- theta )
237 game-loop>> tick-number>> SKY-SPEED * ;
239 BEFORE: terrain-world begin-world
240 "2.0" { "GL_ARB_vertex_buffer_object" "GL_ARB_shader_objects" }
241 require-gl-version-or-extensions
242 GL_DEPTH_TEST glEnable
243 GL_TEXTURE_2D glEnable
244 GL_VERTEX_ARRAY glEnableClientState
247 <perlin-noise-table> 0.01 float-4-with scale-matrix4 { 512 512 } perlin-noise-image
249 make-texture [ set-texture-parameters ] keep >>sky-texture
250 <terrain> [ >>terrain ] keep
251 float-4{ 0.0 0.0 0.0 1.0 } terrain-segment [ >>terrain-segment ] keep
252 make-texture [ set-texture-parameters ] keep >>terrain-texture
253 sky-vertex-shader sky-pixel-shader <simple-gl-program>
255 terrain-vertex-shader terrain-pixel-shader <simple-gl-program>
257 terrain-vertex-size <grid-mesh> >>terrain-mesh
260 AFTER: terrain-world end-world
262 [ terrain-mesh>> dispose ]
263 [ terrain-program>> delete-gl-program ]
264 [ terrain-texture>> delete-texture ]
265 [ sky-program>> delete-gl-program ]
266 [ sky-texture>> delete-texture ]
269 M: terrain-world resize-world
270 GL_PROJECTION glMatrixMode
272 dim>> [ [ 0 0 ] dip first2 glViewport ]
273 [ frustum glFrustum ] bi ;
275 M: terrain-world draw-world*
277 [ set-modelview-matrix ]
278 [ terrain-texture>> GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
279 [ sky-texture>> GL_TEXTURE_2D GL_TEXTURE1 bind-texture-unit ]
280 [ GL_DEPTH_TEST glDisable dup sky-program>> [
281 [ nip "sky" glGetUniformLocation 1 glUniform1i ]
282 [ "sky_gradient" glGetUniformLocation swap sky-gradient glUniform1f ]
283 [ "sky_theta" glGetUniformLocation swap sky-theta glUniform1f ] 2tri
284 { -1.0 -1.0 } { 2.0 2.0 } gl-fill-rect
286 [ GL_DEPTH_TEST glEnable dup terrain-program>> [
287 [ "heightmap" glGetUniformLocation 0 glUniform1i ]
288 [ "component_scale" glGetUniformLocation COMPONENT-SCALE first4 glUniform4f ] bi
289 terrain-mesh>> draw-grid-mesh
293 M: terrain-world pref-dim* drop { 1024 768 } ;
295 : terrain-window ( -- )
297 f T{ world-attributes
298 { world-class terrain-world }
300 { pixel-format-attributes {
303 T{ depth-bits { value 24 } }