1 ! Copyright (C) 2009 Joe Groff, Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays combinators game.input game.loop
4 game.input.scancodes grouping kernel literals locals
5 math math.constants math.functions math.order math.trig
6 math.vectors opengl opengl.capabilities opengl.gl
7 opengl.shaders opengl.textures opengl.textures.private
8 sequences sequences.product specialized-arrays
9 terrain.generation terrain.shaders typed ui ui.gadgets
10 ui.gadgets.worlds ui.pixel-formats game.worlds
11 math.matrices.simd noise ui.gestures combinators.short-circuit
12 destructors grid-meshes math.vectors.simd ;
13 QUALIFIED-WITH: alien.c-types c
14 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/8192. 0.0 0.0 }
26 CONSTANT: JUMP 1/2048.
27 CONSTANT: MOUSE-SCALE 1/20.
28 CONSTANT: MOVEMENT-SPEED 1/32768.
29 CONSTANT: FRICTION float-4{ 0.97 0.995 0.97 1.0 }
30 CONSTANT: COMPONENT-SCALE float-4{ 0.5 0.01 0.0005 0.0 }
31 CONSTANT: SKY-PERIOD 2400
32 CONSTANT: SKY-SPEED 0.00025
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 : frustum ( dim -- -x x -y y near far )
61 NEAR-PLANE FOV / v*n first2 [ [ neg ] keep ] bi@
62 NEAR-PLANE FAR-PLANE ;
64 : set-modelview-matrix ( gadget -- )
65 GL_DEPTH_BUFFER_BIT glClear
66 GL_MODELVIEW glMatrixMode
69 [ pitch>> 1.0 0.0 0.0 glRotatef ]
70 [ yaw>> 0.0 1.0 0.0 glRotatef ]
71 [ location>> vneg first3 glTranslatef ] tri ;
73 TYPED: eye-rotate ( yaw: float pitch: float v: float-4 -- v': float-4 )
74 [ float-4{ 0.0 -1.0 0.0 0.0 } swap deg>rad rotation-matrix4 ]
75 [ float-4{ -1.0 0.0 0.0 0.0 } swap deg>rad rotation-matrix4 m4. ]
76 [ m4.v ] tri* float-4{ t t t f } vand ;
78 : forward-vector ( player -- v )
80 float-4{ 0.0 0.0 $ MOVEMENT-SPEED 1.0 } vneg eye-rotate ; inline
81 : rightward-vector ( player -- v )
83 float-4{ $ MOVEMENT-SPEED 0.0 0.0 1.0 } eye-rotate ; inline
84 : clamp-pitch ( pitch -- pitch' )
85 -90.0 90.0 clamp ; inline
87 : walk-forward ( player -- )
88 dup forward-vector [ v+ ] curry change-velocity drop ; inline
89 : walk-backward ( player -- )
90 dup forward-vector [ v- ] curry change-velocity drop ; inline
91 : walk-leftward ( player -- )
92 dup rightward-vector [ v- ] curry change-velocity drop ; inline
93 : walk-rightward ( player -- )
94 dup rightward-vector [ v+ ] curry change-velocity drop ; inline
96 [ float-4{ 0.0 $ JUMP 0.0 0.0 } v+ ] change-velocity drop ; inline
97 : rotate-leftward ( player x -- )
98 [ - ] curry change-yaw drop ; inline
99 : rotate-rightward ( player x -- )
100 [ + ] curry change-yaw drop ; inline
101 : look-horizontally ( player x -- )
102 [ + ] curry change-yaw drop ; inline
103 : look-vertically ( player x -- )
104 [ + clamp-pitch ] curry change-pitch drop ; inline
107 : rotate-with-mouse ( player mouse -- )
108 [ dx>> MOUSE-SCALE * look-horizontally ]
109 [ dy>> MOUSE-SCALE * look-vertically ] 2bi ;
113 { T{ key-down { mods { A+ } } { sym "RET" } } [ toggle-fullscreen ] }
116 :: handle-input ( world -- )
117 world player>> :> player
118 read-keyboard keys>> :> keys
120 key-left-shift keys nth
121 VELOCITY-MODIFIER-FAST VELOCITY-MODIFIER-NORMAL ? player velocity-modifier<<
124 [ key-1 keys nth 1 f ? ]
125 [ key-2 keys nth 2 f ? ]
126 [ key-3 keys nth 3 f ? ]
127 [ key-4 keys nth 4 f ? ]
128 [ key-5 keys nth 10000 f ? ]
129 } 0|| player reverse-time<<
131 key-w keys nth [ player walk-forward ] when
132 key-s keys nth [ player walk-backward ] when
133 key-a keys nth [ player walk-leftward ] when
134 key-d keys nth [ player walk-rightward ] when
135 key-q keys nth [ player -1 look-horizontally ] when
136 key-e keys nth [ player 1 look-horizontally ] when
137 key-left-arrow keys nth [ player -1 look-horizontally ] when
138 key-right-arrow keys nth [ player 1 look-horizontally ] when
139 key-down-arrow keys nth [ player 1 look-vertically ] when
140 key-up-arrow keys nth [ player -1 look-vertically ] when
141 key-space keys nth [ player jump ] when
142 key-escape keys nth [ world close-window ] when
143 player read-mouse rotate-with-mouse
146 : apply-friction ( velocity -- velocity' )
149 : apply-gravity ( velocity -- velocity' )
152 : clamp-coords ( coords dim -- coords' )
153 { 0 0 } swap { 2 2 } v- vclamp ;
155 :: pixel-indices ( coords dim -- indices )
156 coords vfloor v>integer dim clamp-coords :> floor-coords
157 floor-coords first2 dim first * + :> base-index
158 base-index dim first + :> next-row-index
163 next-row-index 1 + 4array ;
165 :: terrain-height-at ( segment point -- height )
167 dim point v* :> pixel
168 pixel dup vfloor v- :> pixel-mantissa
169 segment bitmap>> 4 <groups> :> pixels
170 pixel dim pixel-indices :> indices
172 indices [ pixels nth COMPONENT-SCALE v. 255.0 / ] map
173 first4 pixel-mantissa bilerp ;
175 : (collide) ( segment location -- location' )
177 { 0 2 3 3 } vshuffle terrain-height-at PLAYER-HEIGHT +
178 -1/0. swap -1/0. -1/0. float-4-boa
181 TYPED:: collide ( world: terrain-world player: player -- )
182 world terrain-segment>> :> segment
183 player location>> :> location
184 segment location (collide) :> location'
186 location location' = not [
189 [ BOUNCE v* ] change-velocity
193 : scaled-velocity ( player -- velocity )
194 [ velocity>> ] [ velocity-modifier>> ] bi v* ;
196 : save-history ( world player -- )
197 clone swap history>> push ;
199 :: tick-player-reverse ( world player -- )
200 player reverse-time>> :> reverse-time
201 world history>> :> history
203 history length reverse-time 1 - - 1 max history set-length
204 history pop world player<<
207 : tick-player-forward ( world player -- )
209 [ apply-friction apply-gravity ] change-velocity
210 dup scaled-velocity [ v+ ] curry change-location
213 : tick-player ( world player -- )
215 [ tick-player-reverse ]
216 [ tick-player-forward ] if ;
218 M: terrain-world tick-game-world
219 [ dup focused?>> [ handle-input ] [ drop ] if ]
220 [ dup player>> tick-player ] bi ;
222 : set-texture-parameters ( texture -- )
223 GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit
224 GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
225 GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
226 GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP_TO_EDGE glTexParameteri
227 GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE glTexParameteri ;
229 : sky-gradient ( world -- t )
230 game-loop>> tick#>> SKY-PERIOD mod SKY-PERIOD /f ;
231 : sky-theta ( world -- theta )
232 game-loop>> tick#>> SKY-SPEED * ;
234 M: terrain-world begin-game-world
235 "2.0" { "GL_ARB_vertex_buffer_object" "GL_ARB_shader_objects" }
236 require-gl-version-or-extensions
237 GL_DEPTH_TEST glEnable
238 GL_TEXTURE_2D glEnable
239 GL_VERTEX_ARRAY glEnableClientState
242 <perlin-noise-table> 0.01 float-4-with scale-matrix4 { 512 512 } perlin-noise-image
244 make-texture [ set-texture-parameters ] keep >>sky-texture
245 <terrain> [ >>terrain ] keep
246 float-4{ 0.0 0.0 0.0 1.0 } terrain-segment [ >>terrain-segment ] keep
247 make-texture [ set-texture-parameters ] keep >>terrain-texture
248 sky-vertex-shader sky-pixel-shader <simple-gl-program>
250 terrain-vertex-shader terrain-pixel-shader <simple-gl-program>
252 terrain-vertex-size <grid-mesh> >>terrain-mesh
255 M: terrain-world end-game-world
257 [ terrain-mesh>> dispose ]
258 [ terrain-program>> delete-gl-program ]
259 [ terrain-texture>> delete-texture ]
260 [ sky-program>> delete-gl-program ]
261 [ sky-texture>> delete-texture ]
264 M: terrain-world resize-world
265 GL_PROJECTION glMatrixMode
267 dim>> [ [ { 0 0 } ] dip gl-viewport ]
268 [ frustum glFrustum ] bi ;
270 M: terrain-world draw-world*
272 [ set-modelview-matrix ]
273 [ terrain-texture>> GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
274 [ sky-texture>> GL_TEXTURE_2D GL_TEXTURE1 bind-texture-unit ]
275 [ GL_DEPTH_TEST glDisable dup sky-program>> [
276 [ nip "sky" glGetUniformLocation 1 glUniform1i ]
277 [ "sky_gradient" glGetUniformLocation swap sky-gradient glUniform1f ]
278 [ "sky_theta" glGetUniformLocation swap sky-theta glUniform1f ] 2tri
279 { -1.0 -1.0 } { 2.0 2.0 } gl-fill-rect
281 [ GL_DEPTH_TEST glEnable dup terrain-program>> [
282 [ "heightmap" glGetUniformLocation 0 glUniform1i ]
283 [ "component_scale" glGetUniformLocation COMPONENT-SCALE first4 glUniform4f ] bi
284 terrain-mesh>> draw-grid-mesh
289 { world-class terrain-world }
291 { pixel-format-attributes {
294 T{ depth-bits { value 24 } }
296 { use-game-input? t }
298 { pref-dim { 1024 768 } }
299 { tick-interval-nanos $[ 60 fps ] }