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