]> gitweb.factorcode.org Git - factor.git/blob - extra/terrain/terrain.factor
math.trig: use deg>rad in a few places to reduce duplication.
[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 math.trig
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
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/8192. 0.0 0.0 }
25 CONSTANT: JUMP 1/2048.
26 CONSTANT: MOUSE-SCALE 1/20.
27 CONSTANT: MOVEMENT-SPEED 1/32768.
28 CONSTANT: FRICTION float-4{ 0.97 0.995 0.97 1.0 }
29 CONSTANT: COMPONENT-SCALE float-4{ 0.5 0.01 0.0005 0.0 }
30 CONSTANT: SKY-PERIOD 2400
31 CONSTANT: SKY-SPEED 0.00025
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 : frustum ( dim -- -x x -y y near far )
59     dup first2 min v/n
60     NEAR-PLANE FOV / v*n first2 [ [ neg ] keep ] bi@
61     NEAR-PLANE FAR-PLANE ;
62
63 : set-modelview-matrix ( gadget -- )
64     GL_DEPTH_BUFFER_BIT glClear
65     GL_MODELVIEW glMatrixMode
66     glLoadIdentity
67     player>>
68     [ pitch>> 1.0 0.0 0.0 glRotatef ]
69     [ yaw>> 0.0 1.0 0.0 glRotatef ]
70     [ location>> vneg first3 glTranslatef ] tri ;
71
72 TYPED: eye-rotate ( yaw: float pitch: float v: float-4 -- v': float-4 )
73     [ float-4{  0.0 -1.0 0.0 0.0 } swap deg>rad rotation-matrix4 ]
74     [ float-4{ -1.0  0.0 0.0 0.0 } swap deg>rad rotation-matrix4 m4. ]
75     [ m4.v ] tri* float-4{ t t t f } vand ;
76
77 : forward-vector ( player -- v )
78     yaw>> 0.0
79     float-4{ 0.0 0.0 $ MOVEMENT-SPEED 1.0 } vneg eye-rotate ; inline
80 : rightward-vector ( player -- v )
81     yaw>> 0.0
82     float-4{ $ MOVEMENT-SPEED 0.0 0.0 1.0 } eye-rotate ; inline
83 : clamp-pitch ( pitch -- pitch' )
84     -90.0 90.0 clamp ; inline
85
86 : walk-forward ( player -- )
87     dup forward-vector [ v+ ] curry change-velocity drop ; inline
88 : walk-backward ( player -- )
89     dup forward-vector [ v- ] curry change-velocity drop ; inline
90 : walk-leftward ( player -- )
91     dup rightward-vector [ v- ] curry change-velocity drop ; inline
92 : walk-rightward ( player -- )
93     dup rightward-vector [ v+ ] curry change-velocity drop ; inline
94 : jump ( player -- )
95     [ float-4{ 0.0 $ JUMP 0.0 0.0 } v+ ] change-velocity drop ; inline
96 : rotate-leftward ( player x -- )
97     [ - ] curry change-yaw drop ; inline
98 : rotate-rightward ( player x -- )
99     [ + ] curry change-yaw drop ; inline
100 : look-horizontally ( player x -- )
101     [ + ] curry change-yaw drop ; inline
102 : look-vertically ( player x -- )
103     [ + clamp-pitch ] curry change-pitch drop ; inline
104
105
106 : rotate-with-mouse ( player mouse -- )
107     [ dx>> MOUSE-SCALE * look-horizontally ]
108     [ dy>> MOUSE-SCALE * look-vertically ] 2bi ;
109
110
111 terrain-world H{
112     { T{ key-down { mods { A+ } } { sym "RET" } } [ toggle-fullscreen ] }
113 } set-gestures
114
115 :: handle-input ( world -- )
116     world player>> :> player
117     read-keyboard keys>> :> keys
118
119     key-left-shift keys nth
120     VELOCITY-MODIFIER-FAST VELOCITY-MODIFIER-NORMAL ? player velocity-modifier<<
121
122     {
123         [ key-1 keys nth 1  f ? ]
124         [ key-2 keys nth 2  f ? ]
125         [ key-3 keys nth 3  f ? ]
126         [ key-4 keys nth 4  f ? ]
127         [ key-5 keys nth 10000 f ? ]
128     } 0|| player reverse-time<<
129
130     key-w keys nth [ player walk-forward ] when
131     key-s keys nth [ player walk-backward ] when
132     key-a keys nth [ player walk-leftward ] when
133     key-d keys nth [ player walk-rightward ] when
134     key-q keys nth [ player -1 look-horizontally ] when
135     key-e keys nth [ player 1 look-horizontally ] when
136     key-left-arrow keys nth [ player -1 look-horizontally ] when
137     key-right-arrow keys nth [ player 1 look-horizontally ] when
138     key-down-arrow keys nth [ player 1 look-vertically ] when
139     key-up-arrow keys nth [ player -1 look-vertically ] when
140     key-space keys nth [ player jump ] when
141     key-escape keys nth [ world close-window ] when
142     player read-mouse rotate-with-mouse
143     reset-mouse ;
144
145 : apply-friction ( velocity -- velocity' )
146     FRICTION v* ;
147
148 : apply-gravity ( velocity -- velocity' )
149     GRAVITY v+ ;
150
151 : clamp-coords ( coords dim -- coords' )
152     { 0 0 } swap { 2 2 } v- vclamp ;
153
154 :: pixel-indices ( coords dim -- indices )
155     coords vfloor v>integer dim clamp-coords :> floor-coords
156     floor-coords first2 dim first * + :> base-index
157     base-index dim first + :> next-row-index
158
159     base-index
160     base-index 1 +
161     next-row-index
162     next-row-index 1 + 4array ;
163
164 :: terrain-height-at ( segment point -- height )
165     segment dim>> :> dim
166     dim point v* :> pixel
167     pixel dup vfloor v- :> pixel-mantissa
168     segment bitmap>> 4 <groups> :> pixels
169     pixel dim pixel-indices :> indices
170
171     indices [ pixels nth COMPONENT-SCALE v. 255.0 / ] map
172     first4 pixel-mantissa bilerp ;
173
174 : (collide) ( segment location -- location' )
175     [
176         { 0 2 3 3 } vshuffle terrain-height-at PLAYER-HEIGHT +
177         -1/0. swap -1/0. -1/0. float-4-boa
178     ] keep vmax ; inline
179
180 TYPED:: collide ( world: terrain-world player: player -- )
181     world terrain-segment>> :> segment
182     player location>> :> location
183     segment location (collide) :> location'
184
185     location location' = not [
186         player
187             location' >>location
188             [ BOUNCE v* ] change-velocity
189             drop
190     ] when ;
191
192 : scaled-velocity ( player -- velocity )
193     [ velocity>> ] [ velocity-modifier>> ] bi v* ;
194
195 : save-history ( world player -- )
196     clone swap history>> push ;
197
198 :: tick-player-reverse ( world player -- )
199     player reverse-time>> :> reverse-time
200     world history>> :> history
201     history length 0 > [
202         history length reverse-time 1 - - 1 max history set-length
203         history pop world player<<
204     ] when ;
205
206 : tick-player-forward ( world player -- )
207     2dup save-history
208     [ apply-friction apply-gravity ] change-velocity
209     dup scaled-velocity [ v+ ] curry change-location
210     collide ;
211
212 : tick-player ( world player -- )
213     dup reverse-time>>
214     [ tick-player-reverse ]
215     [ tick-player-forward ] if ;
216
217 M: terrain-world tick-game-world
218     [ dup focused?>> [ handle-input ] [ drop ] if ]
219     [ dup player>> tick-player ] bi ;
220
221 : set-texture-parameters ( texture -- )
222     GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit
223     GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
224     GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
225     GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP_TO_EDGE glTexParameteri
226     GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE glTexParameteri ;
227
228 : sky-gradient ( world -- t )
229     game-loop>> tick#>> SKY-PERIOD mod SKY-PERIOD /f ;
230 : sky-theta ( world -- theta )
231     game-loop>> tick#>> SKY-SPEED * ;
232
233 M: terrain-world begin-game-world
234     "2.0" { "GL_ARB_vertex_buffer_object" "GL_ARB_shader_objects" }
235     require-gl-version-or-extensions
236     GL_DEPTH_TEST glEnable
237     GL_TEXTURE_2D glEnable
238     GL_VERTEX_ARRAY glEnableClientState
239     <player> >>player
240     V{ } clone >>history
241     <perlin-noise-table> 0.01 float-4-with scale-matrix4 { 512 512 } perlin-noise-image
242     [ >>sky-image ] keep
243     make-texture [ set-texture-parameters ] keep >>sky-texture
244     <terrain> [ >>terrain ] keep
245     float-4{ 0.0 0.0 0.0 1.0 } terrain-segment [ >>terrain-segment ] keep
246     make-texture [ set-texture-parameters ] keep >>terrain-texture
247     sky-vertex-shader sky-pixel-shader <simple-gl-program>
248     >>sky-program
249     terrain-vertex-shader terrain-pixel-shader <simple-gl-program>
250     >>terrain-program
251     terrain-vertex-size <grid-mesh> >>terrain-mesh
252     drop ;
253
254 M: terrain-world end-game-world
255     {
256         [ terrain-mesh>> dispose ]
257         [ terrain-program>> delete-gl-program ]
258         [ terrain-texture>> delete-texture ]
259         [ sky-program>> delete-gl-program ]
260         [ sky-texture>> delete-texture ]
261     } cleave ;
262
263 M: terrain-world resize-world
264     GL_PROJECTION glMatrixMode
265     glLoadIdentity
266     dim>> [ [ { 0 0 } ] dip gl-viewport ]
267     [ frustum glFrustum ] bi ;
268
269 M: terrain-world draw-world*
270     {
271         [ set-modelview-matrix ]
272         [ terrain-texture>> GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
273         [ sky-texture>> GL_TEXTURE_2D GL_TEXTURE1 bind-texture-unit ]
274         [ GL_DEPTH_TEST glDisable dup sky-program>> [
275             [ nip "sky" glGetUniformLocation 1 glUniform1i ]
276             [ "sky_gradient" glGetUniformLocation swap sky-gradient glUniform1f ]
277             [ "sky_theta" glGetUniformLocation swap sky-theta glUniform1f ] 2tri
278             { -1.0 -1.0 } { 2.0 2.0 } gl-fill-rect
279         ] with-gl-program ]
280         [ GL_DEPTH_TEST glEnable dup terrain-program>> [
281             [ "heightmap" glGetUniformLocation 0 glUniform1i ]
282             [ "component_scale" glGetUniformLocation COMPONENT-SCALE first4 glUniform4f ] bi
283             terrain-mesh>> draw-grid-mesh
284         ] with-gl-program ]
285     } cleave gl-error ;
286
287 GAME: terrain-game {
288         { world-class terrain-world }
289         { title "Terrain" }
290         { pixel-format-attributes {
291             windowed
292             double-buffered
293             T{ depth-bits { value 24 } }
294         } }
295         { use-game-input? t }
296         { grab-input? t }
297         { pref-dim { 1024 768 } }
298         { tick-interval-nanos $[ 60 fps ] }
299     } ;