small refactor of model loading
[carveJwlIkooP6JGAAIwe30JlM.git] / scene.h
diff --git a/scene.h b/scene.h
index b76aaee475320ab24a63a3130161de02319539fe..8d6f57a89e279ac8f349f35c2b846769ba2e80dc 100644 (file)
--- a/scene.h
+++ b/scene.h
-#ifndef SCENE_H
-#define SCENE_H
-
+#pragma once
+#include "vg/vg_bvh.h"
+#include "vg/vg_async.h"
 #include "common.h"
 #include "model.h"
-#include "bvh.h"
-
-typedef struct scene scene;
-#if 0
-typedef struct bvh_node bvh_node;
-#endif
-
-struct scene
-{
-   glmesh mesh;
-
-   model_vert *verts;
-   u32 *indices;
-   
-#if 0
-   struct 
-   {
-      bvh_node *nodes;
-      u32 node_count;
-   }
-   bvh;
-#else
-   bh_tree bhtris;
-#endif
-
-   u32 vertex_count,
-       indice_count,
-       vertex_cap,
-       indice_cap;
-
-   boxf bbx;
-
-   u32 shadower_count,
-       shadower_cap;
-
-   submodel submesh;
-};
-
-GLuint tex_dual_noise;
-
-static void scene_init( scene *pscene )
-{
-   pscene->verts = NULL;
-   pscene->indices = NULL;
-   pscene->vertex_count = 0;
-   pscene->indice_count = 0;
-   pscene->shadower_count = 0;
-   pscene->shadower_cap = 0;
-   pscene->submesh.indice_start = 0;
-   pscene->submesh.indice_count = 0;
-
-   v3_fill( pscene->bbx[0],  999999.9f );
-   v3_fill( pscene->bbx[1], -999999.9f );
-
-   static int noise_ready = 0;
-   if( !noise_ready )
-   {
-      noise_ready = 1;
-
-      u8 *buf = malloc( 256*256*2 );
-
-      for( int i=0; i<256*256; i++ )
-      {
-         u8 val = rand()&0xff;
-         buf[i*2] = val;
-      }
-
-      for( int y=0; y<256; y++ )
-      {
-         for( int x=0; x<256; x++ )
-         {
-            u8 *pr = &buf[(y*256+x)*2],
-               *pg = &buf[(((y+17)&0xff)*256+((x+37)&0xff))*2+1];
-            *pg = *pr;
-         }
-      }
-      
-      /* TODO: This texture should be delted somewhere */
-      glGenTextures( 1, &tex_dual_noise );
-      glBindTexture( GL_TEXTURE_2D, tex_dual_noise );
-      glTexImage2D( GL_TEXTURE_2D, 0, GL_RG, 256, 256, 0, GL_RG,
-            GL_UNSIGNED_BYTE, buf );
 
-      vg_tex2d_linear();
-      vg_tex2d_repeat();
+typedef struct scene_context scene_context;
+typedef struct scene_vert scene_vert;
 
-      free( buf );
-   }
-}
+#pragma pack(push,1)
 
-static void *buffer_reserve( void *buffer, u32 count, u32 *cap, u32 amount, 
-      size_t emsize )
+/* 32 byte vertexs, we don't care about the normals too much,
+ *    maybe possible to bring down uv to i16s too */
+struct scene_vert
 {
-   if( count+amount > *cap )
-   {
-      *cap = VG_MAX( (*cap)*2, (*cap)+amount );
-      
-      return realloc( buffer, (*cap) * emsize );
-   }
+   v3f co;        /* 3*32 */
+   v2f uv;        /* 2*32 */
+   i8  norm[4];   /* 4*8 */
+   u16 flags; /* only for the cpu. its junk on the gpu */
+   u16 unused[3];
+};
 
-   return buffer;
-}
+#pragma pack(pop)
 
 /* 
- * Append a model into the scene with a given transform
+ * 1. this should probably be a CONTEXT based approach unlike this mess.
+ * take a bit of the mdl_context ideas and redo this header. its messed up
+ * pretty bad right now.
  */
-static void scene_add_model( scene *pscene, model *mdl, submodel *submodel,
-      v3f pos, float yaw, float scale )
-{
-   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 );
-   
-   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_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++ )
-   {
-      model_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 );
-
-      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++ )
-   {
-      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;
-}
-
-static void scene_add_foliage( scene *pscene, model *mdl, submodel *submodel,
-      m4x3f transform )
-{
-   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 ];
-
-      m4x3_mulv( transform, src->co, pvert->co );
-      m3x3_mulv( transform, 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++ )
-   {
-      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;
-}
-
-static void scene_copy_slice( scene *pscene, submodel *sm )
-{
-   sm->indice_start = pscene->submesh.indice_start;
-   sm->indice_count = pscene->indice_count - sm->indice_start;
-
-   sm->vertex_start = pscene->submesh.vertex_start;
-   sm->vertex_count = pscene->vertex_count - sm->vertex_start;
-   
-   pscene->submesh.indice_start = pscene->indice_count;
-   pscene->submesh.vertex_start = pscene->vertex_count;
-}
 
-static void scene_upload( scene *pscene )
+struct scene_context
 {
-   mesh_upload( &pscene->mesh,
-         pscene->verts, pscene->vertex_count,
-         pscene->indices, pscene->indice_count );
+   scene_vert *arrvertices;
+   u32 *arrindices;
 
-   vg_info( "Scene upload\n" );
-   vg_info( "   indices:%u\n", pscene->indice_count );
-   vg_info( "   verts:%u\n", pscene->vertex_count );
-}
+   u32 vertex_count, indice_count,
+       max_vertices, max_indices;
 
-static void scene_bind( scene *pscene )
-{
-   mesh_bind( &pscene->mesh );
-}
-
-static void scene_draw( scene *pscene )
-{
-   mesh_drawn( 0, pscene->indice_count );
-}
-
-static void scene_register(void)
-{
-}
-
-/*
- * BVH implementation
- */
-
-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] ];
-   
-  box_addpt( bound, pa->co );
-  box_addpt( bound, pb->co );
-  box_addpt( bound, pc->co );
-}
-
-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] ];
-
-   return (pa->co[axis] + pb->co[axis] + pc->co[axis]) * (1.0f/3.0f);
-}
-
-static void scene_bh_swap( void *user, u32 ia, u32 ib )
-{
-   scene *s = user;
-
-   u32 *ti = &s->indices[ia*3];
-   u32 *tj = &s->indices[ib*3];
-
-   u32 temp[3];
-   temp[0] = ti[0];
-   temp[1] = ti[1];
-   temp[2] = ti[2];
-
-   ti[0] = tj[0];
-   ti[1] = tj[1];
-   ti[2] = tj[2];
-
-   tj[0] = temp[0];
-   tj[1] = temp[1];
-   tj[2] = temp[2];
-}
-
-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 ] ];
-
-   vg_line( pa->co, pb->co, 0xff0000ff );
-   vg_line( pb->co, pc->co, 0xff0000ff );
-   vg_line( pc->co, pa->co, 0xff0000ff );
-}
-
-static int scene_bh_ray( void *user, u32 index, v3f co, v3f dir, ray_hit *hit )
-{
-   scene *s = user;
-   v3f positions[3];
-   
-   u32 *tri = &s->indices[ index*3 ];
-
-   for( int i=0; i<3; i++ )
-      v3_copy( s->verts[tri[i]].co, positions[i] );
-   
-   float t;
-   if(ray_tri( positions, co, dir, &t ))
-   {
-      if( t < hit->dist )
-      {
-         hit->dist = t;
-         hit->tri = tri;
-         return 1;
-      }
-   }
-
-   return 0;
-}
-
-static bh_system bh_system_scene = 
-{
-   .expand_bound = scene_bh_expand_bound,
-   .item_centroid = scene_bh_centroid,
-   .item_swap = scene_bh_swap,
-   .item_debug = scene_bh_debug,
-   .cast_ray = scene_bh_ray
+   boxf bbx;
+   mdl_submesh submesh;
 };
 
-/*
- * An extra step is added onto the end to calculate the hit normal
- */
-static int scene_raycast( scene *s, v3f co, v3f dir, ray_hit *hit )
-{
-   int count = bh_ray( &s->bhtris, 0, co, dir, hit );
-
-   if( count )
-   {
-      v3f v0, v1;
-      
-      float *pa = s->verts[hit->tri[0]].co,
-            *pb = s->verts[hit->tri[1]].co,
-            *pc = s->verts[hit->tri[2]].co;
-
-      v3_sub( pa, pb, v0 );
-      v3_sub( pc, pb, v1 );
-      v3_cross( v1, v0, hit->normal );
-      v3_normalize( hit->normal );
-      v3_muladds( co, dir, hit->dist, hit->pos );
-   }
-
-   return count;
-}
-
-static void scene_bh_create( scene *s )
-{
-   u32 triangle_count = s->indice_count / 3;
-   bh_create( &s->bhtris, &bh_system_scene, s, triangle_count );
-}
-
-#endif
+extern bh_system bh_system_scene;
+bh_tree *scene_bh_create( void *lin_alloc, scene_context *s );
+int scene_raycast( scene_context *s, bh_tree *bh, 
+                   v3f co, v3f dir, ray_hit *hit, u16 ignore );
+vg_async_item *scene_alloc_async( scene_context *scene, glmesh *mesh,
+                                  u32 max_vertices, u32 max_indices );
+void scene_copy_slice( scene_context *ctx, mdl_submesh *sm );
+void scene_push_vert( scene_context *ctx, scene_vert *v );
+void scene_vert_pack_norm( scene_vert *vert, v3f norm, f32 blend );
+void scene_push_tri( scene_context *ctx, u32 tri[3] );
+void scene_add_mdl_submesh( scene_context *ctx, mdl_context *mdl, 
+                            mdl_submesh *sm, m4x3f transform );
+void scene_set_vertex_flags( scene_context *ctx, 
+                             u32 start, u32 count, u16 flags );
+void scene_supply_buffer( scene_context *ctx, void *buffer );
+void scene_init( scene_context *ctx, u32 max_vertices, u32 max_indices );
+u32 scene_mem_required( scene_context *ctx );
+void async_scene_upload( void *payload, u32 size );
+void scene_upload_async( scene_context *ctx, glmesh *mesh );