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
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/8192. 0.0 0.0 }
25 CONSTANT: JUMP 1/2048.
26 CONSTANT: MOUSE-SCALE 1/20.
27 CONSTANT: MOVEMENT-SPEED 1/32768.
28 CONSTANT: FRICTION float-4{ 0.97 0.995 0.97 1.0 }
29 CONSTANT: COMPONENT-SCALE float-4{ 0.5 0.01 0.0005 0.0 }
30 CONSTANT: SKY-PERIOD 2400
31 CONSTANT: SKY-SPEED 0.00025
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 : frustum ( dim -- -x x -y y near far )
60 NEAR-PLANE FOV / v*n first2 [ [ neg ] keep ] bi@
61 NEAR-PLANE FAR-PLANE ;
63 : set-modelview-matrix ( gadget -- )
64 GL_DEPTH_BUFFER_BIT glClear
65 GL_MODELVIEW glMatrixMode
68 [ pitch>> 1.0 0.0 0.0 glRotatef ]
69 [ yaw>> 0.0 1.0 0.0 glRotatef ]
70 [ location>> vneg first3 glTranslatef ] tri ;
72 : degrees ( deg -- rad )
75 TYPED: eye-rotate ( yaw: float pitch: float v: float-4 -- v': float-4 )
76 [ float-4{ 0.0 -1.0 0.0 0.0 } swap degrees rotation-matrix4 ]
77 [ float-4{ -1.0 0.0 0.0 0.0 } swap degrees rotation-matrix4 m4. ]
78 [ m4.v ] tri* float-4{ t t t f } vand ;
80 : forward-vector ( player -- v )
82 float-4{ 0.0 0.0 $ MOVEMENT-SPEED 1.0 } vneg eye-rotate ; inline
83 : rightward-vector ( player -- v )
85 float-4{ $ MOVEMENT-SPEED 0.0 0.0 1.0 } eye-rotate ; inline
86 : clamp-pitch ( pitch -- pitch' )
87 -90.0 90.0 clamp ; inline
89 : walk-forward ( player -- )
90 dup forward-vector [ v+ ] curry change-velocity drop ; inline
91 : walk-backward ( player -- )
92 dup forward-vector [ v- ] curry change-velocity drop ; inline
93 : walk-leftward ( player -- )
94 dup rightward-vector [ v- ] curry change-velocity drop ; inline
95 : walk-rightward ( player -- )
96 dup rightward-vector [ v+ ] curry change-velocity drop ; inline
98 [ float-4{ 0.0 $ JUMP 0.0 0.0 } v+ ] change-velocity drop ; inline
99 : rotate-leftward ( player x -- )
100 [ - ] curry change-yaw drop ; inline
101 : rotate-rightward ( player x -- )
102 [ + ] curry change-yaw drop ; inline
103 : look-horizontally ( player x -- )
104 [ + ] curry change-yaw drop ; inline
105 : look-vertically ( player x -- )
106 [ + clamp-pitch ] curry change-pitch drop ; inline
109 : rotate-with-mouse ( player mouse -- )
110 [ dx>> MOUSE-SCALE * look-horizontally ]
111 [ dy>> MOUSE-SCALE * look-vertically ] 2bi ;
115 { T{ key-down { mods { A+ } } { sym "RET" } } [ toggle-fullscreen ] }
118 :: handle-input ( world -- )
119 world player>> :> player
120 read-keyboard keys>> :> keys
122 key-left-shift keys nth
123 VELOCITY-MODIFIER-FAST VELOCITY-MODIFIER-NORMAL ? player velocity-modifier<<
126 [ key-1 keys nth 1 f ? ]
127 [ key-2 keys nth 2 f ? ]
128 [ key-3 keys nth 3 f ? ]
129 [ key-4 keys nth 4 f ? ]
130 [ key-5 keys nth 10000 f ? ]
131 } 0|| player reverse-time<<
133 key-w keys nth [ player walk-forward ] when
134 key-s keys nth [ player walk-backward ] when
135 key-a keys nth [ player walk-leftward ] when
136 key-d keys nth [ player walk-rightward ] when
137 key-q keys nth [ player -1 look-horizontally ] when
138 key-e keys nth [ player 1 look-horizontally ] when
139 key-left-arrow keys nth [ player -1 look-horizontally ] when
140 key-right-arrow keys nth [ player 1 look-horizontally ] when
141 key-down-arrow keys nth [ player 1 look-vertically ] when
142 key-up-arrow keys nth [ player -1 look-vertically ] when
143 key-space keys nth [ player jump ] when
144 key-escape keys nth [ world close-window ] when
145 player read-mouse rotate-with-mouse
148 : apply-friction ( velocity -- velocity' )
151 : apply-gravity ( velocity -- velocity' )
154 : clamp-coords ( coords dim -- coords' )
155 [ { 0 0 } vmax ] dip { 2 2 } v- vmin ;
157 :: pixel-indices ( coords dim -- indices )
158 coords vfloor [ >integer ] map dim clamp-coords :> floor-coords
159 floor-coords first2 dim first * + :> base-index
160 base-index dim first + :> next-row-index
165 next-row-index 1 + 4array ;
167 :: terrain-height-at ( segment point -- height )
169 dim point v* :> pixel
170 pixel dup vfloor v- :> pixel-mantissa
171 segment bitmap>> 4 <groups> :> pixels
172 pixel dim pixel-indices :> indices
174 indices [ pixels nth COMPONENT-SCALE v. 255.0 / ] map
175 first4 pixel-mantissa bilerp ;
177 : (collide) ( segment location -- location' )
179 { 0 2 3 3 } vshuffle terrain-height-at PLAYER-HEIGHT +
180 -1/0. swap -1/0. -1/0. float-4-boa
183 TYPED:: collide ( world: terrain-world player: player -- )
184 world terrain-segment>> :> segment
185 player location>> :> location
186 segment location (collide) :> location'
188 location location' = not [
191 [ BOUNCE v* ] change-velocity
195 : scaled-velocity ( player -- velocity )
196 [ velocity>> ] [ velocity-modifier>> ] bi v* ;
198 : save-history ( world player -- )
199 clone swap history>> push ;
201 :: tick-player-reverse ( world player -- )
202 player reverse-time>> :> reverse-time
203 world history>> :> history
205 history length reverse-time 1 - - 1 max history set-length
206 history pop world player<<
209 : tick-player-forward ( world player -- )
211 [ apply-friction apply-gravity ] change-velocity
212 dup scaled-velocity [ v+ ] curry change-location
215 : tick-player ( world player -- )
217 [ tick-player-reverse ]
218 [ tick-player-forward ] if ;
220 M: terrain-world tick-game-world
221 [ dup focused?>> [ handle-input ] [ drop ] if ]
222 [ dup player>> tick-player ] bi ;
224 : set-texture-parameters ( texture -- )
225 GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit
226 GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
227 GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
228 GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP_TO_EDGE glTexParameteri
229 GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE glTexParameteri ;
231 : sky-gradient ( world -- t )
232 game-loop>> tick#>> SKY-PERIOD mod SKY-PERIOD /f ;
233 : sky-theta ( world -- theta )
234 game-loop>> tick#>> SKY-SPEED * ;
236 M: terrain-world begin-game-world
237 "2.0" { "GL_ARB_vertex_buffer_object" "GL_ARB_shader_objects" }
238 require-gl-version-or-extensions
239 GL_DEPTH_TEST glEnable
240 GL_TEXTURE_2D glEnable
241 GL_VERTEX_ARRAY glEnableClientState
244 <perlin-noise-table> 0.01 float-4-with scale-matrix4 { 512 512 } perlin-noise-image
246 make-texture [ set-texture-parameters ] keep >>sky-texture
247 <terrain> [ >>terrain ] keep
248 float-4{ 0.0 0.0 0.0 1.0 } terrain-segment [ >>terrain-segment ] keep
249 make-texture [ set-texture-parameters ] keep >>terrain-texture
250 sky-vertex-shader sky-pixel-shader <simple-gl-program>
252 terrain-vertex-shader terrain-pixel-shader <simple-gl-program>
254 terrain-vertex-size <grid-mesh> >>terrain-mesh
257 M: terrain-world end-game-world
259 [ terrain-mesh>> dispose ]
260 [ terrain-program>> delete-gl-program ]
261 [ terrain-texture>> delete-texture ]
262 [ sky-program>> delete-gl-program ]
263 [ sky-texture>> delete-texture ]
266 M: terrain-world resize-world
267 GL_PROJECTION glMatrixMode
269 dim>> [ [ 0 0 ] dip first2 glViewport ]
270 [ frustum glFrustum ] bi ;
272 M: terrain-world draw-world*
274 [ set-modelview-matrix ]
275 [ terrain-texture>> GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
276 [ sky-texture>> GL_TEXTURE_2D GL_TEXTURE1 bind-texture-unit ]
277 [ GL_DEPTH_TEST glDisable dup sky-program>> [
278 [ nip "sky" glGetUniformLocation 1 glUniform1i ]
279 [ "sky_gradient" glGetUniformLocation swap sky-gradient glUniform1f ]
280 [ "sky_theta" glGetUniformLocation swap sky-theta glUniform1f ] 2tri
281 { -1.0 -1.0 } { 2.0 2.0 } gl-fill-rect
283 [ GL_DEPTH_TEST glEnable dup terrain-program>> [
284 [ "heightmap" glGetUniformLocation 0 glUniform1i ]
285 [ "component_scale" glGetUniformLocation COMPONENT-SCALE first4 glUniform4f ] bi
286 terrain-mesh>> draw-grid-mesh
291 { world-class terrain-world }
293 { pixel-format-attributes {
296 T{ depth-bits { value 24 } }
298 { use-game-input? t }
300 { pref-dim { 1024 768 } }
301 { tick-interval-nanos $[ 60 fps ] }