8 typedef struct scene scene
;
32 GLuint tex_dual_noise
;
35 static void scene_init( scene
*pscene
)
38 pscene
->indices
= NULL
;
39 pscene
->vertex_count
= 0;
40 pscene
->indice_count
= 0;
41 pscene
->shadower_count
= 0;
42 pscene
->shadower_cap
= 0;
43 pscene
->submesh
.indice_start
= 0;
44 pscene
->submesh
.indice_count
= 0;
46 v3_fill( pscene
->bbx
[0], 999999.9f
);
47 v3_fill( pscene
->bbx
[1], -999999.9f
);
50 static int noise_ready
= 0;
55 u8
*buf
= malloc( 256*256*2 );
57 for( int i
=0; i
<256*256; i
++ )
63 for( int y
=0; y
<256; y
++ )
65 for( int x
=0; x
<256; x
++ )
67 u8
*pr
= &buf
[(y
*256+x
)*2],
68 *pg
= &buf
[(((y
+17)&0xff)*256+((x
+37)&0xff))*2+1];
73 /* TODO: This texture should be delted somewhere */
74 glGenTextures( 1, &tex_dual_noise
);
75 glBindTexture( GL_TEXTURE_2D
, tex_dual_noise
);
76 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RG
, 256, 256, 0, GL_RG
,
77 GL_UNSIGNED_BYTE
, buf
);
88 * Append a model into the scene with a given transform
90 static void scene_add_submesh( scene
*pscene
, mdl_header
*mdl
,
91 mdl_submesh
*sm
, m4x3f transform
)
93 pscene
->verts
= buffer_reserve( pscene
->verts
, pscene
->vertex_count
,
94 &pscene
->vertex_cap
, sm
->vertex_count
, sizeof(mdl_vert
) );
95 pscene
->indices
= buffer_reserve( pscene
->indices
, pscene
->indice_count
,
96 &pscene
->indice_cap
, sm
->indice_count
, sizeof(u32
) );
99 m3x3_copy( transform
, normal_matrix
);
100 v3_normalize( normal_matrix
[0] );
101 v3_normalize( normal_matrix
[1] );
102 v3_normalize( normal_matrix
[2] );
104 /* Transform and place vertices */
105 mdl_vert
*src_verts
= mdl_submesh_vertices( mdl
, sm
);
106 u32
*src_indices
= mdl_submesh_indices( mdl
, sm
);
109 box_copy( sm
->bbx
, bbxnew
);
110 m4x3_transform_aabb( transform
, bbxnew
);
111 box_concat( pscene
->bbx
, bbxnew
);
113 for( u32 i
=0; i
<sm
->vertex_count
; i
++ )
115 mdl_vert
*pvert
= &pscene
->verts
[ pscene
->vertex_count
+i
],
116 *src
= &src_verts
[ i
];
118 m4x3_mulv( transform
, src
->co
, pvert
->co
);
119 m3x3_mulv( normal_matrix
, src
->norm
, pvert
->norm
);
121 pvert
->colour
[0] = src
->colour
[0];
122 pvert
->colour
[1] = src
->colour
[1];
123 pvert
->colour
[2] = src
->colour
[2];
124 pvert
->colour
[3] = src
->colour
[3];
125 v2_copy( src
->uv
, pvert
->uv
);
128 for( u32 i
=0; i
<sm
->indice_count
; i
++ )
130 u32
*pidx
= &pscene
->indices
[ pscene
->indice_count
+i
];
131 *pidx
= src_indices
[i
] + pscene
->vertex_count
;
134 pscene
->vertex_count
+= sm
->vertex_count
;
135 pscene
->indice_count
+= sm
->indice_count
;
139 * One by one adders for simplified access (mostly procedural stuff)
141 static void scene_push_tri( scene
*pscene
, u32 tri
[3] )
143 pscene
->indices
= buffer_reserve( pscene
->indices
, pscene
->indice_count
,
144 &pscene
->indice_cap
, 3, sizeof(u32
) );
146 u32
*dst
= &pscene
->indices
[pscene
->indice_count
];
151 pscene
->indice_count
+= 3;
154 static void scene_push_vert( scene
*pscene
, mdl_vert
*v
)
156 pscene
->verts
= buffer_reserve( pscene
->verts
, pscene
->vertex_count
,
157 &pscene
->vertex_cap
, 1, sizeof(mdl_vert
) );
159 pscene
->verts
[pscene
->vertex_count
++] = *v
;
162 static void scene_copy_slice( scene
*pscene
, mdl_submesh
*sm
)
164 sm
->indice_start
= pscene
->submesh
.indice_start
;
165 sm
->indice_count
= pscene
->indice_count
- sm
->indice_start
;
167 sm
->vertex_start
= pscene
->submesh
.vertex_start
;
168 sm
->vertex_count
= pscene
->vertex_count
- sm
->vertex_start
;
170 pscene
->submesh
.indice_start
= pscene
->indice_count
;
171 pscene
->submesh
.vertex_start
= pscene
->vertex_count
;
174 static void scene_fix( scene
*pscene
)
176 buffer_fix( pscene
->verts
, pscene
->vertex_count
,
177 &pscene
->vertex_cap
, sizeof( mdl_vert
));
179 buffer_fix( pscene
->indices
, pscene
->indice_count
,
180 &pscene
->indice_cap
, sizeof( mdl_vert
));
183 __attribute__((warn_unused_result
))
184 static int scene_upload( scene
*pscene
)
186 if( ! mesh_upload( &pscene
->mesh
,
187 pscene
->verts
, pscene
->vertex_count
,
188 pscene
->indices
, pscene
->indice_count
) )
193 vg_info( "Scene upload\n" );
194 vg_info( " indices:%u\n", pscene
->indice_count
);
195 vg_info( " verts:%u\n", pscene
->vertex_count
);
200 static void scene_bind( scene
*pscene
)
202 mesh_bind( &pscene
->mesh
);
205 static void scene_draw( scene
*pscene
)
207 mesh_drawn( 0, pscene
->indice_count
);
210 static void scene_free_offline_buffers( scene
*pscene
)
212 free( pscene
->verts
);
213 free( pscene
->indices
);
216 static void scene_free( scene
*pscene
)
218 scene_free_offline_buffers( pscene
);
226 static void scene_bh_expand_bound( void *user
, boxf bound
, u32 item_index
)
229 mdl_vert
*pa
= &s
->verts
[ s
->indices
[item_index
*3+0] ],
230 *pb
= &s
->verts
[ s
->indices
[item_index
*3+1] ],
231 *pc
= &s
->verts
[ s
->indices
[item_index
*3+2] ];
233 box_addpt( bound
, pa
->co
);
234 box_addpt( bound
, pb
->co
);
235 box_addpt( bound
, pc
->co
);
238 static float scene_bh_centroid( void *user
, u32 item_index
, int axis
)
241 mdl_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 return (pa
->co
[axis
] + pb
->co
[axis
] + pc
->co
[axis
]) * (1.0f
/3.0f
);
248 static void scene_bh_swap( void *user
, u32 ia
, u32 ib
)
252 u32
*ti
= &s
->indices
[ia
*3];
253 u32
*tj
= &s
->indices
[ib
*3];
269 static void scene_bh_debug( void *user
, u32 item_index
)
272 u32 idx
= item_index
*3;
273 mdl_vert
*pa
= &s
->verts
[ s
->indices
[ idx
+0 ] ],
274 *pb
= &s
->verts
[ s
->indices
[ idx
+1 ] ],
275 *pc
= &s
->verts
[ s
->indices
[ idx
+2 ] ];
277 vg_line( pa
->co
, pb
->co
, 0xff0000ff );
278 vg_line( pb
->co
, pc
->co
, 0xff0000ff );
279 vg_line( pc
->co
, pa
->co
, 0xff0000ff );
282 static int scene_bh_ray( void *user
, u32 index
, v3f co
, v3f dir
, ray_hit
*hit
)
287 u32
*tri
= &s
->indices
[ index
*3 ];
289 for( int i
=0; i
<3; i
++ )
290 v3_copy( s
->verts
[tri
[i
]].co
, positions
[i
] );
293 if(ray_tri( positions
, co
, dir
, &t
))
306 static bh_system bh_system_scene
=
308 .expand_bound
= scene_bh_expand_bound
,
309 .item_centroid
= scene_bh_centroid
,
310 .item_swap
= scene_bh_swap
,
311 .item_debug
= scene_bh_debug
,
312 .cast_ray
= scene_bh_ray
316 * An extra step is added onto the end to calculate the hit normal
318 static int scene_raycast( scene
*s
, v3f co
, v3f dir
, ray_hit
*hit
)
320 int count
= bh_ray( &s
->bhtris
, 0, co
, dir
, hit
);
326 float *pa
= s
->verts
[hit
->tri
[0]].co
,
327 *pb
= s
->verts
[hit
->tri
[1]].co
,
328 *pc
= s
->verts
[hit
->tri
[2]].co
;
330 v3_sub( pa
, pb
, v0
);
331 v3_sub( pc
, pb
, v1
);
332 v3_cross( v1
, v0
, hit
->normal
);
333 v3_normalize( hit
->normal
);
334 v3_muladds( co
, dir
, hit
->dist
, hit
->pos
);
340 static void scene_bh_create( scene
*s
)
342 u32 triangle_count
= s
->indice_count
/ 3;
343 bh_create( &s
->bhtris
, &bh_system_scene
, s
, triangle_count
);