CHGICKEN
[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 );
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
41 static int ray_world( v3f pos, v3f dir, ray_hit *hit )
42 {
43 return bvh_raycast( &world.geo, pos, dir, hit );
44 }
45
46 static int ray_hit_is_ramp( ray_hit *hit )
47 {
48 return hit->tri[0] < world.sm_road.vertex_count;
49 }
50
51 static void world_init_default(void)
52 {
53 /* Setup scene */
54 scene_init( &world.geo );
55 model *mworld = vg_asset_read( "models/mp_dev.mdl" );
56
57 scene_add_model( &world.geo, mworld, submodel_get( mworld, "mp_dev" ),
58 (v3f){0.0f,0.0f,0.0f}, 0.0f, 1.0f );
59 scene_copy_slice( &world.geo, &world.sm_road );
60
61 scene_add_model( &world.geo, mworld, submodel_get( mworld, "terrain" ),
62 (v3f){0.0f,0.0f,0.0f}, 0.0f, 1.0f );
63 scene_copy_slice( &world.geo, &world.sm_terrain );
64
65 v3_copy( model_marker_get( mworld, "mp_dev_tutorial" )->co, world.tutorial );
66
67
68 /* GATE DEV */
69 #if 0
70 {
71 model_marker *ga = model_marker_get(mworld,"gate_a"),
72 *gb = model_marker_get(mworld,"gate_a_recv");
73
74 v3_copy( ga->co, gate_a.co );
75 v3_copy( gb->co, gate_b.co );
76 v4_copy( ga->q, gate_a.q );
77 v4_copy( gb->q, gate_b.q );
78 v2_copy( ga->s, gate_a.dims );
79 v2_copy( gb->s, gate_b.dims );
80
81 gate_a.other = &gate_b;
82 gate_b.other = &gate_a;
83
84 gate_transform_update( &gate_a );
85 gate_transform_update( &gate_b );
86 }
87 #endif
88
89 /* WATER DEV */
90 {
91 glmesh surf;
92 submodel *sm = submodel_get(mworld,"mp_dev_water");
93 model_unpack_submodel( mworld, &surf, sm );
94
95 water_init();
96 water_set_surface( &surf, sm->pivot[1] );
97 }
98
99 free( mworld );
100 scene_upload( &world.geo );
101 bvh_create( &world.geo );
102
103
104
105 scene_init( &world.foliage );
106 model *mfoliage = vg_asset_read("models/rs_foliage.mdl");
107
108 v3f volume;
109 v3_sub( world.geo.bbx[1], world.geo.bbx[0], volume );
110 volume[1] = 1.0f;
111
112 m4x3f transform;
113
114 for( int i=0;i<100000;i++ )
115 {
116 v3f pos;
117 v3_mul( volume, (v3f){ vg_randf(), 1000.0f, vg_randf() }, pos );
118 v3_add( pos, world.geo.bbx[0], pos );
119
120 ray_hit hit;
121 hit.dist = INFINITY;
122
123 if( ray_world( pos, (v3f){0.0f,-1.0f,0.0f}, &hit ))
124 {
125 if( hit.normal[1] > 0.8f && !ray_hit_is_ramp(&hit) )
126 {
127 v4f qsurface, qrandom;
128 v3f axis;
129
130 v3_cross( (v3f){0.0f,1.0f,0.0f}, hit.normal, axis );
131
132 float angle = v3_dot(hit.normal,(v3f){0.0f,1.0f,0.0f});
133 q_axis_angle( qsurface, axis, angle );
134 q_axis_angle( qrandom, (v3f){0.0f,1.0f,0.0f}, vg_randf()*VG_TAUf );
135 q_mul( qsurface, qrandom, qsurface );
136 q_m3x3( qsurface, transform );
137
138 v3_copy( hit.pos, transform[3] );
139
140 scene_add_foliage( &world.foliage, mfoliage,
141 model_get_submodel( mfoliage, 0 ), transform );
142 }
143 }
144 }
145
146 free( mfoliage );
147 scene_upload( &world.foliage );
148 }
149
150 #endif /* WORLD_H */