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