new model modes
[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
12 #include "shaders/standard.h"
13
14 static struct gworld
15 {
16 scene geo, foliage;
17 submodel sm_road, sm_terrain;
18 glmesh skybox;
19
20 v3f tutorial;
21 }
22 world;
23
24 static void render_world( m4x4f projection, m4x3f camera )
25 {
26 render_sky( camera );
27
28 m4x3f identity_matrix;
29 m4x3_identity( identity_matrix );
30
31 render_terrain( projection, camera[3] );
32 scene_bind( &world.geo );
33 scene_draw( &world.geo );
34
35 glDisable(GL_CULL_FACE);
36 scene_bind( &world.foliage );
37 scene_draw( &world.foliage );
38 glEnable(GL_CULL_FACE);
39
40 vg_line_boxf( world.geo.bbx, 0xff00ffff );
41 }
42
43 static void ray_world_get_tri( ray_hit *hit, v3f tri[3] )
44 {
45 for( int i=0; i<3; i++ )
46 v3_copy( world.geo.verts[ hit->tri[i] ].co, tri[i] );
47 }
48
49 static int ray_world( v3f pos, v3f dir, ray_hit *hit )
50 {
51 return bvh_raycast( &world.geo, pos, dir, hit );
52 }
53
54 static int ray_hit_is_ramp( ray_hit *hit )
55 {
56 return hit->tri[0] < world.sm_road.vertex_count;
57 }
58
59 static void world_load(void)
60 {
61 /* Setup scene */
62 scene_init( &world.geo );
63 model *mworld = vg_asset_read( "models/mp_dev.mdl" );
64
65 for( int i=0; i<mworld->layer_count; i++ )
66 {
67 submodel *sm = model_get_submodel( mworld, i );
68 if( !strcmp( sm->material, "surf" ) )
69 scene_add_model( &world.geo, mworld, sm, sm->pivot, 0.0f, 1.0f );
70
71 }
72 scene_copy_slice( &world.geo, &world.sm_road );
73
74 for( int i=0; i<mworld->layer_count; i++ )
75 {
76 submodel *sm = model_get_submodel( mworld, i );
77 if( !strcmp( sm->material, "terrain" ) )
78 scene_add_model( &world.geo, mworld, sm, sm->pivot, 0.0f, 1.0f );
79 }
80
81 scene_copy_slice( &world.geo, &world.sm_terrain );
82
83 /*
84 * TODO: Parametric marker import
85 */
86 v3_copy( model_marker_get( mworld, "mp_dev_tutorial" )->co, world.tutorial );
87
88
89 /* GATE DEV */
90 #if 0
91 {
92 model_marker *ga = model_marker_get(mworld,"gate_a"),
93 *gb = model_marker_get(mworld,"gate_a_recv");
94
95 v3_copy( ga->co, gate_a.co );
96 v3_copy( gb->co, gate_b.co );
97 v4_copy( ga->q, gate_a.q );
98 v4_copy( gb->q, gate_b.q );
99 v2_copy( ga->s, gate_a.dims );
100 v2_copy( gb->s, gate_b.dims );
101
102 gate_a.other = &gate_b;
103 gate_b.other = &gate_a;
104
105 gate_transform_update( &gate_a );
106 gate_transform_update( &gate_b );
107 }
108 #endif
109
110 /* WATER DEV
111 * again, TODO: parametric import (material)
112 */
113 {
114 glmesh surf;
115 submodel *sm = submodel_get(mworld,"mp_dev_water");
116 model_unpack_submodel( mworld, &surf, sm );
117
118 water_init();
119 water_set_surface( &surf, sm->pivot[1] );
120 }
121
122 free( mworld );
123 scene_upload( &world.geo );
124 bvh_create( &world.geo );
125
126 water_compute_depth( world.geo.bbx );
127
128 scene_init( &world.foliage );
129 model *mfoliage = vg_asset_read("models/rs_foliage.mdl");
130
131 v3f volume;
132 v3_sub( world.geo.bbx[1], world.geo.bbx[0], volume );
133 volume[1] = 1.0f;
134
135 m4x3f transform;
136
137 submodel *sm_blob = submodel_get( mfoliage, "blob" ),
138 *sm_tree = submodel_get( mfoliage, "tree" );
139
140 for( int i=0;i<100000;i++ )
141 {
142 v3f pos;
143 v3_mul( volume, (v3f){ vg_randf(), 1000.0f, vg_randf() }, pos );
144 v3_add( pos, world.geo.bbx[0], pos );
145
146 ray_hit hit;
147 hit.dist = INFINITY;
148
149 if( ray_world( pos, (v3f){0.0f,-1.0f,0.0f}, &hit ))
150 {
151 if( hit.normal[1] > 0.8f && !ray_hit_is_ramp(&hit) &&
152 hit.pos[1] > wrender.height )
153 {
154 v4f qsurface, qrandom;
155 v3f axis;
156
157 v3_cross( (v3f){0.0f,1.0f,0.0f}, hit.normal, axis );
158
159 float angle = v3_dot(hit.normal,(v3f){0.0f,1.0f,0.0f});
160 q_axis_angle( qsurface, axis, angle );
161 q_axis_angle( qrandom, (v3f){0.0f,1.0f,0.0f}, vg_randf()*VG_TAUf );
162 q_mul( qsurface, qrandom, qsurface );
163 q_m3x3( qsurface, transform );
164
165 v3_copy( hit.pos, transform[3] );
166
167 if( vg_randf() < 0.00000006f )
168 {
169 m3x3_identity( transform );
170 scene_add_foliage( &world.foliage, mfoliage, sm_tree, transform );
171 }
172 else
173 scene_add_foliage( &world.foliage, mfoliage, sm_blob, transform );
174 }
175 }
176 }
177
178 free( mfoliage );
179 scene_upload( &world.foliage );
180 }
181
182 #endif /* WORLD_H */