]> gitweb.factorcode.org Git - factor.git/blob - extra/terrain/terrain.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / extra / terrain / terrain.factor
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 ui.gestures combinators.short-circuit ;
10 IN: terrain
11
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: VELOCITY-MODIFIER-NORMAL { 1.0 1.0 1.0 }
17 CONSTANT: VELOCITY-MODIFIER-FAST { 2.0 1.0 2.0 }
18 CONSTANT: PLAYER-HEIGHT $[ 1.0 256.0 / ]
19 CONSTANT: GRAVITY $[ 1.0 4096.0 / ]
20 CONSTANT: JUMP $[ 1.0 1024.0 / ]
21 CONSTANT: MOUSE-SCALE $[ 1.0 10.0 / ]
22 CONSTANT: MOVEMENT-SPEED $[ 1.0 16384.0 / ]
23 CONSTANT: FRICTION { 0.95 0.99 0.95 }
24 CONSTANT: COMPONENT-SCALE { 0.5 0.01 0.0005 0.0 }
25 CONSTANT: SKY-PERIOD 1200
26 CONSTANT: SKY-SPEED 0.0005
27
28 CONSTANT: terrain-vertex-size { 512 512 }
29 CONSTANT: terrain-vertex-distance { $[ 1.0 512.0 / ] $[ 1.0 512.0 / ] }
30 CONSTANT: terrain-vertex-row-length $[ 512 1 + 2 * ]
31
32 TUPLE: player
33     location yaw pitch velocity velocity-modifier
34     reverse-time ;
35
36 TUPLE: terrain-world < game-world
37     player
38     sky-image sky-texture sky-program
39     terrain terrain-segment terrain-texture terrain-program
40     terrain-vertex-buffer
41     history ;
42
43 : <player> ( -- player )
44     player new
45         PLAYER-START-LOCATION >>location
46         0.0 >>yaw
47         0.0 >>pitch
48         { 0.0 0.0 0.0 } >>velocity
49         VELOCITY-MODIFIER-NORMAL >>velocity-modifier ;
50
51 M: terrain-world tick-length
52     drop 1000 30 /i ;
53
54 : frustum ( dim -- -x x -y y near far )
55     dup first2 min v/n
56     NEAR-PLANE FOV / v*n first2 [ [ neg ] keep ] bi@
57     NEAR-PLANE FAR-PLANE ;
58
59 : set-modelview-matrix ( gadget -- )
60     GL_DEPTH_BUFFER_BIT glClear
61     GL_MODELVIEW glMatrixMode
62     glLoadIdentity
63     player>>
64     [ pitch>> 1.0 0.0 0.0 glRotatef ]
65     [ yaw>> 0.0 1.0 0.0 glRotatef ]
66     [ location>> vneg first3 glTranslatef ] tri ;
67
68 : vertex-array-vertex ( x z -- vertex )
69     [ terrain-vertex-distance first * ]
70     [ terrain-vertex-distance second * ] bi*
71     [ 0 ] dip float-array{ } 3sequence ;
72
73 : vertex-array-row ( z -- vertices )
74     dup 1 + 2array
75     terrain-vertex-size first 1 + iota
76     2array [ first2 swap vertex-array-vertex ] product-map
77     concat ;
78
79 : vertex-array ( -- vertices )
80     terrain-vertex-size second iota
81     [ vertex-array-row ] map concat ;
82
83 : >vertex-buffer ( bytes -- buffer )
84     [ GL_ARRAY_BUFFER ] dip GL_STATIC_DRAW <gl-buffer> ;
85
86 : draw-vertex-buffer-row ( i -- )
87     [ GL_TRIANGLE_STRIP ] dip
88     terrain-vertex-row-length * terrain-vertex-row-length
89     glDrawArrays ;
90
91 : draw-vertex-buffer ( buffer -- )
92     [ GL_ARRAY_BUFFER ] dip [
93         3 GL_FLOAT 0 f glVertexPointer
94         terrain-vertex-size second iota [ draw-vertex-buffer-row ] each
95     ] with-gl-buffer ;
96
97 : degrees ( deg -- rad )
98     pi 180.0 / * ;
99
100 :: eye-rotate ( yaw pitch v -- v' )
101     yaw degrees neg :> y
102     pitch degrees neg :> p
103     y cos :> cosy
104     y sin :> siny
105     p cos :> cosp
106     p sin :> sinp
107
108     cosy         0.0       siny        neg  3array
109     siny sinp *  cosp      cosy sinp *      3array
110     siny cosp *  sinp neg  cosy cosp *      3array 3array
111     v swap v.m ;
112
113 : forward-vector ( player -- v )
114     yaw>> 0.0
115     ${ 0.0 0.0 MOVEMENT-SPEED } vneg eye-rotate ;
116 : rightward-vector ( player -- v )
117     yaw>> 0.0
118     ${ MOVEMENT-SPEED 0.0 0.0 } eye-rotate ;
119 : clamp-pitch ( pitch -- pitch' )
120     90.0 min -90.0 max ;
121
122
123 : walk-forward ( player -- )
124     dup forward-vector [ v+ ] curry change-velocity drop ;
125 : walk-backward ( player -- )
126     dup forward-vector [ v- ] curry change-velocity drop ;
127 : walk-leftward ( player -- )
128     dup rightward-vector [ v- ] curry change-velocity drop ;
129 : walk-rightward ( player -- )
130     dup rightward-vector [ v+ ] curry change-velocity drop ;
131 : jump ( player -- )
132     [ ${ 0.0 JUMP 0.0 } v+ ] change-velocity drop ;
133 : rotate-leftward ( player x -- )
134     [ - ] curry change-yaw drop ;
135 : rotate-rightward ( player x -- )
136     [ + ] curry change-yaw drop ;
137 : look-horizontally ( player x -- )
138     [ + ] curry change-yaw drop ;
139 : look-vertically ( player x -- )
140     [ + clamp-pitch ] curry change-pitch drop ;
141
142
143 : rotate-with-mouse ( player mouse -- )
144     [ dx>> MOUSE-SCALE * look-horizontally ]
145     [ dy>> MOUSE-SCALE * look-vertically ] 2bi ;
146
147
148 terrain-world H{
149     { T{ key-down { mods { A+ } } { sym "RET" } } [ toggle-fullscreen ] }
150 } set-gestures
151
152 :: handle-input ( world -- )
153     world player>> :> player
154     read-keyboard keys>> :> keys
155
156     key-left-shift keys nth
157     VELOCITY-MODIFIER-FAST VELOCITY-MODIFIER-NORMAL ? player (>>velocity-modifier)
158
159     {
160         [ key-1 keys nth 1  f ? ]
161         [ key-2 keys nth 2  f ? ]
162         [ key-3 keys nth 3  f ? ]
163         [ key-4 keys nth 4  f ? ]
164         [ key-5 keys nth 10000 f ? ]
165     } 0|| player (>>reverse-time)
166
167     key-w keys nth [ player walk-forward ] when 
168     key-s keys nth [ player walk-backward ] when 
169     key-a keys nth [ player walk-leftward ] when 
170     key-d keys nth [ player walk-rightward ] when 
171     key-q keys nth [ player -1 look-horizontally ] when 
172     key-e keys nth [ player 1 look-horizontally ] when 
173     key-left-arrow keys nth [ player -1 look-horizontally ] when 
174     key-right-arrow keys nth [ player 1 look-horizontally ] when 
175     key-down-arrow keys nth [ player 1 look-vertically ] when 
176     key-up-arrow keys nth [ player -1 look-vertically ] when 
177     key-space keys nth [ player jump ] when 
178     key-escape keys nth [ world close-window ] when
179     player read-mouse rotate-with-mouse
180     reset-mouse ;
181
182 : apply-friction ( velocity -- velocity' )
183     FRICTION v* ;
184
185 : apply-gravity ( velocity -- velocity' )
186     1 over [ GRAVITY - ] change-nth ;
187
188 : clamp-coords ( coords dim -- coords' )
189     [ { 0 0 } vmax ] dip { 2 2 } v- vmin ;
190
191 :: pixel-indices ( coords dim -- indices )
192     coords vfloor [ >integer ] map dim clamp-coords :> floor-coords
193     floor-coords first2 dim first * + :> base-index
194     base-index dim first + :> next-row-index
195
196     base-index
197     base-index 1 +
198     next-row-index
199     next-row-index 1 + 4array ;
200
201 :: terrain-height-at ( segment point -- height )
202     segment dim>> :> dim
203     dim point v* :> pixel
204     pixel dup vfloor v- :> pixel-mantissa
205     segment bitmap>> 4 <groups> :> pixels
206     pixel dim pixel-indices :> indices
207     
208     indices [ pixels nth COMPONENT-SCALE v. 255.0 / ] map
209     first4 pixel-mantissa bilerp ;
210
211 : collide ( segment location -- location' )
212     [ [ first ] [ third ] bi 2array terrain-height-at PLAYER-HEIGHT + ]
213     [ [ 1 ] 2dip [ max ] with change-nth ]
214     [ ] tri ;
215
216 : scaled-velocity ( player -- velocity )
217     [ velocity>> ] [ velocity-modifier>> ] bi v* ;
218
219 : save-history ( world player -- )
220     clone swap history>> push ;
221
222 :: tick-player-reverse ( world player -- )
223     player reverse-time>> :> reverse-time
224     world history>> :> history
225     history length 0 > [
226         history length reverse-time 1 - - 1 max history set-length
227         history pop world (>>player)
228     ] when ;
229
230 : tick-player-forward ( world player -- )
231     2dup save-history
232     [ apply-friction apply-gravity ] change-velocity
233     dup scaled-velocity [ v+ [ terrain-segment>> ] dip collide ] curry with change-location
234     drop ;
235
236 : tick-player ( world player -- )
237     dup reverse-time>> [
238         tick-player-reverse
239     ] [
240         tick-player-forward
241     ] if ;
242
243 M: terrain-world tick*
244     [ dup focused?>> [ handle-input ] [ drop ] if ]
245     [ dup player>> tick-player ] bi ;
246
247 : set-texture-parameters ( texture -- )
248     GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit
249     GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
250     GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
251     GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP_TO_EDGE glTexParameteri
252     GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE glTexParameteri ;
253
254 : sky-gradient ( world -- t )
255     game-loop>> tick-number>> SKY-PERIOD mod SKY-PERIOD /f ;
256 : sky-theta ( world -- theta )
257     game-loop>> tick-number>> SKY-SPEED * ;
258
259 BEFORE: terrain-world begin-world
260     "2.0" { "GL_ARB_vertex_buffer_object" "GL_ARB_shader_objects" }
261     require-gl-version-or-extensions
262     GL_DEPTH_TEST glEnable
263     GL_TEXTURE_2D glEnable
264     GL_VERTEX_ARRAY glEnableClientState
265     <player> >>player
266     V{ } clone >>history
267     <perlin-noise-table> 0.01 0.01 <scale> { 512 512 } perlin-noise-image
268     [ >>sky-image ] keep
269     make-texture [ set-texture-parameters ] keep >>sky-texture
270     <terrain> [ >>terrain ] keep
271     { 0 0 } terrain-segment [ >>terrain-segment ] keep
272     make-texture [ set-texture-parameters ] keep >>terrain-texture
273     sky-vertex-shader sky-pixel-shader <simple-gl-program>
274     >>sky-program
275     terrain-vertex-shader terrain-pixel-shader <simple-gl-program>
276     >>terrain-program
277     vertex-array >vertex-buffer >>terrain-vertex-buffer
278     drop ;
279
280 AFTER: terrain-world end-world
281     {
282         [ terrain-vertex-buffer>> delete-gl-buffer ]
283         [ terrain-program>> delete-gl-program ]
284         [ terrain-texture>> delete-texture ]
285         [ sky-program>> delete-gl-program ]
286         [ sky-texture>> delete-texture ]
287     } cleave ;
288
289 M: terrain-world resize-world
290     GL_PROJECTION glMatrixMode
291     glLoadIdentity
292     dim>> [ [ 0 0 ] dip first2 glViewport ]
293     [ frustum glFrustum ] bi ;
294
295 M: terrain-world draw-world*
296     {
297         [ set-modelview-matrix ]
298         [ terrain-texture>> GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
299         [ sky-texture>> GL_TEXTURE_2D GL_TEXTURE1 bind-texture-unit ]
300         [ GL_DEPTH_TEST glDisable dup sky-program>> [
301             [ nip "sky" glGetUniformLocation 1 glUniform1i ]
302             [ "sky_gradient" glGetUniformLocation swap sky-gradient glUniform1f ]
303             [ "sky_theta" glGetUniformLocation swap sky-theta glUniform1f ] 2tri
304             { -1.0 -1.0 } { 2.0 2.0 } gl-fill-rect
305         ] with-gl-program ]
306         [ GL_DEPTH_TEST glEnable dup terrain-program>> [
307             [ "heightmap" glGetUniformLocation 0 glUniform1i ]
308             [ "component_scale" glGetUniformLocation COMPONENT-SCALE first4 glUniform4f ] bi
309             terrain-vertex-buffer>> draw-vertex-buffer
310         ] with-gl-program ]
311     } cleave gl-error ;
312
313 M: terrain-world pref-dim* drop { 640 480 } ;
314
315 : terrain-window ( -- )
316     [
317         f T{ world-attributes
318             { world-class terrain-world }
319             { title "Terrain" }
320             { pixel-format-attributes {
321                 windowed
322                 double-buffered
323                 T{ depth-bits { value 24 } }
324             } }
325             { grab-input? t }
326         } open-window
327     ] with-ui ;
328
329 MAIN: terrain-window