Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

3dsmesh.cpp File Reference

#include <algorithm>
#include <iostream>
#include "gl.h"
#include "glext.h"
#include "vertexprog.h"
#include "texmanager.h"
#include "3dsmesh.h"

Include dependency graph for 3dsmesh.cpp:

Go to the source code of this file.

Namespaces

namespace  std

Functions

static int compareVertexLists (VertexList *vl0, VertexList *vl1)
static VertexListconvertToVertexList (M3DTriangleMesh &mesh, const M3DScene &scene, const string &texturePath)


Function Documentation

static int compareVertexLists VertexList vl0,
VertexList vl1
[static]
 

Definition at line 30 of file 3dsmesh.cpp.

References Color::alpha(), and VertexList::getDiffuseColor().

Referenced by Mesh3DS::Mesh3DS().

00031 {
00032     float a0 = vl0->getDiffuseColor().alpha();
00033     float a1 = vl1->getDiffuseColor().alpha();
00034 
00035 #if _MSC_VER <= 1200
00036     // In some cases, sorting with this comparison function hangs on Celestia
00037     // executables built with MSVC.  For some reason, adding the following crud
00038     // fixes the problem, but I haven't looked at the generated assembly
00039     // instructions to figure out what's going on.  In any case, the output
00040     // should never be printed because alpha is always >= 0.  Blah.
00041     if (a0 == -50.0f)
00042         cout << "Stupid MSVC compiler bug workaround!  (This line will never be printed)\n";
00043 #endif
00044 
00045     if (a0 == a1)
00046     {
00047         return vl0->getTexture() < vl1->getTexture();
00048     }
00049     else
00050     {
00051         return (a0 > a1);
00052     }
00053 }

static VertexList * convertToVertexList M3DTriangleMesh mesh,
const M3DScene scene,
const string texturePath
[static]
 

Definition at line 237 of file 3dsmesh.cpp.

References VertexList::addVertex(), M3DColor::blue, cross(), M3DMaterial::getDiffuseColor(), ResourceManager< T >::getHandle(), M3DMaterial::getName(), M3DMaterial::getOpacity(), M3DMaterial::getShininess(), M3DMaterial::getSpecularColor(), GetTextureManager(), M3DMaterial::getTextureMap(), VertexList::getVertexCount(), M3DColor::green, VertexList::Vertex::normal, Vector3< T >::normalize(), VertexList::Vertex::point, pow(), M3DColor::red, VertexList::setDiffuseColor(), VertexList::setShininess(), VertexList::setSpecularColor(), VertexList::setTexture(), and VertexList::Vertex::texCoords.

Referenced by Mesh3DS::Mesh3DS().

00240 {
00241     int nFaces = mesh.getFaceCount();
00242     int nVertices = mesh.getVertexCount();
00243     int nTexCoords = mesh.getTexCoordCount();
00244     bool smooth = (mesh.getSmoothingGroupCount() == nFaces);
00245     int i;
00246 
00247     uint32 parts = VertexList::VertexNormal;
00248     if (nTexCoords == nVertices)
00249         parts |= VertexList::TexCoord0;
00250     VertexList* vl = new VertexList(parts);
00251     
00252     Vec3f* faceNormals = new Vec3f[nFaces];
00253     Vec3f* vertexNormals = new Vec3f[nFaces * 3];
00254     int* faceCounts = new int[nVertices];
00255     int** vertexFaces = new int*[nVertices];
00256 
00257     for (i = 0; i < nVertices; i++)
00258     {
00259         faceCounts[i] = 0;
00260         vertexFaces[i] = NULL;
00261     }
00262 
00263     // generate face normals
00264     for (i = 0; i < nFaces; i++)
00265     {
00266         uint16 v0, v1, v2;
00267         mesh.getFace(i, v0, v1, v2);
00268 
00269         faceCounts[v0]++;
00270         faceCounts[v1]++;
00271         faceCounts[v2]++;
00272 
00273         Point3f p0 = mesh.getVertex(v0);
00274         Point3f p1 = mesh.getVertex(v1);
00275         Point3f p2 = mesh.getVertex(v2);
00276         faceNormals[i] = cross(p1 - p0, p2 - p1);
00277         faceNormals[i].normalize();
00278     }
00279 
00280     if (!smooth && 0)
00281     {
00282         for (i = 0; i < nFaces; i++)
00283         {
00284             vertexNormals[i * 3] = faceNormals[i];
00285             vertexNormals[i * 3 + 1] = faceNormals[i];
00286             vertexNormals[i * 3 + 2] = faceNormals[i];
00287         }
00288     }
00289     else
00290     {
00291         // allocate space for vertex face indices
00292         for (i = 0; i < nVertices; i++)
00293         {
00294             vertexFaces[i] = new int[faceCounts[i] + 1];
00295             vertexFaces[i][0] = faceCounts[i];
00296         }
00297 
00298         for (i = 0; i < nFaces; i++)
00299         {
00300             uint16 v0, v1, v2;
00301             mesh.getFace(i, v0, v1, v2);
00302             vertexFaces[v0][faceCounts[v0]--] = i;
00303             vertexFaces[v1][faceCounts[v1]--] = i;
00304             vertexFaces[v2][faceCounts[v2]--] = i;
00305         }
00306 
00307         // average face normals to compute the vertex normals
00308         for (i = 0; i < nFaces; i++)
00309         {
00310             uint16 v0, v1, v2;
00311             mesh.getFace(i, v0, v1, v2);
00312             // uint32 smoothingGroups = mesh.getSmoothingGroups(i);
00313 
00314             int j;
00315             Vec3f v = Vec3f(0, 0, 0);
00316             for (j = 1; j <= vertexFaces[v0][0]; j++)
00317             {
00318                 int k = vertexFaces[v0][j];
00319                 // if (k == i || (smoothingGroups & mesh.getSmoothingGroups(k)) != 0)
00320                 if (faceNormals[i] * faceNormals[k] > 0.5f)
00321                     v += faceNormals[k];
00322             }
00323             v.normalize();
00324             vertexNormals[i * 3] = v;
00325 
00326             v = Vec3f(0, 0, 0);
00327             for (j = 1; j <= vertexFaces[v1][0]; j++)
00328             {
00329                 int k = vertexFaces[v1][j];
00330                 // if (k == i || (smoothingGroups & mesh.getSmoothingGroups(k)) != 0)
00331                 if (faceNormals[i] * faceNormals[k] > 0.5f)
00332                     v += faceNormals[k];
00333             }
00334             v.normalize();
00335             vertexNormals[i * 3 + 1] = v;
00336 
00337             v = Vec3f(0, 0, 0);
00338             for (j = 1; j <= vertexFaces[v2][0]; j++)
00339             {
00340                 int k = vertexFaces[v2][j];
00341                 // if (k == i || (smoothingGroups & mesh.getSmoothingGroups(k)) != 0)
00342                 if (faceNormals[i] * faceNormals[k] > 0.5f)
00343                     v += faceNormals[k];
00344             }
00345             v.normalize();
00346             vertexNormals[i * 3 + 2] = v;
00347         }
00348     }
00349 
00350     // build the triangle list
00351     for (i = 0; i < nFaces; i++)
00352     {
00353         uint16 triVert[3];
00354         mesh.getFace(i, triVert[0], triVert[1], triVert[2]);
00355 
00356         for (int j = 0; j < 3; j++)
00357         {
00358             VertexList::Vertex v;
00359             v.point = mesh.getVertex(triVert[j]);
00360             v.normal = vertexNormals[i * 3 + j];
00361             if ((parts & VertexList::TexCoord0) != 0)
00362                 v.texCoords[0] = mesh.getTexCoord(triVert[j]);
00363             vl->addVertex(v);
00364         }
00365     }
00366 
00367     // Set the material properties
00368     {
00369         string materialName = mesh.getMaterialName();
00370         if (materialName.length() > 0)
00371         {
00372             int nMaterials = scene.getMaterialCount();
00373             for (i = 0; i < nMaterials; i++)
00374             {
00375                 M3DMaterial* material = scene.getMaterial(i);
00376                 if (materialName == material->getName())
00377                 {
00378                     M3DColor diffuse = material->getDiffuseColor();
00379                     vl->setDiffuseColor(Color(diffuse.red, diffuse.green, diffuse.blue, material->getOpacity()));
00380                     M3DColor specular = material->getSpecularColor();
00381                     vl->setSpecularColor(Color(specular.red, specular.green, specular.blue));
00382                     float shininess = material->getShininess();
00383                     
00384                     // Map the shininess from the 3DS file into the 0-128
00385                     // range that OpenGL uses for the specular exponent.
00386                     shininess = (float) pow(2, 10.0 * shininess);
00387                     if (shininess > 128.0f)
00388                         shininess = 128.0f;
00389                     vl->setShininess(128.0f);
00390 
00391                     if (material->getTextureMap() != "")
00392                     {
00393                         ResourceHandle tex = GetTextureManager()->getHandle(TextureInfo(material->getTextureMap(), texturePath, TextureInfo::WrapTexture));
00394                         vl->setTexture(tex);
00395                     }
00396 
00397                     break;
00398                 }
00399             }
00400         }
00401     }
00402 
00403     // clean up
00404     if (faceNormals != NULL)
00405         delete[] faceNormals;
00406     if (vertexNormals != NULL)
00407         delete[] vertexNormals;
00408     if (faceCounts != NULL)
00409         delete[] faceCounts;
00410     if (vertexFaces != NULL)
00411     {
00412         for (i = 0; i < nVertices; i++)
00413         {
00414             if (vertexFaces[i] != NULL)
00415                 delete[] vertexFaces[i];
00416         }
00417         delete[] vertexFaces;
00418     }
00419 
00420     return vl;
00421 }


Generated on Sat Jan 14 22:30:34 2006 for Celestia by  doxygen 1.4.1