1 USING: accessors arrays combinators game-input game-loop
2 game-input.scancodes grouping kernel literals locals
3 math math.constants math.functions math.matrices math.order
4 math.vectors opengl opengl.capabilities opengl.gl
5 opengl.shaders opengl.textures opengl.textures.private
6 sequences sequences.product specialized-arrays.float
7 terrain.generation terrain.shaders ui ui.gadgets
8 ui.gadgets.worlds ui.pixel-formats game-worlds method-chains
9 math.affine-transforms noise ;
12 CONSTANT: FOV $[ 2.0 sqrt 1+ ]
13 CONSTANT: NEAR-PLANE $[ 1.0 1024.0 / ]
14 CONSTANT: FAR-PLANE 2.0
15 CONSTANT: PLAYER-START-LOCATION { 0.5 0.51 0.5 }
16 CONSTANT: PLAYER-HEIGHT $[ 1.0 256.0 / ]
17 CONSTANT: GRAVITY $[ 1.0 4096.0 / ]
18 CONSTANT: JUMP $[ 1.0 1024.0 / ]
19 CONSTANT: MOUSE-SCALE $[ 1.0 10.0 / ]
20 CONSTANT: MOVEMENT-SPEED $[ 1.0 16384.0 / ]
21 CONSTANT: FRICTION 0.95
22 CONSTANT: COMPONENT-SCALE { 0.5 0.01 0.0005 0.0 }
23 CONSTANT: SKY-PERIOD 1200
24 CONSTANT: SKY-SPEED 0.0005
26 CONSTANT: terrain-vertex-size { 512 512 }
27 CONSTANT: terrain-vertex-distance { $[ 1.0 512.0 / ] $[ 1.0 512.0 / ] }
28 CONSTANT: terrain-vertex-row-length $[ 512 1 + 2 * ]
31 location yaw pitch velocity ;
33 TUPLE: terrain-world < game-world
35 sky-image sky-texture sky-program
36 terrain terrain-segment terrain-texture terrain-program
37 terrain-vertex-buffer ;
39 M: terrain-world tick-length
42 : frustum ( dim -- -x x -y y near far )
44 NEAR-PLANE FOV / v*n first2 [ [ neg ] keep ] bi@
45 NEAR-PLANE FAR-PLANE ;
47 : set-modelview-matrix ( gadget -- )
48 GL_DEPTH_BUFFER_BIT glClear
49 GL_MODELVIEW glMatrixMode
52 [ pitch>> 1.0 0.0 0.0 glRotatef ]
53 [ yaw>> 0.0 1.0 0.0 glRotatef ]
54 [ location>> vneg first3 glTranslatef ] tri ;
56 : vertex-array-vertex ( x z -- vertex )
57 [ terrain-vertex-distance first * ]
58 [ terrain-vertex-distance second * ] bi*
59 [ 0 ] dip float-array{ } 3sequence ;
61 : vertex-array-row ( z -- vertices )
63 terrain-vertex-size first 1 + iota
64 2array [ first2 swap vertex-array-vertex ] product-map
67 : vertex-array ( -- vertices )
68 terrain-vertex-size second iota
69 [ vertex-array-row ] map concat ;
71 : >vertex-buffer ( bytes -- buffer )
72 [ GL_ARRAY_BUFFER ] dip GL_STATIC_DRAW <gl-buffer> ;
74 : draw-vertex-buffer-row ( i -- )
75 [ GL_TRIANGLE_STRIP ] dip
76 terrain-vertex-row-length * terrain-vertex-row-length
79 : draw-vertex-buffer ( buffer -- )
80 [ GL_ARRAY_BUFFER ] dip [
81 3 GL_FLOAT 0 f glVertexPointer
82 terrain-vertex-size second iota [ draw-vertex-buffer-row ] each
85 : degrees ( deg -- rad )
88 :: eye-rotate ( yaw pitch v -- v' )
90 pitch degrees neg :> p
96 cosy 0.0 siny neg 3array
97 siny sinp * cosp cosy sinp * 3array
98 siny cosp * sinp neg cosy cosp * 3array 3array
101 : forward-vector ( player -- v )
103 { 0.0 0.0 $ MOVEMENT-SPEED } vneg eye-rotate ;
104 : rightward-vector ( player -- v )
106 { $ MOVEMENT-SPEED 0.0 0.0 } eye-rotate ;
108 : walk-forward ( player -- )
109 dup forward-vector [ v+ ] curry change-velocity drop ;
110 : walk-backward ( player -- )
111 dup forward-vector [ v- ] curry change-velocity drop ;
112 : walk-leftward ( player -- )
113 dup rightward-vector [ v- ] curry change-velocity drop ;
114 : walk-rightward ( player -- )
115 dup rightward-vector [ v+ ] curry change-velocity drop ;
117 [ { 0.0 $ JUMP 0.0 } v+ ] change-velocity drop ;
119 : clamp-pitch ( pitch -- pitch' )
122 : rotate-with-mouse ( player mouse -- )
123 [ dx>> MOUSE-SCALE * [ + ] curry change-yaw ]
124 [ dy>> MOUSE-SCALE * [ + clamp-pitch ] curry change-pitch ] bi
127 :: handle-input ( world -- )
128 world player>> :> player
129 read-keyboard keys>> :> keys
130 key-w keys nth [ player walk-forward ] when
131 key-s keys nth [ player walk-backward ] when
132 key-a keys nth [ player walk-leftward ] when
133 key-d keys nth [ player walk-rightward ] when
134 key-space keys nth [ player jump ] when
135 key-escape keys nth [ world close-window ] when
136 player read-mouse rotate-with-mouse
139 : apply-friction ( velocity -- velocity' )
142 : apply-gravity ( velocity -- velocity' )
143 1 over [ GRAVITY - ] change-nth ;
145 : clamp-coords ( coords dim -- coords' )
146 [ { 0 0 } vmax ] dip { 2 2 } v- vmin ;
148 :: pixel-indices ( coords dim -- indices )
149 coords vfloor [ >integer ] map dim clamp-coords :> floor-coords
150 floor-coords first2 dim first * + :> base-index
151 base-index dim first + :> next-row-index
156 next-row-index 1 + 4array ;
158 :: terrain-height-at ( segment point -- height )
160 dim point v* :> pixel
161 pixel dup vfloor v- :> pixel-mantissa
162 segment bitmap>> 4 <groups> :> pixels
163 pixel dim pixel-indices :> indices
165 indices [ pixels nth COMPONENT-SCALE v. 255.0 / ] map
166 first4 pixel-mantissa bilerp ;
168 : collide ( segment location -- location' )
169 [ [ first ] [ third ] bi 2array terrain-height-at PLAYER-HEIGHT + ]
170 [ [ 1 ] 2dip [ max ] with change-nth ]
173 : tick-player ( world player -- )
174 [ apply-friction apply-gravity ] change-velocity
175 dup velocity>> [ v+ [ terrain-segment>> ] dip collide ] curry with change-location
178 M: terrain-world tick*
179 [ dup focused?>> [ handle-input ] [ drop ] if ]
180 [ dup player>> tick-player ] bi ;
182 : set-texture-parameters ( texture -- )
183 GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit
184 GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
185 GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
186 GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP_TO_EDGE glTexParameteri
187 GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE glTexParameteri ;
189 : sky-gradient ( world -- t )
190 game-loop>> tick-number>> SKY-PERIOD mod SKY-PERIOD /f ;
191 : sky-theta ( world -- theta )
192 game-loop>> tick-number>> SKY-SPEED * ;
194 BEFORE: terrain-world begin-world
195 "2.0" { "GL_ARB_vertex_buffer_object" "GL_ARB_shader_objects" }
196 require-gl-version-or-extensions
197 GL_DEPTH_TEST glEnable
198 GL_TEXTURE_2D glEnable
199 GL_VERTEX_ARRAY glEnableClientState
200 PLAYER-START-LOCATION 0.0 0.0 { 0.0 0.0 0.0 } player boa >>player
201 <perlin-noise-table> 0.01 0.01 <scale> { 512 512 } perlin-noise-image
203 make-texture [ set-texture-parameters ] keep >>sky-texture
204 <terrain> [ >>terrain ] keep
205 { 0 0 } terrain-segment [ >>terrain-segment ] keep
206 make-texture [ set-texture-parameters ] keep >>terrain-texture
207 sky-vertex-shader sky-pixel-shader <simple-gl-program>
209 terrain-vertex-shader terrain-pixel-shader <simple-gl-program>
211 vertex-array >vertex-buffer >>terrain-vertex-buffer
214 AFTER: terrain-world end-world
216 [ terrain-vertex-buffer>> delete-gl-buffer ]
217 [ terrain-program>> delete-gl-program ]
218 [ terrain-texture>> delete-texture ]
219 [ sky-program>> delete-gl-program ]
220 [ sky-texture>> delete-texture ]
223 M: terrain-world resize-world
224 GL_PROJECTION glMatrixMode
226 dim>> [ [ 0 0 ] dip first2 glViewport ]
227 [ frustum glFrustum ] bi ;
229 M: terrain-world draw-world*
231 [ set-modelview-matrix ]
232 [ terrain-texture>> GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
233 [ sky-texture>> GL_TEXTURE_2D GL_TEXTURE1 bind-texture-unit ]
234 [ GL_DEPTH_TEST glDisable dup sky-program>> [
235 [ nip "sky" glGetUniformLocation 1 glUniform1i ]
236 [ "sky_gradient" glGetUniformLocation swap sky-gradient glUniform1f ]
237 [ "sky_theta" glGetUniformLocation swap sky-theta glUniform1f ] 2tri
238 { -1.0 -1.0 } { 2.0 2.0 } gl-fill-rect
240 [ GL_DEPTH_TEST glEnable dup terrain-program>> [
241 [ "heightmap" glGetUniformLocation 0 glUniform1i ]
242 [ "component_scale" glGetUniformLocation COMPONENT-SCALE first4 glUniform4f ] bi
243 terrain-vertex-buffer>> draw-vertex-buffer
247 M: terrain-world pref-dim* drop { 640 480 } ;
249 : terrain-window ( -- )
251 f T{ world-attributes
252 { world-class terrain-world }
254 { pixel-format-attributes {
257 T{ depth-bits { value 24 } }