]> gitweb.factorcode.org Git - factor.git/blob - extra/terrain/terrain.factor
terrain demo allows looking around with keyboard now. used ${ in a few places
[factor.git] / extra / terrain / terrain.factor
1 USING: accessors arrays combinators game-input game-loop
2 game-input.scancodes grouping kernel literals locals
3 math math.constants math.functions math.matrices math.order
4 math.vectors opengl opengl.capabilities opengl.gl
5 opengl.shaders opengl.textures opengl.textures.private
6 sequences sequences.product specialized-arrays.float
7 terrain.generation terrain.shaders ui ui.gadgets
8 ui.gadgets.worlds ui.pixel-formats game-worlds method-chains
9 math.affine-transforms noise ;
10 IN: terrain
11
12 CONSTANT: FOV $[ 2.0 sqrt 1+ ]
13 CONSTANT: NEAR-PLANE $[ 1.0 1024.0 / ]
14 CONSTANT: FAR-PLANE 2.0
15 CONSTANT: PLAYER-START-LOCATION { 0.5 0.51 0.5 }
16 CONSTANT: PLAYER-HEIGHT $[ 1.0 256.0 / ]
17 CONSTANT: GRAVITY $[ 1.0 4096.0 / ]
18 CONSTANT: JUMP $[ 1.0 1024.0 / ]
19 CONSTANT: MOUSE-SCALE $[ 1.0 10.0 / ]
20 CONSTANT: MOVEMENT-SPEED $[ 1.0 16384.0 / ]
21 CONSTANT: FRICTION 0.95
22 CONSTANT: COMPONENT-SCALE { 0.5 0.01 0.0005 0.0 }
23 CONSTANT: SKY-PERIOD 1200
24 CONSTANT: SKY-SPEED 0.0005
25
26 CONSTANT: terrain-vertex-size { 512 512 }
27 CONSTANT: terrain-vertex-distance { $[ 1.0 512.0 / ] $[ 1.0 512.0 / ] }
28 CONSTANT: terrain-vertex-row-length $[ 512 1 + 2 * ]
29
30 TUPLE: player
31     location yaw pitch velocity ;
32
33 TUPLE: terrain-world < game-world
34     player
35     sky-image sky-texture sky-program
36     terrain terrain-segment terrain-texture terrain-program
37     terrain-vertex-buffer ;
38
39 M: terrain-world tick-length
40     drop 1000 30 /i ;
41
42 : frustum ( dim -- -x x -y y near far )
43     dup first2 min v/n
44     NEAR-PLANE FOV / v*n first2 [ [ neg ] keep ] bi@
45     NEAR-PLANE FAR-PLANE ;
46
47 : set-modelview-matrix ( gadget -- )
48     GL_DEPTH_BUFFER_BIT glClear
49     GL_MODELVIEW glMatrixMode
50     glLoadIdentity
51     player>>
52     [ pitch>> 1.0 0.0 0.0 glRotatef ]
53     [ yaw>> 0.0 1.0 0.0 glRotatef ]
54     [ location>> vneg first3 glTranslatef ] tri ;
55
56 : vertex-array-vertex ( x z -- vertex )
57     [ terrain-vertex-distance first * ]
58     [ terrain-vertex-distance second * ] bi*
59     [ 0 ] dip float-array{ } 3sequence ;
60
61 : vertex-array-row ( z -- vertices )
62     dup 1 + 2array
63     terrain-vertex-size first 1 + iota
64     2array [ first2 swap vertex-array-vertex ] product-map
65     concat ;
66
67 : vertex-array ( -- vertices )
68     terrain-vertex-size second iota
69     [ vertex-array-row ] map concat ;
70
71 : >vertex-buffer ( bytes -- buffer )
72     [ GL_ARRAY_BUFFER ] dip GL_STATIC_DRAW <gl-buffer> ;
73
74 : draw-vertex-buffer-row ( i -- )
75     [ GL_TRIANGLE_STRIP ] dip
76     terrain-vertex-row-length * terrain-vertex-row-length
77     glDrawArrays ;
78
79 : draw-vertex-buffer ( buffer -- )
80     [ GL_ARRAY_BUFFER ] dip [
81         3 GL_FLOAT 0 f glVertexPointer
82         terrain-vertex-size second iota [ draw-vertex-buffer-row ] each
83     ] with-gl-buffer ;
84
85 : degrees ( deg -- rad )
86     pi 180.0 / * ;
87
88 :: eye-rotate ( yaw pitch v -- v' )
89     yaw degrees neg :> y
90     pitch degrees neg :> p
91     y cos :> cosy
92     y sin :> siny
93     p cos :> cosp
94     p sin :> sinp
95
96     cosy         0.0       siny        neg  3array
97     siny sinp *  cosp      cosy sinp *      3array
98     siny cosp *  sinp neg  cosy cosp *      3array 3array
99     v swap v.m ;
100
101 : forward-vector ( player -- v )
102     yaw>> 0.0
103     ${ 0.0 0.0 MOVEMENT-SPEED } vneg eye-rotate ;
104 : rightward-vector ( player -- v )
105     yaw>> 0.0
106     ${ MOVEMENT-SPEED 0.0 0.0 } eye-rotate ;
107 : clamp-pitch ( pitch -- pitch' )
108     90.0 min -90.0 max ;
109
110
111 : walk-forward ( player -- )
112     dup forward-vector [ v+ ] curry change-velocity drop ;
113 : walk-backward ( player -- )
114     dup forward-vector [ v- ] curry change-velocity drop ;
115 : walk-leftward ( player -- )
116     dup rightward-vector [ v- ] curry change-velocity drop ;
117 : walk-rightward ( player -- )
118     dup rightward-vector [ v+ ] curry change-velocity drop ;
119 : jump ( player -- )
120     [ ${ 0.0 JUMP 0.0 } v+ ] change-velocity drop ;
121 : rotate-leftward ( player x -- )
122     [ - ] curry change-yaw drop ;
123 : rotate-rightward ( player x -- )
124     [ + ] curry change-yaw drop ;
125 : look-horizontally ( player x -- )
126     [ + ] curry change-yaw drop ;
127 : look-vertically ( player x -- )
128     [ - clamp-pitch ] curry change-pitch drop ;
129
130
131 : rotate-with-mouse ( player mouse -- )
132     [ dx>> MOUSE-SCALE * look-horizontally ]
133     [ dy>> MOUSE-SCALE * look-vertically ] 2bi ;
134
135 :: handle-input ( world -- )
136     world player>> :> player
137     read-keyboard keys>> :> keys
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-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*n ;
153
154 : apply-gravity ( velocity -- velocity' )
155     1 over [ GRAVITY - ] change-nth ;
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 : tick-player ( world player -- )
186     [ apply-friction apply-gravity ] change-velocity
187     dup velocity>> [ v+ [ terrain-segment>> ] dip collide ] curry with change-location
188     drop ;
189
190 M: terrain-world tick*
191     [ dup focused?>> [ handle-input ] [ drop ] if ]
192     [ dup player>> tick-player ] bi ;
193
194 : set-texture-parameters ( texture -- )
195     GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit
196     GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
197     GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
198     GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP_TO_EDGE glTexParameteri
199     GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE glTexParameteri ;
200
201 : sky-gradient ( world -- t )
202     game-loop>> tick-number>> SKY-PERIOD mod SKY-PERIOD /f ;
203 : sky-theta ( world -- theta )
204     game-loop>> tick-number>> SKY-SPEED * ;
205
206 BEFORE: terrain-world begin-world
207     "2.0" { "GL_ARB_vertex_buffer_object" "GL_ARB_shader_objects" }
208     require-gl-version-or-extensions
209     GL_DEPTH_TEST glEnable
210     GL_TEXTURE_2D glEnable
211     GL_VERTEX_ARRAY glEnableClientState
212     PLAYER-START-LOCATION 0.0 0.0 { 0.0 0.0 0.0 } player boa >>player
213     <perlin-noise-table> 0.01 0.01 <scale> { 512 512 } perlin-noise-image
214     [ >>sky-image ] keep
215     make-texture [ set-texture-parameters ] keep >>sky-texture
216     <terrain> [ >>terrain ] keep
217     { 0 0 } terrain-segment [ >>terrain-segment ] keep
218     make-texture [ set-texture-parameters ] keep >>terrain-texture
219     sky-vertex-shader sky-pixel-shader <simple-gl-program>
220     >>sky-program
221     terrain-vertex-shader terrain-pixel-shader <simple-gl-program>
222     >>terrain-program
223     vertex-array >vertex-buffer >>terrain-vertex-buffer
224     drop ;
225
226 AFTER: terrain-world end-world
227     {
228         [ terrain-vertex-buffer>> delete-gl-buffer ]
229         [ terrain-program>> delete-gl-program ]
230         [ terrain-texture>> delete-texture ]
231         [ sky-program>> delete-gl-program ]
232         [ sky-texture>> delete-texture ]
233     } cleave ;
234
235 M: terrain-world resize-world
236     GL_PROJECTION glMatrixMode
237     glLoadIdentity
238     dim>> [ [ 0 0 ] dip first2 glViewport ]
239     [ frustum glFrustum ] bi ;
240
241 M: terrain-world draw-world*
242     {
243         [ set-modelview-matrix ]
244         [ terrain-texture>> GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
245         [ sky-texture>> GL_TEXTURE_2D GL_TEXTURE1 bind-texture-unit ]
246         [ GL_DEPTH_TEST glDisable dup sky-program>> [
247             [ nip "sky" glGetUniformLocation 1 glUniform1i ]
248             [ "sky_gradient" glGetUniformLocation swap sky-gradient glUniform1f ]
249             [ "sky_theta" glGetUniformLocation swap sky-theta glUniform1f ] 2tri
250             { -1.0 -1.0 } { 2.0 2.0 } gl-fill-rect
251         ] with-gl-program ]
252         [ GL_DEPTH_TEST glEnable dup terrain-program>> [
253             [ "heightmap" glGetUniformLocation 0 glUniform1i ]
254             [ "component_scale" glGetUniformLocation COMPONENT-SCALE first4 glUniform4f ] bi
255             terrain-vertex-buffer>> draw-vertex-buffer
256         ] with-gl-program ]
257     } cleave gl-error ;
258
259 M: terrain-world pref-dim* drop { 640 480 } ;
260
261 : terrain-window ( -- )
262     [
263         f T{ world-attributes
264             { world-class terrain-world }
265             { title "Terrain" }
266             { pixel-format-attributes {
267                 windowed
268                 double-buffered
269                 T{ depth-bits { value 24 } }
270             } }
271             { grab-input? t }
272         } open-window
273     ] with-ui ;
274
275 MAIN: terrain-window