ea5a5c4b7ec71408b6cd92da2fc545eaa302e8bc
[carveJwlIkooP6JGAAIwe30JlM.git] / world.h
1 #ifndef WORLD_H
2 #define WORLD_H
3
4 #define VG_3D
5 #include "vg/vg.h"
6
7 #include "scene.h"
8 #include "terrain.h"
9 #include "render.h"
10 #include "water.h"
11 #include "rigidbody.h"
12 #include "gate.h"
13 #include "bvh.h"
14
15 #include "shaders/standard.h"
16
17 static struct gworld
18 {
19 scene geo, foliage;
20 submodel sm_road, sm_terrain;
21 glmesh skybox;
22
23 v3f tutorial;
24
25 #if 0
26 rigidbody box;
27 #endif
28
29 teleport_gate gates[16];
30 u32 gate_count;
31
32 rigidbody temp_rbs[32];
33 u32 rb_count;
34
35 bh_tree bhcubes;
36 }
37 world;
38
39 static void render_world( m4x4f projection, m4x3f camera )
40 {
41 render_sky( camera );
42
43 m4x3f identity_matrix;
44 m4x3_identity( identity_matrix );
45
46 render_terrain( projection, camera[3] );
47 scene_bind( &world.geo );
48 scene_draw( &world.geo );
49
50 glDisable(GL_CULL_FACE);
51 scene_bind( &world.foliage );
52 scene_draw( &world.foliage );
53 glEnable(GL_CULL_FACE);
54 }
55
56 static void ray_world_get_tri( ray_hit *hit, v3f tri[3] )
57 {
58 for( int i=0; i<3; i++ )
59 v3_copy( world.geo.verts[ hit->tri[i] ].co, tri[i] );
60 }
61
62 static int ray_world( v3f pos, v3f dir, ray_hit *hit )
63 {
64 return scene_raycast( &world.geo, pos, dir, hit );
65 }
66
67 static int ray_hit_is_ramp( ray_hit *hit )
68 {
69 return hit->tri[0] < world.sm_road.vertex_count;
70 }
71
72 static bh_system bh_system_rigidbodies;
73 static void world_load(void)
74 {
75 /* Setup scene */
76 scene_init( &world.geo );
77 model *mworld = vg_asset_read( "models/mp_dev.mdl" );
78
79 for( int i=0; i<mworld->layer_count; i++ )
80 {
81 submodel *sm = model_get_submodel( mworld, i );
82 if( !strcmp( sm->material, "surf" ) )
83 scene_add_model( &world.geo, mworld, sm, sm->pivot, 0.0f, 1.0f );
84
85 }
86 scene_copy_slice( &world.geo, &world.sm_road );
87
88 for( int i=0; i<mworld->layer_count; i++ )
89 {
90 submodel *sm = model_get_submodel( mworld, i );
91 if( !strcmp( sm->material, "terrain" ) )
92 scene_add_model( &world.geo, mworld, sm, sm->pivot, 0.0f, 1.0f );
93 }
94
95 scene_copy_slice( &world.geo, &world.sm_terrain );
96
97 /*
98 * TODO: Parametric marker import
99 */
100 v3_copy( model_marker_get( mworld, "start" )->co, world.tutorial );
101
102 /*
103 * Initialize gates
104 */
105
106 world.gate_count = 0;
107 for( int i=0; i<mworld->marker_count; i++ )
108 {
109 model_marker *ga = model_get_marker( mworld, i );
110
111 if( ga->classtype == k_classtype_gate )
112 {
113 struct classtype_gate *data = get_entdata_raw( mworld, ga );
114
115 if( data->target )
116 {
117 model_marker *gb = model_get_marker( mworld, data->target );
118
119 teleport_gate *gate = &world.gates[ world.gate_count ++ ];
120
121 v3_copy( ga->co, gate->co[0] );
122 v3_copy( gb->co, gate->co[1] );
123 v4_copy( ga->q, gate->q[0] );
124 v4_copy( gb->q, gate->q[1] );
125 v2_copy( ga->s, gate->dims );
126
127 gate_transform_update( gate );
128 }
129 }
130 }
131
132 /*
133 * Load water mesh (1 per world)
134 */
135 for( int i=0; i<mworld->layer_count; i++ )
136 {
137 submodel *sm = model_get_submodel( mworld, i );
138 if( !strcmp( sm->material, "water" ) )
139 {
140 glmesh surf;
141 model_unpack_submodel( mworld, &surf, sm );
142
143 water_init();
144 water_set_surface( &surf, sm->pivot[1] );
145
146 break;
147 }
148 }
149
150 scene_upload( &world.geo );
151 scene_bh_create( &world.geo );
152
153 water_compute_depth( world.geo.bbx );
154
155 scene_init( &world.foliage );
156 model *mfoliage = vg_asset_read("models/rs_foliage.mdl");
157
158 /*
159 * TODO: Load any other meshes into the foliage scene, and create rbs for
160 * them.
161 *
162 * then compute bvh
163 */
164
165 for( int i=0; i<mworld->layer_count; i++ )
166 {
167 submodel *sm = model_get_submodel( mworld, i );
168 if( !strcmp( sm->material, "surf" ) ||
169 !strcmp( sm->material, "terrain" ) ||
170 !strcmp( sm->material, "water" ) )
171 continue;
172
173 m4x3f transform;
174 q_m3x3( sm->q, transform );
175 v3_copy( sm->pivot, transform[3] );
176 scene_add_foliage( &world.foliage, mworld, sm, transform );
177
178 rigidbody *rb = &world.temp_rbs[ world.rb_count ++ ];
179
180 box_copy( sm->bbx, rb->bbx );
181 v3_copy( sm->pivot, rb->co );
182 rb_init( rb );
183 v4_copy( sm->q, rb->q );
184 rb_update_transform( rb );
185 }
186
187 free( mworld );
188
189 v3f volume;
190 v3_sub( world.geo.bbx[1], world.geo.bbx[0], volume );
191 volume[1] = 1.0f;
192
193 m4x3f transform;
194
195 submodel *sm_blob = submodel_get( mfoliage, "blob" ),
196 *sm_tree = submodel_get( mfoliage, "tree" );
197
198 for( int i=0;i<100000;i++ )
199 {
200 v3f pos;
201 v3_mul( volume, (v3f){ vg_randf(), 1000.0f, vg_randf() }, pos );
202 v3_add( pos, world.geo.bbx[0], pos );
203
204 ray_hit hit;
205 hit.dist = INFINITY;
206
207 if( ray_world( pos, (v3f){0.0f,-1.0f,0.0f}, &hit ))
208 {
209 if( hit.normal[1] > 0.8f && !ray_hit_is_ramp(&hit) &&
210 hit.pos[1] > wrender.height )
211 {
212 v4f qsurface, qrandom;
213 v3f axis;
214
215 v3_cross( (v3f){0.0f,1.0f,0.0f}, hit.normal, axis );
216
217 float angle = v3_dot(hit.normal,(v3f){0.0f,1.0f,0.0f});
218 q_axis_angle( qsurface, axis, angle );
219 q_axis_angle( qrandom, (v3f){0.0f,1.0f,0.0f}, vg_randf()*VG_TAUf );
220 q_mul( qsurface, qrandom, qsurface );
221 q_m3x3( qsurface, transform );
222
223 v3_copy( hit.pos, transform[3] );
224
225 if( vg_randf() < 0.00000006f )
226 {
227 m3x3_identity( transform );
228 scene_add_foliage( &world.foliage, mfoliage, sm_tree, transform);
229 }
230 else
231 scene_add_foliage( &world.foliage, mfoliage, sm_blob, transform);
232 }
233 }
234 }
235
236 free( mfoliage );
237 scene_upload( &world.foliage );
238
239 bh_create( &world.bhcubes,
240 &bh_system_rigidbodies, world.temp_rbs, world.rb_count );
241 }
242
243 #endif /* WORLD_H */