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 SPECIALIZED-ARRAY: float
15 CONSTANT: FOV $[ 2.0 sqrt 1 + ]
16 CONSTANT: NEAR-PLANE $[ 1.0 1024.0 / ]
17 CONSTANT: FAR-PLANE 2.0
18 CONSTANT: PLAYER-START-LOCATION { 0.5 0.51 0.5 }
19 CONSTANT: VELOCITY-MODIFIER-NORMAL { 1.0 1.0 1.0 }
20 CONSTANT: VELOCITY-MODIFIER-FAST { 2.0 1.0 2.0 }
21 CONSTANT: PLAYER-HEIGHT $[ 1.0 256.0 / ]
22 CONSTANT: GRAVITY $[ 1.0 4096.0 / ]
23 CONSTANT: JUMP $[ 1.0 1024.0 / ]
24 CONSTANT: MOUSE-SCALE $[ 1.0 10.0 / ]
25 CONSTANT: MOVEMENT-SPEED $[ 1.0 16384.0 / ]
26 CONSTANT: FRICTION { 0.95 0.99 0.95 }
27 CONSTANT: COMPONENT-SCALE { 0.5 0.01 0.0005 0.0 }
28 CONSTANT: SKY-PERIOD 1200
29 CONSTANT: SKY-SPEED 0.0005
31 CONSTANT: terrain-vertex-size { 512 512 }
34 location yaw pitch velocity velocity-modifier
37 TUPLE: terrain-world < game-world
39 sky-image sky-texture sky-program
40 terrain terrain-segment terrain-texture terrain-program
44 : <player> ( -- player )
46 PLAYER-START-LOCATION >>location
49 { 0.0 0.0 0.0 } >>velocity
50 VELOCITY-MODIFIER-NORMAL >>velocity-modifier ;
52 M: terrain-world tick-length
55 : frustum ( dim -- -x x -y y near far )
57 NEAR-PLANE FOV / v*n first2 [ [ neg ] keep ] bi@
58 NEAR-PLANE FAR-PLANE ;
60 : set-modelview-matrix ( gadget -- )
61 GL_DEPTH_BUFFER_BIT glClear
62 GL_MODELVIEW glMatrixMode
65 [ pitch>> 1.0 0.0 0.0 glRotatef ]
66 [ yaw>> 0.0 1.0 0.0 glRotatef ]
67 [ location>> vneg first3 glTranslatef ] tri ;
69 : degrees ( deg -- rad )
72 :: eye-rotate ( yaw pitch v -- v' )
74 pitch degrees neg :> p
80 cosy 0.0 siny neg 3array
81 siny sinp * cosp cosy sinp * 3array
82 siny cosp * sinp neg cosy cosp * 3array 3array
85 : forward-vector ( player -- v )
87 ${ 0.0 0.0 MOVEMENT-SPEED } vneg eye-rotate ;
88 : rightward-vector ( player -- v )
90 ${ MOVEMENT-SPEED 0.0 0.0 } eye-rotate ;
91 : clamp-pitch ( pitch -- pitch' )
94 : walk-forward ( player -- )
95 dup forward-vector [ v+ ] curry change-velocity drop ;
96 : walk-backward ( player -- )
97 dup forward-vector [ v- ] curry change-velocity drop ;
98 : walk-leftward ( player -- )
99 dup rightward-vector [ v- ] curry change-velocity drop ;
100 : walk-rightward ( player -- )
101 dup rightward-vector [ v+ ] curry change-velocity drop ;
103 [ ${ 0.0 JUMP 0.0 } v+ ] change-velocity drop ;
104 : rotate-leftward ( player x -- )
105 [ - ] curry change-yaw drop ;
106 : rotate-rightward ( player x -- )
107 [ + ] curry change-yaw drop ;
108 : look-horizontally ( player x -- )
109 [ + ] curry change-yaw drop ;
110 : look-vertically ( player x -- )
111 [ + clamp-pitch ] curry change-pitch drop ;
114 : rotate-with-mouse ( player mouse -- )
115 [ dx>> MOUSE-SCALE * look-horizontally ]
116 [ dy>> MOUSE-SCALE * look-vertically ] 2bi ;
120 { T{ key-down { mods { A+ } } { sym "RET" } } [ toggle-fullscreen ] }
123 :: handle-input ( world -- )
124 world player>> :> player
125 read-keyboard keys>> :> keys
127 key-left-shift keys nth
128 VELOCITY-MODIFIER-FAST VELOCITY-MODIFIER-NORMAL ? player (>>velocity-modifier)
131 [ key-1 keys nth 1 f ? ]
132 [ key-2 keys nth 2 f ? ]
133 [ key-3 keys nth 3 f ? ]
134 [ key-4 keys nth 4 f ? ]
135 [ key-5 keys nth 10000 f ? ]
136 } 0|| player (>>reverse-time)
138 key-w keys nth [ player walk-forward ] when
139 key-s keys nth [ player walk-backward ] when
140 key-a keys nth [ player walk-leftward ] when
141 key-d keys nth [ player walk-rightward ] when
142 key-q keys nth [ player -1 look-horizontally ] when
143 key-e keys nth [ player 1 look-horizontally ] when
144 key-left-arrow keys nth [ player -1 look-horizontally ] when
145 key-right-arrow keys nth [ player 1 look-horizontally ] when
146 key-down-arrow keys nth [ player 1 look-vertically ] when
147 key-up-arrow keys nth [ player -1 look-vertically ] when
148 key-space keys nth [ player jump ] when
149 key-escape keys nth [ world close-window ] when
150 player read-mouse rotate-with-mouse
153 : apply-friction ( velocity -- velocity' )
156 : apply-gravity ( velocity -- velocity' )
157 1 over [ GRAVITY - ] change-nth ;
159 : clamp-coords ( coords dim -- coords' )
160 [ { 0 0 } vmax ] dip { 2 2 } v- vmin ;
162 :: pixel-indices ( coords dim -- indices )
163 coords vfloor [ >integer ] map dim clamp-coords :> floor-coords
164 floor-coords first2 dim first * + :> base-index
165 base-index dim first + :> next-row-index
170 next-row-index 1 + 4array ;
172 :: terrain-height-at ( segment point -- height )
174 dim point v* :> pixel
175 pixel dup vfloor v- :> pixel-mantissa
176 segment bitmap>> 4 <groups> :> pixels
177 pixel dim pixel-indices :> indices
179 indices [ pixels nth COMPONENT-SCALE v. 255.0 / ] map
180 first4 pixel-mantissa bilerp ;
182 : collide ( segment location -- location' )
183 [ [ first ] [ third ] bi 2array terrain-height-at PLAYER-HEIGHT + ]
184 [ [ 1 ] 2dip [ max ] with change-nth ]
187 : scaled-velocity ( player -- velocity )
188 [ velocity>> ] [ velocity-modifier>> ] bi v* ;
190 : save-history ( world player -- )
191 clone swap history>> push ;
193 :: tick-player-reverse ( world player -- )
194 player reverse-time>> :> reverse-time
195 world history>> :> history
197 history length reverse-time 1 - - 1 max history set-length
198 history pop world (>>player)
201 : tick-player-forward ( world player -- )
203 [ apply-friction apply-gravity ] change-velocity
204 dup scaled-velocity [ v+ [ terrain-segment>> ] dip collide ] curry with change-location
207 : tick-player ( world player -- )
214 M: terrain-world tick*
215 [ dup focused?>> [ handle-input ] [ drop ] if ]
216 [ dup player>> tick-player ] bi ;
218 : set-texture-parameters ( texture -- )
219 GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit
220 GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
221 GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
222 GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP_TO_EDGE glTexParameteri
223 GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE glTexParameteri ;
225 : sky-gradient ( world -- t )
226 game-loop>> tick-number>> SKY-PERIOD mod SKY-PERIOD /f ;
227 : sky-theta ( world -- theta )
228 game-loop>> tick-number>> SKY-SPEED * ;
230 BEFORE: terrain-world begin-world
231 "2.0" { "GL_ARB_vertex_buffer_object" "GL_ARB_shader_objects" }
232 require-gl-version-or-extensions
233 GL_DEPTH_TEST glEnable
234 GL_TEXTURE_2D glEnable
235 GL_VERTEX_ARRAY glEnableClientState
238 <perlin-noise-table> 0.01 0.01 <scale> { 512 512 } perlin-noise-image
240 make-texture [ set-texture-parameters ] keep >>sky-texture
241 <terrain> [ >>terrain ] keep
242 { 0 0 } terrain-segment [ >>terrain-segment ] keep
243 make-texture [ set-texture-parameters ] keep >>terrain-texture
244 sky-vertex-shader sky-pixel-shader <simple-gl-program>
246 terrain-vertex-shader terrain-pixel-shader <simple-gl-program>
248 terrain-vertex-size <grid-mesh> >>terrain-mesh
251 AFTER: terrain-world end-world
253 [ terrain-mesh>> dispose ]
254 [ terrain-program>> delete-gl-program ]
255 [ terrain-texture>> delete-texture ]
256 [ sky-program>> delete-gl-program ]
257 [ sky-texture>> delete-texture ]
260 M: terrain-world resize-world
261 GL_PROJECTION glMatrixMode
263 dim>> [ [ 0 0 ] dip first2 glViewport ]
264 [ frustum glFrustum ] bi ;
266 M: terrain-world draw-world*
268 [ set-modelview-matrix ]
269 [ terrain-texture>> GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
270 [ sky-texture>> GL_TEXTURE_2D GL_TEXTURE1 bind-texture-unit ]
271 [ GL_DEPTH_TEST glDisable dup sky-program>> [
272 [ nip "sky" glGetUniformLocation 1 glUniform1i ]
273 [ "sky_gradient" glGetUniformLocation swap sky-gradient glUniform1f ]
274 [ "sky_theta" glGetUniformLocation swap sky-theta glUniform1f ] 2tri
275 { -1.0 -1.0 } { 2.0 2.0 } gl-fill-rect
277 [ GL_DEPTH_TEST glEnable dup terrain-program>> [
278 [ "heightmap" glGetUniformLocation 0 glUniform1i ]
279 [ "component_scale" glGetUniformLocation COMPONENT-SCALE first4 glUniform4f ] bi
280 terrain-mesh>> draw-grid-mesh
284 M: terrain-world pref-dim* drop { 640 480 } ;
286 : terrain-window ( -- )
288 f T{ world-attributes
289 { world-class terrain-world }
291 { pixel-format-attributes {
294 T{ depth-bits { value 24 } }