Manta Interactive Ray Tracer Development Mailing List

Text archives Help


[MANTA] r1364 - in trunk: Model/Groups SwigInterface


Chronological Thread 
  • From: abe@sci.utah.edu
  • To: manta@sci.utah.edu
  • Subject: [MANTA] r1364 - in trunk: Model/Groups SwigInterface
  • Date: Thu, 3 May 2007 01:21:00 -0600 (MDT)

Author: abe
Date: Thu May  3 01:20:59 2007
New Revision: 1364

Added:
   trunk/Model/Groups/ObjGroup.cc
   trunk/Model/Groups/ObjGroup.h
Modified:
   trunk/Model/Groups/CMakeLists.txt
   trunk/SwigInterface/manta.i
   trunk/SwigInterface/mantainterface.i
Log:


Added Group which takes an .obj file in the constructor and creates a
group containing the mesh (with code extracted from scene
objviewer). This group can then be passed to a DynBVH or whatever:

obj = manta_new( ObjGroup( filename ) )
bvh = manta_new( DynBVH() )
bvh.rebuild( obj )

A    Model/Groups/ObjGroup.cc
A    Model/Groups/ObjGroup.h

M    SwigInterface/manta.i
M    SwigInterface/mantainterface.i
M    Model/Groups/CMakeLists.txt



Modified: trunk/Model/Groups/CMakeLists.txt
==============================================================================
--- trunk/Model/Groups/CMakeLists.txt   (original)
+++ trunk/Model/Groups/CMakeLists.txt   Thu May  3 01:20:59 2007
@@ -41,6 +41,8 @@
      Groups/TriangleIW.cc
      Groups/TriangleIW.h
      Groups/VolumeGrid.h
+     Groups/ObjGroup.h
+     Groups/ObjGroup.cc
 )
 
 # Determine if VerticalKDTree can be included

Added: trunk/Model/Groups/ObjGroup.cc
==============================================================================
--- (empty file)
+++ trunk/Model/Groups/ObjGroup.cc      Thu May  3 01:20:59 2007
@@ -0,0 +1,283 @@
+
+#include <MantaTypes.h>
+#include <Interface/Texture.h>
+#include <Core/Color/Color.h>
+#include <Core/Color/ColorSpace.h>
+#include <Model/Groups/ObjGroup.h>
+#include <Model/Textures/Constant.h>
+#include <Model/Textures/ImageTexture.h>
+#include <Model/Textures/NormalTexture.h>
+#include <Model/Materials/Lambertian.h>
+#include <Model/Materials/Phong.h>
+#include <Model/Materials/Dielectric.h>
+#include <Model/Materials/Flat.h>
+#include <Model/Primitives/TexTriangle.h>
+#include <Model/Primitives/HeavyTriangle.h>
+
+#include <iostream>
+
+using namespace Manta;
+using namespace Glm;
+
+// Check to see if the specified file can be loaded, otherwise use the 
specified color.
+static Texture<Color> *check_for_texture( const string &path_name,
+                                   const string &file_name,
+                                   const Color &constant_color,
+                                   const float* map_scaling ) {
+
+    Texture<Color> *texture = 0;
+
+    if (file_name.size()) {
+        // Load the image texture.
+        try {
+          ImageTexture<Color>* it = LoadColorImageTexture( path_name + 
file_name , &cerr);
+          // The values are assigned to zero in in unintialized state
+          if (map_scaling[0] != 0) {
+            it->setScale(map_scaling[0], map_scaling[1]);
+          }
+          if (true/*bilinear_textures*/) {
+            it->setInterpolationMethod(ImageTexture<Color>::Bilinear);
+          }
+          texture = it;
+        }
+        catch (SCIRun::Exception &e) {
+            std::cerr << "Could not load diffuse map: "
+                      << file_name
+                      << ": " << e.message() << std::endl;
+        }
+    }
+
+    if (texture == 0) {
+
+        // Otherwise use a constant color.
+        texture = new Constant<Color>( constant_color );
+    }
+
+    return texture;
+}
+
+
+
+void ObjGroup::create_materials( GLMmodel *model ) {
+
+    
//////////////////////////////////////////////////////////////////////////
+    // Path to the model for loading textures.
+    string model_path = model->pathname;
+
+    int pos = model_path.rfind( '/' );
+    if (pos != string::npos) {
+        model_path = model_path.substr( 0, pos+1 );
+    }
+
+    material_array = new Material *[ model->nummaterials ];
+    material_array_size = model->nummaterials;
+
+    // Read in the materials.
+    for (int i=0;i<model->nummaterials;++i) {
+
+        
//////////////////////////////////////////////////////////////////////
+        // Copy out material attributes.
+        string mtl_name(model->materials[i].name);
+        std::cerr << "Name ("<<mtl_name<<") ";
+
+        // Color ambient (RGB( model->materials[i].ambient[0],
+        //                     model->materials[i].ambient[1],
+        //                     model->materials[i].ambient[2] ));
+
+        Color diffuse (RGB( model->materials[i].diffuse[0],
+                            model->materials[i].diffuse[1],
+                            model->materials[i].diffuse[2] ));
+        Color specular(RGB( model->materials[i].specular[0],
+                            model->materials[i].specular[1],
+                            model->materials[i].specular[2] ));
+
+        float Ns     = model->materials[i].shininess;
+        float Tr     = model->materials[i].refraction;
+        float alpha  = model->materials[i].alpha;
+        float reflectivity = model->materials[i].reflectivity;
+        int   shader = model->materials[i].shader;
+
+        // Copy out texture names.
+        // string ambient_map_name  = (model->materials[i].ambient_map[0]  
!= '\0') ?
+        //   model->materials[i].ambient_map : "";
+        string diffuse_map_name((model->materials[i].diffuse_map[0]  != 
'\0') ?
+                                model->materials[i].diffuse_map : "");
+        string specular_map_name((model->materials[i].specular_map[0] != 
'\0') ?
+                                 model->materials[i].specular_map : "");
+        float* diffuse_map_scaling = model->materials[i].diffuse_map_scaling;
+        float* specular_map_scaling = 
model->materials[i].specular_map_scaling;
+
+        int index = i;
+        // Texture<Color> *diffuse_map  = check_for_texture( model_path, 
diffuse_map_name, diffuse, diffuse_map_scaling );
+
+        // Lambertian Shader.
+        // material_array[index] = new Flat( diffuse_map );
+
+        
//////////////////////////////////////////////////////////////////////
+        // Check for a dielectric.
+        if (Tr != 1 && Tr != 0) {
+
+          // Constant textures for refraction parameters.
+          Texture<Real> *n  = new Constant<Real>( 1.6 );
+          Texture<Real> *nt = new Constant<Real>( 1.0 );          
+          Texture<Color> *diffuse_map = check_for_texture( model_path, 
diffuse_map_name, diffuse , diffuse_map_scaling);
+          
+          // Create a dielectric shader.
+          material_array[index] = new Dielectric(new 
Constant<ColorComponent>(1), new Constant<ColorComponent>(1), diffuse_map);
+
+        }
+
+        
//////////////////////////////////////////////////////////////////////
+        // Check for a specular term.
+        else if (Ns != 0) {
+            Texture<Color> *diffuse_map  = check_for_texture( model_path, 
diffuse_map_name, diffuse, diffuse_map_scaling );
+            Texture<Color> *specular_map = check_for_texture( model_path, 
specular_map_name, specular, specular_map_scaling );
+            Texture<ColorComponent> *reflection = new 
Constant<ColorComponent>( reflectivity );
+
+            // Phong shader.
+            material_array[index] = new Phong( diffuse_map, specular_map,
+                                               static_cast<int>(Ns), 
reflection );
+        }
+
+        
//////////////////////////////////////////////////////////////////////
+        // Use a lambertian shader.
+        else {
+            Texture<Color> *diffuse_map  = check_for_texture( model_path, 
diffuse_map_name, diffuse, diffuse_map_scaling );
+
+            // Lambertian Shader.
+            material_array[index] = new Lambertian( diffuse_map );
+        }
+
+    }
+
+}
+
+
+
+ObjGroup::ObjGroup( const char *filename ) throw (InputError) {
+
+  // Load the model.
+  GLMmodel *model = glmReadOBJ( filename );
+  if (model == 0) {
+    throw InputError( "Error opening input file." );
+  }
+
+  // Allocate storage for primitives and materials.
+  create_materials( model );
+
+  Material *default_material = new Flat( new NormalTexture() );
+
+  // Read in the groups.
+  GLMgroup *group = model->groups;
+  while (group != 0) {
+
+    Material *material;
+
+    // Determine the material for this group.
+    int material_index = group->material;
+    if (material_index < model->nummaterials) {
+      material = material_array[material_index];
+    }
+    else {
+      material = default_material;
+    }
+    
+    // Copy out triangles.
+    int total_faces = group->numtriangles;
+    for (int i=0;i<total_faces;++i) {
+      
+      Vector vertex[3];
+      Vector normal[3];
+      Vector texcoord[3];
+      
+      bool have_normals = true;
+      bool have_textures = true;
+      
+      for (int v=0;v<3;++v) {
+        int    index = model->triangles[ group->triangles[i] ].vindices[v];
+        float *f     = model->vertices+(index*3);
+        
+        // Copy out the vertex.
+        vertex[v][0] = f[0];
+        vertex[v][1] = f[1];
+        vertex[v][2] = f[2];
+        
+        index = model->triangles[ group->triangles[i] ].nindices[v];
+        if (index > 0) {
+          f = model->normals+(index*3);
+          
+          // Copy out the normal.
+          normal[v][0] = f[0];
+          normal[v][1] = f[1];
+          normal[v][2] = f[2];
+        }
+        else {
+          // If one of the vertices doesn't have texture
+          // coordinates, then all don't.
+          have_normals = false;
+        }
+        
+        index = model->triangles[ group->triangles[i] ].tindices[v];
+        if (index > 0) {
+          f = model->texcoords+(index*2);
+          
+          // Copy out the texcoord
+          texcoord[v][0] = f[0];
+          texcoord[v][1] = f[1];
+          texcoord[v][2] = 1.0;
+        }
+        else {
+          have_textures = false;
+        }
+        
+      }
+      
+      // Create a new triangle.
+      Object *triangle = 0;
+      if (have_textures) {
+        if (have_normals) {
+          triangle = new TexTriangle( material,
+                                      vertex[0], vertex[1]-vertex[0], 
vertex[2]-vertex[0],
+                                      normal[0], normal[1], normal[2],
+                                      texcoord[0], texcoord[1], texcoord[2]);
+        } else {
+          triangle = new TexTriangle( material,
+                                      vertex[0], vertex[1], vertex[2],
+                                      texcoord[0], texcoord[1], texcoord[2]);
+        }
+      } else {
+        // No texture coordinates
+        if (have_normals) {
+          triangle = new HeavyTriangle( material,
+                                        vertex[0], vertex[1]-vertex[0], 
vertex[2]-vertex[0],
+                                        normal[0], normal[1], normal[2]);
+        } else {
+          triangle = new HeavyTriangle( material,
+                                        vertex[0], vertex[1], vertex[2]);
+        }
+      }
+
+      // Add the triangle to the group.
+      add(triangle);
+    }
+    
+    // Move to the next group.
+    group = group->next;
+  }
+  
+  // std::cerr << "Total triangles added: " << tri << std::endl;
+    
+}
+
+ ObjGroup::~ObjGroup() {
+
+   // Delete the individual materials.
+   for (unsigned i=0;i<material_array_size;++i) {
+     if (material_array[i])
+       delete material_array[i];
+   }
+   
+   // Delete the array.
+   delete [] material_array;
+ }
+

Added: trunk/Model/Groups/ObjGroup.h
==============================================================================
--- (empty file)
+++ trunk/Model/Groups/ObjGroup.h       Thu May  3 01:20:59 2007
@@ -0,0 +1,26 @@
+
+#ifndef MODEL_GROUPS_OBJGROUP__H
+#define MODEL_GROUPS_OBJGROUP__H
+
+#include <Model/Groups/Group.h>
+#include <Core/Exceptions/InputError.h>
+#include <Model/Readers/glm/glm.h>
+
+namespace Manta {
+
+  class Material;
+  
+  class ObjGroup : public Group {
+  public:
+    ObjGroup( const char *filename ) throw (InputError);
+    virtual ~ObjGroup();
+    
+  private:
+    void create_materials( Glm::GLMmodel *model );
+
+    Material **material_array;
+    unsigned material_array_size;
+  };
+};
+
+#endif

Modified: trunk/SwigInterface/manta.i
==============================================================================
--- trunk/SwigInterface/manta.i (original)
+++ trunk/SwigInterface/manta.i Thu May  3 01:20:59 2007
@@ -54,6 +54,7 @@
 #include <Core/Exceptions/BadPrimitive.h>
 #include <Core/Exceptions/InvalidState.h>
 #include <Core/Exceptions/UnknownColor.h>
+#include <Core/Exceptions/InputError.h>
 %}
 
 ///////////////////////////////////////////////////////
@@ -229,6 +230,7 @@
 #include <Model/Groups/Build_Approx_cc.h>
 #include <Model/Groups/RealisticBvh.h>
 #include <Model/Groups/DynBVH.h>
+#include <Model/Groups/ObjGroup.h>
 %}
 
 %include <Model/Groups/KDTreeDyn.h>
@@ -237,6 +239,7 @@
 %include <Model/Groups/Build_Approx_cc.h>
 %include <Model/Groups/RealisticBvh.h>
 %include <Model/Groups/DynBVH.h>
+%include <Model/Groups/ObjGroup.h>
 
 /////////////////////////////////////////////////
 // GLM.

Modified: trunk/SwigInterface/mantainterface.i
==============================================================================
--- trunk/SwigInterface/mantainterface.i        (original)
+++ trunk/SwigInterface/mantainterface.i        Thu May  3 01:20:59 2007
@@ -61,6 +61,7 @@
 #include <Core/Exceptions/IllegalValue.h>
 #include <Core/Exceptions/BadPrimitive.h>
 #include <Core/Exceptions/InvalidState.h>
+#include <Core/Exceptions/InputError.h>
 %}
 
 %include <Core/Exceptions/share.h>
@@ -68,6 +69,7 @@
 %include <Core/Exceptions/IllegalArgument.h>
 %include <Core/Exceptions/BadPrimitive.h>
 %include <Core/Exceptions/InvalidState.h>
+%include <Core/Exceptions/InputError.h>
 
 %define %manta_Exception_exception(Function)
 %exception Function {




  • [MANTA] r1364 - in trunk: Model/Groups SwigInterface, abe, 05/03/2007

Archive powered by MHonArc 2.6.16.

Top of page