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.matrices 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 ui ui.gadgets
9 ui.gadgets.worlds ui.pixel-formats game.worlds method-chains
10 math.affine-transforms noise ui.gestures combinators.short-circuit
11 destructors grid-meshes ;
12 FROM: alien.c-types => float ;
13 SPECIALIZED-ARRAY: float
16 CONSTANT: FOV $[ 2.0 sqrt 1 + ]
17 CONSTANT: NEAR-PLANE $[ 1.0 1024.0 / ]
18 CONSTANT: FAR-PLANE 2.0
19 CONSTANT: PLAYER-START-LOCATION { 0.5 0.51 0.5 }
20 CONSTANT: VELOCITY-MODIFIER-NORMAL { 1.0 1.0 1.0 }
21 CONSTANT: VELOCITY-MODIFIER-FAST { 2.0 1.0 2.0 }
22 CONSTANT: PLAYER-HEIGHT $[ 1.0 256.0 / ]
23 CONSTANT: GRAVITY $[ 1.0 4096.0 / ]
24 CONSTANT: JUMP $[ 1.0 1024.0 / ]
25 CONSTANT: MOUSE-SCALE $[ 1.0 10.0 / ]
26 CONSTANT: MOVEMENT-SPEED $[ 1.0 16384.0 / ]
27 CONSTANT: FRICTION { 0.95 0.99 0.95 }
28 CONSTANT: COMPONENT-SCALE { 0.5 0.01 0.0005 0.0 }
29 CONSTANT: SKY-PERIOD 1200
30 CONSTANT: SKY-SPEED 0.0005
32 CONSTANT: terrain-vertex-size { 512 512 }
35 location yaw pitch velocity velocity-modifier
38 TUPLE: terrain-world < game-world
40 sky-image sky-texture sky-program
41 terrain terrain-segment terrain-texture terrain-program
45 : <player> ( -- player )
47 PLAYER-START-LOCATION >>location
50 { 0.0 0.0 0.0 } >>velocity
51 VELOCITY-MODIFIER-NORMAL >>velocity-modifier ;
53 M: terrain-world tick-length
56 : frustum ( dim -- -x x -y y near far )
58 NEAR-PLANE FOV / v*n first2 [ [ neg ] keep ] bi@
59 NEAR-PLANE FAR-PLANE ;
61 : set-modelview-matrix ( gadget -- )
62 GL_DEPTH_BUFFER_BIT glClear
63 GL_MODELVIEW glMatrixMode
66 [ pitch>> 1.0 0.0 0.0 glRotatef ]
67 [ yaw>> 0.0 1.0 0.0 glRotatef ]
68 [ location>> vneg first3 glTranslatef ] tri ;
70 : degrees ( deg -- rad )
73 :: eye-rotate ( yaw pitch v -- v' )
75 pitch degrees neg :> p
81 cosy 0.0 siny neg 3array
82 siny sinp * cosp cosy sinp * 3array
83 siny cosp * sinp neg cosy cosp * 3array 3array
86 : forward-vector ( player -- v )
88 ${ 0.0 0.0 MOVEMENT-SPEED } vneg eye-rotate ;
89 : rightward-vector ( player -- v )
91 ${ MOVEMENT-SPEED 0.0 0.0 } eye-rotate ;
92 : clamp-pitch ( pitch -- pitch' )
95 : walk-forward ( player -- )
96 dup forward-vector [ v+ ] curry change-velocity drop ;
97 : walk-backward ( player -- )
98 dup forward-vector [ v- ] curry change-velocity drop ;
99 : walk-leftward ( player -- )
100 dup rightward-vector [ v- ] curry change-velocity drop ;
101 : walk-rightward ( player -- )
102 dup rightward-vector [ v+ ] curry change-velocity drop ;
104 [ ${ 0.0 JUMP 0.0 } v+ ] change-velocity drop ;
105 : rotate-leftward ( player x -- )
106 [ - ] curry change-yaw drop ;
107 : rotate-rightward ( player x -- )
108 [ + ] curry change-yaw drop ;
109 : look-horizontally ( player x -- )
110 [ + ] curry change-yaw drop ;
111 : look-vertically ( player x -- )
112 [ + clamp-pitch ] curry change-pitch drop ;
115 : rotate-with-mouse ( player mouse -- )
116 [ dx>> MOUSE-SCALE * look-horizontally ]
117 [ dy>> MOUSE-SCALE * look-vertically ] 2bi ;
121 { T{ key-down { mods { A+ } } { sym "RET" } } [ toggle-fullscreen ] }
124 :: handle-input ( world -- )
125 world player>> :> player
126 read-keyboard keys>> :> keys
128 key-left-shift keys nth
129 VELOCITY-MODIFIER-FAST VELOCITY-MODIFIER-NORMAL ? player (>>velocity-modifier)
132 [ key-1 keys nth 1 f ? ]
133 [ key-2 keys nth 2 f ? ]
134 [ key-3 keys nth 3 f ? ]
135 [ key-4 keys nth 4 f ? ]
136 [ key-5 keys nth 10000 f ? ]
137 } 0|| player (>>reverse-time)
139 key-w keys nth [ player walk-forward ] when
140 key-s keys nth [ player walk-backward ] when
141 key-a keys nth [ player walk-leftward ] when
142 key-d keys nth [ player walk-rightward ] when
143 key-q keys nth [ player -1 look-horizontally ] when
144 key-e keys nth [ player 1 look-horizontally ] when
145 key-left-arrow keys nth [ player -1 look-horizontally ] when
146 key-right-arrow keys nth [ player 1 look-horizontally ] when
147 key-down-arrow keys nth [ player 1 look-vertically ] when
148 key-up-arrow keys nth [ player -1 look-vertically ] when
149 key-space keys nth [ player jump ] when
150 key-escape keys nth [ world close-window ] when
151 player read-mouse rotate-with-mouse
154 : apply-friction ( velocity -- velocity' )
157 : apply-gravity ( velocity -- velocity' )
158 1 over [ GRAVITY - ] change-nth ;
160 : clamp-coords ( coords dim -- coords' )
161 [ { 0 0 } vmax ] dip { 2 2 } v- vmin ;
163 :: pixel-indices ( coords dim -- indices )
164 coords vfloor [ >integer ] map dim clamp-coords :> floor-coords
165 floor-coords first2 dim first * + :> base-index
166 base-index dim first + :> next-row-index
171 next-row-index 1 + 4array ;
173 :: terrain-height-at ( segment point -- height )
175 dim point v* :> pixel
176 pixel dup vfloor v- :> pixel-mantissa
177 segment bitmap>> 4 <groups> :> pixels
178 pixel dim pixel-indices :> indices
180 indices [ pixels nth COMPONENT-SCALE v. 255.0 / ] map
181 first4 pixel-mantissa bilerp ;
183 : collide ( segment location -- location' )
184 [ [ first ] [ third ] bi 2array terrain-height-at PLAYER-HEIGHT + ]
185 [ [ 1 ] 2dip [ max ] with change-nth ]
188 : scaled-velocity ( player -- velocity )
189 [ velocity>> ] [ velocity-modifier>> ] bi v* ;
191 : save-history ( world player -- )
192 clone swap history>> push ;
194 :: tick-player-reverse ( world player -- )
195 player reverse-time>> :> reverse-time
196 world history>> :> history
198 history length reverse-time 1 - - 1 max history set-length
199 history pop world (>>player)
202 : tick-player-forward ( world player -- )
204 [ apply-friction apply-gravity ] change-velocity
205 dup scaled-velocity [ v+ [ terrain-segment>> ] dip collide ] curry with change-location
208 : tick-player ( world player -- )
215 M: terrain-world tick*
216 [ dup focused?>> [ handle-input ] [ drop ] if ]
217 [ dup player>> tick-player ] bi ;
219 : set-texture-parameters ( texture -- )
220 GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit
221 GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
222 GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
223 GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP_TO_EDGE glTexParameteri
224 GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE glTexParameteri ;
226 : sky-gradient ( world -- t )
227 game-loop>> tick-number>> SKY-PERIOD mod SKY-PERIOD /f ;
228 : sky-theta ( world -- theta )
229 game-loop>> tick-number>> SKY-SPEED * ;
231 BEFORE: terrain-world begin-world
232 "2.0" { "GL_ARB_vertex_buffer_object" "GL_ARB_shader_objects" }
233 require-gl-version-or-extensions
234 GL_DEPTH_TEST glEnable
235 GL_TEXTURE_2D glEnable
236 GL_VERTEX_ARRAY glEnableClientState
239 <perlin-noise-table> 0.01 0.01 <scale> { 512 512 } perlin-noise-image
241 make-texture [ set-texture-parameters ] keep >>sky-texture
242 <terrain> [ >>terrain ] keep
243 { 0 0 } terrain-segment [ >>terrain-segment ] keep
244 make-texture [ set-texture-parameters ] keep >>terrain-texture
245 sky-vertex-shader sky-pixel-shader <simple-gl-program>
247 terrain-vertex-shader terrain-pixel-shader <simple-gl-program>
249 terrain-vertex-size <grid-mesh> >>terrain-mesh
252 AFTER: terrain-world end-world
254 [ terrain-mesh>> dispose ]
255 [ terrain-program>> delete-gl-program ]
256 [ terrain-texture>> delete-texture ]
257 [ sky-program>> delete-gl-program ]
258 [ sky-texture>> delete-texture ]
261 M: terrain-world resize-world
262 GL_PROJECTION glMatrixMode
264 dim>> [ [ 0 0 ] dip first2 glViewport ]
265 [ frustum glFrustum ] bi ;
267 M: terrain-world draw-world*
269 [ set-modelview-matrix ]
270 [ terrain-texture>> GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
271 [ sky-texture>> GL_TEXTURE_2D GL_TEXTURE1 bind-texture-unit ]
272 [ GL_DEPTH_TEST glDisable dup sky-program>> [
273 [ nip "sky" glGetUniformLocation 1 glUniform1i ]
274 [ "sky_gradient" glGetUniformLocation swap sky-gradient glUniform1f ]
275 [ "sky_theta" glGetUniformLocation swap sky-theta glUniform1f ] 2tri
276 { -1.0 -1.0 } { 2.0 2.0 } gl-fill-rect
278 [ GL_DEPTH_TEST glEnable dup terrain-program>> [
279 [ "heightmap" glGetUniformLocation 0 glUniform1i ]
280 [ "component_scale" glGetUniformLocation COMPONENT-SCALE first4 glUniform4f ] bi
281 terrain-mesh>> draw-grid-mesh
285 M: terrain-world pref-dim* drop { 640 480 } ;
287 : terrain-window ( -- )
289 f T{ world-attributes
290 { world-class terrain-world }
292 { pixel-format-attributes {
295 T{ depth-bits { value 24 } }