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