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 math.trig
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
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
32 CONSTANT: terrain-vertex-size { 512 512 }
39 { velocity-modifier float-4 }
42 TUPLE: terrain-world < game-world
44 sky-image sky-texture sky-program
45 terrain terrain-segment terrain-texture terrain-program
49 : <player> ( -- player )
51 PLAYER-START-LOCATION >>location
54 float-4{ 0.0 0.0 0.0 1.0 } >>velocity
55 VELOCITY-MODIFIER-NORMAL >>velocity-modifier ;
57 : frustum ( dim -- -x x -y y near far )
59 NEAR-PLANE FOV / v*n first2 [ [ neg ] keep ] bi@
60 NEAR-PLANE FAR-PLANE ;
62 : set-modelview-matrix ( gadget -- )
63 GL_DEPTH_BUFFER_BIT glClear
64 GL_MODELVIEW glMatrixMode
67 [ pitch>> 1.0 0.0 0.0 glRotatef ]
68 [ yaw>> 0.0 1.0 0.0 glRotatef ]
69 [ location>> vneg first3 glTranslatef ] tri ;
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 ;
76 : forward-vector ( player -- v )
78 float-4{ 0.0 0.0 $ MOVEMENT-SPEED 1.0 } vneg eye-rotate ; inline
79 : rightward-vector ( player -- v )
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
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
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
105 : rotate-with-mouse ( player mouse -- )
106 [ dx>> MOUSE-SCALE * look-horizontally ]
107 [ dy>> MOUSE-SCALE * look-vertically ] 2bi ;
111 { T{ key-down { mods { A+ } } { sym "RET" } } [ toggle-fullscreen ] }
114 :: handle-input ( world -- )
115 world player>> :> player
116 read-keyboard keys>> :> keys
118 key-left-shift keys nth
119 VELOCITY-MODIFIER-FAST VELOCITY-MODIFIER-NORMAL ? player velocity-modifier<<
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<<
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
144 : apply-friction ( velocity -- velocity' )
147 : apply-gravity ( velocity -- velocity' )
150 : clamp-coords ( coords dim -- coords' )
151 { 0 0 } swap { 2 2 } v- vclamp ;
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
161 next-row-index 1 + 4array ;
163 :: terrain-height-at ( segment point -- height )
165 dim point v* :> pixel
166 pixel dup vfloor v- :> pixel-mantissa
167 segment bitmap>> 4 <groups> :> pixels
168 pixel dim pixel-indices :> indices
170 indices [ pixels nth COMPONENT-SCALE vdot 255.0 / ] map
171 first4 pixel-mantissa bilerp ;
173 : (collide) ( segment location -- location' )
175 { 0 2 3 3 } vshuffle terrain-height-at PLAYER-HEIGHT +
176 -1/0. swap -1/0. -1/0. float-4-boa
179 TYPED:: collide ( world: terrain-world player: player -- )
180 world terrain-segment>> :> segment
181 player location>> :> location
182 segment location (collide) :> location'
184 location location' = not [
187 [ BOUNCE v* ] change-velocity
191 : scaled-velocity ( player -- velocity )
192 [ velocity>> ] [ velocity-modifier>> ] bi v* ;
194 : save-history ( world player -- )
195 clone swap history>> push ;
197 :: tick-player-reverse ( world player -- )
198 player reverse-time>> :> reverse-time
199 world history>> :> history
201 history length reverse-time 1 - - 1 max history set-length
202 history pop world player<<
205 : tick-player-forward ( world player -- )
207 [ apply-friction apply-gravity ] change-velocity
208 dup scaled-velocity [ v+ ] curry change-location
211 : tick-player ( world player -- )
213 [ tick-player-reverse ]
214 [ tick-player-forward ] if ;
216 M: terrain-world tick-game-world
217 [ dup focused?>> [ handle-input ] [ drop ] if ]
218 [ dup player>> tick-player ] bi ;
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 ;
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 * ;
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
240 <perlin-noise-table> 0.01 float-4-with scale-matrix4 { 512 512 } perlin-noise-image
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>
248 terrain-vertex-shader terrain-pixel-shader <simple-gl-program>
250 terrain-vertex-size <grid-mesh> >>terrain-mesh
253 M: terrain-world end-game-world
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 ]
262 M: terrain-world resize-world
263 GL_PROJECTION glMatrixMode
265 dim>> [ [ { 0 0 } ] dip gl-viewport ]
266 [ frustum glFrustum ] bi ;
268 M: terrain-world draw-world*
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
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
287 { world-class terrain-world }
289 { pixel-format-attributes {
292 T{ depth-bits { value 24 } }
294 { use-game-input? t }
296 { pref-dim { 1024 768 } }
297 { tick-interval-nanos $[ 60 fps ] }