8 typedef struct scene scene
;
31 GLuint tex_dual_noise
;
33 static void scene_init( scene
*pscene
)
36 pscene
->indices
= NULL
;
37 pscene
->vertex_count
= 0;
38 pscene
->indice_count
= 0;
39 pscene
->shadower_count
= 0;
40 pscene
->shadower_cap
= 0;
41 pscene
->submesh
.indice_start
= 0;
42 pscene
->submesh
.indice_count
= 0;
44 v3_fill( pscene
->bbx
[0], 999999.9f
);
45 v3_fill( pscene
->bbx
[1], -999999.9f
);
47 static int noise_ready
= 0;
52 u8
*buf
= malloc( 256*256*2 );
54 for( int i
=0; i
<256*256; i
++ )
60 for( int y
=0; y
<256; y
++ )
62 for( int x
=0; x
<256; x
++ )
64 u8
*pr
= &buf
[(y
*256+x
)*2],
65 *pg
= &buf
[(((y
+17)&0xff)*256+((x
+37)&0xff))*2+1];
70 /* TODO: This texture should be delted somewhere */
71 glGenTextures( 1, &tex_dual_noise
);
72 glBindTexture( GL_TEXTURE_2D
, tex_dual_noise
);
73 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RG
, 256, 256, 0, GL_RG
,
74 GL_UNSIGNED_BYTE
, buf
);
83 static void *buffer_reserve( void *buffer
, u32 count
, u32
*cap
, u32 amount
,
86 if( count
+amount
> *cap
)
88 *cap
= VG_MAX( (*cap
)*2, (*cap
)+amount
);
90 return realloc( buffer
, (*cap
) * emsize
);
97 * Append a model into the scene with a given transform
99 static void scene_add_model( scene
*pscene
, model
*mdl
, submodel
*submodel
,
100 v3f pos
, float yaw
, float scale
)
102 pscene
->verts
= buffer_reserve( pscene
->verts
, pscene
->vertex_count
,
103 &pscene
->vertex_cap
, submodel
->vertex_count
, sizeof(model_vert
) );
104 pscene
->indices
= buffer_reserve( pscene
->indices
, pscene
->indice_count
,
105 &pscene
->indice_cap
, submodel
->indice_count
, sizeof(u32
) );
107 /* Transform and place vertices */
108 model_vert
*src_verts
= submodel_vert_data( mdl
, submodel
);
109 u32
*src_indices
= submodel_indice_data( mdl
, submodel
);
112 m4x3_identity( mtx
);
113 m4x3_translate( mtx
, pos
);
114 m4x3_rotate_y( mtx
, yaw
);
115 m4x3_scale( mtx
, scale
);
118 box_copy( submodel
->bbx
, bbxnew
);
119 m4x3_transform_aabb( mtx
, bbxnew
);
120 box_concat( pscene
->bbx
, bbxnew
);
123 m4x3_to_3x3( mtx
, rotation
);
125 float rand_hue
= vg_randf();
127 for( u32 i
=0; i
<submodel
->vertex_count
; i
++ )
129 model_vert
*pvert
= &pscene
->verts
[ pscene
->vertex_count
+i
],
130 *src
= &src_verts
[ i
];
132 m4x3_mulv( mtx
, src
->co
, pvert
->co
);
133 m3x3_mulv( rotation
, src
->norm
, pvert
->norm
);
135 v4_copy( src
->colour
, pvert
->colour
);
136 v2_copy( src
->uv
, pvert
->uv
);
138 float rel_y
= src
->co
[1] / submodel
->bbx
[1][1];
139 pvert
->colour
[0] = rel_y
;
140 pvert
->colour
[2] = rand_hue
;
143 for( u32 i
=0; i
<submodel
->indice_count
; i
++ )
145 u32
*pidx
= &pscene
->indices
[ pscene
->indice_count
+i
];
146 *pidx
= src_indices
[i
] + pscene
->vertex_count
;
149 pscene
->vertex_count
+= submodel
->vertex_count
;
150 pscene
->indice_count
+= submodel
->indice_count
;
153 static void scene_add_foliage( scene
*pscene
, model
*mdl
, submodel
*submodel
,
156 pscene
->verts
= buffer_reserve( pscene
->verts
, pscene
->vertex_count
,
157 &pscene
->vertex_cap
, submodel
->vertex_count
, sizeof(model_vert
) );
158 pscene
->indices
= buffer_reserve( pscene
->indices
, pscene
->indice_count
,
159 &pscene
->indice_cap
, submodel
->indice_count
, sizeof(u32
) );
161 /* Transform and place vertices */
162 model_vert
*src_verts
= submodel_vert_data( mdl
, submodel
);
163 u32
*src_indices
= submodel_indice_data( mdl
, submodel
);
166 box_copy( submodel
->bbx
, bbxnew
);
167 m4x3_transform_aabb( transform
, bbxnew
);
168 box_concat( pscene
->bbx
, bbxnew
);
170 float rand_hue
= vg_randf();
171 for( u32 i
=0; i
<submodel
->vertex_count
; i
++ )
173 model_vert
*pvert
= &pscene
->verts
[ pscene
->vertex_count
+i
],
174 *src
= &src_verts
[ i
];
176 m4x3_mulv( transform
, src
->co
, pvert
->co
);
177 m3x3_mulv( transform
, src
->norm
, pvert
->norm
);
179 v4_copy( src
->colour
, pvert
->colour
);
180 v2_copy( src
->uv
, pvert
->uv
);
182 float rel_y
= src
->co
[1] / submodel
->bbx
[1][1];
183 pvert
->colour
[0] = rel_y
;
184 pvert
->colour
[2] = rand_hue
;
187 for( u32 i
=0; i
<submodel
->indice_count
; i
++ )
189 u32
*pidx
= &pscene
->indices
[ pscene
->indice_count
+i
];
190 *pidx
= src_indices
[i
] + pscene
->vertex_count
;
193 pscene
->vertex_count
+= submodel
->vertex_count
;
194 pscene
->indice_count
+= submodel
->indice_count
;
197 static void scene_copy_slice( scene
*pscene
, submodel
*sm
)
199 sm
->indice_start
= pscene
->submesh
.indice_start
;
200 sm
->indice_count
= pscene
->indice_count
- sm
->indice_start
;
202 sm
->vertex_start
= pscene
->submesh
.vertex_start
;
203 sm
->vertex_count
= pscene
->vertex_count
- sm
->vertex_start
;
205 pscene
->submesh
.indice_start
= pscene
->indice_count
;
206 pscene
->submesh
.vertex_start
= pscene
->vertex_count
;
209 static void scene_upload( scene
*pscene
)
211 mesh_upload( &pscene
->mesh
,
212 pscene
->verts
, pscene
->vertex_count
,
213 pscene
->indices
, pscene
->indice_count
);
215 vg_info( "Scene upload\n" );
216 vg_info( " indices:%u\n", pscene
->indice_count
);
217 vg_info( " verts:%u\n", pscene
->vertex_count
);
220 static void scene_bind( scene
*pscene
)
222 mesh_bind( &pscene
->mesh
);
225 static void scene_draw( scene
*pscene
)
227 mesh_drawn( 0, pscene
->indice_count
);
230 static void scene_register(void)
238 static void scene_bh_expand_bound( void *user
, boxf bound
, u32 item_index
)
241 model_vert
*pa
= &s
->verts
[ s
->indices
[item_index
*3+0] ],
242 *pb
= &s
->verts
[ s
->indices
[item_index
*3+1] ],
243 *pc
= &s
->verts
[ s
->indices
[item_index
*3+2] ];
245 box_addpt( bound
, pa
->co
);
246 box_addpt( bound
, pb
->co
);
247 box_addpt( bound
, pc
->co
);
250 static float scene_bh_centroid( void *user
, u32 item_index
, int axis
)
253 model_vert
*pa
= &s
->verts
[ s
->indices
[item_index
*3+0] ],
254 *pb
= &s
->verts
[ s
->indices
[item_index
*3+1] ],
255 *pc
= &s
->verts
[ s
->indices
[item_index
*3+2] ];
257 return (pa
->co
[axis
] + pb
->co
[axis
] + pc
->co
[axis
]) * (1.0f
/3.0f
);
260 static void scene_bh_swap( void *user
, u32 ia
, u32 ib
)
264 u32
*ti
= &s
->indices
[ia
*3];
265 u32
*tj
= &s
->indices
[ib
*3];
281 static void scene_bh_debug( void *user
, u32 item_index
)
284 u32 idx
= item_index
*3;
285 model_vert
*pa
= &s
->verts
[ s
->indices
[ idx
+0 ] ],
286 *pb
= &s
->verts
[ s
->indices
[ idx
+1 ] ],
287 *pc
= &s
->verts
[ s
->indices
[ idx
+2 ] ];
289 vg_line( pa
->co
, pb
->co
, 0xff0000ff );
290 vg_line( pb
->co
, pc
->co
, 0xff0000ff );
291 vg_line( pc
->co
, pa
->co
, 0xff0000ff );
294 static int scene_bh_ray( void *user
, u32 index
, v3f co
, v3f dir
, ray_hit
*hit
)
299 u32
*tri
= &s
->indices
[ index
*3 ];
301 for( int i
=0; i
<3; i
++ )
302 v3_copy( s
->verts
[tri
[i
]].co
, positions
[i
] );
305 if(ray_tri( positions
, co
, dir
, &t
))
318 static bh_system bh_system_scene
=
320 .expand_bound
= scene_bh_expand_bound
,
321 .item_centroid
= scene_bh_centroid
,
322 .item_swap
= scene_bh_swap
,
323 .item_debug
= scene_bh_debug
,
324 .cast_ray
= scene_bh_ray
328 * An extra step is added onto the end to calculate the hit normal
330 static int scene_raycast( scene
*s
, v3f co
, v3f dir
, ray_hit
*hit
)
332 int count
= bh_ray( &s
->bhtris
, 0, co
, dir
, hit
);
338 float *pa
= s
->verts
[hit
->tri
[0]].co
,
339 *pb
= s
->verts
[hit
->tri
[1]].co
,
340 *pc
= s
->verts
[hit
->tri
[2]].co
;
342 v3_sub( pa
, pb
, v0
);
343 v3_sub( pc
, pb
, v1
);
344 v3_cross( v1
, v0
, hit
->normal
);
345 v3_normalize( hit
->normal
);
346 v3_muladds( co
, dir
, hit
->dist
, hit
->pos
);
352 static void scene_bh_create( scene
*s
)
354 u32 triangle_count
= s
->indice_count
/ 3;
355 bh_create( &s
->bhtris
, &bh_system_scene
, s
, triangle_count
);