Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
objimporter.hpp
Go to the documentation of this file.
1 
23 #ifndef __OBJIMPORTER__
24 #define __OBJIMPORTER__
25 
26 #include <tucano/mesh.hpp>
27 
28 using namespace std;
29 
30 namespace Tucano
31 {
32 
33 namespace MeshImporter
34 {
35 
36 #if _WIN32 //define something for Windows (32-bit and 64-bit, this part is common)
37  #pragma warning(disable:4996)
38 #else
39 // avoid warnings of unused function
40 static void loadObjFile (Mesh* mesh, string filename) __attribute__ ((unused));
41 #endif
42 
50 static void loadObjFile (Mesh* mesh, string filename)
51 {
52  vector<Eigen::Vector4f> vert;
53  vector<Eigen::Vector3f> norm;
54  vector<Eigen::Vector2f> texCoord;
55  vector<Eigen::Vector4f> color;
56  vector<GLuint> elementsVertices;
57  vector<GLuint> elementsNormals;
58  vector<GLuint> elementsTexIDs;
59 
60  //Opening file:
61  #ifdef TUCANODEBUG
62  cout << "Opening Wavefront obj file " << filename.c_str() << endl << endl;
63  #endif
64 
65  ifstream in(filename.c_str(),ios::in);
66  if (!in)
67  {
68  cerr << "Cannot open " << filename.c_str() << endl; exit(1);
69  }
70 
71  //Reading file:
72  string line;
73  while(getline(in,line))
74  {
75 
76  //Vertices reading:
77  if(line.substr(0,2) == "v ")
78  {
79 
80  istringstream s(line.substr(2));
81  Eigen::Vector4f v;
82  s >> v[0]; s >> v[1]; s >> v[2]; v[3] = 1.0f;
83  vert.push_back(v);
84 
85  if(s.rdbuf()->in_avail())
86  {
87  Eigen::Vector4f c;
88  s >> c[0]; s >> c[1]; s >> c[2]; c[3] = 1.0f;
89  color.push_back(c);
90  }
91  }
92 
93  //Normals reading:
94  else if(line.substr(0,2) == "vn")
95  {
96  istringstream s(line.substr(3));
97  Eigen::Vector3f vn;
98  s >> vn[0]; s >> vn[1]; s >> vn[2];
99  norm.push_back(vn);
100  }
101 
102  //Texture Coordinates reading:
103  else if(line.substr(0,2) == "vt")
104  {
105  istringstream s(line.substr(2));
106  Eigen::Vector2f vt;
107  s >> vt[0]; s >> vt[1];
108  texCoord.push_back(vt);
109  }
110 
111  //Elements reading: Elements are given through a string: "f vertexID/TextureID/NormalID". If no texture is given, then the string will be: "vertexID//NormalID".
112  else if(line.substr(0,2) == "f ")
113  {
114  GLuint vertexID1, normalID1, textureID1;
115  istringstream s(line.substr(2));
116  while(!s.eof())
117  {
118  s >> vertexID1;
119  elementsVertices.push_back(vertexID1-1);
120  if(s.peek() == '/')
121  {
122  s.get();
123  if(s.peek() == '/')
124  {
125  s.get();
126  s >> normalID1;
127  elementsNormals.push_back(normalID1-1);
128  }
129  else
130  {
131  s >> textureID1;
132  elementsTexIDs.push_back(textureID1-1);
133  if(s.peek() == '/')
134  {
135  s.get();
136  s >> normalID1;
137  elementsNormals.push_back(normalID1-1);
138  }
139  }
140  }
141  }
142  }
143 
144  //Ignoring comment lines:
145  else if(line[0] == '#') { }
146 
147  //Ignoring any other lines:
148  else {};
149  }
150  if(in.is_open())
151  {
152  in.close();
153  }
154 
155  // load attributes found in file
156  if (vert.size() > 0)
157  {
158  mesh->loadVertices(vert);
159  }
160  if (norm.size() > 0)
161  {
162  mesh->loadNormals(norm);
163  }
164  if (texCoord.size() > 0)
165  {
166  mesh->loadTexCoords(texCoord);
167  }
168  if (color.size() > 0)
169  {
170  mesh->loadColors(color);
171  }
172  if (elementsVertices.size() > 0)
173  {
174  mesh->loadIndices(elementsVertices);
175  }
176 
177  // sets the default locations for accesing attributes in shaders
179 
180  #ifdef TUCANODEBUG
181  Misc::errorCheckFunc(__FILE__, __LINE__);
182  #endif
183 }
184 
185 }
186 }
187 #endif
void loadNormals(vector< Eigen::Vector3f > &norm)
Load normals (x,y,z) as a vertex attribute.
Definition: mesh.hpp:384
void loadColors(vector< Eigen::Vector4f > &clrs)
Load colors (r,g,b,a) as a vertex attribute.
Definition: mesh.hpp:438
Definition: bufferobject.hpp:34
static void loadObjFile(Mesh *mesh, string filename) __attribute__((unused))
Loads a mesh from an OBJ file.
Definition: objimporter.hpp:50
void errorCheckFunc(std::string file, int line, std::string message="")
GL error check method.
Definition: misc.hpp:53
void loadIndices(vector< GLuint > &ind)
Load indices into indices array.
Definition: mesh.hpp:448
void setDefaultAttribLocations(void)
Sets default attribute locations. vertex coords -> location 0 normals -> location 1 colors -> locatio...
Definition: mesh.hpp:474
void loadVertices(vector< Eigen::Vector4f > &vert)
Load vertices (x,y,z,w) and creates appropriate vertex attribute. The default attribute name is "in_P...
Definition: mesh.hpp:312
void loadTexCoords(vector< Eigen::Vector2f > &tex, bool normalize=false)
Load tex coords (u,v) as a vertex attribute. Optionally normalizes coords in range [0...
Definition: mesh.hpp:397
A common Mesh, usually containing triagles or points.
Definition: mesh.hpp:194