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 ( coords dim -- index )
141 [ drop first ] [ [ second ] [ first ] bi* * ] 2bi + ;
143 : terrain-height-at ( segment point -- height )
144 over dim>> [ v* vfloor ] [ pixel >integer ] bi
145 swap bitmap>> 4 <groups> nth COMPONENT-SCALE v. 255.0 / ;
147 : collide ( segment location -- location' )
148 [ [ first ] [ third ] bi 2array terrain-height-at PLAYER-HEIGHT + ]
149 [ [ 1 ] 2dip [ max ] with change-nth ]
152 : tick-player ( world player -- )
153 [ apply-friction apply-gravity ] change-velocity
154 dup velocity>> [ v+ [ terrain-segment>> ] dip collide ] curry with change-location
158 M: terrain-world tick*
159 [ dup focused?>> [ handle-input ] [ drop ] if ]
160 [ dup player>> tick-player ] bi ;
162 M: terrain-world draw*
165 : set-heightmap-texture-parameters ( texture -- )
166 GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit
167 GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
168 GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
169 GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP_TO_EDGE glTexParameteri
170 GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE glTexParameteri ;
172 M: terrain-world begin-world
173 "2.0" { "GL_ARB_vertex_buffer_object" "GL_ARB_shader_objects" }
174 require-gl-version-or-extensions
175 GL_DEPTH_TEST glEnable
176 GL_TEXTURE_2D glEnable
177 GL_VERTEX_ARRAY glEnableClientState
178 0.5 0.5 0.5 1.0 glClearColor
179 PLAYER-START-LOCATION 0.0 0.0 { 0.0 0.0 0.0 } player boa >>player
180 <terrain> [ >>terrain ] keep
181 { 0 0 } terrain-segment [ >>terrain-segment ] keep
182 make-texture [ set-heightmap-texture-parameters ] keep >>terrain-texture
183 terrain-vertex-shader terrain-pixel-shader <simple-gl-program>
185 vertex-array >vertex-buffer >>terrain-vertex-buffer
186 TICK-LENGTH over <game-loop> [ >>game-loop ] keep start-loop
190 M: terrain-world end-world
193 [ game-loop>> stop-loop ]
194 [ terrain-vertex-buffer>> delete-gl-buffer ]
195 [ terrain-program>> delete-gl-program ]
196 [ terrain-texture>> delete-texture ]
199 M: terrain-world resize-world
200 GL_PROJECTION glMatrixMode
202 dim>> [ [ 0 0 ] dip first2 glViewport ]
203 [ frustum glFrustum ] bi ;
205 M: terrain-world draw-world*
206 [ set-modelview-matrix ]
207 [ terrain-texture>> GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
208 [ dup terrain-program>> [
209 [ "heightmap" glGetUniformLocation 0 glUniform1i ]
210 [ "component_scale" glGetUniformLocation COMPONENT-SCALE first4 glUniform4f ] bi
211 terrain-vertex-buffer>> draw-vertex-buffer
215 M: terrain-world focusable-child* drop t ;
216 M: terrain-world pref-dim* drop { 640 480 } ;
218 : terrain-window ( -- )
220 f T{ world-attributes
221 { world-class terrain-world }
223 { pixel-format-attributes {
226 T{ depth-bits { value 24 } }