stuff
[carveJwlIkooP6JGAAIwe30JlM.git] / scene.h
diff --git a/scene.h b/scene.h
index 6f7c28cbe63b3ac4a0bfd1e6feed6eab22a45f40..b4deaa95bfc96c6ab56095dfe0e2bd1e1d6edb90 100644 (file)
--- a/scene.h
+++ b/scene.h
@@ -11,7 +11,7 @@ struct scene
 {
    glmesh mesh;
 
-   model_vert *verts;
+   mdl_vert *verts;
    u32 *indices;
    
    bh_tree bhtris;
@@ -25,10 +25,12 @@ struct scene
    u32 shadower_count,
        shadower_cap;
 
-   submodel submesh;
+   mdl_submesh submesh;
 };
 
+#if 0
 GLuint tex_dual_noise;
+#endif
 
 static void scene_init( scene *pscene )
 {
@@ -44,6 +46,7 @@ static void scene_init( scene *pscene )
    v3_fill( pscene->bbx[0],  999999.9f );
    v3_fill( pscene->bbx[1], -999999.9f );
 
+#if 0
    static int noise_ready = 0;
    if( !noise_ready )
    {
@@ -78,6 +81,7 @@ static void scene_init( scene *pscene )
 
       free( buf );
    }
+#endif
 }
 
 static void *buffer_reserve( void *buffer, u32 count, u32 *cap, u32 amount, 
@@ -93,108 +97,85 @@ static void *buffer_reserve( void *buffer, u32 count, u32 *cap, u32 amount,
    return buffer;
 }
 
+static void *buffer_fix( void *buffer, u32 count, u32 *cap, size_t emsize )
+{
+   *cap = count;
+   return realloc( buffer, (*cap) * emsize );
+}
+
 /* 
  * Append a model into the scene with a given transform
  */
-static void scene_add_model( scene *pscene, model *mdl, submodel *submodel,
-      v3f pos, float yaw, float scale )
+static void scene_add_submesh( scene *pscene, mdl_header *mdl, 
+      mdl_submesh *sm, m4x3f transform )
 {
    pscene->verts = buffer_reserve( pscene->verts, pscene->vertex_count, 
-         &pscene->vertex_cap, submodel->vertex_count, sizeof(model_vert) );
+         &pscene->vertex_cap, sm->vertex_count, sizeof(mdl_vert) );
    pscene->indices = buffer_reserve( pscene->indices, pscene->indice_count,
-         &pscene->indice_cap, submodel->indice_count, sizeof(u32) );
+         &pscene->indice_cap, sm->indice_count, sizeof(u32) );
+
+   m3x3f normal_matrix;
+   m3x3_copy( transform, normal_matrix );
+   v3_normalize( normal_matrix[0] );
+   v3_normalize( normal_matrix[1] );
+   v3_normalize( normal_matrix[2] );
    
    /* Transform and place vertices */
-   model_vert *src_verts = submodel_vert_data( mdl, submodel );
-   u32 *src_indices = submodel_indice_data( mdl, submodel );
+   mdl_vert *src_verts = mdl_submesh_vertices( mdl, sm );
+   u32 *src_indices    = mdl_submesh_indices( mdl, sm );
    
-   m4x3f mtx;
-   m4x3_identity( mtx );
-   m4x3_translate( mtx, pos );
-   m4x3_rotate_y( mtx, yaw );
-   m4x3_scale( mtx, scale );
-
    boxf bbxnew;
-   box_copy( submodel->bbx, bbxnew );
-   m4x3_transform_aabb( mtx, bbxnew );
+   box_copy( sm->bbx, bbxnew );
+   m4x3_transform_aabb( transform, bbxnew );
    box_concat( pscene->bbx, bbxnew );
    
-   m3x3f rotation;
-   m4x3_to_3x3( mtx, rotation );
-
-   float rand_hue = vg_randf();
-
-   for( u32 i=0; i<submodel->vertex_count; i++ )
+   for( u32 i=0; i<sm->vertex_count; i++ )
    {
-      model_vert *pvert = &pscene->verts[ pscene->vertex_count+i ],
-                 *src = &src_verts[ i ];
+      mdl_vert *pvert = &pscene->verts[ pscene->vertex_count+i ],
+               *src = &src_verts[ i ];
 
-      m4x3_mulv( mtx, src->co, pvert->co );
-      m3x3_mulv( rotation, src->norm, pvert->norm );
+      m4x3_mulv( transform, src->co, pvert->co );
+      m3x3_mulv( normal_matrix, src->norm, pvert->norm );
 
       v4_copy( src->colour, pvert->colour );
       v2_copy( src->uv, pvert->uv );
-      
-      float rel_y = src->co[1] / submodel->bbx[1][1];
-      pvert->colour[0] = rel_y;
-      pvert->colour[2] = rand_hue;
    }
 
-   for( u32 i=0; i<submodel->indice_count; i++ )
+   for( u32 i=0; i<sm->indice_count; i++ )
    {
       u32 *pidx = &pscene->indices[ pscene->indice_count+i ];
       *pidx = src_indices[i] + pscene->vertex_count;
    }
 
-   pscene->vertex_count += submodel->vertex_count;
-   pscene->indice_count += submodel->indice_count;
+   pscene->vertex_count += sm->vertex_count;
+   pscene->indice_count += sm->indice_count;
 }
 
-static void scene_add_foliage( scene *pscene, model *mdl, submodel *submodel,
-      m4x3f transform )
+/*
+ * One by one adders for simplified access (mostly procedural stuff)
+ */
+static void scene_push_tri( scene *pscene, u32 tri[3] )
 {
-   pscene->verts = buffer_reserve( pscene->verts, pscene->vertex_count, 
-         &pscene->vertex_cap, submodel->vertex_count, sizeof(model_vert) );
    pscene->indices = buffer_reserve( pscene->indices, pscene->indice_count,
-         &pscene->indice_cap, submodel->indice_count, sizeof(u32) );
-   
-   /* Transform and place vertices */
-   model_vert *src_verts = submodel_vert_data( mdl, submodel );
-   u32 *src_indices = submodel_indice_data( mdl, submodel );
-   
-   boxf bbxnew;
-   box_copy( submodel->bbx, bbxnew );
-   m4x3_transform_aabb( transform, bbxnew );
-   box_concat( pscene->bbx, bbxnew );
-   
-   float rand_hue = vg_randf();
-   for( u32 i=0; i<submodel->vertex_count; i++ )
-   {
-      model_vert *pvert = &pscene->verts[ pscene->vertex_count+i ],
-                 *src = &src_verts[ i ];
+         &pscene->indice_cap, 3, sizeof(u32) );
 
-      m4x3_mulv( transform, src->co, pvert->co );
-      m3x3_mulv( transform, src->norm, pvert->norm );
+   u32 *dst = &pscene->indices[pscene->indice_count];
+   dst[0] = tri[0];
+   dst[1] = tri[1];
+   dst[2] = tri[2];
 
-      v4_copy( src->colour, pvert->colour );
-      v2_copy( src->uv, pvert->uv );
-      
-      float rel_y = src->co[1] / submodel->bbx[1][1];
-      pvert->colour[0] = rel_y;
-      pvert->colour[2] = rand_hue;
-   }
+   pscene->indice_count += 3;
+}
 
-   for( u32 i=0; i<submodel->indice_count; i++ )
-   {
-      u32 *pidx = &pscene->indices[ pscene->indice_count+i ];
-      *pidx = src_indices[i] + pscene->vertex_count;
-   }
+static void scene_push_vert( scene *pscene, mdl_vert *v )
+{
+   pscene->verts = buffer_reserve( pscene->verts, pscene->vertex_count, 
+         &pscene->vertex_cap, 1, sizeof(mdl_vert) );
 
-   pscene->vertex_count += submodel->vertex_count;
-   pscene->indice_count += submodel->indice_count;
+   pscene->verts[pscene->vertex_count ++] = *v;
 }
 
-static void scene_copy_slice( scene *pscene, submodel *sm )
+static void scene_copy_slice( scene *pscene, mdl_submesh *sm )
 {
    sm->indice_start = pscene->submesh.indice_start;
    sm->indice_count = pscene->indice_count - sm->indice_start;
@@ -206,6 +187,15 @@ static void scene_copy_slice( scene *pscene, submodel *sm )
    pscene->submesh.vertex_start = pscene->vertex_count;
 }
 
+static void scene_fix( scene *pscene )
+{
+   buffer_fix( pscene->verts, pscene->vertex_count, 
+         &pscene->vertex_cap, sizeof( mdl_vert ));
+
+   buffer_fix( pscene->indices, pscene->indice_count, 
+         &pscene->indice_cap, sizeof( mdl_vert ));
+}
+
 static void scene_upload( scene *pscene )
 {
    mesh_upload( &pscene->mesh,
@@ -227,6 +217,18 @@ static void scene_draw( scene *pscene )
    mesh_drawn( 0, pscene->indice_count );
 }
 
+static void scene_free_offline_buffers( scene *pscene )
+{
+   free( pscene->verts );
+   free( pscene->indices );
+}
+
+static void scene_free( scene *pscene )
+{
+   scene_free_offline_buffers( pscene );
+   /* TODO: bvh */
+}
+
 static void scene_register(void)
 {
 }
@@ -238,9 +240,9 @@ static void scene_register(void)
 static void scene_bh_expand_bound( void *user, boxf bound, u32 item_index )
 {
    scene *s = user;
-   model_vert *pa = &s->verts[ s->indices[item_index*3+0] ],
-              *pb = &s->verts[ s->indices[item_index*3+1] ],
-              *pc = &s->verts[ s->indices[item_index*3+2] ];
+   mdl_vert *pa = &s->verts[ s->indices[item_index*3+0] ],
+            *pb = &s->verts[ s->indices[item_index*3+1] ],
+            *pc = &s->verts[ s->indices[item_index*3+2] ];
    
   box_addpt( bound, pa->co );
   box_addpt( bound, pb->co );
@@ -250,9 +252,9 @@ static void scene_bh_expand_bound( void *user, boxf bound, u32 item_index )
 static float scene_bh_centroid( void *user, u32 item_index, int axis )
 {
    scene *s = user;
-   model_vert *pa = &s->verts[ s->indices[item_index*3+0] ],
-              *pb = &s->verts[ s->indices[item_index*3+1] ],
-              *pc = &s->verts[ s->indices[item_index*3+2] ];
+   mdl_vert *pa = &s->verts[ s->indices[item_index*3+0] ],
+            *pb = &s->verts[ s->indices[item_index*3+1] ],
+            *pc = &s->verts[ s->indices[item_index*3+2] ];
 
    return (pa->co[axis] + pb->co[axis] + pc->co[axis]) * (1.0f/3.0f);
 }
@@ -282,9 +284,9 @@ static void scene_bh_debug( void *user, u32 item_index )
 {
    scene *s = user;
    u32 idx = item_index*3;
-   model_vert *pa = &s->verts[ s->indices[ idx+0 ] ],
-              *pb = &s->verts[ s->indices[ idx+1 ] ],
-              *pc = &s->verts[ s->indices[ idx+2 ] ];
+   mdl_vert *pa = &s->verts[ s->indices[ idx+0 ] ],
+            *pb = &s->verts[ s->indices[ idx+1 ] ],
+            *pc = &s->verts[ s->indices[ idx+2 ] ];
 
    vg_line( pa->co, pb->co, 0xff0000ff );
    vg_line( pb->co, pc->co, 0xff0000ff );