TexturedMesh.cpp
Go to the documentation of this file.
1 
28 /*
29  * TexturedMesh.cpp
30  *
31  * Created on: 14.04.2012
32  * Author: Thomas Wiemann
33  */
34 
35 
37 
38 #include "lvr2/util/Util.hpp"
39 
40 #include <map>
41 using std::multimap;
42 using std::map;
43 
44 namespace lvr2
45 {
46 
47 void TexturedMesh::getBufferArray(unsigned int* buffer, MaterialGroup* g)
48 {
49  size_t n = g->faceBuffer.size();
50  for(size_t i = 0; i < n; i++)
51  {
52  size_t pos = 3 * i;
53 
54  buffer[pos ] = m_faces[3 * g->faceBuffer[i] ];
55  buffer[pos + 1] = m_faces[3 * g->faceBuffer[i] + 1];
56  buffer[pos + 2] = m_faces[3 * g->faceBuffer[i] + 2];
57  }
58 
59 }
60 
62 {
63  // Store internal buffers
64  m_numFaces = mesh->numFaces();
65  m_numVertices = mesh->numVertices();
66  m_numTextures = mesh->getTextures().size();
67  m_numMaterials = mesh->getMaterials().size();
68 
69  m_faces = mesh->getFaceIndices();
70  m_faceMaterials = mesh->getFaceMaterialIndices();
71  m_vertices = mesh->getVertices();
72  m_normals = mesh->getVertexNormals();
73  m_texcoords = mesh->getTextureCoordinates();
75 
76  // convert to GlTexture*
77  m_textures = textureArr( new GlTexture* [mesh->getTextures().size()] );
78  for (size_t i = 0; i < mesh->getTextures().size(); i++)
79  {
80  m_textures[i] = new GlTexture(mesh->getTextures()[i]);
81  }
82 
83  // Calc bounding box
84  for(size_t i = 0; i < m_numVertices; i++)
85  {
86  float x = m_vertices[3 * i];
87  float y = m_vertices[3 * i + 1];
88  float z = m_vertices[3 * i + 2];
89  m_boundingBox->expand(Vec(x, y, z));
90  }
91 
92  // Create material groups for optimized rendering
94 
95  // Compile display list
98 
99  m_finalized = true;
100 
101  m_renderMode = 0;
104 }
105 
106 
108 {
109  map<int, MaterialGroup* > texMatMap;
110  map<VecUChar, MaterialGroup*, Util::ColorVecCompare> colorMatMap;
111 
112  // Iterate over face material buffer and
113  // sort faces by their material
114  for(size_t i = 0; i < m_numFaces; i++)
115  {
116  map<int, MaterialGroup*>::iterator texIt;
117  map<VecUChar, MaterialGroup*, Util::ColorVecCompare>::iterator colIt;
118 
119  // Get material by index and lookup in map. If present
120  // add face index to the corresponding group. Create a new
121  // group if none was found. For efficient rendering we have to
122  // create groups by color and texture index,
124 
125  if(m.m_texture)
126  {
127 
128  texIt = texMatMap.find(m.m_texture->idx());
129  if(texIt == texMatMap.end())
130  {
131  MaterialGroup* g = new MaterialGroup;
132  g->textureIndex = m.m_texture->idx();
133  g->color = Vec(1.0, 1.0, 1.0);
134  g->faceBuffer.push_back(i);
135  m_textureMaterials.push_back(g);
136  texMatMap[m.m_texture->idx()] = g;
137  }
138  else
139  {
140  texIt->second->faceBuffer.push_back(i);
141  }
142  }
143  else
144  {
145  colIt = colorMatMap.find(VecUChar(m.m_color->at(0), m.m_color->at(1), m.m_color->at(2)));
146  if(colIt == colorMatMap.end())
147  {
148  MaterialGroup* g = new MaterialGroup;
149  g->textureIndex = -1;
150  g->faceBuffer.push_back(i);
151  g->color = Vec(m.m_color->at(0) / 255.0f, m.m_color->at(0) / 255.0f, m.m_color->at(0) / 255.0f);
152  m_colorMaterials.push_back(g);
153  }
154  else
155  {
156  colIt->second->faceBuffer.push_back(i);
157  }
158  }
159  }
160 }
161 
163 {
164 
165  // Enable vertex arrays
166  glEnableClientState(GL_VERTEX_ARRAY);
167  glEnableClientState(GL_NORMAL_ARRAY);
168  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
169 
170 
171  // Get display list index and compile display list
172  m_textureDisplayList = glGenLists(1);
173 
174 
175  glNewList(m_textureDisplayList, GL_COMPILE);
176 
177  // Bind arrays
178  glVertexPointer( 3, GL_FLOAT, 0, m_vertices.get() );
179  glNormalPointer( GL_FLOAT, 0, m_normals.get() );
180  glTexCoordPointer(3, GL_FLOAT, 0, m_texcoords.get());
181 
182 
183  // Draw textured materials
184  for(size_t i = 0; i < m_textureMaterials.size(); i++)
185  {
187 
188  unsigned int* buf = new unsigned int [3 * g->faceBuffer.size()];
189  getBufferArray(buf, g);
190  m_textures[g->textureIndex]->bind();
191  glColor3f(1.0, 1.0, 1.0);
192  setColorMaterial(g->color[0], g->color[1], g->color[2]);
193  glDrawElements(GL_TRIANGLES, 3 * g->faceBuffer.size(), GL_UNSIGNED_INT, buf);
194  delete[] buf;
195  }
196 
197  // Draw colored materials
198 
199  glDisable(GL_TEXTURE_2D);
200  glBegin(GL_TRIANGLES);
201  for(size_t i = 0; i < m_colorMaterials.size(); i++)
202  {
204  glColor3f(g->color[0], g->color[1], g->color[2]);
205  for(size_t i = 0; i < g->faceBuffer.size(); i++)
206  {
207  int a = m_faces[g->faceBuffer[i] * 3 ];
208  int b = m_faces[g->faceBuffer[i] * 3 + 1];
209  int c = m_faces[g->faceBuffer[i] * 3 + 2];
210 
211 
212  glNormal3f(m_normals [3 * a], m_normals [3 * a + 1], m_normals [3 * a + 2]);
213  glVertex3f(m_vertices [3 * a], m_vertices [3 * a + 1], m_vertices [3 * a + 2]);
214 
215 
216  glNormal3f(m_normals [3 * b], m_normals [3 * b + 1], m_normals [3 * b + 2]);
217  glVertex3f(m_vertices [3 * b], m_vertices [3 * b + 1], m_vertices [3 * b + 2]);
218 
219  glNormal3f(m_normals [3 * c], m_normals [3 * c + 1], m_normals [3 * c + 2]);
220  glVertex3f(m_vertices [3 * c], m_vertices [3 * c + 1], m_vertices [3 * c + 2]);
221  }
222 
223  }
224  glEnd();
225  glEnable(GL_TEXTURE_2D);
226  glEndList();
227 
228 }
229 
230 
231 
232 } // namespace lvr2
vector< size_t > faceBuffer
A material that stores information about color and texture of a cluster.
Definition: Material.hpp:42
vector< MaterialGroup * > m_colorMaterials
HalfEdgeMesh< Vec > mesh
std::shared_ptr< MeshBuffer > MeshBufferPtr
Definition: MeshBuffer.hpp:217
BaseVector< float > Vec
Definition: Renderable.hpp:69
boost::shared_array< GlTexture *> textureArr
Definition: DataStruct.hpp:159
indexArray m_faces
Definition: StaticMesh.hpp:126
floatArr m_normals
Definition: StaticMesh.hpp:120
A dynamic bounding box class.
Definition: BoundingBox.hpp:49
void setColorMaterial(float r, float g, float b)
Definition: StaticMesh.cpp:176
BaseVector< unsigned char > VecUChar
Definition: Util.hpp:50
OutMapT< typename InMapT::HandleType, std::result_of_t< MapF(typename InMapT::ValueType)> > map(const InMapT &mapIn, MapF func)
Calls func for each value of the given map and save the result in the output map. ...
vector< Material > & m_materials
indexArray m_faceMaterials
TexturedMesh(MeshBufferPtr mesh)
void compileTexureDisplayList()
vector< MaterialGroup * > m_textureMaterials
BoundingBox< Vec > * m_boundingBox
Definition: Renderable.hpp:165
boost::optional< Rgb8Color > m_color
Optional color.
Definition: Material.hpp:47
floatArr m_vertices
Definition: StaticMesh.hpp:122
void getBufferArray(unsigned int *, MaterialGroup *g)
boost::optional< TextureHandle > m_texture
Optional texture handle.
Definition: Material.hpp:45
void compileWireframeList()
Definition: StaticMesh.cpp:203


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Mon Feb 28 2022 22:46:09