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