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 
61 TexturedMesh::TexturedMesh(MeshBufferPtr mesh) : m_materials(mesh->getMaterials())
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
TexturedMesh.hpp
lvr2::GlTexture
Definition: GlTexture.hpp:56
lvr2::TexturedMesh::compileTexureDisplayList
void compileTexureDisplayList()
Definition: TexturedMesh.cpp:162
lvr2::TexturedMesh::m_materials
vector< Material > & m_materials
Definition: TexturedMesh.hpp:117
lvr2::StaticMesh::m_renderMode
int m_renderMode
Definition: StaticMesh.hpp:135
lvr2::StaticMesh::m_faces
indexArray m_faces
Definition: StaticMesh.hpp:126
lvr2::TexturedMesh::m_numMaterials
size_t m_numMaterials
Definition: TexturedMesh.hpp:124
lvr2::TexturedMesh::m_faceMaterials
indexArray m_faceMaterials
Definition: TexturedMesh.hpp:114
lvr2::TexturedMesh::m_textures
textureArr m_textures
Definition: TexturedMesh.hpp:116
lvr2::TexturedMesh::generateMaterialGroups
void generateMaterialGroups()
Definition: TexturedMesh.cpp:107
lvr2::MaterialGroup::textureIndex
int textureIndex
Definition: TexturedMesh.hpp:48
lvr2::MaterialGroup::faceBuffer
vector< size_t > faceBuffer
Definition: TexturedMesh.hpp:51
lvr2::RenderTriangles
@ RenderTriangles
Definition: StaticMesh.hpp:69
lvr2::TexturedMesh::m_texcoords
floatArr m_texcoords
Definition: TexturedMesh.hpp:115
lvr2::StaticMesh::m_normals
floatArr m_normals
Definition: StaticMesh.hpp:120
lvr2::MaterialGroup
Definition: TexturedMesh.hpp:46
lvr2::StaticMesh::m_finalized
bool m_finalized
Definition: StaticMesh.hpp:128
lvr2::TexturedMesh::m_textureDisplayList
int m_textureDisplayList
Definition: TexturedMesh.hpp:128
lvr2::Renderable::m_boundingBox
BoundingBox< Vec > * m_boundingBox
Definition: Renderable.hpp:165
lvr2::map
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.
lvr2::TexturedMesh::m_numTextures
size_t m_numTextures
Definition: TexturedMesh.hpp:125
lvr2::StaticMesh::setColorMaterial
void setColorMaterial(float r, float g, float b)
Definition: StaticMesh.cpp:176
lvr2::StaticMesh::compileWireframeList
void compileWireframeList()
Definition: StaticMesh.cpp:203
lvr2::VecUChar
BaseVector< unsigned char > VecUChar
Definition: Util.hpp:50
lvr2::TexturedMesh::m_numFaces
size_t m_numFaces
Definition: TexturedMesh.hpp:123
lvr2::textureArr
boost::shared_array< GlTexture * > textureArr
Definition: DataStruct.hpp:159
lvr2::BoundingBox
A dynamic bounding box class.
Definition: BoundingBox.hpp:49
lvr2::Renderable::Vec
BaseVector< float > Vec
Definition: Renderable.hpp:69
Util.hpp
lvr2::Material
A material that stores information about color and texture of a cluster.
Definition: Material.hpp:42
lvr2
Definition: BaseBufferManipulators.hpp:39
lvr2::TexturedMesh::TexturedMesh
TexturedMesh(MeshBufferPtr mesh)
Definition: TexturedMesh.cpp:61
lvr2::StaticMesh::m_vertices
floatArr m_vertices
Definition: StaticMesh.hpp:122
lvr2::MeshBufferPtr
std::shared_ptr< MeshBuffer > MeshBufferPtr
Definition: MeshBuffer.hpp:217
lvr2::TexturedMesh::m_numVertices
size_t m_numVertices
Definition: TexturedMesh.hpp:126
lvr2::TexturedMesh::m_textureMaterials
vector< MaterialGroup * > m_textureMaterials
Definition: TexturedMesh.hpp:120
lvr2::TexturedMesh::m_colorMaterials
vector< MaterialGroup * > m_colorMaterials
Definition: TexturedMesh.hpp:121
lvr2::Material::m_texture
boost::optional< TextureHandle > m_texture
Optional texture handle.
Definition: Material.hpp:45
mesh
HalfEdgeMesh< Vec > mesh
Definition: src/tools/lvr2_gs_reconstruction/Main.cpp:26
lvr2::Material::m_color
boost::optional< Rgb8Color > m_color
Optional color.
Definition: Material.hpp:47
lvr2::TexturedMesh::getBufferArray
void getBufferArray(unsigned int *, MaterialGroup *g)
Definition: TexturedMesh.cpp:47
lvr2::MaterialGroup::color
Vec color
Definition: TexturedMesh.hpp:50
lvr2::RenderSurfaces
@ RenderSurfaces
Definition: StaticMesh.hpp:68


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 Wed Mar 2 2022 00:37:25