Manta Interactive Ray Tracer Development Mailing List

Text archives Help


[Manta] r2338 - in trunk: Model/Materials scenes


Chronological Thread 
  • From:
  • To:
  • Subject: [Manta] r2338 - in trunk: Model/Materials scenes
  • Date: Wed, 22 Oct 2008 21:38:21 -0600 (MDT)

Author: aek
Date: Wed Oct 22 21:38:17 2008
New Revision: 2338

Modified:
   trunk/Model/Materials/Lambertian.h
   trunk/scenes/triangleSceneViewer.cc
Log:
Add new option to the triangleSceneViewer to save an OBJ file with an
accompanying MTL file that preserves colors of constant-colored Lambertian
material.  This allows Manta's readers to be used to convert triangle mesh
scenes in other formats to OBJ.



Modified: trunk/Model/Materials/Lambertian.h
==============================================================================
--- trunk/Model/Materials/Lambertian.h  (original)
+++ trunk/Model/Materials/Lambertian.h  Wed Oct 22 21:38:17 2008
@@ -51,6 +51,7 @@
 
     void readwrite(ArchiveElement* archive);
     void setDiffuse(const Texture<Color>* tex) { colortex = tex; }
+    const Texture<Color>* getDiffuse() { return colortex; }
   private:
     const Texture<Color>* colortex;
        

Modified: trunk/scenes/triangleSceneViewer.cc
==============================================================================
--- trunk/scenes/triangleSceneViewer.cc (original)
+++ trunk/scenes/triangleSceneViewer.cc Wed Oct 22 21:38:17 2008
@@ -58,6 +58,7 @@
   cerr << "             Can call this multiple times to load an 
animation.\n";
   cerr << " -save [filename]    - save acceleration structure to file 
(currently kdtree and bsp).\n";
   cerr << " -load [filename]    - load acceleration structure from file 
(currently kdtree and bsp).\n";
+  cerr << " -saveOBJ [filename] - convert the mesh to an OBJ and MTL file 
(omit filename extension).\n";
   cerr << " -animationLength    - Number of seconds animation takes\n";
   cerr << " -interpolateNormals - creates vertex normals if the data does"
        << "                       not already contain vertex normals.\n";
@@ -143,6 +144,69 @@
   return result;
 }
 
+void writeMeshOBJ(Mesh* mesh, string& filename) {
+  cerr << "Writing mesh to OBJ/MTL..." << endl;
+  string obj_name = filename+".obj";
+  string mtl_name = filename+".mtl";
+  FILE* output = fopen( obj_name.c_str(), "w" );
+  fprintf( output, "mtllib %s\n\n", mtl_name.c_str() );
+  for (size_t i = 0; i < mesh->vertices.size(); ++i)
+    fprintf(output, "v %f %f %f\n", mesh->vertices[i][0], 
mesh->vertices[i][1], mesh->vertices[i][2]);
+  fprintf(output, "\n");
+  for (size_t i = 0; i < mesh->texCoords.size(); ++i)
+    fprintf(output, "vt %f %f %f\n", mesh->texCoords[i][0], 
mesh->texCoords[i][1], mesh->texCoords[i][2]);
+  fprintf(output, "\n");
+  for (size_t i = 0; i < mesh->vertexNormals.size(); ++i)
+    fprintf(output, "vn %f %f %f\n", mesh->vertexNormals[i][0], 
mesh->vertexNormals[i][1], mesh->vertexNormals[i][2]);
+  fprintf(output, "\n");
+  int last_mtl = -1;
+  for (size_t i = 0; i < mesh->vertex_indices.size(); i+=3) {
+    int mtl = mesh->face_material[i/3];
+    if (mtl != last_mtl) {
+      fprintf(output, "usemtl mtl_%d\n", mtl);
+      last_mtl = mtl;
+    }
+    if (mesh->texture_indices.size() < i+3 ||
+        mesh->texture_indices[i+0] == Mesh::kNoTextureIndex ||
+        mesh->texture_indices[i+1] == Mesh::kNoTextureIndex ||
+        mesh->texture_indices[i+2] == Mesh::kNoTextureIndex)
+      fprintf(output, "f %d//%d %d//%d %d//%d\n",
+              mesh->vertex_indices[i+0]+1,
+              mesh->normal_indices[i+0]+1,
+              mesh->vertex_indices[i+1]+1,
+              mesh->normal_indices[i+1]+1,
+              mesh->vertex_indices[i+2]+1,
+              mesh->normal_indices[i+2]+1);
+    else
+      fprintf(output, "f %d/%d/%d %d/%d/%d %d/%d/%d\n",
+              mesh->vertex_indices[i+0]+1,
+              mesh->texture_indices[i+0]+1,
+              mesh->normal_indices[i+0]+1,
+              mesh->vertex_indices[i+1]+1,
+              mesh->texture_indices[i+1]+1,
+              mesh->normal_indices[i+1]+1,
+              mesh->vertex_indices[i+2]+1,
+              mesh->texture_indices[i+2]+1,
+              mesh->normal_indices[i+2]+1);
+  }
+  fprintf(output, "\n");
+  fclose(output);
+  output = fopen(mtl_name.c_str(), "w");
+  for (size_t i = 0; i < mesh->materials.size(); ++i) {
+    // TODO: Handle other material types, such as Phong.
+    Lambertian* mat = dynamic_cast<Lambertian*>(mesh->materials[i]);
+    const Constant<Color>* con = mat ? dynamic_cast<const 
Constant<Color>*>(mat->getDiffuse()) : 0;
+    Color rgb = con ? con->getValue() : Color(RGB(1,1,1));
+    fprintf(output, "newmtl mtl_%d\n", static_cast<int>(i));
+    fprintf(output, "Ka 0 0 0\n");
+    fprintf(output, "Kd %f %f %f\n", rgb[0], rgb[1], rgb[2]);
+    fprintf(output, "Ks 0 0 0\n");
+    fprintf(output, "illum 2\n");
+    fprintf(output, "Ns 0\n");
+  }
+  fclose(output);
+}
+
 MANTA_PLUGINEXPORT
 Scene* make_scene(const ReadContext&, const vector<string>& args)
 {
@@ -163,6 +227,7 @@
 
   string saveName;
   string loadName;
+  string saveOBJName;
 
   for(size_t i=0;i<args.size();i++){
     string arg = args[i];
@@ -196,6 +261,9 @@
     } else if(arg == "-load"){
       if (!getStringArg(i, args, loadName))
         throw IllegalArgument("wrong argument to -load", i, args);
+    } else if(arg == "-saveOBJ"){
+      if (!getStringArg(i, args, saveOBJName))
+        throw IllegalArgument("wrong argument to -saveOBJ", i, args);
     } else if (arg == "-animationLength") {
       if(!getArg<float>(i, args, animationLength))
         throw IllegalArgument("scene MeshLoader -animationLength", i, args);
@@ -282,6 +350,9 @@
     Mesh* singleFrame = LoadModel(fileNames[0], defaultMatl, overrideMatl, 
triangleType,
                                   useFaceNormals, interpolateNormals);
     as->setGroup(singleFrame);
+
+    if (!saveOBJName.empty())
+      writeMeshOBJ(singleFrame, saveOBJName);
 
     if (!loadName.empty()) {
       bool success = as->buildFromFile(loadName);


  • [Manta] r2338 - in trunk: Model/Materials scenes, aek, 10/22/2008

Archive powered by MHonArc 2.6.16.

Top of page