]> gitweb.factorcode.org Git - factor.git/blob - extra/terrain/terrain.factor
Delete empty unit tests files, remove 1- and 1+, reorder IN: lines in a lot of places...
[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.matrices math.order
5 math.vectors opengl opengl.capabilities opengl.gl
6 opengl.shaders opengl.textures opengl.textures.private
7 sequences sequences.product specialized-arrays.float
8 terrain.generation terrain.shaders ui ui.gadgets
9 ui.gadgets.worlds ui.pixel-formats game-worlds method-chains
10 math.affine-transforms noise ui.gestures combinators.short-circuit
11 destructors grid-meshes ;
12 IN: terrain
13
14 CONSTANT: FOV $[ 2.0 sqrt 1 + ]
15 CONSTANT: NEAR-PLANE $[ 1.0 1024.0 / ]
16 CONSTANT: FAR-PLANE 2.0
17 CONSTANT: PLAYER-START-LOCATION { 0.5 0.51 0.5 }
18 CONSTANT: VELOCITY-MODIFIER-NORMAL { 1.0 1.0 1.0 }
19 CONSTANT: VELOCITY-MODIFIER-FAST { 2.0 1.0 2.0 }
20 CONSTANT: PLAYER-HEIGHT $[ 1.0 256.0 / ]
21 CONSTANT: GRAVITY $[ 1.0 4096.0 / ]
22 CONSTANT: JUMP $[ 1.0 1024.0 / ]
23 CONSTANT: MOUSE-SCALE $[ 1.0 10.0 / ]
24 CONSTANT: MOVEMENT-SPEED $[ 1.0 16384.0 / ]
25 CONSTANT: FRICTION { 0.95 0.99 0.95 }
26 CONSTANT: COMPONENT-SCALE { 0.5 0.01 0.0005 0.0 }
27 CONSTANT: SKY-PERIOD 1200
28 CONSTANT: SKY-SPEED 0.0005
29
30 CONSTANT: terrain-vertex-size { 512 512 }
31
32 TUPLE: player
33     location yaw pitch velocity velocity-modifier
34     reverse-time ;
35
36 TUPLE: terrain-world < game-world
37     player
38     sky-image sky-texture sky-program
39     terrain terrain-segment terrain-texture terrain-program
40     terrain-mesh
41     history ;
42
43 : <player> ( -- player )
44     player new
45         PLAYER-START-LOCATION >>location
46         0.0 >>yaw
47         0.0 >>pitch
48         { 0.0 0.0 0.0 } >>velocity
49         VELOCITY-MODIFIER-NORMAL >>velocity-modifier ;
50
51 M: terrain-world tick-length
52     drop 1000 30 /i ;
53
54 : frustum ( dim -- -x x -y y near far )
55     dup first2 min v/n
56     NEAR-PLANE FOV / v*n first2 [ [ neg ] keep ] bi@
57     NEAR-PLANE FAR-PLANE ;
58
59 : set-modelview-matrix ( gadget -- )
60     GL_DEPTH_BUFFER_BIT glClear
61     GL_MODELVIEW glMatrixMode
62     glLoadIdentity
63     player>>
64     [ pitch>> 1.0 0.0 0.0 glRotatef ]
65     [ yaw>> 0.0 1.0 0.0 glRotatef ]
66     [ location>> vneg first3 glTranslatef ] tri ;
67
68 : degrees ( deg -- rad )
69     pi 180.0 / * ;
70
71 :: eye-rotate ( yaw pitch v -- v' )
72     yaw degrees neg :> y
73     pitch degrees neg :> p
74     y cos :> cosy
75     y sin :> siny
76     p cos :> cosp
77     p sin :> sinp
78
79     cosy         0.0       siny        neg  3array
80     siny sinp *  cosp      cosy sinp *      3array
81     siny cosp *  sinp neg  cosy cosp *      3array 3array
82     v swap v.m ;
83
84 : forward-vector ( player -- v )
85     yaw>> 0.0
86     ${ 0.0 0.0 MOVEMENT-SPEED } vneg eye-rotate ;
87 : rightward-vector ( player -- v )
88     yaw>> 0.0
89     ${ MOVEMENT-SPEED 0.0 0.0 } eye-rotate ;
90 : clamp-pitch ( pitch -- pitch' )
91     -90.0 90.0 clamp ;
92
93 : walk-forward ( player -- )
94     dup forward-vector [ v+ ] curry change-velocity drop ;
95 : walk-backward ( player -- )
96     dup forward-vector [ v- ] curry change-velocity drop ;
97 : walk-leftward ( player -- )
98     dup rightward-vector [ v- ] curry change-velocity drop ;
99 : walk-rightward ( player -- )
100     dup rightward-vector [ v+ ] curry change-velocity drop ;
101 : jump ( player -- )
102     [ ${ 0.0 JUMP 0.0 } v+ ] change-velocity drop ;
103 : rotate-leftward ( player x -- )
104     [ - ] curry change-yaw drop ;
105 : rotate-rightward ( player x -- )
106     [ + ] curry change-yaw drop ;
107 : look-horizontally ( player x -- )
108     [ + ] curry change-yaw drop ;
109 : look-vertically ( player x -- )
110     [ + clamp-pitch ] curry change-pitch drop ;
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     1 over [ GRAVITY - ] change-nth ;
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     [ [ first ] [ third ] bi 2array terrain-height-at PLAYER-HEIGHT + ]
183     [ [ 1 ] 2dip [ max ] with change-nth ]
184     [ ] tri ;
185
186 : scaled-velocity ( player -- velocity )
187     [ velocity>> ] [ velocity-modifier>> ] bi v* ;
188
189 : save-history ( world player -- )
190     clone swap history>> push ;
191
192 :: tick-player-reverse ( world player -- )
193     player reverse-time>> :> reverse-time
194     world history>> :> history
195     history length 0 > [
196         history length reverse-time 1 - - 1 max history set-length
197         history pop world (>>player)
198     ] when ;
199
200 : tick-player-forward ( world player -- )
201     2dup save-history
202     [ apply-friction apply-gravity ] change-velocity
203     dup scaled-velocity [ v+ [ terrain-segment>> ] dip collide ] curry with change-location
204     drop ;
205
206 : tick-player ( world player -- )
207     dup reverse-time>> [
208         tick-player-reverse
209     ] [
210         tick-player-forward
211     ] if ;
212
213 M: terrain-world tick*
214     [ dup focused?>> [ handle-input ] [ drop ] if ]
215     [ dup player>> tick-player ] bi ;
216
217 : set-texture-parameters ( texture -- )
218     GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit
219     GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
220     GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
221     GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP_TO_EDGE glTexParameteri
222     GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE glTexParameteri ;
223
224 : sky-gradient ( world -- t )
225     game-loop>> tick-number>> SKY-PERIOD mod SKY-PERIOD /f ;
226 : sky-theta ( world -- theta )
227     game-loop>> tick-number>> SKY-SPEED * ;
228
229 BEFORE: terrain-world begin-world
230     "2.0" { "GL_ARB_vertex_buffer_object" "GL_ARB_shader_objects" }
231     require-gl-version-or-extensions
232     GL_DEPTH_TEST glEnable
233     GL_TEXTURE_2D glEnable
234     GL_VERTEX_ARRAY glEnableClientState
235     <player> >>player
236     V{ } clone >>history
237     <perlin-noise-table> 0.01 0.01 <scale> { 512 512 } perlin-noise-image
238     [ >>sky-image ] keep
239     make-texture [ set-texture-parameters ] keep >>sky-texture
240     <terrain> [ >>terrain ] keep
241     { 0 0 } terrain-segment [ >>terrain-segment ] keep
242     make-texture [ set-texture-parameters ] keep >>terrain-texture
243     sky-vertex-shader sky-pixel-shader <simple-gl-program>
244     >>sky-program
245     terrain-vertex-shader terrain-pixel-shader <simple-gl-program>
246     >>terrain-program
247     terrain-vertex-size <grid-mesh> >>terrain-mesh
248     drop ;
249
250 AFTER: terrain-world end-world
251     {
252         [ terrain-mesh>> dispose ]
253         [ terrain-program>> delete-gl-program ]
254         [ terrain-texture>> delete-texture ]
255         [ sky-program>> delete-gl-program ]
256         [ sky-texture>> delete-texture ]
257     } cleave ;
258
259 M: terrain-world resize-world
260     GL_PROJECTION glMatrixMode
261     glLoadIdentity
262     dim>> [ [ 0 0 ] dip first2 glViewport ]
263     [ frustum glFrustum ] bi ;
264
265 M: terrain-world draw-world*
266     {
267         [ set-modelview-matrix ]
268         [ terrain-texture>> GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
269         [ sky-texture>> GL_TEXTURE_2D GL_TEXTURE1 bind-texture-unit ]
270         [ GL_DEPTH_TEST glDisable dup sky-program>> [
271             [ nip "sky" glGetUniformLocation 1 glUniform1i ]
272             [ "sky_gradient" glGetUniformLocation swap sky-gradient glUniform1f ]
273             [ "sky_theta" glGetUniformLocation swap sky-theta glUniform1f ] 2tri
274             { -1.0 -1.0 } { 2.0 2.0 } gl-fill-rect
275         ] with-gl-program ]
276         [ GL_DEPTH_TEST glEnable dup terrain-program>> [
277             [ "heightmap" glGetUniformLocation 0 glUniform1i ]
278             [ "component_scale" glGetUniformLocation COMPONENT-SCALE first4 glUniform4f ] bi
279             terrain-mesh>> draw-grid-mesh
280         ] with-gl-program ]
281     } cleave gl-error ;
282
283 M: terrain-world pref-dim* drop { 640 480 } ;
284
285 : terrain-window ( -- )
286     [
287         f T{ world-attributes
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             { grab-input? t }
296         } open-window
297     ] with-ui ;
298
299 MAIN: terrain-window