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 ;
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: MOUSE-SCALE $[ 1.0 10.0 / ]
19 CONSTANT: MOVEMENT-SPEED $[ 1.0 16384.0 / ]
20 CONSTANT: FRICTION 0.95
21 CONSTANT: COMPONENT-SCALE { 0.5 0.01 0.002 0.0 }
23 CONSTANT: terrain-vertex-size { 512 512 }
24 CONSTANT: terrain-vertex-distance { $[ 1.0 512.0 / ] $[ 1.0 512.0 / ] }
25 CONSTANT: terrain-vertex-row-length $[ 512 1 + 2 * ]
28 location yaw pitch velocity ;
30 TUPLE: terrain-world < game-world
32 terrain terrain-segment terrain-texture terrain-program
33 terrain-vertex-buffer ;
35 M: terrain-world tick-length
38 : frustum ( dim -- -x x -y y near far )
40 NEAR-PLANE FOV / v*n first2 [ [ neg ] keep ] bi@
41 NEAR-PLANE FAR-PLANE ;
43 : set-modelview-matrix ( gadget -- )
44 GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT bitor glClear
45 GL_MODELVIEW glMatrixMode
48 [ pitch>> 1.0 0.0 0.0 glRotatef ]
49 [ yaw>> 0.0 1.0 0.0 glRotatef ]
50 [ location>> vneg first3 glTranslatef ] tri ;
52 : vertex-array-vertex ( x z -- vertex )
53 [ terrain-vertex-distance first * ]
54 [ terrain-vertex-distance second * ] bi*
55 [ 0 ] dip float-array{ } 3sequence ;
57 : vertex-array-row ( z -- vertices )
59 terrain-vertex-size first 1 + iota
60 2array [ first2 swap vertex-array-vertex ] product-map
63 : vertex-array ( -- vertices )
64 terrain-vertex-size second iota
65 [ vertex-array-row ] map concat ;
67 : >vertex-buffer ( bytes -- buffer )
68 [ GL_ARRAY_BUFFER ] dip GL_STATIC_DRAW <gl-buffer> ;
70 : draw-vertex-buffer-row ( i -- )
71 [ GL_TRIANGLE_STRIP ] dip
72 terrain-vertex-row-length * terrain-vertex-row-length
75 : draw-vertex-buffer ( buffer -- )
76 [ GL_ARRAY_BUFFER ] dip [
77 3 GL_FLOAT 0 f glVertexPointer
78 terrain-vertex-size second iota [ draw-vertex-buffer-row ] each
81 : degrees ( deg -- rad )
84 :: eye-rotate ( yaw pitch v -- v' )
86 pitch degrees neg :> p
92 cosy 0.0 siny neg 3array
93 siny sinp * cosp cosy sinp * 3array
94 siny cosp * sinp neg cosy cosp * 3array 3array
97 : forward-vector ( player -- v )
99 { 0.0 0.0 $ MOVEMENT-SPEED } vneg eye-rotate ;
100 : rightward-vector ( player -- v )
102 { $ MOVEMENT-SPEED 0.0 0.0 } eye-rotate ;
104 : walk-forward ( player -- )
105 dup forward-vector [ v+ ] curry change-velocity drop ;
106 : walk-backward ( player -- )
107 dup forward-vector [ v- ] curry change-velocity drop ;
108 : walk-leftward ( player -- )
109 dup rightward-vector [ v- ] curry change-velocity drop ;
110 : walk-rightward ( player -- )
111 dup rightward-vector [ v+ ] curry change-velocity drop ;
113 [ { 0.0 $ JUMP 0.0 } v+ ] change-velocity drop ;
115 : clamp-pitch ( pitch -- pitch' )
118 : rotate-with-mouse ( player mouse -- )
119 [ dx>> MOUSE-SCALE * [ + ] curry change-yaw ]
120 [ dy>> MOUSE-SCALE * [ + clamp-pitch ] curry change-pitch ] bi
123 :: handle-input ( world -- )
124 world player>> :> player
125 read-keyboard keys>> :> keys
126 key-w keys nth [ player walk-forward ] when
127 key-s keys nth [ player walk-backward ] when
128 key-a keys nth [ player walk-leftward ] when
129 key-d keys nth [ player walk-rightward ] when
130 key-space keys nth [ player jump ] when
131 key-escape keys nth [ world close-window ] when
132 player read-mouse rotate-with-mouse
135 : apply-friction ( velocity -- velocity' )
138 : apply-gravity ( velocity -- velocity' )
139 1 over [ GRAVITY - ] change-nth ;
141 :: pixel-indices ( coords dim -- indices )
142 coords vfloor [ >integer ] map :> floor-coords
143 floor-coords first2 dim first * + :> base-index
144 base-index dim first + :> next-row-index
149 next-row-index 1 + 4array ;
151 :: terrain-height-at ( segment point -- height )
153 dim point v* :> pixel
154 pixel dup vfloor v- :> pixel-mantissa
155 segment bitmap>> 4 <groups> :> pixels
156 pixel dim pixel-indices :> indices
158 indices [ pixels nth COMPONENT-SCALE v. 255.0 / ] map
159 first4 pixel-mantissa bilerp ;
161 : collide ( segment location -- location' )
162 [ [ first ] [ third ] bi 2array terrain-height-at PLAYER-HEIGHT + ]
163 [ [ 1 ] 2dip [ max ] with change-nth ]
166 : tick-player ( world player -- )
167 [ apply-friction apply-gravity ] change-velocity
168 dup velocity>> [ v+ [ terrain-segment>> ] dip collide ] curry with change-location
171 M: terrain-world tick*
172 [ dup focused?>> [ handle-input ] [ drop ] if ]
173 [ dup player>> tick-player ] bi ;
175 : set-heightmap-texture-parameters ( texture -- )
176 GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit
177 GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
178 GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
179 GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP_TO_EDGE glTexParameteri
180 GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE glTexParameteri ;
182 BEFORE: terrain-world begin-world
183 "2.0" { "GL_ARB_vertex_buffer_object" "GL_ARB_shader_objects" }
184 require-gl-version-or-extensions
185 GL_DEPTH_TEST glEnable
186 GL_TEXTURE_2D glEnable
187 GL_VERTEX_ARRAY glEnableClientState
188 0.5 0.5 0.5 1.0 glClearColor
189 PLAYER-START-LOCATION 0.0 0.0 { 0.0 0.0 0.0 } player boa >>player
190 <terrain> [ >>terrain ] keep
191 { 0 0 } terrain-segment [ >>terrain-segment ] keep
192 make-texture [ set-heightmap-texture-parameters ] keep >>terrain-texture
193 terrain-vertex-shader terrain-pixel-shader <simple-gl-program>
195 vertex-array >vertex-buffer >>terrain-vertex-buffer
198 AFTER: terrain-world end-world
200 [ terrain-vertex-buffer>> delete-gl-buffer ]
201 [ terrain-program>> delete-gl-program ]
202 [ terrain-texture>> delete-texture ]
205 M: terrain-world resize-world
206 GL_PROJECTION glMatrixMode
208 dim>> [ [ 0 0 ] dip first2 glViewport ]
209 [ frustum glFrustum ] bi ;
211 M: terrain-world draw-world*
212 [ set-modelview-matrix ]
213 [ terrain-texture>> GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
214 [ dup terrain-program>> [
215 [ "heightmap" glGetUniformLocation 0 glUniform1i ]
216 [ "component_scale" glGetUniformLocation COMPONENT-SCALE first4 glUniform4f ] bi
217 terrain-vertex-buffer>> draw-vertex-buffer
221 M: terrain-world pref-dim* drop { 640 480 } ;
223 : terrain-window ( -- )
225 f T{ world-attributes
226 { world-class terrain-world }
228 { pixel-format-attributes {
231 T{ depth-bits { value 24 } }