$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 .......: MeshSet3DNode.cpp 00012 * authors ....: Benjamin Pitzer 00013 * organization: Robert Bosch LLC 00014 * creation ...: 09/04/2008 00015 * modified ...: $Date: 2009-09-10 19:23:45 -0700 (Thu, 10 Sep 2009) $ 00016 * changed by .: $Author: benjaminpitzer $ 00017 * revision ...: $Revision: 917 $ 00018 */ 00019 00020 //== INCLUDES ================================================================== 00021 #include <GL/glew.h> 00022 #include <GL/gl.h> 00023 #include <GL/glu.h> 00024 #include "rtc/rtcRenderer.h" 00025 #include "rtc/rtcBoundingBox.h" 00026 #include "rtc/rtcMesh3DNode.h" 00027 #include "rtc/rtcMeshSet3DNode.h" 00028 00029 //== NAMESPACES ================================================================ 00030 namespace rtc { 00031 00032 //== IMPLEMENTATION ============================================================ 00033 00034 // constructor 00035 MeshSet3DNode::MeshSet3DNode(Renderer* renderer, MeshSet3D* meshset) 00036 : RenderNode(renderer) 00037 { 00038 // setup color table 00039 setupColorTable(); 00040 // initialize 00041 initialize(meshset); 00042 } 00043 00044 MeshSet3DNode::MeshSet3DNode(Renderer* renderer, const char* filename) 00045 : RenderNode(renderer) 00046 { 00047 // setup color table 00048 setupColorTable(); 00049 // initialize 00050 initialize(filename); 00051 } 00052 00053 // destructor 00054 MeshSet3DNode::~MeshSet3DNode() 00055 { 00056 clear(); 00057 } 00058 00059 void MeshSet3DNode::initialize(MeshSet3D* meshset) 00060 { 00061 RenderNode::initialize(); 00062 for (unsigned int i=0;i<meshset->meshes.size();i++) { 00063 Mesh3D* mesh = meshset->meshes[i]; 00064 Mesh3DNode* node = new Mesh3DNode(m_renderer,mesh); 00065 Transformf transform = meshset->meshes[i]->pose.getTransform(); 00066 node->setTransform(transform); 00067 nodes.push_back(node); 00068 } 00069 } 00070 00071 void MeshSet3DNode::initialize(const char* filename) 00072 { 00073 size_t res = 0; 00074 int flags; 00075 Point3Df topleft,bottomright; 00076 int num_meshes; 00077 RenderNode::initialize(); 00078 00079 FILE *fp=fopen(filename,"rb"); 00080 if (fp==NULL) rtc_die("Cannot open file %s\n",filename); 00081 00082 // read meshset header 00083 res+=fread(&flags,sizeof(flags),1,fp); 00084 res+=fread(&num_meshes,sizeof(num_meshes),1,fp); 00085 // load meshes 00086 for(int i=0;i<num_meshes;++i) { 00087 Mesh3D* mesh = new Mesh3D(); 00088 mesh->read(fp); 00089 Mesh3DNode* node = new Mesh3DNode(m_renderer,mesh); 00090 Transformf transform = mesh->pose.getTransform(); 00091 node->setTransform(transform); 00092 nodes.push_back(node); 00093 // free memory 00094 delete mesh; 00095 } 00096 // don't have to read AABB here 00097 fclose(fp); 00098 } 00099 00100 void MeshSet3DNode::rebuild(MeshSet3D* meshset) 00101 { 00102 clear(); 00103 initialize(meshset); 00104 } 00105 00106 void MeshSet3DNode::rebuild(const char* filename) 00107 { 00108 clear(); 00109 initialize(filename); 00110 } 00111 00112 void MeshSet3DNode::setupColorTable() 00113 { 00114 // Define reference colors used for drawing meshes, etc. 00115 ref_colors.setSize(7); 00116 float a = 0.6f; 00117 // ref_colors(0) = Vec4<float>(a,a,a,1.0); 00118 // ref_colors(1) = Vec4<float>(0.5*a,a,a,1.0); 00119 ref_colors(0) = Vec4<float>(101.0/255.0,144.0/255.0,191.0/255.0,1.0); 00120 ref_colors(1) = Vec4<float>(211.0/255.0,228.0/255.0,191.0/255.0,1.0); 00121 // ref_colors(0) = Vec4<float>(150.0/255.0,215.0/255.0,150.0/255.0,1.0); 00122 // ref_colors(1) = Vec4<float>(101.0/255.0,144.0/255.0,191.0/255.0,1.0); 00123 ref_colors(2) = Vec4<float>(a,0.5*a,a,1.0); 00124 ref_colors(3) = Vec4<float>(a,a,0.5*a,1.0); 00125 ref_colors(4) = Vec4<float>(0.5*a,0.5*a,a,1.0); 00126 ref_colors(5) = Vec4<float>(0.5*a,a,0.5*a,1.0); 00127 ref_colors(6) = Vec4<float>(a,0.5*a,0.5*a,1.0); 00128 } 00129 00130 void MeshSet3DNode::clear() 00131 { 00132 RenderNode::clear(); 00133 for (unsigned int i=0;i<nodes.size();i++) delete nodes[i]; 00134 nodes.clear(); 00135 } 00136 00137 void MeshSet3DNode::render() 00138 { 00139 for (unsigned int i=0;i<nodes.size();i++) { 00140 nodes[i]->preRender(); 00141 00142 if(nodes[i]->getParameters().highlite) 00143 glColor3f(1.0,1.0,0.0); // yellow 00144 else { 00145 switch(nodes[i]->getParameters().color_mode) 00146 { 00147 case RenderNode::Parameters::NO_COLOR: 00148 glColor3f(1,1,1); 00149 break; 00150 case RenderNode::Parameters::VERTEX_COLOR: 00151 glColor3f(1,1,1); 00152 break; 00153 case RenderNode::Parameters::TEXTURE_COLOR: 00154 glColor3f(1,1,1); 00155 break; 00156 default: 00157 glColor3f(1,1,1); 00158 break; 00159 } 00160 } 00161 00162 nodes[i]->render(); 00163 nodes[i]->postRender(); 00164 } 00165 } 00166 00167 void MeshSet3DNode::preRender() 00168 { 00169 RenderNode::preRender(); 00170 m_renderer->switchTo3DMode(); 00171 } 00172 00173 void MeshSet3DNode::postRender() 00174 { 00175 RenderNode::postRender(); 00176 } 00177 00178 void MeshSet3DNode::displayFaces(bool b) 00179 { 00180 RenderNode::displayFaces(b); 00181 for (unsigned int i=0;i<nodes.size();i++) { 00182 nodes[i]->displayFaces(b); 00183 } 00184 } 00185 00186 void MeshSet3DNode::displayVertices(bool b) 00187 { 00188 RenderNode::displayVertices(b); 00189 for (unsigned int i=0;i<nodes.size();i++) { 00190 nodes[i]->displayVertices(b); 00191 } 00192 } 00193 00194 void MeshSet3DNode::displayWireframe(bool b) 00195 { 00196 RenderNode::displayWireframe(b); 00197 for (unsigned int i=0;i<nodes.size();i++) { 00198 nodes[i]->displayWireframe(b); 00199 } 00200 } 00201 00202 void MeshSet3DNode::setColorMode(Parameters::ColorMode b) 00203 { 00204 RenderNode::setColorMode(b); 00205 for (unsigned int i=0;i<nodes.size();i++) { 00206 nodes[i]->setColorMode(b); 00207 } 00208 } 00209 00210 // draws n-th mesh in a highlite color 00211 void MeshSet3DNode::highliteMesh(int n) 00212 { 00213 for (unsigned int i=0;i<nodes.size();i++) { 00214 nodes[i]->setHighlite(false); 00215 } 00216 if(n>=0 && n<(int)nodes.size()) 00217 nodes[n]->setHighlite(true); 00218 } 00219 00220 // get the n-th child 00221 Mesh3DNode* MeshSet3DNode::getChild(int n) 00222 { 00223 return nodes[n]; 00224 } 00225 00226 void MeshSet3DNode::unloadGPU() 00227 { 00228 for (size_t i=0;i<nodes.size();i++) 00229 nodes[i]->unloadGPU(); 00230 } 00231 void MeshSet3DNode::loadToGPU() 00232 { 00233 for (size_t i=0;i<nodes.size();i++) 00234 nodes[i]->loadToGPU(); 00235 } 00236 //============================================================================== 00237 } // namespace puma 00238 //==============================================================================