StaticMesh.cpp
Go to the documentation of this file.
1 
28  /*
29  * StaticMesh.cpp
30  *
31  * Created on: 12.11.2008
32  * Author: Thomas Wiemann
33  */
34 
36 
37 #include <cassert>
38 
39 namespace lvr2
40 {
41 
43 
44  m_normals.reset();
45  m_faceNormals = 0;
46  m_blackColors = 0;
47  m_vertices.reset();
48  m_colors.reset();
49  m_faces.reset();
50 
51  m_numFaces = 0;
52  m_numVertices = 0;
53  m_numMaterials = 0;
54 
55  m_finalized = false;
56  m_haveMaterials = false;
57 
58  m_renderMode = 0;
59 
60  m_nameList = -1;
61 
62 }
63 
65  : Renderable( name )
66 {
67 
68  m_model = model;
69  m_nameList = -1;
70  m_blackColors = 0;
71 
72  init( model->m_mesh );
77 
78 }
79 
81  : Renderable( name )
82 {
83 
84  m_model = ModelPtr( new Model( mesh ) );
85  m_nameList = -1;
86  m_blackColors = 0;
87  init( mesh );
88 
93 
94 }
95 
97 {
98  m_lineWidth = 2.0;
99  if(mesh)
100  {
101  m_faceNormals = 0;
102  m_numVertices = mesh->numVertices();
103  m_numFaces = mesh->numFaces();
104  size_t w_color;
105 
106  m_normals = mesh->getVertexNormals();
107  m_colors = mesh->getVertexColors(w_color);
108 
109  // does only support RGB vertex colors
110  assert(w_color == 3);
111 
112  m_vertices = mesh->getVertices();
113  m_faces = mesh->getFaceIndices();
114  m_blackColors = new unsigned char[ 3 * m_numVertices ];
115 
116  for ( size_t i = 0; i < 3 * m_numVertices; i++ )
117  {
118  m_blackColors[i] = 0;
119  }
120 
121  m_finalized = true;
122  m_visible = true;
123  m_active = true;
124 
125  m_renderMode = 0;
128 
130 
131  if(!m_faceNormals)
132  {
134  } else
135  {
136  // cout << "Face normals: " << m_faceNormals << endl;
137  }
138 
139  if(!m_colors)
140  {
142  }
143  }
144 }
145 
146 
148 {
149  if( m_faceNormals != 0 )
150  {
151  delete[] m_faceNormals;
152  }
153 
154  m_faceNormals = new float[3 * o.m_numVertices];
155  m_vertices = floatArr( new float[3 * o.m_numVertices] );
156  m_colors = ucharArr( new unsigned char[3 * o.m_numVertices] );
157  m_faces = indexArray( new unsigned int[3 * o.m_numFaces] );
158 
159  for ( size_t i(0); i < 3 * o.m_numVertices; i++ )
160  {
161  m_faceNormals[i] = o.m_faceNormals[i];
162  m_vertices[i] = o.m_vertices[i];
163  m_colors[i] = o.m_colors[i];
164  }
165 
166  for( size_t i = 0; i < 3 * o.m_numFaces; ++i )
167  {
168  m_faces[i] = o.m_faces[i];
169  }
170 
172  m_model = o.m_model;
173 
174 }
175 
176 void StaticMesh::setColorMaterial(float r, float g, float b)
177 {
178  float ambient_color[] = {r, g, b};
179  float diffuse_color[] = {0.45f * r, 0.5f * g, 0.55f * b};
180 
181  float specular_color[] = {0.1f, 0.15f, 0.1f};
182  float shine[] = {0.1f};
183 
184  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient_color);
185  glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse_color);
186  glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular_color);
187  glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shine);
188 }
189 
190 
192  if (m_blackColors)
193  delete[] m_blackColors;
194 
195  if (m_faceNormals)
196  delete[] m_faceNormals;
197 }
198 
200 
201 }
202 
204 {
205  if(m_finalized){
206 
207  m_wireframeList = glGenLists(1);
208 
209  // Start new display list
210  glNewList(m_wireframeList, GL_COMPILE);
211 
212  glDisable(GL_LIGHTING);
213  glDisable(GL_TEXTURE_2D);
214  glColor3f(0.0, 0.0, 0.0);
215 
216  for(size_t i = 0; i < m_numFaces; i++)
217  {
218  size_t index = 3 * i;
219  int a = 3 * m_faces[index];
220  int b = 3 * m_faces[index + 1];
221  int c = 3 * m_faces[index + 2];
222  glBegin(GL_TRIANGLES);
223  glVertex3f(m_vertices[a], m_vertices[a + 1], m_vertices[a + 2]);
224  glVertex3f(m_vertices[b], m_vertices[b + 1], m_vertices[b + 2]);
225  glVertex3f(m_vertices[c], m_vertices[c + 1], m_vertices[c + 2]);
226  glEnd();
227 
228  }
229  glEnable(GL_LIGHTING);
230  glEnable(GL_TEXTURE_2D);
231  glEndList();
232 
233  }
234 }
235 
236 
238 
239  if(m_finalized){
240 
241  m_coloredMeshList = glGenLists(1);
242 
243  // Enable vertex / normal / color arrays
244  glEnableClientState(GL_VERTEX_ARRAY);
245  glEnableClientState(GL_NORMAL_ARRAY);
246  glEnableClientState(GL_COLOR_ARRAY);
247 
248  // Start new display list
249  glNewList(m_coloredMeshList, GL_COMPILE);
250 
251  glEnable(GL_LIGHTING);
252  glEnable(GL_COLOR_MATERIAL);
253 
254  setColorMaterial(0.1f, 0.1f, 0.1f);
255 
256  // Assign element pointers
257  glVertexPointer( 3, GL_FLOAT, 0, m_vertices.get() );
258  glNormalPointer( GL_FLOAT, 0, m_faceNormals );
259  glColorPointer( 3, GL_UNSIGNED_BYTE, 0, m_colors.get() );
260 
261  // Draw elements
262  glDrawElements(GL_TRIANGLES, (GLsizei)3 * m_numFaces, GL_UNSIGNED_INT, m_faces.get());
263 
264 
265  // Draw mesh descriptions
266 
267 
268  glEndList();
269 
270  }
271 }
272 
273 void StaticMesh::setName(string name)
274 {
275  m_name = name;
276  compileNameList();
277 }
278 
280 {
281  // Release old name list
282  if(m_nameList != -1)
283  {
284  glDeleteLists(m_nameList, 1);
285  }
286 
287  // Compile a new one
288  m_nameList = glGenLists(1);
289  glNewList(m_nameList, GL_COMPILE);
290  Vec v = m_boundingBox->getCentroid();
291  glDisable(GL_LIGHTING);
292  glColor3f(1.0, 1.0, 0.0);
293  glRasterPos3f(v.x, v.y, v.z);
294  for(int i = 0; i < Name().size(); i++)
295  {
296  //glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, Name()[i]);
297  }
298  glEnable(GL_LIGHTING);
299  glEndList();
300 }
301 
303 {
304 
305  // Be sure that vertex and indexbuffer exist
306  assert(m_vertices);
307  assert(m_faces);
308 
309  // Alloc new normal array
310  m_faceNormals = new float[3 * m_numVertices];
311  memset(m_faceNormals, 0, 3 * m_numVertices * sizeof(float));
312 
313  // Interpolate surface m_normals for each face
314  // and interpolate sum up the normal coordinates
315  // at each vertex position
316  size_t a, b, c, buffer_pos;
317  for(size_t i = 0; i < m_numFaces; i++)
318  {
319  buffer_pos = i * 3;
320 
321  // Interpolate a perpendicular vector to the
322  // current triangle (p)
323  //
324  // CAUTION:
325  // --------
326  // buffer_pos is the face number
327  // to get real position of the vertex in the buffer
328  // we have to remember, that each vertex has three
329  // coordinates! cout << 1 << endl;
330  a = m_faces[buffer_pos] * 3;
331  b = m_faces[buffer_pos + 1] * 3;
332  c = m_faces[buffer_pos + 2] * 3;
333 
334  Vec v0(m_vertices[a], m_vertices[a + 1], m_vertices[a + 2]);
335  Vec v1(m_vertices[b], m_vertices[b + 1], m_vertices[b + 2]);
336  Vec v2(m_vertices[c], m_vertices[c + 1], m_vertices[c + 2]);
337 
338  Vec d1 = v0 - v1;
339  Vec d2 = v2 - v1;
340 
341  Normal<float> p(d1.cross(d2));
342  p = p * -1;
343 
344  // Sum up coordinate values in normal array
345  m_faceNormals[a ] = p.x;
346  m_faceNormals[a + 1] = p.y;
347  m_faceNormals[a + 2] = p.z;
348 
349  m_faceNormals[b ] = p.x;
350  m_faceNormals[b + 1] = p.y;
351  m_faceNormals[b + 2] = p.z;
352 
353  m_faceNormals[c ] = p.x;
354  m_faceNormals[c + 1] = p.y;
355  m_faceNormals[c + 2] = p.z;
356 
357  }
358 
359  // Normalize
360  for(size_t i = 0; i < m_numVertices; i++)
361  {
362  Normal<float> n(m_faceNormals[i * 3], m_faceNormals[i * 3 + 1], m_faceNormals[i * 3 + 2]);
363  m_faceNormals[i * 3] = n.x;
364  m_faceNormals[i * 3 + 1] = n.y;
365  m_faceNormals[i * 3 + 2] = n.z;
366  }
367 
368 }
369 
371 {
372  m_colors = ucharArr( new unsigned char[3 * m_numVertices] );
373  for(size_t i = 0; i < m_numVertices; i++)
374  {
375  m_colors[i*3 + 0] = 0;
376  m_colors[i*3 + 1] = 255;
377  m_colors[i*3 + 2] = 0;
378  }
379 }
380 
382 {
383  for(size_t i = 0; i < m_numVertices; i++)
384  {
385  m_boundingBox->expand(Vec(
386  m_vertices[3 * i],
387  m_vertices[3 * i + 1],
388  m_vertices[3 * i + 2] ));
389 
390  }
391 }
392 
394 {
395 
396  return m_finalized ? m_faces : uintArr();
397 
398 }
399 
401 {
402 
403  return m_finalized ? m_vertices : floatArr();
404 
405 }
406 
408 {
409 
410  return m_finalized ? m_faceNormals : 0;
411 
412 }
413 
415 {
416  return m_numVertices;
417 }
419 {
420  return m_numFaces;
421 }
422 
424 {
425 
426 }
427 
428 
429 } // namespace lvr2
virtual void setName(string name)
Definition: StaticMesh.cpp:273
unsigned char * m_blackColors
Definition: StaticMesh.hpp:124
HalfEdgeMesh< Vec > mesh
virtual void finalize()
Definition: StaticMesh.cpp:199
std::shared_ptr< MeshBuffer > MeshBufferPtr
Definition: MeshBuffer.hpp:217
boost::shared_array< unsigned int > uintArr
Definition: DataStruct.hpp:130
void compileNameList()
Definition: StaticMesh.cpp:279
size_t getNumberOfFaces()
Definition: StaticMesh.cpp:418
BaseVector< CoordT > cross(const BaseVector &other) const
Calculates the cross product between this and the given vector. Returns a new BaseVector instance...
BaseVector< float > Vec
Definition: Renderable.hpp:69
void interpolateNormals()
Definition: StaticMesh.cpp:302
boost::shared_array< unsigned char > ucharArr
Definition: DataStruct.hpp:137
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
floatArr getVertices()
Definition: StaticMesh.cpp:400
boost::shared_array< unsigned int > indexArray
Definition: DataStruct.hpp:128
float * m_faceNormals
Definition: StaticMesh.hpp:121
uintArr getIndices()
Definition: StaticMesh.cpp:393
size_t getNumberOfVertices()
Definition: StaticMesh.cpp:414
void setDefaultColors()
Definition: StaticMesh.cpp:370
boost::shared_array< float > floatArr
Definition: DataStruct.hpp:133
SharedPointer p
void init(MeshBufferPtr mesh)
Definition: StaticMesh.cpp:96
void compileColoredMeshList()
Definition: StaticMesh.cpp:237
void calcBoundingBox()
Definition: StaticMesh.cpp:381
std::shared_ptr< Model > ModelPtr
Definition: Model.hpp:80
BoundingBox< Vec > * m_boundingBox
Definition: Renderable.hpp:165
floatArr m_vertices
Definition: StaticMesh.hpp:122
virtual ModelPtr model()
Definition: Renderable.hpp:126
float * getNormals()
Definition: StaticMesh.cpp:407
virtual void savePLY(string filename)
Definition: StaticMesh.cpp:423
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