i forgo
[carveJwlIkooP6JGAAIwe30JlM.git] / scene.h
diff --git a/scene.h b/scene.h
index 10015f5fa43eca1494ca1540b8c5c03025d81a6b..e00445aba4f8b38ff320a3e3713cdd6c70b27ef3 100644 (file)
--- a/scene.h
+++ b/scene.h
@@ -421,8 +421,12 @@ 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_shadow_sphere( scene *pscene, v3f sphere, 
@@ -971,6 +975,23 @@ static void bvh_subdiv( scene *s, u32 inode )
 
    float split = node->bbx[0][axis] + extent[axis]*0.5f;
 
+   /* To beat: 121,687 / 136,579 
+    *          136,375
+    */
+
+   float avg = 0.0;
+   for( u32 t=0; t<node->count; t++ )
+   {
+      u32 *ti = &s->indices[(node->start+t)*3];
+      float a = s->verts[ti[0]].co[axis],
+             b = s->verts[ti[1]].co[axis],
+             c = s->verts[ti[2]].co[axis];
+      avg += (a+b+c)/3.0;
+   }
+   avg /= (float)node->count;
+
+   split = avg;
+
    i32 i = node->start,
        j = i + node->count-1;
    
@@ -1090,55 +1111,6 @@ static void bvh_debug( scene *s, v3f pos )
    bvh_debug_node( s, 0, pos, 0x4000ffa8 );
 }
 
-static int bvh_scene_sample_node( scene *s, u32 inode, v3f pos, v3f norm )
-{
-   bvh_node *node = &s->bvh.nodes[ inode ];
-
-   if( (pos[0] >= node->bbx[0][0] && pos[0] <= node->bbx[1][0]) &&
-       (pos[2] >= node->bbx[0][2] && pos[2] <= node->bbx[1][2]) )
-   {
-      if( !node->count )
-      {
-         if( bvh_scene_sample_node( s, node->il, pos, norm )) return 1;
-         if( bvh_scene_sample_node( s, node->ir, pos, norm )) return 1;
-      }
-      else
-      {
-         for( u32 i=0; i<node->count; i++ )
-         {
-            u32 idx = (node->start+i)*3;
-            model_vert *pa = &s->verts[ s->indices[ idx+0 ] ],
-                       *pb = &s->verts[ s->indices[ idx+1 ] ],
-                       *pc = &s->verts[ s->indices[ idx+2 ] ];
-            
-            float height;
-            if( triangle_raycast2d( pa->co, pb->co, pc->co, pos, &height ))
-            {
-               pos[1] = height;
-               
-               if( norm )
-               {
-                  v3f v0, v1;
-                  v3_sub( pa->co, pb->co, v0 );
-                  v3_sub( pc->co, pb->co, v1 );
-                  v3_cross( v1, v0, norm );
-                  v3_normalize( norm );
-               }
-
-               return 1;
-            }
-         }
-      }
-   }
-
-   return 0;
-}
-
-static int bvh_scene_sample( scene *s, v3f pos, v3f norm)
-{
-   return bvh_scene_sample_node( s, 0, pos, norm );
-}
-
 typedef struct ray_hit ray_hit;
 struct ray_hit
 {
@@ -1248,7 +1220,7 @@ static int bvh_raycast( scene *s, v3f co, v3f dir, ray_hit *hit )
 
    if( count )
    {
-      vg_line( co, pb, 0xff00ffff );
+      //vg_line( co, pb, 0xff00ffff );
 
       v3f v0, v1;
       
@@ -1263,9 +1235,76 @@ static int bvh_raycast( scene *s, v3f co, v3f dir, ray_hit *hit )
       v3_muladds( co, dir, hit->dist, hit->pos );
    }
    else
-      vg_line( co, pb, 0xff0000ff );
+   {
+      //vg_line( co, pb, 0xff0000ff );
+   }
 
    return count;
 }
 
+static int bvh_scene_sample_node_h( scene *s, u32 inode, v3f pos, v3f norm )
+{
+   bvh_node *node = &s->bvh.nodes[ inode ];
+
+   if( (pos[0] >= node->bbx[0][0] && pos[0] <= node->bbx[1][0]) &&
+       (pos[2] >= node->bbx[0][2] && pos[2] <= node->bbx[1][2]) )
+   {
+      if( !node->count )
+      {
+         if( bvh_scene_sample_node_h( s, node->il, pos, norm )) return 1;
+         if( bvh_scene_sample_node_h( s, node->ir, pos, norm )) return 1;
+      }
+      else
+      {
+         for( u32 i=0; i<node->count; i++ )
+         {
+            u32 idx = (node->start+i)*3;
+            model_vert *pa = &s->verts[ s->indices[ idx+0 ] ],
+                       *pb = &s->verts[ s->indices[ idx+1 ] ],
+                       *pc = &s->verts[ s->indices[ idx+2 ] ];
+            
+            float height;
+            if( triangle_raycast2d( pa->co, pb->co, pc->co, pos, &height ))
+            {
+               pos[1] = height;
+               
+               if( norm )
+               {
+                  v3f v0, v1;
+                  v3_sub( pa->co, pb->co, v0 );
+                  v3_sub( pc->co, pb->co, v1 );
+                  v3_cross( v1, v0, norm );
+                  v3_normalize( norm );
+               }
+
+               return 1;
+            }
+         }
+      }
+   }
+
+   return 0;
+}
+
+static int bvh_scene_sample_h( scene *s, v3f pos, v3f norm)
+{
+   return bvh_scene_sample_node_h( s, 0, pos, norm );
+}
+
+static int bvh_scene_sample( scene *s, v3f pos, ray_hit *hit )
+{
+   hit->dist = INFINITY;
+
+   v3f ray_pos;
+   v3_add( pos, (v3f){0.0f,4.0f,0.0f}, ray_pos );
+
+   if( bvh_raycast( s, ray_pos, (v3f){0.0f,-1.0f,0.0f}, hit ))
+   {
+      pos[1] = hit->pos[1];
+      return 1;
+   }
+
+   return 0;
+}
+
 #endif