]> gitweb.factorcode.org Git - factor.git/blob - extra/terrain/terrain.factor
Merge branch 'reentrantvm' of git://github.com/phildawes/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 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: 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 1000 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     [ [ first ] [ third ] bi 2array terrain-height-at PLAYER-HEIGHT + ]
182     [ [ 1 ] 2dip [ max ] with change-nth ]
183     [ ] tri ;
184
185 : scaled-velocity ( player -- velocity )
186     [ velocity>> ] [ velocity-modifier>> ] bi v* ;
187
188 : save-history ( world player -- )
189     clone swap history>> push ;
190
191 :: tick-player-reverse ( world player -- )
192     player reverse-time>> :> reverse-time
193     world history>> :> history
194     history length 0 > [
195         history length reverse-time 1 - - 1 max history set-length
196         history pop world (>>player)
197     ] when ;
198
199 : tick-player-forward ( world player -- )
200     2dup save-history
201     [ apply-friction apply-gravity ] change-velocity
202     dup scaled-velocity [ v+ [ terrain-segment>> ] dip collide ] curry with change-location
203     drop ;
204
205 : tick-player ( world player -- )
206     dup reverse-time>>
207     [ tick-player-reverse ]
208     [ tick-player-forward ] if ;
209
210 M: terrain-world tick*
211     [ dup focused?>> [ handle-input ] [ drop ] if ]
212     [ dup player>> tick-player ] bi ;
213
214 : set-texture-parameters ( texture -- )
215     GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit
216     GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
217     GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
218     GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP_TO_EDGE glTexParameteri
219     GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE glTexParameteri ;
220
221 : sky-gradient ( world -- t )
222     game-loop>> tick-number>> SKY-PERIOD mod SKY-PERIOD /f ;
223 : sky-theta ( world -- theta )
224     game-loop>> tick-number>> SKY-SPEED * ;
225
226 BEFORE: terrain-world begin-world
227     "2.0" { "GL_ARB_vertex_buffer_object" "GL_ARB_shader_objects" }
228     require-gl-version-or-extensions
229     GL_DEPTH_TEST glEnable
230     GL_TEXTURE_2D glEnable
231     GL_VERTEX_ARRAY glEnableClientState
232     <player> >>player
233     V{ } clone >>history
234     <perlin-noise-table> 0.01 float-4-with scale-matrix4 { 512 512 } perlin-noise-image
235     [ >>sky-image ] keep
236     make-texture [ set-texture-parameters ] keep >>sky-texture
237     <terrain> [ >>terrain ] keep
238     float-4{ 0.0 0.0 0.0 1.0 } terrain-segment [ >>terrain-segment ] keep
239     make-texture [ set-texture-parameters ] keep >>terrain-texture
240     sky-vertex-shader sky-pixel-shader <simple-gl-program>
241     >>sky-program
242     terrain-vertex-shader terrain-pixel-shader <simple-gl-program>
243     >>terrain-program
244     terrain-vertex-size <grid-mesh> >>terrain-mesh
245     drop ;
246
247 AFTER: terrain-world end-world
248     {
249         [ terrain-mesh>> dispose ]
250         [ terrain-program>> delete-gl-program ]
251         [ terrain-texture>> delete-texture ]
252         [ sky-program>> delete-gl-program ]
253         [ sky-texture>> delete-texture ]
254     } cleave ;
255
256 M: terrain-world resize-world
257     GL_PROJECTION glMatrixMode
258     glLoadIdentity
259     dim>> [ [ 0 0 ] dip first2 glViewport ]
260     [ frustum glFrustum ] bi ;
261
262 M: terrain-world draw-world*
263     {
264         [ set-modelview-matrix ]
265         [ terrain-texture>> GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
266         [ sky-texture>> GL_TEXTURE_2D GL_TEXTURE1 bind-texture-unit ]
267         [ GL_DEPTH_TEST glDisable dup sky-program>> [
268             [ nip "sky" glGetUniformLocation 1 glUniform1i ]
269             [ "sky_gradient" glGetUniformLocation swap sky-gradient glUniform1f ]
270             [ "sky_theta" glGetUniformLocation swap sky-theta glUniform1f ] 2tri
271             { -1.0 -1.0 } { 2.0 2.0 } gl-fill-rect
272         ] with-gl-program ]
273         [ GL_DEPTH_TEST glEnable dup terrain-program>> [
274             [ "heightmap" glGetUniformLocation 0 glUniform1i ]
275             [ "component_scale" glGetUniformLocation COMPONENT-SCALE first4 glUniform4f ] bi
276             terrain-mesh>> draw-grid-mesh
277         ] with-gl-program ]
278     } cleave gl-error ;
279
280 M: terrain-world pref-dim* drop { 1024 768 } ;
281
282 : terrain-window ( -- )
283     [
284         f T{ world-attributes
285             { world-class terrain-world }
286             { title "Terrain" }
287             { pixel-format-attributes {
288                 windowed
289                 double-buffered
290                 T{ depth-bits { value 24 } }
291             } }
292             { grab-input? t }
293         } open-window
294     ] with-ui ;
295
296 MAIN: terrain-window