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