1 USING: accessors arrays combinators game-input
2 game-input.scancodes game-loop 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 ;
11 CONSTANT: FOV $[ 2.0 sqrt 1+ ]
12 CONSTANT: NEAR-PLANE $[ 1.0 2048.0 / ]
13 CONSTANT: FAR-PLANE 1.0
14 CONSTANT: PLAYER-START-LOCATION { 0.5 0.51 0.5 }
15 CONSTANT: PLAYER-HEIGHT $[ 3.0 1024.0 / ]
16 CONSTANT: GRAVITY $[ 1.0 4096.0 / ]
17 CONSTANT: JUMP $[ 1.0 1024.0 / ]
18 CONSTANT: TICK-LENGTH $[ 1000 30 /i ]
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.002 0.0 }
24 CONSTANT: terrain-vertex-size { 512 512 }
25 CONSTANT: terrain-vertex-distance { $[ 1.0 512.0 / ] $[ 1.0 512.0 / ] }
26 CONSTANT: terrain-vertex-row-length $[ 512 1 + 2 * ]
29 location yaw pitch velocity ;
31 TUPLE: terrain-world < world
33 terrain terrain-segment terrain-texture terrain-program
37 : frustum ( dim -- -x x -y y near far )
39 NEAR-PLANE FOV / v*n first2 [ [ neg ] keep ] bi@
40 NEAR-PLANE FAR-PLANE ;
42 : set-modelview-matrix ( gadget -- )
43 GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT bitor glClear
44 GL_MODELVIEW glMatrixMode
47 [ pitch>> 1.0 0.0 0.0 glRotatef ]
48 [ yaw>> 0.0 1.0 0.0 glRotatef ]
49 [ location>> vneg first3 glTranslatef ] tri ;
51 : vertex-array-vertex ( x z -- vertex )
52 [ terrain-vertex-distance first * ]
53 [ terrain-vertex-distance second * ] bi*
54 [ 0 ] dip float-array{ } 3sequence ;
56 : vertex-array-row ( z -- vertices )
58 terrain-vertex-size first 1 + iota
59 2array [ first2 swap vertex-array-vertex ] product-map
62 : vertex-array ( -- vertices )
63 terrain-vertex-size second iota
64 [ vertex-array-row ] map concat ;
66 : >vertex-buffer ( bytes -- buffer )
67 [ GL_ARRAY_BUFFER ] dip GL_STATIC_DRAW <gl-buffer> ;
69 : draw-vertex-buffer-row ( i -- )
70 [ GL_TRIANGLE_STRIP ] dip
71 terrain-vertex-row-length * terrain-vertex-row-length
74 : draw-vertex-buffer ( buffer -- )
75 [ GL_ARRAY_BUFFER ] dip [
76 3 GL_FLOAT 0 f glVertexPointer
77 terrain-vertex-size second iota [ draw-vertex-buffer-row ] each
80 : degrees ( deg -- rad )
83 :: eye-rotate ( yaw pitch v -- v' )
85 pitch degrees neg :> p
91 cosy 0.0 siny neg 3array
92 siny sinp * cosp cosy sinp * 3array
93 siny cosp * sinp neg cosy cosp * 3array 3array
96 : forward-vector ( player -- v )
98 { 0.0 0.0 $ MOVEMENT-SPEED } vneg eye-rotate ;
99 : rightward-vector ( player -- v )
101 { $ MOVEMENT-SPEED 0.0 0.0 } eye-rotate ;
103 : walk-forward ( player -- )
104 dup forward-vector [ v+ ] curry change-velocity drop ;
105 : walk-backward ( player -- )
106 dup forward-vector [ v- ] curry change-velocity drop ;
107 : walk-leftward ( player -- )
108 dup rightward-vector [ v- ] curry change-velocity drop ;
109 : walk-rightward ( player -- )
110 dup rightward-vector [ v+ ] curry change-velocity drop ;
112 [ { 0.0 $ JUMP 0.0 } v+ ] change-velocity drop ;
114 : clamp-pitch ( pitch -- pitch' )
117 : rotate-with-mouse ( player mouse -- )
118 [ dx>> MOUSE-SCALE * [ + ] curry change-yaw ]
119 [ dy>> MOUSE-SCALE * [ + clamp-pitch ] curry change-pitch ] bi
122 :: handle-input ( world -- )
123 world player>> :> player
124 read-keyboard keys>> :> keys
125 key-w keys nth [ player walk-forward ] when
126 key-s keys nth [ player walk-backward ] when
127 key-a keys nth [ player walk-leftward ] when
128 key-d keys nth [ player walk-rightward ] when
129 key-space keys nth [ player jump ] when
130 key-escape keys nth [ world close-window ] when
131 player read-mouse rotate-with-mouse
134 : apply-friction ( velocity -- velocity' )
137 : apply-gravity ( velocity -- velocity' )
138 1 over [ GRAVITY - ] change-nth ;
140 :: pixel-indices ( coords dim -- indices )
141 coords vfloor [ >integer ] map :> floor-coords
142 floor-coords first2 dim first * + :> base-index
143 base-index dim first + :> next-row-index
148 next-row-index 1 + 4array ;
150 :: terrain-height-at ( segment point -- height )
152 dim point v* :> pixel
153 pixel dup vfloor v- :> pixel-mantissa
154 segment bitmap>> 4 <groups> :> pixels
155 pixel dim pixel-indices :> indices
157 indices [ pixels nth COMPONENT-SCALE v. 255.0 / ] map
158 first4 pixel-mantissa bilerp ;
160 : collide ( segment location -- location' )
161 [ [ first ] [ third ] bi 2array terrain-height-at PLAYER-HEIGHT + ]
162 [ [ 1 ] 2dip [ max ] with change-nth ]
165 : tick-player ( world player -- )
166 [ apply-friction apply-gravity ] change-velocity
167 dup velocity>> [ v+ [ terrain-segment>> ] dip collide ] curry with change-location
170 M: terrain-world tick*
171 [ dup focused?>> [ handle-input ] [ drop ] if ]
172 [ dup player>> tick-player ] bi ;
174 M: terrain-world draw*
177 : set-heightmap-texture-parameters ( texture -- )
178 GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit
179 GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
180 GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
181 GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP_TO_EDGE glTexParameteri
182 GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE glTexParameteri ;
184 M: terrain-world begin-world
185 "2.0" { "GL_ARB_vertex_buffer_object" "GL_ARB_shader_objects" }
186 require-gl-version-or-extensions
187 GL_DEPTH_TEST glEnable
188 GL_TEXTURE_2D glEnable
189 GL_VERTEX_ARRAY glEnableClientState
190 0.5 0.5 0.5 1.0 glClearColor
191 PLAYER-START-LOCATION 0.0 0.0 { 0.0 0.0 0.0 } player boa >>player
192 <terrain> [ >>terrain ] keep
193 { 0 0 } terrain-segment [ >>terrain-segment ] keep
194 make-texture [ set-heightmap-texture-parameters ] keep >>terrain-texture
195 terrain-vertex-shader terrain-pixel-shader <simple-gl-program>
197 vertex-array >vertex-buffer >>terrain-vertex-buffer
198 TICK-LENGTH over <game-loop> [ >>game-loop ] keep start-loop
202 M: terrain-world end-world
205 [ game-loop>> stop-loop ]
206 [ terrain-vertex-buffer>> delete-gl-buffer ]
207 [ terrain-program>> delete-gl-program ]
208 [ terrain-texture>> delete-texture ]
211 M: terrain-world resize-world
212 GL_PROJECTION glMatrixMode
214 dim>> [ [ 0 0 ] dip first2 glViewport ]
215 [ frustum glFrustum ] bi ;
217 M: terrain-world draw-world*
218 [ set-modelview-matrix ]
219 [ terrain-texture>> GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
220 [ dup terrain-program>> [
221 [ "heightmap" glGetUniformLocation 0 glUniform1i ]
222 [ "component_scale" glGetUniformLocation COMPONENT-SCALE first4 glUniform4f ] bi
223 terrain-vertex-buffer>> draw-vertex-buffer
227 M: terrain-world focusable-child* drop t ;
228 M: terrain-world pref-dim* drop { 640 480 } ;
230 : terrain-window ( -- )
232 f T{ world-attributes
233 { world-class terrain-world }
235 { pixel-format-attributes {
238 T{ depth-bits { value 24 } }