rtcMesh3DNode.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008
00003  * Robert Bosch LLC
00004  * Research and Technology Center North America
00005  * Palo Alto, California
00006  *
00007  * All rights reserved.
00008  *
00009  *------------------------------------------------------------------------------
00010  * project ....: PUMA: Probablistic Unsupervised Model Acquisition
00011  * file .......: Mesh3DNode.cpp
00012  * authors ....: Benjamin Pitzer
00013  * organization: Robert Bosch LLC
00014  * creation ...: 04/16/2007
00015  * modified ...: $Date: 2009-09-11 00:34:59 -0700 (Fri, 11 Sep 2009) $
00016  * changed by .: $Author: benjaminpitzer $
00017  * revision ...: $Revision: 918 $
00018  */
00019 
00020 //== INCLUDES ==================================================================
00021 #define GL_GLEXT_PROTOTYPES
00022 #include <GL/glew.h>
00023 #include <GL/gl.h>
00024 #include <GL/glu.h>
00025 #include "rtc/rtcMesh3DNode.h"
00026 
00027 //== NAMESPACES ================================================================
00028 namespace rtc {
00029 
00030 //== IMPLEMENTATION ============================================================
00031 
00032 // constructor
00033 Mesh3DNode::Mesh3DNode(Renderer* renderer, Mesh3D* mesh)
00034 : RenderNode(renderer), has_texture(false), num_vertices(0), num_faces(0),
00035   position_buffer(0), position_size(0), position_data(NULL),
00036   index_buffer(0), index_size(0), index_data(NULL), pose(mesh->pose)
00037 {
00038   // initialize
00039   initialize(mesh);
00040 }
00041 
00042 // destructor
00043 Mesh3DNode::~Mesh3DNode()
00044 {
00045   clear();
00046 }
00047 
00048 void Mesh3DNode::initialize(Mesh3D* mesh)
00049 {
00050   RenderNode::initialize();
00051   int n;
00052   num_vertices = mesh->vertices.size();
00053   num_faces = mesh->faces.size();
00054 
00055   // generate data arrays
00056   position_size = num_vertices * 3 * sizeof(GLfloat);
00057   position_data = (GLfloat*)malloc(position_size);
00058 
00059   normal_size = num_vertices * 3 * sizeof(GLfloat);
00060   normal_data = (GLfloat*)malloc(normal_size);
00061 
00062   index_size = num_faces * 3 * sizeof(GLuint);
00063   index_data = (GLuint*)malloc(index_size);
00064 
00065   if(mesh->hasTexture()) {
00066     // create vertex color array
00067     texture_coordinate_size = num_vertices * 2 * sizeof(GLfloat);
00068     texture_coordinate_data = (GLfloat*)malloc(texture_coordinate_size);
00069     has_texture = true;
00070   }
00071 
00072   // create vertex color array
00073   color_size = num_vertices * 3 * sizeof(GLubyte);
00074   color_data = (GLubyte*)malloc(color_size);
00075 
00076   int* vertex_map = new int[num_vertices];
00077 
00078   // iterate through all vertices
00079   n=0;
00080   for (unsigned int i=0; i<num_vertices; ++i) {
00081     if(mesh->vertices[i]->hidden()) {
00082       vertex_map[i] = -1;
00083     }
00084     else {
00085       Vec3f p=mesh->vertices[i]->p;
00086       // copy position data
00087       position_data[n * 3]     = (GLfloat)p[0];
00088       position_data[n * 3 + 1] = (GLfloat)p[1];
00089       position_data[n * 3 + 2] = (GLfloat)p[2];
00090 
00091       Vec3f nor=mesh->vertices[i]->n;
00092       // copy normal data
00093       normal_data[n * 3]     = (GLfloat)nor[0];
00094       normal_data[n * 3 + 1] = (GLfloat)nor[1];
00095       normal_data[n * 3 + 2] = (GLfloat)nor[2];
00096 
00097       if(mesh->hasTexture()) {
00098         // copy texture coordinate data
00099         texture_coordinate_data[n * 2]     = (GLfloat)mesh->vertices[i]->t[0];
00100         texture_coordinate_data[n * 2 + 1] = (GLfloat)mesh->vertices[i]->t[1];
00101       }
00102 
00103       // copy color data
00104       color_data[n * 3]     = (GLubyte)mesh->vertices[i]->c[0];
00105       color_data[n * 3 + 1] = (GLubyte)mesh->vertices[i]->c[1];
00106       color_data[n * 3 + 2] = (GLubyte)mesh->vertices[i]->c[2];
00107 
00108       vertex_map[i] = n;
00109       n++;
00110     }
00111   }
00112   num_vertices = n;
00113 
00114   n=0;
00115   for (unsigned int i=0; i<num_faces; ++i) {
00116     Face3D* f = mesh->faces[i];
00117     if(!f->hidden() &&
00118        !mesh->vertices[f->v(0)]->hidden() &&
00119        !mesh->vertices[f->v(1)]->hidden() &&
00120        !mesh->vertices[f->v(2)]->hidden()) {
00121 
00122       // assign index to vertex
00123       index_data[n * 3    ] = vertex_map[f->v(0)];
00124       index_data[n * 3 + 1] = vertex_map[f->v(1)];
00125       index_data[n * 3 + 2] = vertex_map[f->v(2)];
00126 
00127       n++;
00128     }
00129   }
00130   num_faces = n;
00131 
00132   position_size = num_vertices * 3 * sizeof(GLfloat);
00133   normal_size = num_vertices * 3 * sizeof(GLfloat);
00134   index_size = num_faces * 3 * sizeof(GLuint);
00135   color_size = num_vertices * 3 * sizeof(GLubyte);
00136 
00137   if(has_texture) {
00138     texture_coordinate_size = num_vertices * 2 * sizeof(GLfloat);
00139     teximage = mesh->teximage;
00140   }
00141 
00142   delete[] vertex_map;
00143 }
00144 
00145 void Mesh3DNode::clear()
00146 {
00147   RenderNode::clear();
00148 
00149   unloadGPU();
00150 
00151   free(position_data);
00152   free(normal_data);
00153   if(has_texture) {
00154     free(texture_coordinate_data);
00155   }
00156   free(color_data);
00157   free(index_data);
00158 }
00159 
00160 void Mesh3DNode::render()
00161 {
00162   // Set up nice color-tracking
00163   glEnable(GL_LIGHTING);
00164   glEnable(GL_COLOR_MATERIAL);
00165   glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
00166   glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00167 
00168   if(m_params->draw_faces)
00169     drawFacesVBO();
00170 
00171   if(m_params->draw_vertices)
00172     drawPointsVBO();
00173 
00174   if(m_params->draw_wireframe)
00175     drawWireFrameVBO();
00176 
00177   // disable color material
00178   glDisable(GL_COLOR_MATERIAL);
00179 }
00180 
00181 void Mesh3DNode::preRender()
00182 {
00183   RenderNode::preRender();
00184 }
00185 
00186 void Mesh3DNode::postRender()
00187 {
00188   RenderNode::postRender();
00189 }
00190 
00191 void Mesh3DNode::drawPointsVBO()
00192 {
00193   if (!m_is_initialized) loadToGPU();
00194 
00195   glDisable(GL_LIGHTING);
00196 
00197   bindVBO();
00198 //  glColor3f(0.0,0.0,0.0);
00199   glDrawArrays(GL_POINTS, 0, num_vertices);
00200 
00201   unbindVBO();
00202 }
00203 
00204 void Mesh3DNode::drawFacesVBO()
00205 {
00206   if (!m_is_initialized) loadToGPU();
00207 
00208   glEnable(GL_LIGHTING);
00209   //glEnable(GL_TEXTURE_2D);
00210   glEnableClientState(GL_NORMAL_ARRAY);
00211   bindVBO();
00212 //  glColor3f(1.0,1.0,1.0);
00213   glDrawElements(GL_TRIANGLES, num_faces*3, GL_UNSIGNED_INT, (GLuint*)0+0);
00214   unbindVBO();
00215 }
00216 
00217 // Draws the actual m_mesh as wireframe
00218 void Mesh3DNode::drawWireFrameVBO()
00219 {
00220   if (!m_is_initialized) loadToGPU();
00221 
00222   glDisable(GL_LIGHTING);
00223   glLineWidth(1.0);
00224   glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
00225 
00226   bindVBO();
00227   glDisable(GL_TEXTURE_2D);
00228 
00229   glDrawElements(GL_TRIANGLES, num_faces*3, GL_UNSIGNED_INT, (GLuint*)0+0);
00230   unbindVBO();
00231   glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00232 }
00233 
00234 void Mesh3DNode::drawPoints()
00235 {
00236 }
00237 
00238 void Mesh3DNode::drawFaces()
00239 {
00240 }
00241 
00242 void Mesh3DNode::drawWireFrame()
00243 {
00244 }
00245 
00246 //=============================================================================
00247 void Mesh3DNode::loadToGPU()
00248 {
00249   // setup opengl buffer objects
00250   glGenBuffersARB(1, &position_buffer);
00251   glBindBufferARB(GL_ARRAY_BUFFER_ARB, position_buffer);
00252   glBufferDataARB(GL_ARRAY_BUFFER_ARB, position_size, position_data, GL_STATIC_DRAW_ARB);
00253   glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
00254 
00255   glGenBuffersARB(1, &normal_buffer);
00256   glBindBufferARB(GL_ARRAY_BUFFER_ARB, normal_buffer);
00257   glBufferDataARB(GL_ARRAY_BUFFER_ARB, normal_size, normal_data, GL_STATIC_DRAW_ARB);
00258   glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
00259 
00260   if(has_texture) {
00261     glGenBuffersARB(1, &texture_coordinate_buffer);
00262     glBindBufferARB(GL_ARRAY_BUFFER_ARB, texture_coordinate_buffer);
00263     glBufferDataARB(GL_ARRAY_BUFFER_ARB, texture_coordinate_size, texture_coordinate_data, GL_STATIC_DRAW_ARB);
00264     glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
00265   }
00266 
00267   glGenBuffersARB(1, &color_buffer);
00268   glBindBufferARB(GL_ARRAY_BUFFER_ARB, color_buffer);
00269   glBufferDataARB(GL_ARRAY_BUFFER_ARB, color_size, color_data, GL_STATIC_DRAW_ARB);
00270   glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
00271 
00272   glGenBuffersARB(1, &index_buffer);
00273   glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, index_buffer);
00274   glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, index_size, index_data, GL_STATIC_DRAW_ARB);
00275   glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
00276 
00277   if (has_texture && texture.texture_id==0) {
00278     texture.fromImage(teximage);
00279     teximage.reset();
00280   }
00281 
00282   m_is_initialized = true;
00283 }
00284 
00285 void Mesh3DNode::unloadGPU()
00286 {
00287   if(m_is_initialized) {
00288     glDeleteBuffersARB(1, &position_buffer);
00289     glDeleteBuffersARB(1, &normal_buffer);
00290     if(has_texture)
00291       glDeleteBuffersARB(1, &texture_coordinate_buffer);
00292     glDeleteBuffersARB(1, &color_buffer);
00293     glDeleteBuffersARB(1, &index_buffer);
00294     m_is_initialized=false;
00295   }
00296 }
00297 
00298 void Mesh3DNode::bindVBO()
00299 {
00300   if(!m_params->highlite) {
00301     switch(m_params->color_mode)
00302     {
00303     case Parameters::NO_COLOR:
00304       glDisable(GL_TEXTURE_2D);
00305       break;
00306     case Parameters::VERTEX_COLOR:
00307       glDisable(GL_TEXTURE_2D);
00308       glEnableClientState(GL_COLOR_ARRAY);
00309       break;
00310     case Parameters::TEXTURE_COLOR:
00311       if(has_texture) glEnableClientState(GL_TEXTURE_COORD_ARRAY);
00312       glEnable(GL_TEXTURE_2D);
00313       texture.bind();
00314       break;
00315     }
00316   }
00317   glEnableClientState(GL_VERTEX_ARRAY);
00318   glEnableClientState(GL_INDEX_ARRAY);
00319 
00320   // bind vertex normal buffer
00321   glBindBufferARB(GL_ARRAY_BUFFER_ARB, normal_buffer);
00322   glNormalPointer(GL_FLOAT, 0, 0);
00323 
00324   if(!m_params->highlite) {
00325     // bind vertex colors
00326     switch(m_params->color_mode)
00327     {
00328     case Parameters::NO_COLOR:
00329       glColor3f(101.0/255.0,144.0/255.0,191.0/255.0);
00330       break;
00331     case Parameters::VERTEX_COLOR:
00332       glBindBufferARB(GL_ARRAY_BUFFER_ARB, color_buffer);
00333       glColorPointer(3, GL_UNSIGNED_BYTE, 0, 0);
00334       break;
00335     case Parameters::TEXTURE_COLOR:
00336       if(has_texture) {
00337         glBindBufferARB(GL_ARRAY_BUFFER_ARB, texture_coordinate_buffer);
00338         glTexCoordPointer(2, GL_FLOAT, 0, 0);
00339         glColor3f(1,1,1);
00340       }
00341       break;
00342     }
00343   }
00344 
00345   // bind vertex buffer
00346   glBindBufferARB(GL_ARRAY_BUFFER_ARB, position_buffer);
00347   glVertexPointer(3, GL_FLOAT, 0, 0);
00348 
00349   // bind face buffer
00350   glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, index_buffer);
00351   glIndexPointer(GL_INT, 0, 0);
00352 }
00353 
00354 void Mesh3DNode::unbindVBO()
00355 {
00356   glDisableClientState(GL_NORMAL_ARRAY);
00357   if(!m_params->highlite) {
00358     switch(m_params->color_mode)
00359     {
00360     case Parameters::NO_COLOR:
00361       break;
00362     case Parameters::VERTEX_COLOR:
00363       glDisableClientState(GL_COLOR_ARRAY);
00364       break;
00365     case Parameters::TEXTURE_COLOR:
00366       if(has_texture) glDisableClientState(GL_TEXTURE_COORD_ARRAY);
00367       texture.unbind();
00368       glDisable(GL_TEXTURE);
00369       break;
00370     }
00371   }
00372   glDisableClientState(GL_VERTEX_ARRAY);
00373   glDisableClientState(GL_INDEX_ARRAY);
00374 }
00375 
00376 } // namespace puma
00377 
00378 //=============================================================================


rtc
Author(s): Benjamin Pitzer
autogenerated on Mon Oct 6 2014 10:07:35