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