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.float
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 ;
14 CONSTANT: FOV $[ 2.0 sqrt 1+ ]
15 CONSTANT: NEAR-PLANE $[ 1.0 1024.0 / ]
16 CONSTANT: FAR-PLANE 2.0
17 CONSTANT: PLAYER-START-LOCATION { 0.5 0.51 0.5 }
18 CONSTANT: VELOCITY-MODIFIER-NORMAL { 1.0 1.0 1.0 }
19 CONSTANT: VELOCITY-MODIFIER-FAST { 2.0 1.0 2.0 }
20 CONSTANT: PLAYER-HEIGHT $[ 1.0 256.0 / ]
21 CONSTANT: GRAVITY $[ 1.0 4096.0 / ]
22 CONSTANT: JUMP $[ 1.0 1024.0 / ]
23 CONSTANT: MOUSE-SCALE $[ 1.0 10.0 / ]
24 CONSTANT: MOVEMENT-SPEED $[ 1.0 16384.0 / ]
25 CONSTANT: FRICTION { 0.95 0.99 0.95 }
26 CONSTANT: COMPONENT-SCALE { 0.5 0.01 0.0005 0.0 }
27 CONSTANT: SKY-PERIOD 1200
28 CONSTANT: SKY-SPEED 0.0005
30 CONSTANT: terrain-vertex-size { 512 512 }
33 location yaw pitch velocity velocity-modifier
36 TUPLE: terrain-world < game-world
38 sky-image sky-texture sky-program
39 terrain terrain-segment terrain-texture terrain-program
43 : <player> ( -- player )
45 PLAYER-START-LOCATION >>location
48 { 0.0 0.0 0.0 } >>velocity
49 VELOCITY-MODIFIER-NORMAL >>velocity-modifier ;
51 M: terrain-world tick-length
54 : frustum ( dim -- -x x -y y near far )
56 NEAR-PLANE FOV / v*n first2 [ [ neg ] keep ] bi@
57 NEAR-PLANE FAR-PLANE ;
59 : set-modelview-matrix ( gadget -- )
60 GL_DEPTH_BUFFER_BIT glClear
61 GL_MODELVIEW glMatrixMode
64 [ pitch>> 1.0 0.0 0.0 glRotatef ]
65 [ yaw>> 0.0 1.0 0.0 glRotatef ]
66 [ location>> vneg first3 glTranslatef ] tri ;
68 : degrees ( deg -- rad )
71 :: eye-rotate ( yaw pitch v -- v' )
73 pitch degrees neg :> p
79 cosy 0.0 siny neg 3array
80 siny sinp * cosp cosy sinp * 3array
81 siny cosp * sinp neg cosy cosp * 3array 3array
84 : forward-vector ( player -- v )
86 ${ 0.0 0.0 MOVEMENT-SPEED } vneg eye-rotate ;
87 : rightward-vector ( player -- v )
89 ${ MOVEMENT-SPEED 0.0 0.0 } eye-rotate ;
90 : clamp-pitch ( pitch -- pitch' )
93 : walk-forward ( player -- )
94 dup forward-vector [ v+ ] curry change-velocity drop ;
95 : walk-backward ( player -- )
96 dup forward-vector [ v- ] curry change-velocity drop ;
97 : walk-leftward ( player -- )
98 dup rightward-vector [ v- ] curry change-velocity drop ;
99 : walk-rightward ( player -- )
100 dup rightward-vector [ v+ ] curry change-velocity drop ;
102 [ ${ 0.0 JUMP 0.0 } v+ ] change-velocity drop ;
103 : rotate-leftward ( player x -- )
104 [ - ] curry change-yaw drop ;
105 : rotate-rightward ( player x -- )
106 [ + ] curry change-yaw drop ;
107 : look-horizontally ( player x -- )
108 [ + ] curry change-yaw drop ;
109 : look-vertically ( player x -- )
110 [ + clamp-pitch ] curry change-pitch drop ;
113 : rotate-with-mouse ( player mouse -- )
114 [ dx>> MOUSE-SCALE * look-horizontally ]
115 [ dy>> MOUSE-SCALE * look-vertically ] 2bi ;
119 { T{ key-down { mods { A+ } } { sym "RET" } } [ toggle-fullscreen ] }
122 :: handle-input ( world -- )
123 world player>> :> player
124 read-keyboard keys>> :> keys
126 key-left-shift keys nth
127 VELOCITY-MODIFIER-FAST VELOCITY-MODIFIER-NORMAL ? player (>>velocity-modifier)
130 [ key-1 keys nth 1 f ? ]
131 [ key-2 keys nth 2 f ? ]
132 [ key-3 keys nth 3 f ? ]
133 [ key-4 keys nth 4 f ? ]
134 [ key-5 keys nth 10000 f ? ]
135 } 0|| player (>>reverse-time)
137 key-w keys nth [ player walk-forward ] when
138 key-s keys nth [ player walk-backward ] when
139 key-a keys nth [ player walk-leftward ] when
140 key-d keys nth [ player walk-rightward ] when
141 key-q keys nth [ player -1 look-horizontally ] when
142 key-e keys nth [ player 1 look-horizontally ] when
143 key-left-arrow keys nth [ player -1 look-horizontally ] when
144 key-right-arrow keys nth [ player 1 look-horizontally ] when
145 key-down-arrow keys nth [ player 1 look-vertically ] when
146 key-up-arrow keys nth [ player -1 look-vertically ] when
147 key-space keys nth [ player jump ] when
148 key-escape keys nth [ world close-window ] when
149 player read-mouse rotate-with-mouse
152 : apply-friction ( velocity -- velocity' )
155 : apply-gravity ( velocity -- velocity' )
156 1 over [ GRAVITY - ] change-nth ;
158 : clamp-coords ( coords dim -- coords' )
159 [ { 0 0 } vmax ] dip { 2 2 } v- vmin ;
161 :: pixel-indices ( coords dim -- indices )
162 coords vfloor [ >integer ] map dim clamp-coords :> floor-coords
163 floor-coords first2 dim first * + :> base-index
164 base-index dim first + :> next-row-index
169 next-row-index 1 + 4array ;
171 :: terrain-height-at ( segment point -- height )
173 dim point v* :> pixel
174 pixel dup vfloor v- :> pixel-mantissa
175 segment bitmap>> 4 <groups> :> pixels
176 pixel dim pixel-indices :> indices
178 indices [ pixels nth COMPONENT-SCALE v. 255.0 / ] map
179 first4 pixel-mantissa bilerp ;
181 : collide ( segment location -- location' )
182 [ [ first ] [ third ] bi 2array terrain-height-at PLAYER-HEIGHT + ]
183 [ [ 1 ] 2dip [ max ] with change-nth ]
186 : scaled-velocity ( player -- velocity )
187 [ velocity>> ] [ velocity-modifier>> ] bi v* ;
189 : save-history ( world player -- )
190 clone swap history>> push ;
192 :: tick-player-reverse ( world player -- )
193 player reverse-time>> :> reverse-time
194 world history>> :> history
196 history length reverse-time 1 - - 1 max history set-length
197 history pop world (>>player)
200 : tick-player-forward ( world player -- )
202 [ apply-friction apply-gravity ] change-velocity
203 dup scaled-velocity [ v+ [ terrain-segment>> ] dip collide ] curry with change-location
206 : tick-player ( world player -- )
213 M: terrain-world tick*
214 [ dup focused?>> [ handle-input ] [ drop ] if ]
215 [ dup player>> tick-player ] bi ;
217 : set-texture-parameters ( texture -- )
218 GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit
219 GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
220 GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
221 GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP_TO_EDGE glTexParameteri
222 GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE glTexParameteri ;
224 : sky-gradient ( world -- t )
225 game-loop>> tick-number>> SKY-PERIOD mod SKY-PERIOD /f ;
226 : sky-theta ( world -- theta )
227 game-loop>> tick-number>> SKY-SPEED * ;
229 BEFORE: terrain-world begin-world
230 "2.0" { "GL_ARB_vertex_buffer_object" "GL_ARB_shader_objects" }
231 require-gl-version-or-extensions
232 GL_DEPTH_TEST glEnable
233 GL_TEXTURE_2D glEnable
234 GL_VERTEX_ARRAY glEnableClientState
237 <perlin-noise-table> 0.01 0.01 <scale> { 512 512 } perlin-noise-image
239 make-texture [ set-texture-parameters ] keep >>sky-texture
240 <terrain> [ >>terrain ] keep
241 { 0 0 } terrain-segment [ >>terrain-segment ] keep
242 make-texture [ set-texture-parameters ] keep >>terrain-texture
243 sky-vertex-shader sky-pixel-shader <simple-gl-program>
245 terrain-vertex-shader terrain-pixel-shader <simple-gl-program>
247 terrain-vertex-size <grid-mesh> >>terrain-mesh
250 AFTER: terrain-world end-world
252 [ terrain-mesh>> dispose ]
253 [ terrain-program>> delete-gl-program ]
254 [ terrain-texture>> delete-texture ]
255 [ sky-program>> delete-gl-program ]
256 [ sky-texture>> delete-texture ]
259 M: terrain-world resize-world
260 GL_PROJECTION glMatrixMode
262 dim>> [ [ 0 0 ] dip first2 glViewport ]
263 [ frustum glFrustum ] bi ;
265 M: terrain-world draw-world*
267 [ set-modelview-matrix ]
268 [ terrain-texture>> GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
269 [ sky-texture>> GL_TEXTURE_2D GL_TEXTURE1 bind-texture-unit ]
270 [ GL_DEPTH_TEST glDisable dup sky-program>> [
271 [ nip "sky" glGetUniformLocation 1 glUniform1i ]
272 [ "sky_gradient" glGetUniformLocation swap sky-gradient glUniform1f ]
273 [ "sky_theta" glGetUniformLocation swap sky-theta glUniform1f ] 2tri
274 { -1.0 -1.0 } { 2.0 2.0 } gl-fill-rect
276 [ GL_DEPTH_TEST glEnable dup terrain-program>> [
277 [ "heightmap" glGetUniformLocation 0 glUniform1i ]
278 [ "component_scale" glGetUniformLocation COMPONENT-SCALE first4 glUniform4f ] bi
279 terrain-mesh>> draw-grid-mesh
283 M: terrain-world pref-dim* drop { 640 480 } ;
285 : terrain-window ( -- )
287 f T{ world-attributes
288 { world-class terrain-world }
290 { pixel-format-attributes {
293 T{ depth-bits { value 24 } }