Manta Interactive Ray Tracer Development Mailing List

Text archives Help


[MANTA] r994 - trunk/Model/Groups


Chronological Thread 
  • From: bigler@sci.utah.edu
  • To: manta@sci.utah.edu
  • Subject: [MANTA] r994 - trunk/Model/Groups
  • Date: Thu, 30 Mar 2006 16:57:03 -0700 (MST)

Author: bigler
Date: Thu Mar 30 16:57:02 2006
New Revision: 994

Modified:
   trunk/Model/Groups/KDTreeLoaderIW.cc
Log:

Got the data loading properly as far as I can tell.  The conference
scene has holes in it, but that could be due to the processor in my
laptop.


Modified: trunk/Model/Groups/KDTreeLoaderIW.cc
==============================================================================
--- trunk/Model/Groups/KDTreeLoaderIW.cc        (original)
+++ trunk/Model/Groups/KDTreeLoaderIW.cc        Thu Mar 30 16:57:02 2006
@@ -30,6 +30,7 @@
 #include <Model/Groups/KDTreeLoaderIW.h>
 
 #include <stack>
+#include <iostream>
 #include <stdio.h>
 
 using namespace Manta;
@@ -227,22 +228,45 @@
        kdtree->tris = new VArray<Triangle>(num_tris);
        kdtree->tris->setLen(num_tris);
   kdtree->normals = reinterpret_cast<TriangleNormal*>(new 
char[sizeof(TriangleNormal)*num_tris]);
+#if 0
   kdtree->triToGroupMap = new VArray<int>(num_tris);
   kdtree->triToGroupMap->setLen(num_tris);
+#else
+  kdtree->triToGroupMap = 0;
+#endif
+
+       kdtree->triFlags = new VArray<unsigned char>(num_tris);
+       kdtree->triFlags->setLen(num_tris);
+       kdtree->triFlags->clear();
+
+  BBox bbox;
   
   for(unsigned int ti = 0; ti < num_tris; ++ti) {
     unsigned int a,b,c,group;
     fscanf(geomf, "\t%u %u %u %u\n", &a, &b, &c, &group);
+
+    // Do bounding box extension
+    bbox.extendByPoint(vertices+(a*3));
+    bbox.extendByPoint(vertices+(b*3));
+    bbox.extendByPoint(vertices+(c*3));
+
+    // Add the triangle geometry
     Triangle& t = kdtree->tris->get(ti);
     t.v = Vectorf(vertices+(a*3));
     t.edge1 = Vectorf(vertices+(b*3)) - t.v;
     t.edge2 = Vectorf(vertices+(c*3)) - t.v;
-    kdtree->triToGroupMap->get(ti) = group;
+    //    kdtree->triToGroupMap->get(ti) = group;
+
+    // Set up the vertex normal
+    TriangleNormal& tn= kdtree->normals[ti];
+    tn[0] = Vectorf(vtxnormals+(a*3));
+    tn[1] = Vectorf(vtxnormals+(b*3));
+    tn[2] = Vectorf(vtxnormals+(c*3));
   }
 
   // Here you should parse out the shaders, but we skip this for now
   
-  Color all_color(RGBColor(1,0,0));
+  Color all_color(RGBColor(0.5,0.5,0.5));
   for(unsigned int ti = 0; ti < num_tris; ++ti) {
     Triangle& t = kdtree->tris->get(ti);
     // int group = kdtree->triToGroupMap.get(ti);
@@ -258,6 +282,9 @@
   free(vtxnormals);
   fclose(geomf);
 
+  // Copy the bounding box over.
+  kdtree->bbox = bbox;  
+       cerr << "Model bounds: min: " << kdtree->bbox[0] << " max: " << 
kdtree->bbox[1] << "\n";
 
   ///////////////////////////////////////////////////////
   // Load in the BSP stuff
@@ -276,14 +303,14 @@
   IWNode* top = head;
 
   while((getline(&bline, &bline_len, bspf)) != -1) {
-    //    fprintf (stderr, "line(%u): %s\n", bline_len, bline);
+//     fprintf (stderr, "line(%u): %s\n", bline_len, bline);
     // Check the first token
     char token[2];
     int num = sscanf(bline, "%1s", token);
     //    fprintf(stderr, "num = %d, token = %s\n", num, token);
     if (strcmp(token, "N") == 0) {
       num_nodes ++;
-      //      fprintf(stderr, "Node %u found\n", num_nodes);
+//       fprintf(stderr, "Node %u found\n", num_nodes);
 
       IWNode* node = new IWNode(top);
       // Is this the first node?
@@ -302,7 +329,7 @@
 
       char axis_c;
       sscanf(bline, "%*s %c=%f,d=%*d", &axis_c, &(node->split));
-      //      fprintf(stderr, "axis = %c, split = %g\n", axis_c, 
node->split);
+//       fprintf(stderr, "axis = %c, split = %g\n", axis_c, node->split);
       switch (axis_c) {
       case 'x': node->flags |= 0; break;
       case 'y': node->flags |= 1; break;
@@ -324,7 +351,7 @@
 
       // Grab how many objects we have
       sscanf(bline, "%*s %u", &(node->num_tris));
-      //      fprintf(stderr, "L %u\n", node->num_tris);
+//       fprintf(stderr, "L %u\n", node->num_tris);
       num_tri_indicies += node->num_tris;
       if (node->num_tris) {
         num_nodes++;
@@ -341,6 +368,7 @@
         for(unsigned int i = 0; i < node->num_tris; ++i) {
           sscanf(bline_p, "%d %n", tri_ids_p, &pos);
           //          fprintf(stderr, "pos = %d, %d\n", pos, *tri_ids_p);
+//           fprintf(stderr, "tri (%d) = %d\n", i, *tri_ids_p);
           bline_p += pos;
           ++tri_ids_p;
         }
@@ -370,128 +398,93 @@
   unsigned int current_tri_index = 0;
   int* tri_ids_p = kdtree->triIndices->getArray();
 
-#if 1
+  bool bsp_debug = false;
+  
   // Now traverse the data structure and fill in the kd tree data.
   top = head;
-  top->index = current_tri_index++;
+  top->index = current_node_index++;
   stack<IWNode*> nodestack;
   nodestack.push(top);
   while(!nodestack.empty()) {
     KDTreeNode& node = nodes[top->index];
     if (top->isNode()) {
       // NODE
-      node.internal.flags = top->flags;
+      node.internal.flags = top->flags & KDNODE_AXIS_MASK;
       node.internal.split = top->split;
 
-      node.internal.left = current_tri_index;
-      bool has_left;
+      // This index needs to be relative to the current nodex index.
+      node.internal.left = current_node_index - top->index;
       // Need to leave enough space for the children
       IWNode* child = top->left;
-      if (child && child->isLeaf() && child->num_tris) {
-        has_left = true;
-        child->index = current_tri_index;
-        ++current_tri_index;
-      } else {
-        has_left = false;
+      if (child &&
+          (child->isNode() || (child->isLeaf() && child->num_tris))) {
+        node.internal.flags |= KDNODE_LEFT_CHILD_MASK;
+        child->index = current_node_index++;
       }
 
       // Right
       child = top->right;
-      if (child && child->isLeaf() && child->num_tris) {
-        if (!has_left) {
-          // we need to fix the index of the left
-          node.internal.left--;
-        }
-        child->index = current_tri_index;
-        ++current_tri_index;
-      } else {
-        has_left = false;
+      if (child &&
+          (child->isNode() || (child->isLeaf() && child->num_tris))) {
+        // I used to think that the right child was always left+1, but
+        // this isn't true.  The right child is left+1 which the left
+        // child is there, otherwise it is just left.
+        child->index = current_node_index++;
+        node.internal.flags |= KDNODE_RIGHT_CHILD_MASK;
       }
 
-      if (top->left) nodestack.push(top->left);
+      if (bsp_debug) {
+        fprintf(stderr, "Node(#%u): split = %g, left = %u, flags = %d\n",
+                top->index, node.internal.split, node.internal.left,
+                node.internal.flags);
+        
+      }
+      
       if (top->right) nodestack.push(top->right);
+      if (top->left) nodestack.push(top->left);
       
     } else {
       // LEAF
 
       if(top->num_tris > 0) {
-      
+
+        if (bsp_debug)
+          fprintf(stderr, "Leaf(#%u): num_tris = %d (",
+                  top->index, top->num_tris);
         // Copy the indicies
         for(unsigned int i = 0; i < top->num_tris; ++i) {
           *tri_ids_p = top->tri_ids[i];
+          if (bsp_debug) fprintf(stderr, "%d, ", *tri_ids_p);
           ++tri_ids_p;
         }
 
         node.leaf.flags = 0;
         node.leaf.listBegin = current_tri_index;
         node.leaf.listLen = top->num_tris;
+        if (bsp_debug)
+          fprintf(stderr, ")\n\t\tlistBegin = %u, listLen = %u\n",
+                  node.leaf.listBegin, node.leaf.listLen);
         current_tri_index += top->num_tris;
-
-        ++current_node_index;
       }
     }
     top = nodestack.top();
     nodestack.pop();
   }
-#else
-  int depth = 0;
-  
-  while((getline(&bline, &bline_len, bspf)) != -1) {
-    // Check the first token
-    char token;
-    sscanf(bline, "% c", &token);
-    if (token == 'N') {
-      char axis_c;
-      float pos;
-      sscanf(bline, "% c=%f,d=%*", &axis_c, &pos);
-      int axis;
-      switch (axis_c) {
-      case 'x': axis = 0; break;
-      case 'y': axis = 1; break;
-      case 'z': axis = 2; break;
-      }
-      KDTreeNode* node = nodes[current_node_index];
-      node->internal.flags = axis;
-      node->internal.split = pos;
 
-      
-      // OK, so we've hit an internal node.  If this is a left child
-      // we need to leave space for it and it's right node.
-    } else if (token == 'L') {
-      // Grap how many objects we have
-      unsigned int num_tris;
-      sscanf(bspf, "%u", &num_tris);
-      if (num_tris) {
-        // If you ever change what type triIndices is you will need to
-        // update this bit of code.
-        for(unsigned int i = 0; i < num_tris; ++i) {
-          sscanf(bspf, "%d", tri_ids_p);
-          ++tri_ids_p;
-        }
-        KDTreeNode node;
-        node.leaf.flags = 0;
-        node.leaf.listBegin = current_tri_index;
-        node.leaf.listLen = num_tris;
-        current_tri_index += num_tris;
-      } else {
-        // empty leaf
-      }
-    } else {
-      fprintf(stderr, "Unrecongnized token (%c)\n", token);
-      // Eat to the end of the line.  In the unlikely event that you
-      // don't eat the whole line, you will loop back and grab the
-      // next character.  You may get really unlucky, you get an 'N'
-      // or 'L' where you left off on this line, otherwise you just
-      // eat more of the bad line.  I can't check everything....
-      char buf[1000];
-      fgets(buf, 1000, bspf);
-    }
-  }
-#endif
+  fprintf(stderr, "num_tri_indicies = %u\n", num_tri_indicies);
+  fprintf(stderr, "current_tri_index = %u\n", current_tri_index);
+  fprintf(stderr, "num_nodes = %u, current_node_index = %u\n",
+          num_nodes, current_node_index);
+
+  delete head;
 
+       // Specify the root node loaded from the file.
+       kdtree->rootNode = nodes;
+  
   if (bline) free(bline);
   fclose(bspf);
 
+  
   return 1;
 } // int loadIW()
 




  • [MANTA] r994 - trunk/Model/Groups, bigler, 03/30/2006

Archive powered by MHonArc 2.6.16.

Top of page