]> gitweb.factorcode.org Git - factor.git/blob - extra/terrain/terrain.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / extra / terrain / terrain.factor
1 USING: accessors arrays combinators game-input
2 game-input.scancodes game-loop 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 ;
9 IN: terrain
10
11 CONSTANT: FOV $[ 2.0 sqrt 1+ ]
12 CONSTANT: NEAR-PLANE $[ 1.0 2048.0 / ]
13 CONSTANT: FAR-PLANE 1.0
14 CONSTANT: PLAYER-START-LOCATION { 0.5 0.51 0.5 }
15 CONSTANT: PLAYER-HEIGHT $[ 3.0 1024.0 / ]
16 CONSTANT: GRAVITY $[ 1.0 4096.0 / ]
17 CONSTANT: JUMP $[ 1.0 1024.0 / ]
18 CONSTANT: TICK-LENGTH $[ 1000 30 /i ]
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.002 0.0 }
23
24 CONSTANT: terrain-vertex-size { 512 512 }
25 CONSTANT: terrain-vertex-distance { $[ 1.0 512.0 / ] $[ 1.0 512.0 / ] }
26 CONSTANT: terrain-vertex-row-length $[ 512 1 + 2 * ]
27
28 TUPLE: player
29     location yaw pitch velocity ;
30
31 TUPLE: terrain-world < world
32     player
33     terrain terrain-segment terrain-texture terrain-program
34     terrain-vertex-buffer
35     game-loop ;
36
37 : frustum ( dim -- -x x -y y near far )
38     dup first2 min v/n
39     NEAR-PLANE FOV / v*n first2 [ [ neg ] keep ] bi@
40     NEAR-PLANE FAR-PLANE ;
41
42 : set-modelview-matrix ( gadget -- )
43     GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT bitor glClear
44     GL_MODELVIEW glMatrixMode
45     glLoadIdentity
46     player>>
47     [ pitch>> 1.0 0.0 0.0 glRotatef ]
48     [ yaw>> 0.0 1.0 0.0 glRotatef ]
49     [ location>> vneg first3 glTranslatef ] tri ;
50
51 : vertex-array-vertex ( x z -- vertex )
52     [ terrain-vertex-distance first * ]
53     [ terrain-vertex-distance second * ] bi*
54     [ 0 ] dip float-array{ } 3sequence ;
55
56 : vertex-array-row ( z -- vertices )
57     dup 1 + 2array
58     terrain-vertex-size first 1 + iota
59     2array [ first2 swap vertex-array-vertex ] product-map
60     concat ;
61
62 : vertex-array ( -- vertices )
63     terrain-vertex-size second iota
64     [ vertex-array-row ] map concat ;
65
66 : >vertex-buffer ( bytes -- buffer )
67     [ GL_ARRAY_BUFFER ] dip GL_STATIC_DRAW <gl-buffer> ;
68
69 : draw-vertex-buffer-row ( i -- )
70     [ GL_TRIANGLE_STRIP ] dip
71     terrain-vertex-row-length * terrain-vertex-row-length
72     glDrawArrays ;
73
74 : draw-vertex-buffer ( buffer -- )
75     [ GL_ARRAY_BUFFER ] dip [
76         3 GL_FLOAT 0 f glVertexPointer
77         terrain-vertex-size second iota [ draw-vertex-buffer-row ] each
78     ] with-gl-buffer ;
79
80 : degrees ( deg -- rad )
81     pi 180.0 / * ;
82
83 :: eye-rotate ( yaw pitch v -- v' )
84     yaw degrees neg :> y
85     pitch degrees neg :> p
86     y cos :> cosy
87     y sin :> siny
88     p cos :> cosp
89     p sin :> sinp
90
91     cosy         0.0       siny        neg  3array
92     siny sinp *  cosp      cosy sinp *      3array
93     siny cosp *  sinp neg  cosy cosp *      3array 3array
94     v swap v.m ;
95
96 : forward-vector ( player -- v )
97     yaw>> 0.0
98     { 0.0 0.0 $ MOVEMENT-SPEED } vneg eye-rotate ;
99 : rightward-vector ( player -- v )
100     yaw>> 0.0
101     { $ MOVEMENT-SPEED 0.0 0.0 } eye-rotate ;
102
103 : walk-forward ( player -- )
104     dup forward-vector [ v+ ] curry change-velocity drop ;
105 : walk-backward ( player -- )
106     dup forward-vector [ v- ] curry change-velocity drop ;
107 : walk-leftward ( player -- )
108     dup rightward-vector [ v- ] curry change-velocity drop ;
109 : walk-rightward ( player -- )
110     dup rightward-vector [ v+ ] curry change-velocity drop ;
111 : jump ( player -- )
112     [ { 0.0 $ JUMP 0.0 } v+ ] change-velocity drop ;
113
114 : clamp-pitch ( pitch -- pitch' )
115     90.0 min -90.0 max ;
116
117 : rotate-with-mouse ( player mouse -- )
118     [ dx>> MOUSE-SCALE * [ + ] curry change-yaw ]
119     [ dy>> MOUSE-SCALE * [ + clamp-pitch ] curry change-pitch ] bi
120     drop ;
121
122 :: handle-input ( world -- )
123     world player>> :> player
124     read-keyboard keys>> :> keys
125     key-w keys nth [ player walk-forward ] when 
126     key-s keys nth [ player walk-backward ] when 
127     key-a keys nth [ player walk-leftward ] when 
128     key-d keys nth [ player walk-rightward ] when 
129     key-space keys nth [ player jump ] when 
130     key-escape keys nth [ world close-window ] when
131     player read-mouse rotate-with-mouse
132     reset-mouse ;
133
134 : apply-friction ( velocity -- velocity' )
135     FRICTION v*n ;
136
137 : apply-gravity ( velocity -- velocity' )
138     1 over [ GRAVITY - ] change-nth ;
139
140 :: pixel-indices ( coords dim -- indices )
141     coords vfloor [ >integer ] map :> floor-coords
142     floor-coords first2 dim first * + :> base-index
143     base-index dim first + :> next-row-index
144
145     base-index
146     base-index 1 +
147     next-row-index
148     next-row-index 1 + 4array ;
149
150 :: terrain-height-at ( segment point -- height )
151     segment dim>> :> dim
152     dim point v* :> pixel
153     pixel dup vfloor v- :> pixel-mantissa
154     segment bitmap>> 4 <groups> :> pixels
155     pixel dim pixel-indices :> indices
156     
157     indices [ pixels nth COMPONENT-SCALE v. 255.0 / ] map
158     first4 pixel-mantissa bilerp ;
159
160 : collide ( segment location -- location' )
161     [ [ first ] [ third ] bi 2array terrain-height-at PLAYER-HEIGHT + ]
162     [ [ 1 ] 2dip [ max ] with change-nth ]
163     [ ] tri ;
164
165 : tick-player ( world player -- )
166     [ apply-friction apply-gravity ] change-velocity
167     dup velocity>> [ v+ [ terrain-segment>> ] dip collide ] curry with change-location
168     drop ;
169
170 M: terrain-world tick*
171     [ dup focused?>> [ handle-input ] [ drop ] if ]
172     [ dup player>> tick-player ] bi ;
173
174 M: terrain-world draw*
175     nip draw-world ;
176
177 : set-heightmap-texture-parameters ( texture -- )
178     GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit
179     GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
180     GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
181     GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP_TO_EDGE glTexParameteri
182     GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE glTexParameteri ;
183
184 M: terrain-world begin-world
185     "2.0" { "GL_ARB_vertex_buffer_object" "GL_ARB_shader_objects" }
186     require-gl-version-or-extensions
187     GL_DEPTH_TEST glEnable
188     GL_TEXTURE_2D glEnable
189     GL_VERTEX_ARRAY glEnableClientState
190     0.5 0.5 0.5 1.0 glClearColor
191     PLAYER-START-LOCATION 0.0 0.0 { 0.0 0.0 0.0 } player boa >>player
192     <terrain> [ >>terrain ] keep
193     { 0 0 } terrain-segment [ >>terrain-segment ] keep
194     make-texture [ set-heightmap-texture-parameters ] keep >>terrain-texture
195     terrain-vertex-shader terrain-pixel-shader <simple-gl-program>
196     >>terrain-program
197     vertex-array >vertex-buffer >>terrain-vertex-buffer
198     TICK-LENGTH over <game-loop> [ >>game-loop ] keep start-loop
199     open-game-input
200     drop ;
201
202 M: terrain-world end-world
203     close-game-input
204     {
205         [ game-loop>> stop-loop ]
206         [ terrain-vertex-buffer>> delete-gl-buffer ]
207         [ terrain-program>> delete-gl-program ]
208         [ terrain-texture>> delete-texture ]
209     } cleave ;
210
211 M: terrain-world resize-world
212     GL_PROJECTION glMatrixMode
213     glLoadIdentity
214     dim>> [ [ 0 0 ] dip first2 glViewport ]
215     [ frustum glFrustum ] bi ;
216
217 M: terrain-world draw-world*
218     [ set-modelview-matrix ]
219     [ terrain-texture>> GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
220     [ dup terrain-program>> [
221         [ "heightmap" glGetUniformLocation 0 glUniform1i ]
222         [ "component_scale" glGetUniformLocation COMPONENT-SCALE first4 glUniform4f ] bi
223         terrain-vertex-buffer>> draw-vertex-buffer
224     ] with-gl-program ]
225     tri gl-error ;
226
227 M: terrain-world focusable-child* drop t ;
228 M: terrain-world pref-dim* drop { 640 480 } ;
229
230 : terrain-window ( -- )
231     [
232         f T{ world-attributes
233             { world-class terrain-world }
234             { title "Terrain" }
235             { pixel-format-attributes {
236                 windowed
237                 double-buffered
238                 T{ depth-bits { value 24 } }
239             } }
240             { grab-input? t }
241         } open-window
242     ] with-ui ;
243
244 MAIN: terrain-window