Manta Interactive Ray Tracer Development Mailing List

Text archives Help


[MANTA] r911 - in trunk: Model/Groups StandAlone scenes


Chronological Thread 
  • From: abe@sci.utah.edu
  • To: manta@sci.utah.edu
  • Subject: [MANTA] r911 - in trunk: Model/Groups StandAlone scenes
  • Date: Wed, 8 Feb 2006 18:28:59 -0700 (MST)

Author: abe
Date: Wed Feb  8 18:28:58 2006
New Revision: 911

Modified:
   trunk/Model/Groups/KDTree.cc
   trunk/Model/Groups/KDTree.h
   trunk/Model/Groups/KDTreeLoader.cc
   trunk/Model/Groups/SSEKDTree.h
   trunk/Model/Groups/TransparentKDTree.cc
   trunk/Model/Groups/TransparentKDTree.h
   trunk/Model/Groups/VerticalKDTree.h
   trunk/StandAlone/v3c1_tools.cc
   trunk/scenes/boeing777.cc
Log:

Added the ability to specify texture coordinates for a KDTree using a 
.v3c1.tex2 file.
M    scenes/boeing777.cc
M    Model/Groups/SSEKDTree.h
M    Model/Groups/TransparentKDTree.h
M    Model/Groups/KDTreeLoader.cc
M    Model/Groups/KDTree.cc
M    Model/Groups/TransparentKDTree.cc
M    Model/Groups/VerticalKDTree.h
M    Model/Groups/KDTree.h

Extract per vertex texture coordinates with -texcoords commnd line option.
M    StandAlone/v3c1_tools.cc


Modified: trunk/Model/Groups/KDTree.cc
==============================================================================
--- trunk/Model/Groups/KDTree.cc        (original)
+++ trunk/Model/Groups/KDTree.cc        Wed Feb  8 18:28:58 2006
@@ -324,7 +324,9 @@
         
///////////////////////////////////////////////////////////////////////
         
///////////////////////////////////////////////////////////////////////
         // Check against the hit record.
-        if (rays.hit(which, isectData.rayHit.t, getMaterial(), this, 0 )) {
+        static const KDTreeTexCoordMapper texcoord_mapper;
+        
+        if (rays.hit(which, isectData.rayHit.t, getMaterial(), this, 
&texcoord_mapper )) {
 
           // Interpolate between the three vertices.
           Vectorf &n0 = normals[isectData.rayHitTriIndex][0];
@@ -338,6 +340,7 @@
           scratch_pad.normal = (n1 * u) + (n2 * v) + (n0 * (1 - u - v));
           scratch_pad.a = u;
           scratch_pad.b = v;
+          scratch_pad.hit_index = isectData.rayHitTriIndex;
 
 #if SHOW_DUPLICATES
           if (isectData.duplicate == 0)
@@ -618,6 +621,39 @@
   }
 }
 
+///////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////
+// KDTREE TEXCOORD MAPPER
+///////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////
+
+void KDTreeTexCoordMapper::computeTexCoords2(const RenderContext& context, 
RayPacket& rays) const {
+
+  // Obtain a pointer to the kdtree.
+  const KDTreeDebug *kdtree = dynamic_cast<const KDTreeDebug *>( 
rays.getHitPrimitive( rays.begin() ) );
 
+  // Interpolate texture coordinates.
+  for(int i=rays.begin();i<rays.end();i++){
+
+    // Get the texture coords and barycentric coordinates.
+    KDTree::ScratchPadInfo &info = 
rays.scratchpad<KDTree::ScratchPadInfo>(i);
+    TextureCoord &t = kdtree->getTextureCoord( info.hit_index );
+    float u = info.a;
+    float v = info.b;
+
+    VectorT<float,2> texcoord = (t[1] * u) + (t[2] * v) + (t[0] * (1 - u - 
v));
+    
+    rays.setTexCoords(i, Point( texcoord[0], texcoord[1], 0 ) );
+  }
+  
+  rays.setFlag(RayPacket::HaveTexture2);
+}
+
+void KDTreeTexCoordMapper::computeTexCoords3(const RenderContext& context, 
RayPacket& rays) const {
+
+  // Currently only support 2D textures.
+  computeTexCoords2( context, rays );
+  rays.setFlag(RayPacket::HaveTexture3);
+}
 
 

Modified: trunk/Model/Groups/KDTree.h
==============================================================================
--- trunk/Model/Groups/KDTree.h (original)
+++ trunk/Model/Groups/KDTree.h Wed Feb  8 18:28:58 2006
@@ -109,6 +109,17 @@
       Vectorf &operator[] ( int i ) { return normal[i]; };
       const Vectorf &operator[] ( int i ) const { return normal[i]; };
     };
+
+    
///////////////////////////////////////////////////////////////////////////
+    // TEXTURE COORD  TEXTURE COORD  TEXTURE COORD  TEXTURE COORD  TEXTURE 
COOR
+    
///////////////////////////////////////////////////////////////////////////
+    class TextureCoord {
+    private:
+      VectorT<float,2> coord[3];
+    public:
+      VectorT<float,2> &operator[] ( int i ) { return coord[i]; };
+      const VectorT<float,2> &operator[] ( int i ) const { return coord[i]; 
};
+    };    
     
     
///////////////////////////////////////////////////////////////////////////
     // PACKED TRIANGLES  PACKED TRIANGLES  PACKED TRIANGLES  PACKED TRIANGLES
@@ -281,10 +292,14 @@
     
///////////////////////////////////////////////////////////////////////////
     class KDTreeDebug {
     public:
+      
+      virtual void        computeBounds( BBox &bounds ) const   = 0;
+
+      // Accessors.
       virtual void        setRootNode( KDTreeNode *node ) = 0;
       virtual KDTreeNode *getRootNode()                   = 0;
-      virtual void        computeBounds( BBox &bounds ) const   = 0;
       virtual Triangle   &getFace( int index ) const = 0;
+      virtual TextureCoord &getTextureCoord( int index ) const = 0;
     };
     
     
///////////////////////////////////////////////////////////////////////////
@@ -311,6 +326,7 @@
       VArray<int>      *triIndices;
       VArray<Triangle> *tris;
       TriangleNormal   *normals;
+      TextureCoord     *texcoords;
 
                        VArray<int> *triToGroupMap;
                        VArray<int> *groupToNameMap;
@@ -378,10 +394,10 @@
 
       // This structure is used to record info about the hit.
       struct ScratchPadInfo {
-        Vector normal;          // Normal of the intersected face.
-        Color payload;          // Payload of the intersected face.
-        unsigned int hit_index; // Triangle index for defered normal 
interpolation.
-        Real a, b;              // Barycentric coordinates of the hit.
+        Vector normal;              // Normal of the intersected face.
+        Color payload;              // Payload of the intersected face.
+        unsigned int hit_index;     // Triangle index for defered normal 
interpolation.
+        Real a, b;                  // Barycentric coordinates of the hit.
       };
 
       virtual ~KDTree() {}
@@ -404,6 +420,7 @@
       virtual void        setRootNode( KDTreeNode *node ) { rootNode = node; 
};
       virtual KDTreeNode *getRootNode()                   { return rootNode; 
};
       virtual Triangle   &getFace( int index ) const            { return 
(*tris)[index]; };
+      virtual TextureCoord &getTextureCoord( int index ) const { return 
(texcoords)[index]; };
       
     };
 
@@ -419,6 +436,20 @@
                              Color results[]) const;
     };
 
+    
///////////////////////////////////////////////////////////////////////////
+    
///////////////////////////////////////////////////////////////////////////
+    // KDTREE TEXCOORD MAPPER  
+    
///////////////////////////////////////////////////////////////////////////
+    
///////////////////////////////////////////////////////////////////////////   
 
+    class KDTreeTexCoordMapper : public TexCoordMapper {
+    public:
+      KDTreeTexCoordMapper() { };
+      virtual ~KDTreeTexCoordMapper() { };
+      
+      virtual void computeTexCoords2(const RenderContext& context, 
RayPacket& rays) const;
+      virtual void computeTexCoords3(const RenderContext& context, 
RayPacket& rays) const;
+    };
+    
   } // end namespace Kdtree
 
 } // end namespace Manta

Modified: trunk/Model/Groups/KDTreeLoader.cc
==============================================================================
--- trunk/Model/Groups/KDTreeLoader.cc  (original)
+++ trunk/Model/Groups/KDTreeLoader.cc  Wed Feb  8 18:28:58 2006
@@ -49,6 +49,8 @@
 #include <SCIRun/Core/Thread/Runnable.h>
 #include <SCIRun/Core/Thread/Barrier.h>
 
+#include <SCIRun/Core/Exceptions/InternalError.h>
+
 using namespace Manta;
 using namespace Manta::Kdtree;
 using namespace SCIRun;
@@ -87,8 +89,12 @@
        // Target data.
        float *rawVertColData;
        float *rawNormData;
+  float *rawTexData;
+  
        VArray<Triangle> **tris;
-       TriangleNormal **perVertNormals;
+       TriangleNormal   **perVertNormals;
+  TextureCoord     **perVertTexCoords;
+  
        
        // Region of data this thread is responsible for.
        long begin_offset;
@@ -96,14 +102,36 @@
        
        
/////////////////////////////////////////////////////////////////////////////
        // Constructor.
-       V3C1Worker( Barrier &v3c1_barrier_, int np_, float *rawVertColData_, 
float *rawNormData_, VArray<Triangle> **tris_, TriangleNormal 
**perVertNormals_,
-                   long begin_offset_, long end_offset_, BBox &bounds_ ) :
+       V3C1Worker( Barrier &v3c1_barrier_,
+              int np_,
+
+              // Float arrays--which will contain raw data after the second
+              // barrier.
+              float *rawVertColData_,
+              float *rawNormData_,
+              float *rawTexData_,
+
+              // Processed (endian flipped) data.
+              VArray<Triangle> **tris_,
+              TriangleNormal **perVertNormals_,
+              TextureCoord   **perVertTexCoords_,
+
+                   long begin_offset_,
+              long end_offset_, BBox
+              &bounds_ ) :
+    
                Runnable( true ), 
                bounds( bounds_ ),
                v3c1_barrier( v3c1_barrier_ ),
                np( np_ ),
-               rawVertColData( rawVertColData_ ), rawNormData( rawNormData_ 
),
-    tris( tris_ ), perVertNormals( perVertNormals_ ),
+               rawVertColData( rawVertColData_ ),
+    rawNormData( rawNormData_ ),
+    rawTexData ( rawTexData_ ),
+
+    tris( tris_ ),
+    perVertNormals( perVertNormals_ ),
+    perVertTexCoords( perVertTexCoords_ ),
+
                begin_offset( begin_offset_ ), end_offset( end_offset_ ) {  }
        
        
/////////////////////////////////////////////////////////////////////////////
@@ -115,6 +143,7 @@
                
                        (**tris)[i][0][0] = 0.0;
                        (*perVertNormals)[i][0][0] = 0.0;
+      (*perVertTexCoords)[i][0][0] = 0.0;
                }
                
                // Wait for other threads to initialize.
@@ -127,7 +156,8 @@
                        
                        // Offset into the data.
                        float *_rawVertColData = rawVertColData + 12 * i;
-                       float *_rawNormData = rawNormData + 9 * i;
+                       float *_rawNormData    = rawNormData    + 9  * i;
+      float *_rawTexData     = rawTexData     + 6  * i; 
                        
                        // Check to see if we are on a big endian system
                        // Data was generate on Altix (little endian)
@@ -154,9 +184,17 @@
                                        _rawVertColData += 3;
 
                                        if(rawNormData) {
-                                               (*perVertNormals)[i][j] = 
Vectorf( _rawNormData[0], _rawNormData[1], _rawNormData[2] );
+                                               (*perVertNormals)[i][j] = 
Vectorf( _rawNormData[0],
+                                               _rawNormData[1],
+                                               _rawNormData[2] );
                                                _rawNormData += 3;
                                        }
+
+          if (rawTexData) {
+            (*perVertTexCoords)[i][j] = VectorT<float,2>( _rawTexData[0],
+                                                          _rawTexData[1] );
+            _rawTexData += 2;
+          }
                                }
                        }
                        
@@ -173,9 +211,6 @@
                                r = _rawVertColData[0];
                                g = _rawVertColData[1];
                                b = _rawVertColData[2];
-                               // r = int(_rawData[0] * 255 + .5f);
-                               // g = int(_rawData[1] * 255 + .5f);
-                               // b = int(_rawData[2] * 255 + .5f);
                                _rawVertColData += 3;                   
                        }
                        
@@ -221,7 +256,8 @@
 
 // Load a binary v3c1 file containing the triangles.
 static int LoadBin_V3C1(const char *filename, VArray<Triangle> **tris,
-                        /*Vectorf **perVertNormals,*/ TriangleNormal 
**perVertNormals,
+                        TriangleNormal **perVertNormals,
+                        TextureCoord   **perVertTexCoords,
                         BBox &bounds, int np ) {
        
        FILE *f;
@@ -245,49 +281,97 @@
   if (rawVertColData == 0) {
     std::cerr << "Could not allocate " << (fileSize/4) << " bytes." << 
std::endl;
   }
-  
+
+       
/////////////////////////////////////////////////////////////////////////////
+  
///////////////////////////////////////////////////////////////////////////// 
 
        // Allocate triangles.
        *tris = new VArray<Triangle> (nTris);
        (*tris)->setLen(nTris);
-       
-       char fn[512];
-       strcpy(fn, filename);
-       strcat(fn, ".nor"); // the kd-tree file
 
-       FILE *f2;
+       
/////////////////////////////////////////////////////////////////////////////
+  
/////////////////////////////////////////////////////////////////////////////
+  // Attempt to load triangles
+  char normal_file_name[512];
+       strcpy(normal_file_name, filename);
+       strcat(normal_file_name, ".nor"); // the kd-tree file
+
+       FILE *normal_file;
        float *rawNormData = 0;
-       long fileSize2;
-       if ((f2=fopen(fn, "r")) == NULL) {
-               fprintf(stderr, "Cannot open file: %s\n", fn);
+       size_t normal_file_size;
+       if ((normal_file=fopen(normal_file_name, "r")) == NULL) {
+               fprintf(stderr, "Cannot open file: %s\n", normal_file_name);
                fprintf(stderr, "Using per triangle normals\n");
        } else {
        
                // Determine the input file size.
-               fseek(f2, 0, SEEK_END);
-               fileSize2 = ftell(f2);
-               fseek(f2, 0, SEEK_SET);
+               fseek(normal_file, 0, SEEK_END);
+               normal_file_size = ftell(normal_file);
+               fseek(normal_file, 0, SEEK_SET);
        
                
/////////////////////////////////////////////////////////////////////////////
                // Determine the amount of memory needed.
-       if( (fileSize2 / (9*sizeof(float))) != nTris) {
-                       fprintf(stderr, "normal file size doesn't match v3c1 
file size\n");
-                       return 0;
+       if( (normal_file_size / (9*sizeof(float))) != nTris) {
+                       throw InternalError( "normal file size doesn't match 
v3c1 file size\n",
+                           __FILE__, __LINE__ );
                }
 
                // Allocate input buffer.
-               rawNormData = new float [fileSize2 / 4];
+               rawNormData = new float [normal_file_size / 4];
 
        if (rawNormData == 0) {
-       std::cerr << "Could not allocate " << (fileSize2/4) << " bytes." << 
std::endl;
+       std::cerr << "Could not allocate " << (normal_file_size) << " bytes." 
<< std::endl;
        }
        }
+
+  
/////////////////////////////////////////////////////////////////////////////
+  
/////////////////////////////////////////////////////////////////////////////
+  // Load texture coordinates.
+  char tex_file_name[512];
+       strcpy(tex_file_name, filename);
+       strcat(tex_file_name, ".tex2");
+
+       FILE *tex_file;
+       float *rawTexData = 0;
+  size_t tex_file_size;
+       if ((tex_file=fopen(tex_file_name, "r")) == NULL) {
+               fprintf(stderr, "Cannot open file: %s\n", tex_file_name);
+    fprintf(stderr, "Not using texture coordinates.\n" );
+
+       } else {
+       
+               // Determine the input file size.
+               fseek(tex_file, 0, SEEK_END);
+               tex_file_size = ftell(tex_file);
+               fseek(tex_file, 0, SEEK_SET);
+       
+               
/////////////////////////////////////////////////////////////////////////////
+               // Determine the amount of memory needed.
+       if( (tex_file_size / (6*sizeof(float))) != nTris) {
+                       throw InternalError( "texture coord file size doesn't 
match v3c1 file size\n",
+                           __FILE__, __LINE__ );
+               }
+
+               // Allocate input buffer.
+               rawTexData = new float [tex_file_size / 4];
+
+       if (rawTexData == 0) {
+       std::cerr << "Could not allocate " << (tex_file_size) << " bytes." << 
std::endl;
+       }
+       }  
+  
+  
+  
/////////////////////////////////////////////////////////////////////////////
+  
/////////////////////////////////////////////////////////////////////////////
        // Allocate normals.
        *perVertNormals = new TriangleNormal[nTris];
-       
+
+  // Allocate texture coords.
+  *perVertTexCoords = new TextureCoord[nTris*3];
+  
        
/////////////////////////////////////////////////////////////////////////////
        // Start up the worker threads and touch the memory to distribute it.
        
-       std::cerr << "Creating " << np << " workers." << std::endl;
+       // std::cerr << "Creating " << np << " workers." << std::endl;
        
        // Create worker threads to preprocess and bound the data.
        Thread **workers = new Thread *[ np ];
@@ -299,7 +383,7 @@
        long begin = 0;
        long end   = work_per_thread;
        
-       std::cerr << "Faces per worker: " << work_per_thread << std::endl;
+       // std::cerr << "Faces per worker: " << work_per_thread << std::endl;
        
        char worker_name[32];
        
@@ -307,7 +391,18 @@
        for (int i=0;i<np-1;++i) {
                
                sprintf( worker_name, "V3C1 Worker %d", i );
-               workers[i] = new Thread( new V3C1Worker( v3c1_barrier, np, 
rawVertColData, rawNormData, tris, perVertNormals, begin, end, 
worker_box[i].box ), worker_name );   
+               workers[i] = new Thread( new V3C1Worker( v3c1_barrier,
+                                             np,
+                                             rawVertColData,
+                                             rawNormData,
+                                             rawTexData,
+                                             tris,
+                                             perVertNormals,
+                                             perVertTexCoords,
+                                             begin,
+                                             end,
+                                             worker_box[i].box ),
+                             worker_name );    
                
                begin = end;
                end += work_per_thread;
@@ -315,14 +410,28 @@
        
        // Assign the rest of the work to the last worker.
        sprintf( worker_name, "V3C1 Worker %d", np-1 );
-       workers[np-1] = new Thread( new V3C1Worker( v3c1_barrier, np, 
rawVertColData, rawNormData, tris, perVertNormals, begin, nTris, 
worker_box[np-1].box ), "V3C1 Worker" );
+       workers[np-1] = new Thread( new V3C1Worker( v3c1_barrier,
+                                              np,
+                                              rawVertColData,
+                                              rawNormData,
+                                              rawTexData,
+                                              tris,
+                                              perVertNormals,
+                                              perVertTexCoords,
+                                              begin,
+                                              nTris,
+                                              worker_box[np-1].box ),
+                              "V3C1 Worker" );
        
        // Wait for all of workers to initialize.
-       std::cerr << "Waiting for workers to initalize." << std::endl;
+       // std::cerr << "Waiting for workers to initalize." << std::endl;
        v3c1_barrier.wait( np+1 );
        
        
/////////////////////////////////////////////////////////////////////////////
+  
/////////////////////////////////////////////////////////////////////////////
        // Read in the data.
+  
/////////////////////////////////////////////////////////////////////////////
+  
/////////////////////////////////////////////////////////////////////////////
        double time_begin = Time::currentSeconds();
        
        long trisRead = fread_64(rawVertColData, 48, fileSize/48, f);
@@ -331,11 +440,18 @@
        }
        
        if(rawNormData) {
-               trisRead = fread_64(rawNormData, 36, fileSize2/36, f2);
+               trisRead = fread_64(rawNormData, 36, normal_file_size/36, 
normal_file );
                if (trisRead != nTris) {
-                       fprintf(stderr, "Error reading file: %s Tris read: 
%ld; expected: %ld\n", fn, trisRead, nTris);
+                       fprintf(stderr, "Error reading file: %s Tris read: 
%ld; expected: %ld\n", normal_file_name, trisRead, nTris);
                }
        }
+
+  if (rawTexData) {
+               trisRead = fread_64(rawTexData, 24, tex_file_size/24, 
tex_file );
+               if (trisRead != nTris) {
+                       fprintf(stderr, "Error reading file: %s Tris read: 
%ld; expected: %ld\n", tex_file_name, trisRead, nTris);
+               }    
+  }
        
        double time_end = Time::currentSeconds();
        
@@ -353,14 +469,14 @@
        // Record the time for the actual preprocess.
        time_begin = Time::currentSeconds();
        
-       std::cerr << "Beginning preprocess" << std::endl;
+       std::cerr << "Please wait..." << std::endl;
        v3c1_barrier.wait( np+1 );
        
        // Wait for all of the threads to finish.
        for (int i=0;i<np;++i) {
                workers[i]->join();
        
-               std::cerr << "Worker " << i << " finished." << std::endl;
+               // std::cerr << "Worker " << i << " finished." << std::endl;
                                
                // Enlarge the bounding box.
                bounds.extendByBox( worker_box[i].box );
@@ -372,18 +488,19 @@
        
        time_end = Time::currentSeconds();
        
-       std::cerr << "Time to parse triangles: " << (time_end - time_begin) 
<< " Seconds." << std::endl;
-       std::cerr << "Triangles loaded: " << nTris << std::endl;
+       // std::cerr << "Time to parse triangles: " << (time_end - 
time_begin) << " Seconds." << std::endl;
+       // std::cerr << "Triangles loaded: " << nTris << std::endl;
        
        time_begin = Time::currentSeconds();
        
        // Delete the intermediate raw data.
        delete [] rawVertColData;
        delete [] rawNormData;
+  delete [] rawTexData;
        
        memory_time += (Time::currentSeconds() - time_begin);
        
-       std::cerr << "Total time for memory allocation/deallocation: " << 
memory_time << " Seconds." << std::endl;
+       // std::cerr << "Total time for memory allocation/deallocation: " << 
memory_time << " Seconds." << std::endl;
        
        return 1;
 }
@@ -421,7 +538,12 @@
        
/////////////////////////////////////////////////////////////////////////////
        // Call a helper function to load the triangles.
        if (strstr(fn, ".v3c1")) {
-               LoadBin_V3C1(fn, &kdtree->tris, &kdtree->normals, 
kdtree->bbox, np );
+               LoadBin_V3C1(fn,
+                 &kdtree->tris,
+                 &kdtree->normals,
+                 &kdtree->texcoords,
+                 kdtree->bbox,
+                 np );
        } else {
                fprintf(stderr, "Unrecognized file type.\n");
                return 0;
@@ -429,7 +551,7 @@
        
        // Stop timing triangle load.
        double time_end = Time::currentSeconds();
-       std::cerr << "Total time for loading triangles: " << (time_end - 
time_begin) << std::endl << std::endl;
+       // std::cerr << "Total time for loading triangles: " << (time_end - 
time_begin) << std::endl << std::endl;
        
        
/////////////////////////////////////////////////////////////////////////////
        // Load the kd tree.
@@ -444,14 +566,14 @@
        }
        
        fseek(f, 0, SEEK_END);
-       long long fileSize = ftell(f);
+       size_t fileSize = ftell(f);
        void *buffer = (void*)malloc(fileSize);
        fseek(f, 0, SEEK_SET);
        
        time_begin = Time::currentSeconds();
        
        // Read in the whole tree.
-       long long nread = fread_64(buffer, 1, fileSize, f);
+       size_t nread = fread_64(buffer, 1, fileSize, f);
        if (nread != fileSize) {
                fprintf(stderr, "error reading file %s (size: %lld read: 
%lld)\n",
                                                filename, fileSize, nread);
@@ -506,7 +628,7 @@
        fileSize = ftell(f);
        fseek(f, 0, SEEK_SET);
        
-       long long nInt = fileSize/sizeof(int);
+       size_t nInt = fileSize/sizeof(int);
        VArray<int> *indices = new VArray<int>(nInt);
        indices->setLen(nInt);
        
@@ -540,14 +662,14 @@
                // there is a igg file, read it
                while (! feof(f)) {
                        char buf[128];
-                       long long iggbegin, iggend;
+                       size_t iggbegin, iggend;
                        fgets(buf, 128, f);
                        int n = sscanf(buf, "%lld%lld", &iggbegin, &iggend);
                        if (n!=2) {
                                fprintf(stderr, "error processing file %s\n", 
filename);
                                break;
                        }
-                       for (long long i=iggbegin; i<=iggend; i++)
+                       for (size_t i=iggbegin; i<=iggend; i++)
                                memset(&kdtree->tris->get(i), 0, 
sizeof(Triangle)); 
                }
                fclose(f);
@@ -567,7 +689,7 @@
                return 1;
        }
        fprintf(stderr, "end loading grp\n");
-       long long nTris = kdtree->tris->getLen(); 
+       size_t nTris = kdtree->tris->getLen(); 
        kdtree->triToGroupMap = new VArray<int>(nTris);
        kdtree->triToGroupMap->setLen(nTris);
        nread = fread_64(kdtree->triToGroupMap->getArray(), sizeof(int), 
nTris, f);

Modified: trunk/Model/Groups/SSEKDTree.h
==============================================================================
--- trunk/Model/Groups/SSEKDTree.h      (original)
+++ trunk/Model/Groups/SSEKDTree.h      Wed Feb  8 18:28:58 2006
@@ -81,6 +81,7 @@
       virtual void        setRootNode( KDTreeNode *node ) { rootNode = node; 
};
       virtual KDTreeNode *getRootNode()                   { return rootNode; 
};      
       virtual Triangle   &getFace( int index ) const      { return 
(*tris)[index]; };
+      virtual TextureCoord &getTextureCoord( int index ) const { return 
(texcoords)[index]; };
       
     private:
       BBox bbox;
@@ -89,6 +90,7 @@
       VArray<int>      *triIndices;
       VArray<Triangle> *tris;
       TriangleNormal   *normals;
+      TextureCoord     *texcoords;
 
                        VArray<int> *triToGroupMap;
                        VArray<int> *groupToNameMap;

Modified: trunk/Model/Groups/TransparentKDTree.cc
==============================================================================
--- trunk/Model/Groups/TransparentKDTree.cc     (original)
+++ trunk/Model/Groups/TransparentKDTree.cc     Wed Feb  8 18:28:58 2006
@@ -254,6 +254,8 @@
     scratch_pad.payload = ray_color;
     scratch_pad.alpha = ray_alpha;
 
+    // scratch_pad.hit_index = array[isectbuf->getLen()-1].triIdx;
+
     return 1;
   }
   else {

Modified: trunk/Model/Groups/TransparentKDTree.h
==============================================================================
--- trunk/Model/Groups/TransparentKDTree.h      (original)
+++ trunk/Model/Groups/TransparentKDTree.h      Wed Feb  8 18:28:58 2006
@@ -60,6 +60,7 @@
       VArray<int>      *triIndices;
       VArray<Triangle> *tris;
       TriangleNormal          *normals;
+      TextureCoord     *texcoords;
 
       VArray<int> *triToGroupMap;
       VArray<int> *groupToNameMap;
@@ -161,6 +162,7 @@
       virtual void        setRootNode( KDTreeNode *node ) { rootNode = node; 
};
       virtual KDTreeNode *getRootNode()                   { return rootNode; 
};
       virtual Triangle   &getFace( int index ) const      { return 
(*tris)[index]; };
+      virtual TextureCoord &getTextureCoord( int index ) const { return 
(texcoords)[index]; };
     };
   }
 

Modified: trunk/Model/Groups/VerticalKDTree.h
==============================================================================
--- trunk/Model/Groups/VerticalKDTree.h (original)
+++ trunk/Model/Groups/VerticalKDTree.h Wed Feb  8 18:28:58 2006
@@ -209,6 +209,7 @@
       virtual void        setRootNode( KDTreeNode *node ) { rootNode = node; 
};
       virtual KDTreeNode *getRootNode()                   { return rootNode; 
};      
       virtual Triangle   &getFace( int index ) const      { return 
(*tris)[index]; };
+      virtual TextureCoord &getTextureCoord( int index ) const { return 
(texcoords)[index]; };
       
     private:
       BBox bbox;
@@ -217,6 +218,7 @@
       VArray<int>      *triIndices;
       VArray<Triangle> *tris;
       TriangleNormal   *normals;
+      TextureCoord     *texcoords;
 
                        VArray<int> *triToGroupMap;
                        VArray<int> *groupToNameMap;

Modified: trunk/StandAlone/v3c1_tools.cc
==============================================================================
--- trunk/StandAlone/v3c1_tools.cc      (original)
+++ trunk/StandAlone/v3c1_tools.cc      Wed Feb  8 18:28:58 2006
@@ -43,15 +43,22 @@
 void v3c1_clip( const char *file_name, const Point &plane_point, const 
Vector &plane_normal );
 void v3c1_scale( const char *file_name );
 
-FILE *out_file = 0;
-FILE *normal_out_file = 0;
+FILE *out_file          = 0;
+FILE *normal_out_file   = 0;
+FILE *texcoord_out_file = 0;
+
 int total_faces = 0;
 bool use_normals = false;
+bool use_texcoords = false;
+bool flip_face_winding = false;
 
-// Output file name.
-char *output_file_name = 0;
+bool  compute_vertex_normals = false;
+float vertex_normals_angle = 90.0f;
 
+bool loud = false;
 
+// Output file name.
+char *output_file_name = 0;
 
 struct nv3_face {
        float normal0[3];
@@ -75,8 +82,6 @@
   }
 };
 
-
-
 struct v3c1_face {
        float vertex0[3];
        float vertex1[3];
@@ -113,8 +118,8 @@
   Vectorf normal[3];
 };
 
-vector<nv3_face>  in_buffer;
-vector<v3c1_face> out_buffer;
+vector<nv3_face>         in_buffer;
+vector<v3c1_face>        out_buffer;
 vector<vertex_normals>   normal_buffer;
 vector<int>              group_buffer;
 
@@ -184,9 +189,20 @@
                else if (arg == "-obj") {
                        command = CONVERT_OBJ;
                }
+    else if (arg == "-flip") {
+      flip_face_winding = true;
+    }
     else if (arg == "-normals") {
       use_normals = true;
     }
+    else if (arg == "-vertex_normals") {
+      use_normals = true;
+      compute_vertex_normals = true;
+      vertex_normals_angle = atof(argv[++i]);
+    }    
+    else if (arg == "-texcoords") {
+      use_texcoords = true;
+    }
     else if (arg == "-m") {
       command = CONVERT_M;
     }
@@ -207,6 +223,9 @@
                else if (arg == "-out") {
                        output_file_name = argv[++i];
                }
+    else if (arg == "-loud") {
+      loud = true;
+               }
                else if (arg == "-in") {
                        file_list_begin = ++i;
                        file_list_end   = file_list_begin+1;
@@ -242,18 +261,25 @@
        if ((command == NONE) || (file_list_begin == 0)) {
                printf( "Usage: v3c1_tools -vn3 -in <tanker paths...> -out 
<outfile.v3c1>\n"
                                                "       v3c1_tools -obj -in 
<file1.obj ...>  [-out <outfile.v3c1>]\n"
-            "          Related options:\n"
-            "          -scale <x> <y> <z> -- Scale the model before 
writting\n"
-            "          -normals           -- Output a vertex normal file.\n"
-            "          Outfile optional uses group names otherwise.\n"
+            "          Related options: (might not work with other 
conversions)\n"
+            "          -scale <x> <y> <z>  -- Scale the model before 
writting\n"
+            "          -normals            -- Output a vertex normal file.\n"
+            "          -flip               -- Use glm to reverse winding 
direction.\n"
+            "          -vertex_normals <d> -- Use glm to compute vertex 
normals using\n"
+            "                                 the specified angle threshold 
(in degrees)\n"
+            "          -texcoords          -- Output a vertex texture 
file.\n"
+            "          -loud               -- Print per group info.\n"
             "       v3c1_tools -m   -in <file1.m ...>  -color <r> <g> <b> 
[-out <outfile.v3c1>]\n"
-            "          Hughes Hoppe .m file format conversion.\n"
+            "          Hughes Hoppe .m file format conversion.\n\n"
                        "       v3c1_tools -info <infile.v3c1 ...>\n"
             "          Computes bounds and number of triangles, colors.\n"
                                                "       v3c1_tools -clip <px> 
<py> <pz> <nx> <ny> <nz>\n"
             "          Apply a clipping plane to the input files given a 
point and normal.\n"
             "       v3c1_tools -scale <x> <y> <z>\n"
-            "          Scale the input v3c1 file and write to the output 
file.\n");
+            "          Scale the input v3c1 file and write to the output 
file.\n"
+            "\n"
+            "          Outfile optional uses group names otherwise.\n"
+);
                return 1;
        }
        
@@ -299,6 +325,16 @@
                     << file_name << std::endl;
         }
       }
+
+      // Determine texcoord file name.
+      if (use_texcoords) {
+        char file_name[128];
+        sprintf( file_name, "%s.tex2", output_file_name );
+        if ((texcoord_out_file = fopen( file_name, "w" )) == 0) {
+          std::cout << "Error could not open texcoord output file: "
+                    << file_name << std::endl;
+        }
+      }
                }
                
                for (int i=file_list_begin;i<file_list_end;++i) {
@@ -515,6 +551,12 @@
        total_faces += file_faces;
 }
 
+///////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////
+// OBJ FILE  OBJ FILE  OBJ FILE  OBJ FILE  OBJ FILE  OBJ FILE  OBJ FILE  OBJ 
FI
+///////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////
+
 void process_obj_file( const char *file_name ) {
 
        // Load in the file using glm.
@@ -525,16 +567,36 @@
        }
        
        // Flip the face winding.
-       // glmReverseWinding( model );
+  if (flip_face_winding) {
+    std::cout << "Reversing winding direction.\n";
+    glmReverseWinding( model );
+  }
+
+  // Compute vertex normals.
+  if (compute_vertex_normals) {
+
+    // Check to see if 
+    if (!model->facetnorms) {
+      std::cout << "Computing facet normals\n";
+      glmFacetNormals( model );
+    }
+    
+    std::cout << "Computing vertex normals, threshold="
+              << vertex_normals_angle
+              << "\n";
+    glmVertexNormals( model, vertex_normals_angle );
+  }
        
        FILE *out;
   FILE *normal_out = 0;
+  FILE *texcoord_out = 0;
+  
        int group_num = 0;
 
   // Output structures
   v3c1_face out_face;
-  float normal[3][3] = { {0, 0, 0}, {1, 1, 1}, {2, 2, 2} };
-
+  VectorT<float,3> normal[3];
+  float texcoord[3][2] = { {0, 0}, {1, 1}, {2, 2} };
   
        // Iterate over the groups.
        GLMgroup *group = model->groups;
@@ -561,6 +623,16 @@
                     << normal_file_name << std::endl;
         }
       }
+
+      // Open a texcoord file if necessary.
+      if (use_texcoords) {
+        char texcoord_file_name[128];
+        sprintf( texcoord_file_name, "%s.tex2", file_name );        
+        if ((texcoord_out = fopen( texcoord_file_name, "w" )) == 0) {
+          std::cout << "Error could not open texcoord output file: "
+                    << texcoord_file_name << std::endl;
+        }
+      }
             
                        printf( "Group: %d File: %s ", group_num, file_name );
                }
@@ -568,9 +640,8 @@
                        // Use a single output file.
                        out        = out_file;
       normal_out = normal_out_file;
-                       
-                       printf( "Group: %d\n", group_num );
-               }
+      texcoord_out = texcoord_out_file;
+               }
        
                // Output all of the triangles in the group.
                for (int i=0;i<group->numtriangles;++i) {
@@ -593,13 +664,15 @@
         // Do we need to copy out normals?
         if (normal_out) {
           int normal_index = model->triangles[tri_index].nindices[v];
-          if (normal_index < model->numnormals) {
-            float *f = model->normals+(normal_index*3);
+          if (normal_index <= model->numnormals) {
+            const float *f = model->normals+(normal_index*3);
             
             // Copy out the normal.
             normal[v][0] = f[0];
             normal[v][1] = f[1];
             normal[v][2] = f[2];
+
+            normal[v].normalize();
           }
           else {
             std::cout << "Invalid normal " << normal_index
@@ -607,6 +680,24 @@
                       << std::endl;
           }
         }
+
+        // Copy texcoords?
+        if (texcoord_out) {
+          int texcoord_index = model->triangles[tri_index].tindices[v];
+          if (texcoord_index <= model->numtexcoords) {
+            const float *f = model->texcoords+(texcoord_index*2);
+            
+            // Copy out the texcoord.
+            texcoord[v][0] = f[0];
+            texcoord[v][1] = f[1];
+          }
+          else {
+            std::cout << "Invalid texcoord " << texcoord_index
+                      << " specified for face: " << tri_index
+                      << " number of texcoords: " << model->numtexcoords
+                      << std::endl;
+          }
+        }        
                        }
                        
       
/////////////////////////////////////////////////////////////////////////
@@ -632,6 +723,10 @@
       if (normal_out) {
         fwrite( &normal, sizeof(float)*9, 1, normal_out );
       }
+
+      if (texcoord_out) {
+        fwrite( &texcoord, sizeof(float)*6, 1, texcoord_out );
+      }
                }
 
                // Close the file if we are using one per group.
@@ -641,9 +736,17 @@
       // Check to see if the normal file should be closed.
       if (normal_out)
         fclose( normal_out );
+
+      if (texcoord_out)
+        fclose( texcoord_out );
                }
-               
-               printf( "Faces: %d\n", group->numtriangles );
+
+    if (loud) {
+      printf( "Group %d Faces: %d \"%s\"\n",
+              group_num,
+              group->numtriangles,
+              group->name );
+    }
 
                total_faces += group->numtriangles;
        
@@ -652,6 +755,12 @@
                group = group->next;
        }
 }
+
+///////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////
+// M FILE  M FILE  M FILE  M FILE  M FILE  M FILE  M FILE  M FILE  M FILE  M 
FI
+///////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////
 
 void process_m_file( const char *file_name ) {
   

Modified: trunk/scenes/boeing777.cc
==============================================================================
--- trunk/scenes/boeing777.cc   (original)
+++ trunk/scenes/boeing777.cc   Wed Feb  8 18:28:58 2006
@@ -58,6 +58,7 @@
 #include <Model/Textures/CheckerTexture.h>
 #include <Model/Textures/MarbleTexture.h>
 #include <Model/Textures/NormalTexture.h>
+#include <Model/Textures/TexCoordTexture.h>
 #include <Model/Cameras/PinholeCamera.h>
 #include <Core/Geometry/AffineTransform.h>
 #include <Core/Util/NotFinished.h>
@@ -80,6 +81,7 @@
 enum MtlType          { MTL_PHONG,
                         MTL_AMBIENT,
                         MTL_FLAT,
+                        MTL_TEXCOORD,
                         MTL_NORMAL,
                         MTL_SOLID_WIRE,
                         MTL_WIRE,
@@ -154,6 +156,9 @@
     else if (args[i] == "-flat") {
       material_type = MTL_FLAT;
     }
+    else if (args[i] == "-texcoord") {
+      material_type = MTL_TEXCOORD;
+    }
     else if (args[i] == "-wire") {
       material_type = MTL_WIRE;
     }
@@ -203,8 +208,9 @@
       cerr << "-phong <exp> <reflection> **default**" << endl;
       cerr << "-ambient <max dist> <secondary rays>"  << endl;
       cerr << "-lambertian\n";
-      cerr << "-normal   -- Use normal material.\n";
+      cerr << "-normal    -- Use normal material.\n";
       cerr << "-flat\n";
+      cerr << "-texcoord  -- Display texture coords.\n";
       cerr << "-wire\n    -- wireframe only";
       cerr << "-solidwire -- wireframe on shaded surfaces\n";
       cerr << "-raydirection\n";
@@ -248,6 +254,9 @@
     break;
   case MTL_FLAT:
     kd_material = new Flat( new KDTreeTexture );
+    break;
+  case MTL_TEXCOORD:
+    kd_material = new Flat( new TexCoordTexture );
     break;
   case MTL_LAMBERTIAN:
     kd_material = new Lambertian( new KDTreeTexture );




  • [MANTA] r911 - in trunk: Model/Groups StandAlone scenes, abe, 02/08/2006

Archive powered by MHonArc 2.6.16.

Top of page