]> gitweb.factorcode.org Git - factor.git/blob - extra/terrain/terrain.factor
gravity, jetpack, collision detection for terrain demo
[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 ( coords dim -- index )
141     [ drop first ] [ [ second ] [ first ] bi* * ] 2bi + ;
142
143 : terrain-height-at ( segment point -- height )
144     over dim>> [ v* vfloor ] [ pixel >integer ] bi
145     swap bitmap>> 4 <groups> nth COMPONENT-SCALE v. 255.0 / ;
146
147 : collide ( segment location -- location' )
148     [ [ first ] [ third ] bi 2array terrain-height-at PLAYER-HEIGHT + ]
149     [ [ 1 ] 2dip [ max ] with change-nth ]
150     [ ] tri ;
151
152 : tick-player ( world player -- )
153     [ apply-friction apply-gravity ] change-velocity
154     dup velocity>> [ v+ [ terrain-segment>> ] dip collide ] curry with change-location
155     P
156     drop ;
157
158 M: terrain-world tick*
159     [ dup focused?>> [ handle-input ] [ drop ] if ]
160     [ dup player>> tick-player ] bi ;
161
162 M: terrain-world draw*
163     nip draw-world ;
164
165 : set-heightmap-texture-parameters ( texture -- )
166     GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit
167     GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
168     GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
169     GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP_TO_EDGE glTexParameteri
170     GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE glTexParameteri ;
171
172 M: terrain-world begin-world
173     "2.0" { "GL_ARB_vertex_buffer_object" "GL_ARB_shader_objects" }
174     require-gl-version-or-extensions
175     GL_DEPTH_TEST glEnable
176     GL_TEXTURE_2D glEnable
177     GL_VERTEX_ARRAY glEnableClientState
178     0.5 0.5 0.5 1.0 glClearColor
179     PLAYER-START-LOCATION 0.0 0.0 { 0.0 0.0 0.0 } player boa >>player
180     <terrain> [ >>terrain ] keep
181     { 0 0 } terrain-segment [ >>terrain-segment ] keep
182     make-texture [ set-heightmap-texture-parameters ] keep >>terrain-texture
183     terrain-vertex-shader terrain-pixel-shader <simple-gl-program>
184     >>terrain-program
185     vertex-array >vertex-buffer >>terrain-vertex-buffer
186     TICK-LENGTH over <game-loop> [ >>game-loop ] keep start-loop
187     open-game-input
188     drop ;
189
190 M: terrain-world end-world
191     close-game-input
192     {
193         [ game-loop>> stop-loop ]
194         [ terrain-vertex-buffer>> delete-gl-buffer ]
195         [ terrain-program>> delete-gl-program ]
196         [ terrain-texture>> delete-texture ]
197     } cleave ;
198
199 M: terrain-world resize-world
200     GL_PROJECTION glMatrixMode
201     glLoadIdentity
202     dim>> [ [ 0 0 ] dip first2 glViewport ]
203     [ frustum glFrustum ] bi ;
204
205 M: terrain-world draw-world*
206     [ set-modelview-matrix ]
207     [ terrain-texture>> GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
208     [ dup terrain-program>> [
209         [ "heightmap" glGetUniformLocation 0 glUniform1i ]
210         [ "component_scale" glGetUniformLocation COMPONENT-SCALE first4 glUniform4f ] bi
211         terrain-vertex-buffer>> draw-vertex-buffer
212     ] with-gl-program ]
213     tri gl-error ;
214
215 M: terrain-world focusable-child* drop t ;
216 M: terrain-world pref-dim* drop { 640 480 } ;
217
218 : terrain-window ( -- )
219     [
220         f T{ world-attributes
221             { world-class terrain-world }
222             { title "Terrain" }
223             { pixel-format-attributes {
224                 windowed
225                 double-buffered
226                 T{ depth-bits { value 24 } }
227             } }
228             { grab-input? t }
229         } open-window
230     ] with-ui ;
231
232 MAIN: terrain-window