Main Page | File List

TriangleMesh.h

Go to the documentation of this file.
00001 /**
00002  *\file         TriangleMesh.h
00003  *
00004  *\brief        For reading from an obj 3D model file.
00005  *
00006  *\author       Xianming Chen\n
00007  *              Computer Science Department\n
00008  *              University of Utah
00009  *
00010  *\date         11 Jul 2004\n
00011  *              Copyright (c) 2004, University of Utah
00012  */
00013 
00014 
00015 #ifndef _TRIANGLEMESH_H
00016 #define _TRIANGLEMESH_H
00017 
00018 #include "BBox.h"
00019 #include "Canvas.h"
00020 #include <vector>
00021 #include <GL/glut.h>
00022 
00023 namespace columbia
00024 {
00025   struct TriangleFace
00026   {
00027       TriangleFace(int I, int J, int K, int Ti=-1, int Tj=-1, int Tk=-1, int Ni=-1, int Nj=-1, int Nk=-1) : 
00028         i(I), j(J), k(K), ti(Ti), tj(Tj), tk(Tk), ni(Ni), nj(Nj), nk(Nk) { }
00029       int i, j, k;                                               // indices of 3 vertices.
00030       int ni, nj, nk;                                            // indices of 3 normals.
00031       int ti, tj, tk;                                            // indices of 3 tex coords.
00032   };
00033   
00034   struct TriangleMesh
00035   {
00036       TriangleMesh(const char* obj_fname);
00037       TriangleMesh()       { }
00038       
00039       void glDraw() const;
00040       void SetTexImage(Canvas* tex)      { texImage = tex;  initTexture(); }
00041       
00042       void init();
00043       void initTexture();
00044 
00045       std :: vector<Vertex> V;           // vertex (x, y, z)
00046       std :: vector<Vector> N;           // normal (x, y, z)
00047       std :: vector<Vertex> T;           // texture (s, t, w)
00048       std :: vector<TriangleFace> F;     // triangle (i, j, k)
00049       int total_triangles;
00050       bool has_normal;
00051       BBox bbox;
00052 
00053       bool has_texture;
00054       GLuint texName;
00055       Canvas* texImage;
00056   };
00057 
00058   inline ostream& operator<< (ostream& os, TriangleMesh const& msh)
00059   {
00060     for(int i=0; i<msh.V.size(); i++)
00061     {
00062       os << "v " << msh.V[i].x << " " << msh.V[i].y << " " << msh.V[i].z << endl;
00063     }
00064     os << endl;
00065 
00066     for(int i=0; i<msh.N.size(); i++)
00067     {
00068       os << "vn " << msh.N[i].x << " " << msh.N[i].y << " " << msh.N[i].z << endl;
00069     }
00070     os << endl;
00071 
00072     for(int i=0; i<msh.T.size(); i++)
00073     {
00074       os << "vt " << msh.T[i].x << " " << msh.T[i].y << " " << msh.T[i].z << endl;
00075     }
00076     os << endl;
00077 
00078     for(int i=0; i<msh.F.size(); i++)
00079     {
00080       if(msh.has_normal && msh.has_texture)
00081       {
00082         os << "f ";
00083         os << msh.F[i].i+1 << "/" << msh.F[i].ti+1 << "/" << msh.F[i].ni+1 << " ";
00084         os << msh.F[i].j+1 << "/" << msh.F[i].tj+1 << "/" << msh.F[i].nj+1 << " ";
00085         os << msh.F[i].k+1 << "/" << msh.F[i].tk+1 << "/" << msh.F[i].nk+1 << endl;
00086       }
00087       else if(msh.has_texture)
00088         os << "f " << msh.F[i].i+1 << "/" << msh.F[i].ti+1 << " " << msh.F[i].j+1 << "/" << msh.F[i].tj+1 << " " << msh.F[i].k+1 << "/" << msh.F[i].tk+1 << endl;
00089       else if(msh.has_normal)
00090         os << "f " << msh.F[i].i+1 << "//" << msh.F[i].ni+1 << " " << msh.F[i].j+1 << "//" << msh.F[i].nj+1 << " " << msh.F[i].k+1 << "//" << msh.F[i].nk+1 << endl;
00091       else
00092         os << "f " << msh.F[i].i+1 << " " << msh.F[i].j+1 <<  " " << msh.F[i].k+1 << endl;
00093     }
00094     os << endl;
00095     return os;
00096   }
00097     
00098 
00099 }//end namespace xchen
00100 
00101 
00102 #endif

Generated on Mon Jul 19 17:20:27 2004 by doxygen 1.3.6