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