$search
00001 00002 #include <blort/Tracker/TrackerModel.h> 00003 #include <blort/Tracker/Resources.h> 00004 00005 using namespace std; 00006 using namespace Tracking; 00007 using namespace TomGine; 00008 00009 // *** PUBLIC *** 00010 00011 // Constructors 00012 TrackerModel::TrackerModel(){ 00013 m_tex_original = 0; 00014 m_texture = 0; 00015 m_textured = false; 00016 m_bfc = false; 00017 m_boundingSphereRadius = 0.0; 00018 m_shadeTexturingID = 0; 00019 m_dlTexturedFaces = 0; 00020 m_dlUntexturedFaces = 0; 00021 m_dlPass = 0; 00022 m_dlFaces = 0; 00023 m_dlEdges = 0; 00024 m_dlNormals = 0; 00025 00026 if((m_shadeTexturingID = g_Resources->AddShader("texturing", "texturing.vert", "texturing.frag")) == -1) 00027 exit(1); 00028 m_shadeTexturing = g_Resources->GetShader(m_shadeTexturingID); 00029 } 00030 00031 TrackerModel::~TrackerModel(){ 00032 releasePassList(); 00033 g_Resources->ReleaseShader(m_shadeTexturingID); 00034 00035 if(m_texture) delete(m_texture); 00036 if(m_tex_original) delete(m_tex_original); 00037 00038 if(glIsList(m_dlTexturedFaces)) glDeleteLists(m_dlTexturedFaces, 1); 00039 if(glIsList(m_dlUntexturedFaces)) glDeleteLists(m_dlUntexturedFaces, 1); 00040 if(glIsList(m_dlPass)) glDeleteLists(m_dlPass, 1); 00041 if(glIsList(m_dlFaces)) glDeleteLists(m_dlFaces, 1); 00042 if(glIsList(m_dlEdges)) glDeleteLists(m_dlEdges, 1); 00043 if(glIsList(m_dlNormals)) glDeleteLists(m_dlNormals, 1); 00044 00045 } 00046 00047 TrackerModel& TrackerModel::operator=(const TrackerModel& m){ 00048 m_vertices = m.m_vertices; 00049 m_faces = m.m_faces; 00050 m_lines = m.m_lines; 00051 m_facepixellist = m.m_facepixellist; 00052 00053 for(unsigned i=0; i<m.m_passlist.size(); i++){ 00054 Pass* p = new(Pass); 00055 p->f = m.m_passlist[i]->f; 00056 p->modelviewprojection = m.m_passlist[i]->modelviewprojection; 00057 p->x = m.m_passlist[i]->x; 00058 p->y = m.m_passlist[i]->y; 00059 p->w = m.m_passlist[i]->w; 00060 p->h = m.m_passlist[i]->h; 00061 00062 // Copy Texture 00063 if(m.m_passlist[i]->texture){ 00064 int w = m.m_passlist[i]->texture->getWidth(); 00065 int h = m.m_passlist[i]->texture->getHeight(); 00066 int ipw = g_Resources->GetImageProcessor()->getWidth() >> 1; 00067 int iph = g_Resources->GetImageProcessor()->getHeight() >> 1; 00068 g_Resources->GetImageProcessor()->render(m.m_passlist[i]->texture, -int(w>>1),-int(h>>1),w,h); 00069 p->texture->copyTexImage2D(ipw-int(w>>1),iph-int(h>>1),w,h); 00070 } 00071 m_passlist.push_back(p); 00072 } 00073 00074 Update(); 00075 return (*this); 00076 } 00077 00078 TrackerModel::TrackerModel(const TomGine::tgModel& m) 00079 : TomGine::tgModel(m) 00080 { 00081 m_tex_original = 0; 00082 m_texture = 0; 00083 m_textured = false; 00084 m_bfc = false; 00085 m_boundingSphereRadius = 0.0; 00086 m_shadeTexturingID = 0; 00087 m_dlTexturedFaces = 0; 00088 m_dlUntexturedFaces = 0; 00089 m_dlPass = 0; 00090 m_dlFaces = 0; 00091 m_dlEdges = 0; 00092 m_dlNormals = 0; 00093 00094 if((m_shadeTexturingID = g_Resources->AddShader("texturing", "texturing.vert", "texturing.frag")) == -1) 00095 exit(1); 00096 m_shadeTexturing = g_Resources->GetShader(m_shadeTexturingID); 00097 00098 m_lines.clear(); 00099 m_passlist.clear(); 00100 m_facepixellist.assign(m_faces.size(), 0); 00101 00102 computeEdges(); 00103 Update(); 00104 } 00105 00106 void TrackerModel::releasePassList(){ 00107 for(unsigned i=0; i<m_passlist.size(); i++){ 00108 delete(m_passlist[i]); 00109 } 00110 m_passlist.clear(); 00111 m_facepixellist.assign(m_faces.size(), 0); 00112 m_textured = false; 00113 // UpdateDisplayLists(); 00114 } 00115 00116 // computes, updates 00117 void TrackerModel::computeEdges(){ 00118 int i,j; 00119 00120 // Extract edges from faces 00121 for(i=0; i<(int)m_faces.size(); i++){ 00122 for(j=0; j<(int)m_faces[i].v.size(); j++){ 00123 tgLine e; 00124 e.start = m_vertices[m_faces[i].v[j]].pos; 00125 e.end = m_vertices[m_faces[i].v[(j+1)%m_faces[i].v.size()]].pos; 00126 if(!isRedundant(&e)){ 00127 //ROS_DEBUG("Edge: %d %d\n", m_edgelist[k].start, m_edgelist[k].end); 00128 m_lines.push_back(e); 00129 } 00130 } 00131 } 00132 } 00133 00134 void TrackerModel::computeBoundingSphere(){ 00135 m_boundingSphereRadius = 0.0; 00136 00137 float r = 0.0; 00138 vec3 v; 00139 00140 for(unsigned i=0; i<m_vertices.size(); i++){ 00141 v = m_vertices[i].pos; 00142 r = sqrt( pow(v.x,2) + pow(v.y,2) + pow(v.z,2) ); 00143 if(r>m_boundingSphereRadius) 00144 m_boundingSphereRadius = r; 00145 } 00146 } 00147 00148 void TrackerModel::Update(){ 00149 unsigned i,j; 00150 if(m_facepixellist.size() != m_faces.size()) 00151 m_facepixellist.assign(m_faces.size(), 0); 00152 00153 for(i=0; i<m_passlist.size(); i++){ 00154 for(j=0; j<m_passlist[i]->f.size(); j++){ 00155 m_facepixellist[m_passlist[i]->f[j]] = 1; 00156 } 00157 } 00158 00159 computeBoundingSphere(); 00160 00161 // UpdateDisplayLists(); 00162 } 00163 00164 // draws, prints 00165 void TrackerModel::Print() const{ 00166 tgModel::Print(); 00167 for(unsigned i=0; i<m_lines.size(); i++){ 00168 ROS_DEBUG("Edge %i: %f %f %f, %f %f %f\n", i, 00169 m_lines[i].start.x, m_lines[i].start.y, m_lines[i].start.z, 00170 m_lines[i].end.x, m_lines[i].end.y, m_lines[i].end.z); 00171 } 00172 } 00173 00174 void TrackerModel::drawNormals(){ 00175 genListNormals(0.01f); 00176 // glCallList(m_dlNormals); 00177 } 00178 00179 void TrackerModel::drawTexturedFaces(){ 00180 if(!m_passlist.empty()){ 00181 genListTexturedFaces(); 00182 // glCallList(m_dlTexturedFaces); 00183 } 00184 } 00185 00186 void TrackerModel::drawUntexturedFaces(){ 00187 genListUntexturedFaces(); 00188 // glCallList(m_dlUntexturedFaces); 00189 } 00190 00191 void TrackerModel::drawPass(bool colorful){ 00192 genListPass(colorful); 00193 // glCallList(m_dlPass); 00194 } 00195 00196 void TrackerModel::drawFaces(bool colorful){ 00197 if(m_texture) m_texture->bind(); 00198 00199 genListFaces(colorful); 00200 // glCallList(m_dlFaces); 00201 00202 if(m_texture) glDisable(GL_TEXTURE_2D); 00203 } 00204 00205 void TrackerModel::drawEdges(){ 00206 genListEdges(); 00207 // glCallList(m_dlEdges); 00208 } 00209 00210 void TrackerModel::drawFace(int i){ 00211 int j; 00212 tgFace* f; 00213 00214 if(m_texture){ 00215 glActiveTexture(GL_TEXTURE0); 00216 glEnable(GL_TEXTURE_2D); 00217 m_texture->bind(); 00218 } 00219 00220 f = &m_faces[i]; 00221 if((int)f->v.size() == 3) 00222 glBegin(GL_TRIANGLES); 00223 else if(f->v.size() == 4) 00224 glBegin(GL_QUADS); 00225 else 00226 ROS_DEBUG("[TrackerModel::drawFaces] Warning unsupported face structure"); 00227 00228 for(j=0; j<(int)f->v.size(); j++){ 00229 glTexCoord2f(m_vertices[f->v[j]].texCoord.x, m_vertices[f->v[j]].texCoord.y); 00230 glNormal3f(m_vertices[f->v[j]].normal.x, m_vertices[f->v[j]].normal.y, m_vertices[f->v[j]].normal.z); 00231 glVertex3f(m_vertices[f->v[j]].pos.x, m_vertices[f->v[j]].pos.y, m_vertices[f->v[j]].pos.z); 00232 } 00233 00234 glEnd(); 00235 00236 if(m_texture){ 00237 glDisable(GL_TEXTURE_2D); 00238 } 00239 } 00240 00241 void TrackerModel::drawCoordinates(){ 00242 glDisable(GL_DEPTH_TEST); 00243 glDisable(GL_CULL_FACE); 00244 00245 float l1 = 0.06f; 00246 float l2 = 0.02f; 00247 float b1 = 0.001f; 00248 float b2 = 0.003f; 00249 00250 // X - Axis 00251 glPushMatrix(); 00252 glColor3f(1.0,0.0,0.0); 00253 glBegin(GL_TRIANGLE_FAN); 00254 glVertex3f(l1, 0.0, b1); 00255 glVertex3f(0.0, 0.0, b1); 00256 glVertex3f(0.0, 0.0, -b1); 00257 glVertex3f(l1, 0.0, -b1); 00258 glVertex3f(l1, 0.0, -b1-b2); 00259 glVertex3f(l1+l2, 0.0, 0.0); 00260 glVertex3f(l1, 0.0, b1+b2); 00261 glEnd(); 00262 glPopMatrix(); 00263 00264 // Y - Axis 00265 glPushMatrix(); 00266 glColor3f(0.0,1.0,0.0); 00267 glRotatef(90, 0.0, 0.0, 1.0); 00268 glBegin(GL_TRIANGLE_FAN); 00269 glVertex3f(l1, 0.0, b1); 00270 glVertex3f(0.0, 0.0, b1); 00271 glVertex3f(0.0, 0.0, -b1); 00272 glVertex3f(l1, 0.0, -b1); 00273 glVertex3f(l1, 0.0, -b1-b2); 00274 glVertex3f(l1+l2, 0.0, 0.0); 00275 glVertex3f(l1, 0.0, b1+b2); 00276 glEnd(); 00277 glPopMatrix(); 00278 00279 // Z - Axis 00280 glPushMatrix(); 00281 glColor3f(0.0,0.0,1.0); 00282 glRotatef(-90, 0.0, 1.0, 0.0); 00283 glBegin(GL_TRIANGLE_FAN); 00284 glVertex3f(l1, 0.0, b1); 00285 glVertex3f(0.0, 0.0, b1); 00286 glVertex3f(0.0, 0.0, -b1); 00287 glVertex3f(l1, 0.0, -b1); 00288 glVertex3f(l1, 0.0, -b1-b2); 00289 glVertex3f(l1+l2, 0.0, 0.0); 00290 glVertex3f(l1, 0.0, b1+b2); 00291 glEnd(); 00292 glPopMatrix(); 00293 } 00294 00295 // counts pixels of each face 00296 // if pixels of face are > than in any previouse view 00297 // set update flag = true 00298 vector<unsigned> TrackerModel::getFaceUpdateList(tgPose& p_max, vec3 view, float minTexGrabAngle, bool use_num_pixels){ 00299 int i, n; 00300 vector<unsigned> faceUpdateList; 00301 float alpha; 00302 vec3 vT; 00303 mat3 mR; 00304 p_max.GetPose(mR, vT); 00305 view.normalize(); 00306 00307 p_max.Activate(); 00308 00309 // count pixels for each face and choose if its texture has to be updated 00310 00311 unsigned int* queryPixels; // Occlussion query for counting pixels 00312 queryPixels = (unsigned int*)malloc( sizeof(unsigned int) * m_faces.size() ); 00313 glGenQueriesARB(m_faces.size(), queryPixels); 00314 for(i=0; i<(int)m_faces.size(); i++){ 00315 glBeginQueryARB(GL_SAMPLES_PASSED_ARB, queryPixels[i]); 00316 00317 drawFace(i); 00318 00319 glEndQueryARB(GL_SAMPLES_PASSED_ARB); 00320 } 00321 00322 for(i=0; i<(int)m_faces.size(); i++){ 00323 00324 glGetQueryObjectivARB(queryPixels[i], GL_QUERY_RESULT_ARB, &n); 00325 00326 vec3 vn = mR * m_faces[i].normal; 00327 alpha = acos(vn*view); 00328 00329 if(alpha>minTexGrabAngle){ 00330 if(use_num_pixels){ 00331 if(m_facepixellist[i]==0){ 00332 faceUpdateList.push_back(i); 00333 m_facepixellist[i] = n; 00334 } 00335 }else{ 00336 faceUpdateList.push_back(i); 00337 m_facepixellist[i] = n; 00338 } 00339 } 00340 00341 } 00342 p_max.Deactivate(); 00343 00344 glDeleteQueriesARB(m_faces.size(), queryPixels); 00345 free(queryPixels); 00346 00347 return faceUpdateList; 00348 } 00349 00350 void TrackerModel::getBoundingBox2D( int width, int height, TomGine::tgPose& p_max, TomGine::tgCamera* m_cam, 00351 int &minX, int &maxX, int &minY, int &maxY) 00352 { 00353 // Extract transformation matrix (from object to image space) 00354 mat4 modelview, projection, modelviewprojection; 00355 mat3 rot; 00356 vec3 pos; 00357 p_max.GetPose(rot,pos); 00358 mat4 matModel = mat4(rot); 00359 matModel[12]=pos.x; matModel[13]=pos.y; matModel[14]=pos.z; 00360 mat4 matView = m_cam->GetExtrinsic(); 00361 projection = m_cam->GetIntrinsic(); 00362 modelview = matView * matModel; 00363 modelviewprojection = projection * modelview; 00364 00365 // calculate bounding rectangle 00366 vec4 texCoords; 00367 vec3 p; 00368 float x_min=(float)width; 00369 float y_min=(float)height; 00370 float x_max=0.0; 00371 float y_max=0.0; 00372 float x,y; 00373 for(unsigned j=0; j<m_vertices.size(); j++){ 00374 p = m_vertices[j].pos; 00375 texCoords = modelviewprojection * vec4(p.x, p.y, p.z, 1.0); 00376 x = (texCoords.x / texCoords.w + 1.0f) * 0.5f; 00377 y = (texCoords.y / texCoords.w + 1.0f) * 0.5f; 00378 if(x<x_min) x_min = x; 00379 if(y<y_min) y_min = y; 00380 if(x>x_max) x_max = x; 00381 if(y>y_max) y_max = y; 00382 } 00383 00384 // Calculate bounding rectangle in pixels 00385 minX = (int)round(x_min * width); 00386 maxX = (int)round(x_max * width); 00387 minY = (int)round(y_min * height); 00388 maxY = (int)round(y_max * height); 00389 } 00390 00391 void TrackerModel::textureFromImage(Texture* image, 00392 int width, int height, 00393 tgPose& p_max, 00394 vec3 view, 00395 float minTexGrabAngle, 00396 std::vector<unsigned> faceUpdateList, 00397 std::vector<tgVertex> &vertices, 00398 TomGine::tgCamera* m_cam) 00399 { 00400 unsigned i,j; 00401 int k; 00402 vec4 texcoords_model; 00403 vec4 vertex; 00404 //double mv[16]; 00405 //double pj[16]; 00406 mat4 modelview, projection, modelviewprojection; 00407 00408 // add new rendering pass 00409 Pass* newpass = new Pass; 00410 00411 // query modelview and projection matrix 00412 // p_max.activate(); 00413 // glGetDoublev(GL_MODELVIEW_MATRIX, mv); // TODO to inaccurate, replace with modelview of p_max and projection of camera 00414 // glGetDoublev(GL_PROJECTION_MATRIX, pj); 00415 // p_max.deactivate(); 00416 // for(i=0; i<16; i++){ 00417 // modelview.mat[i] = (float)mv[i]; 00418 // projection.mat[i] = (float)pj[i]; 00419 // } 00420 // newpass->modelviewprojection = projection * modelview; 00421 mat3 rot; 00422 vec3 pos; 00423 p_max.GetPose(rot,pos); 00424 mat4 matModel = mat4(rot); 00425 matModel[12]=pos.x; matModel[13]=pos.y; matModel[14]=pos.z; 00426 mat4 matView = m_cam->GetExtrinsic(); 00427 projection = m_cam->GetIntrinsic(); 00428 modelview = matView * matModel; 00429 modelviewprojection = projection * modelview; 00430 newpass->modelviewprojection = modelviewprojection; 00431 00432 00433 // calculate bounding rectangle 00434 vec4 texCoords; 00435 vec3 p; 00436 tgVertex v; 00437 float x_min=(float)width; 00438 float y_min=(float)height; 00439 float x_max=0.0; 00440 float y_max=0.0; 00441 float x,y; 00442 float w2 = float(width>>1); 00443 float h2 = float(height>>1); 00444 for(i=0; i<faceUpdateList.size(); i++){ 00445 k = faceUpdateList[i]; 00446 for(j=0; j<m_faces[k].v.size(); j++){ 00447 p = m_vertices[m_faces[k].v[j]].pos; 00448 texCoords = modelviewprojection * vec4(p.x, p.y, p.z, 1.0); 00449 x = (texCoords.x / texCoords.w + 1.0f) * 0.5f; 00450 y = (texCoords.y / texCoords.w + 1.0f) * 0.5f; 00451 m_vertices[m_faces[k].v[j]].texCoord.x = x; 00452 m_vertices[m_faces[k].v[j]].texCoord.y = y; 00453 if(x<x_min) x_min = x; 00454 if(y<y_min) y_min = y; 00455 if(x>x_max) x_max = x; 00456 if(y>y_max) y_max = y; 00457 } 00458 } 00459 00460 // Store bounding box 00461 newpass->x = x_min; 00462 newpass->w = x_max-x_min; 00463 newpass->y = y_min; 00464 newpass->h = y_max-y_min; 00465 00466 // Calculate bounding rectangle in pixels 00467 x_min = (x_min * width); 00468 x_max = (x_max * width); 00469 y_min = (y_min * height); 00470 y_max = (y_max * height); 00471 00472 v.pos.x = x_min - w2; 00473 v.pos.y = y_min - h2; 00474 vertices.push_back(v); 00475 v.pos.x = x_max - w2; 00476 v.pos.y = y_min - h2; 00477 vertices.push_back(v); 00478 v.pos.x = x_max - w2; 00479 v.pos.y = y_max - h2; 00480 vertices.push_back(v); 00481 v.pos.x = x_min - w2; 00482 v.pos.y = y_max - h2; 00483 vertices.push_back(v); 00484 00485 // store texture 00486 newpass->texture->copyFromTexture(image, (int)x_min, (int)y_min, unsigned(x_max-x_min), unsigned(y_max-y_min)); 00487 00488 // add faces to pass 00489 newpass->f = faceUpdateList; 00490 00491 m_passlist.push_back(newpass); 00492 00493 // clean up passes 00494 std::vector<unsigned int> texturedfaces; 00495 int m=0; 00496 for(m=(m_passlist.size()-1); m>=0; m--){ // parse through passlist topdown 00497 Pass* p = m_passlist[m]; // current pass 00498 bool destroy = true; 00499 00500 for(j=0; j<p->f.size(); j++){ // for each face of pass 00501 bool face_allready_in_use = false; 00502 for(k=0; k<(int)texturedfaces.size(); k++){ 00503 if(p->f[j] == texturedfaces[k]) // compare with each face in usedfaces 00504 face_allready_in_use = true; 00505 } 00506 if(!face_allready_in_use){ 00507 texturedfaces.push_back(p->f[j]); 00508 destroy=false; 00509 } 00510 } 00511 00512 if(destroy){ 00513 delete(p); 00514 m_passlist.erase(m_passlist.begin()+m); 00515 } 00516 } 00517 00518 if(!m_passlist.empty()) 00519 m_textured = true; 00520 00521 // UpdateDisplayLists(); 00522 } 00523 00524 void TrackerModel::useTexCoords(bool useTC){ 00525 00526 m_shadeTexturing->bind(); 00527 m_shadeTexturing->setUniform("useTexCoords",useTC); 00528 m_shadeTexturing->unbind(); 00529 00530 } 00531 00532 void TrackerModel::unwarpTexturesBox_hacky(const char* name){ 00533 00534 char charbuffer[8]; 00535 Texture tex; 00536 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 00537 00538 for(unsigned p=0; p<m_passlist.size(); p++){ 00539 00540 if(m_passlist[p]->f.size() != 1){ 00541 ROS_DEBUG("[TrackerModel::unwarpTexturesBox_hacky] Warning no more than one face per pass allowed\n"); 00542 return; 00543 } 00544 00545 tgFace* f = &m_faces[m_passlist[p]->f[0]]; 00546 if(f->v.size()!=4){ 00547 ROS_DEBUG("[TrackerModel::unwarpTexturesBox_hacky] Warning only quad faces allowed\n"); 00548 return; 00549 } 00550 00551 ImageProcessor *ip = g_Resources->GetImageProcessor(); 00552 ip->setCamOrtho(); 00553 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 00554 00555 float x = m_passlist[p]->h * ip->getHeight() * 0.75; 00556 float y = m_passlist[p]->w * ip->getWidth() * 0.75; 00557 float z = 0.0; 00558 float nz = 1.0; 00559 00560 glEnable(GL_TEXTURE_2D); 00561 00562 m_passlist[p]->texture->bind(); 00563 00564 glBegin(GL_QUADS); 00565 unsigned j=0; 00566 glTexCoord2f(m_vertices[f->v[j]].texCoord.x, m_vertices[f->v[j]].texCoord.y); 00567 glNormal3f(0.0, 0.0, nz); 00568 glVertex3f(-x, -y, z); 00569 00570 j=1; 00571 glTexCoord2f(m_vertices[f->v[j]].texCoord.x, m_vertices[f->v[j]].texCoord.y); 00572 glNormal3f(0.0, 0.0, nz); 00573 glVertex3f( x, -y, z); 00574 00575 j=2; 00576 glTexCoord2f(m_vertices[f->v[j]].texCoord.x, m_vertices[f->v[j]].texCoord.y); 00577 glNormal3f(0.0, 0.0, nz); 00578 glVertex3f( x, y, z); 00579 00580 j=3; 00581 glTexCoord2f(m_vertices[f->v[j]].texCoord.x, m_vertices[f->v[j]].texCoord.y); 00582 glNormal3f(0.0, 0.0, nz); 00583 glVertex3f(-x, y, z); 00584 glEnd(); 00585 00586 int ix = (ip->getWidth() >> 1) - (int)x; 00587 int iy = (ip->getHeight() >> 1) - (int)y; 00588 00589 tex.copyTexImage2D(ix, iy, int(x)<<1, int(y)<<1); 00590 00591 std::string texname = std::string(name); 00592 texname.append("-unwrap-"); 00593 sprintf(charbuffer, "%.5d", p); 00594 texname.append(charbuffer); 00595 texname.append(".jpg"); 00596 tex.save(texname.c_str()); 00597 00598 } 00599 00600 } 00601 00602 // *** PROTECTED *** 00603 00604 // Tests redundancy of edge 00605 bool TrackerModel::isRedundant(TomGine::tgLine* e1){ 00606 tgLine* e2; 00607 vec3 vs, ve; 00608 00609 for(int i=0; i<(int)m_lines.size(); i++){ 00610 e2 = &m_lines[i]; 00611 00612 // Get Vector between start-start and end-end points of edges 00613 vs = e1->start - e2->start; 00614 ve = e1->end - e2->end; 00615 // vs = vec3( m_vertices[e1->start].x - m_vertices[e2->start].pos.x, 00616 // m_vertices[e1->start].pos.y - m_vertices[e2->start].pos.y, 00617 // m_vertices[e1->start].pos.z - m_vertices[e2->start].pos.z); 00618 // ve = vec3( m_vertices[e1->end].pos.x - m_vertices[e2->end].pos.x, 00619 // m_vertices[e1->end].pos.y - m_vertices[e2->end].pos.y, 00620 // m_vertices[e1->end].pos.z - m_vertices[e2->end].pos.z); 00621 // if sum of length between vertices is insignificant then redundancy is detected 00622 if(vs.length() + ve.length() < 0.01){ 00623 //ROS_DEBUG("Redundant edge detected: %d %d\n", e1->start, e1->end); 00624 return true; 00625 } 00626 00627 // Get Vector between start-end and end-start points of edges 00628 vs = e1->start - e2->end; 00629 ve = e1->end - e2->start; 00630 // vs = vec3( m_vertices[e1->start].pos.x - m_vertices[e2->end].pos.x, 00631 // m_vertices[e1->start].pos.y - m_vertices[e2->end].pos.y, 00632 // m_vertices[e1->start].pos.z - m_vertices[e2->end].pos.z); 00633 // ve = vec3( m_vertices[e1->end].pos.x - m_vertices[e2->start].pos.x, 00634 // m_vertices[e1->end].pos.y - m_vertices[e2->start].pos.y, 00635 // m_vertices[e1->end].pos.z - m_vertices[e2->start].pos.z); 00636 // if sum of length between vertices is insignificant then redundancy is detected 00637 if(vs.length() + ve.length() < 0.01){ 00638 //ROS_DEBUG("Redundant edge detected: %d %d\n", e1->start, e1->end); 00639 return true; 00640 } 00641 00642 } 00643 return false; 00644 } 00645 00646 // Generate all display lists 00647 void TrackerModel::UpdateDisplayLists(){ 00648 00649 if(glIsList(m_dlTexturedFaces)) glDeleteLists(m_dlTexturedFaces, 1); 00650 if(glIsList(m_dlUntexturedFaces)) glDeleteLists(m_dlUntexturedFaces, 1); 00651 if(glIsList(m_dlPass)) glDeleteLists(m_dlPass, 1); 00652 if(glIsList(m_dlFaces)) glDeleteLists(m_dlFaces, 1); 00653 if(glIsList(m_dlEdges)) glDeleteLists(m_dlEdges, 1); 00654 if(glIsList(m_dlNormals)) glDeleteLists(m_dlNormals, 1); 00655 00656 m_dlTexturedFaces = glGenLists(1); 00657 m_dlUntexturedFaces = glGenLists(1); 00658 m_dlPass = glGenLists(1); 00659 m_dlFaces = glGenLists(1); 00660 m_dlEdges = glGenLists(1); 00661 m_dlNormals = glGenLists(1); 00662 00663 if(!m_passlist.empty()){ 00664 glNewList(m_dlTexturedFaces, GL_COMPILE); 00665 genListTexturedFaces(); 00666 glEndList(); 00667 00668 glNewList(m_dlPass, GL_COMPILE); 00669 genListPass(); 00670 glEndList(); 00671 }else{ 00672 glNewList(m_dlPass, GL_COMPILE); 00673 genListFaces(); 00674 glEndList(); 00675 } 00676 00677 glNewList(m_dlUntexturedFaces, GL_COMPILE); 00678 genListUntexturedFaces(); 00679 glEndList(); 00680 00681 glNewList(m_dlFaces, GL_COMPILE); 00682 genListFaces(); 00683 glEndList(); 00684 00685 glNewList(m_dlEdges, GL_COMPILE); 00686 genListEdges(); 00687 glEndList(); 00688 00689 glNewList(m_dlNormals, GL_COMPILE); 00690 genListNormals(0.01f); 00691 glEndList(); 00692 } 00693 00694 // Display List generators 00695 void TrackerModel::genListTexturedFaces(){ // draw only textured faces 00696 00697 int p,i,j; 00698 tgFace* f; 00699 00700 if(m_bfc) glEnable(GL_CULL_FACE); 00701 else glDisable(GL_CULL_FACE); 00702 00703 for(p=0; p<(int)m_passlist.size(); p++){ 00704 // parse through faces of pass 00705 for(i=0; i<(int)m_passlist[p]->f.size(); i++){ 00706 f = &m_faces[m_passlist[p]->f[i]]; 00707 00708 if(f->v.size() == 3) 00709 glBegin(GL_TRIANGLES); 00710 else if(f->v.size() == 4) 00711 glBegin(GL_QUADS); 00712 else 00713 ROS_DEBUG("[TrackerModel::drawFaces] Warning unsupported face structure"); 00714 00715 for(j=0; j<(int)f->v.size(); j++){ 00716 glTexCoord2f(m_vertices[f->v[j]].texCoord.x, m_vertices[f->v[j]].texCoord.y); 00717 glNormal3f(m_vertices[f->v[j]].normal.x, m_vertices[f->v[j]].normal.y, m_vertices[f->v[j]].normal.z); 00718 glVertex3f(m_vertices[f->v[j]].pos.x, m_vertices[f->v[j]].pos.y, m_vertices[f->v[j]].pos.z); 00719 } 00720 glEnd(); 00721 }//for i 00722 }//for p 00723 } 00724 00725 void TrackerModel::genListUntexturedFaces(){ // draw only untextured faces 00726 int i,j; 00727 tgFace* f; 00728 00729 if(m_bfc) glEnable(GL_CULL_FACE); 00730 else glDisable(GL_CULL_FACE); 00731 00732 for(i=0; i<(int)m_faces.size(); i++){ 00733 00734 f = &m_faces[i]; 00735 if(m_facepixellist[i] == 0){ 00736 00737 if(f->v.size() == 3) 00738 glBegin(GL_TRIANGLES); 00739 else if(f->v.size() == 4) 00740 glBegin(GL_QUADS); 00741 else 00742 ROS_DEBUG("[TrackerModel::drawFaces] Warning unsupported face structure"); 00743 00744 for(j=0; j<(int)f->v.size(); j++){ 00745 glTexCoord2f(m_vertices[f->v[j]].texCoord.x, m_vertices[f->v[j]].texCoord.y); 00746 glNormal3f(m_vertices[f->v[j]].normal.x, m_vertices[f->v[j]].normal.y, m_vertices[f->v[j]].normal.z); 00747 glVertex3f(m_vertices[f->v[j]].pos.x, m_vertices[f->v[j]].pos.y, m_vertices[f->v[j]].pos.z); 00748 } 00749 00750 glEnd(); 00751 }//if 00752 } //for 00753 } 00754 00755 void TrackerModel::genListPass(bool colorful){ // draw faces using passlist and shader for texturing (modelviewmatrix) 00756 int p,i,j; 00757 tgFace* f; 00758 vec2 texCoords; 00759 00760 if(m_bfc) glEnable(GL_CULL_FACE); 00761 else glDisable(GL_CULL_FACE); 00762 00763 glActiveTexture(GL_TEXTURE0); 00764 glEnable(GL_TEXTURE_2D); 00765 glColor3f(1.0,1.0,1.0); 00766 00767 m_shadeTexturing->bind(); 00768 00769 // Draw render passes (textured) 00770 for(p=0; p<(int)m_passlist.size(); p++){ 00771 00772 // bind texture of pass 00773 m_passlist[p]->texture->bind(); 00774 // set modelview matrix for texture 00775 // ROS_DEBUG("x,y,w,h: %f %f %f %f\n", m_passlist[p]->x, m_passlist[p]->y, m_passlist[p]->w, m_passlist[p]->h); 00776 // ROS_DEBUG("face %d: %d %d %d %d\n", m_passlist[p]->f[0], 00777 // m_faces[m_passlist[p]->f[0]].v[0], 00778 // m_faces[m_passlist[p]->f[0]].v[1], 00779 // m_faces[m_passlist[p]->f[0]].v[2], 00780 // m_faces[m_passlist[p]->f[0]].v[3]); 00781 // ROS_DEBUG("modelview: %f %f %f %f\n", m_passlist[p]->modelviewprojection.mat[0], m_passlist[p]->modelviewprojection.mat[1], m_passlist[p]->modelviewprojection.mat[2], m_passlist[p]->modelviewprojection.mat[3]); 00782 00783 m_shadeTexturing->setUniform("modelviewprojection", m_passlist[p]->modelviewprojection, GL_FALSE); 00784 m_shadeTexturing->setUniform("x", m_passlist[p]->x); 00785 m_shadeTexturing->setUniform("y", m_passlist[p]->y); 00786 m_shadeTexturing->setUniform("w", m_passlist[p]->w); 00787 m_shadeTexturing->setUniform("h", m_passlist[p]->h); 00788 00789 // parse through faces of pass 00790 for(i=0; i<(int)m_passlist[p]->f.size(); i++){ 00791 f = &m_faces[m_passlist[p]->f[i]]; 00792 00793 if(f->v.size() == 3) 00794 glBegin(GL_TRIANGLES); 00795 else if(f->v.size() == 4) 00796 glBegin(GL_QUADS); 00797 else 00798 ROS_DEBUG("[TrackerModel::drawFaces] Warning unsupported face structure"); 00799 00800 for(j=0; j<(int)f->v.size(); j++){ 00801 00802 // vec4 v = vec4(m_vertices[f->v[j]].pos.x, m_vertices[f->v[j]].pos.y, m_vertices[f->v[j]].pos.z, 1.0); 00803 // v = m_passlist[p]->modelviewprojection * v; 00804 // texCoords.x = (v.x/v.w + 1.0) * 0.5; 00805 // texCoords.y = (v.y/v.w + 1.0) * 0.5; 00806 // glTexCoord2f(texCoords.x, texCoords.y); 00807 00808 glTexCoord2fv(m_vertices[f->v[j]].texCoord); 00809 glNormal3fv(m_vertices[f->v[j]].normal); 00810 glVertex3fv(m_vertices[f->v[j]].pos); 00811 } 00812 00813 glEnd(); 00814 }//for i 00815 }//for p 00816 00817 glDisable(GL_TEXTURE_2D); 00818 m_shadeTexturing->unbind(); 00819 00820 if(colorful) glDisable(GL_LIGHTING); 00821 vec3 color; 00822 00823 // Draw remaining faces for contours 00824 for(i=0; i<(int)m_faces.size(); i++){ 00825 00826 if(m_facepixellist[i] == 0){ 00827 00828 if(m_faces[i].v.size() == 3) 00829 glBegin(GL_TRIANGLES); 00830 else if(m_faces[i].v.size() == 4) 00831 glBegin(GL_QUADS); 00832 else 00833 ROS_DEBUG("[TrackerModel::drawFaces] Warning unsupported face structure"); 00834 00835 color.random(); 00836 for(j=0; j<(int)m_faces[i].v.size(); j++){ 00837 glNormal3fv(m_vertices[m_faces[i].v[j]].normal); 00838 if(colorful) glColor3fv(color); 00839 glVertex3fv(m_vertices[m_faces[i].v[j]].pos); 00840 // ROS_DEBUG("%f %f %f\n", m_vertices[m_faces[i].v[j]].normal.x, m_vertices[m_faces[i].v[j]].normal.y, m_vertices[m_faces[i].v[j]].normal.z); 00841 } 00842 00843 glEnd(); 00844 }//if 00845 }//for i 00846 } 00847 00848 void TrackerModel::genListFaces(bool colorful){ // draw all faces of model 00849 int i,j; 00850 tgFace* f; 00851 00852 if(m_bfc) glEnable(GL_CULL_FACE); 00853 else glDisable(GL_CULL_FACE); 00854 00855 for(i=0; i<(int)m_faces.size(); i++){ 00856 f = &m_faces[i]; 00857 00858 if((int)f->v.size() == 3) 00859 glBegin(GL_TRIANGLES); 00860 else if((int)f->v.size() == 4) 00861 glBegin(GL_QUADS); 00862 else 00863 ROS_DEBUG("[TrackerModel::drawFaces] Warning unsupported face structure"); 00864 00865 for(j=0; j<(int)f->v.size(); j++){ 00866 glTexCoord2f(m_vertices[f->v[j]].texCoord.x, m_vertices[f->v[j]].texCoord.y); 00867 glNormal3f(m_vertices[f->v[j]].normal.x, m_vertices[f->v[j]].normal.y, m_vertices[f->v[j]].normal.z); 00868 glVertex3f(m_vertices[f->v[j]].pos.x, m_vertices[f->v[j]].pos.y, m_vertices[f->v[j]].pos.z); 00869 } 00870 00871 glEnd(); 00872 } 00873 } 00874 00875 void TrackerModel::genListEdges(){ // draw all edges of model 00876 mat4 mv; 00877 mat3 rot; 00878 vec3 v_cam_object; 00879 float s = -0.001f; // = 1mm 00880 00881 glGetFloatv(GL_MODELVIEW_MATRIX, mv); 00882 00883 //ROS_DEBUG("%f %f %f %f\n", mv[0], mv[4], mv[8], mv[12]); 00884 //ROS_DEBUG("%f %f %f %f\n", mv[1], mv[5], mv[9], mv[13]); 00885 //ROS_DEBUG("%f %f %f %f\n", mv[2], mv[6], mv[10], mv[14]); 00886 //ROS_DEBUG("%f %f %f %f\n\n", mv[3], mv[7], mv[11], mv[15]); 00887 00888 rot[0] = mv[0]; rot[1] = mv[4]; rot[2] = mv[8]; 00889 rot[3] = mv[1]; rot[4] = mv[5]; rot[5] = mv[9]; 00890 rot[6] = mv[2]; rot[7] = mv[6]; rot[8] = mv[10]; 00891 00892 v_cam_object[0] = mv[12]; 00893 v_cam_object[1] = mv[13]; 00894 v_cam_object[2] = mv[14]; 00895 00896 v_cam_object = rot * v_cam_object * s; 00897 00898 glPushMatrix(); 00899 // draw edges slightly closer to camera to avoid edge-flickering 00900 glTranslatef(v_cam_object[0], v_cam_object[1], v_cam_object[2]); 00901 00902 glBegin(GL_LINES); 00903 for(int i=0; i<(int)m_lines.size(); i++){ 00904 glNormal3f( m_lines[i].end.x - m_lines[i].start.x, 00905 m_lines[i].end.y - m_lines[i].start.y, 00906 m_lines[i].end.z - m_lines[i].start.z ); 00907 glVertex3f(m_lines[i].start.x, m_lines[i].start.y, m_lines[i].start.z); 00908 glVertex3f(m_lines[i].end.x, m_lines[i].end.y, m_lines[i].end.z); 00909 } 00910 glEnd(); 00911 00912 glPopMatrix(); 00913 } 00914 00915 void TrackerModel::genListNormals(float normal_length){ // draw normals 00916 unsigned i,j; 00917 tgFace* f; 00918 00919 glDisable(GL_TEXTURE_2D); 00920 glColor3f(0.0, 0.0, 1.0); 00921 00922 glBegin(GL_LINES); 00923 for(i=0; i<m_faces.size(); i++){ 00924 f = &m_faces[i]; 00925 for(j=0; j<f->v.size(); j++){ 00926 glVertex3f( m_vertices[f->v[j]].pos.x, 00927 m_vertices[f->v[j]].pos.y, 00928 m_vertices[f->v[j]].pos.z ); 00929 glVertex3f( m_vertices[f->v[j]].pos.x + m_vertices[f->v[j]].normal.x * normal_length, 00930 m_vertices[f->v[j]].pos.y + m_vertices[f->v[j]].normal.y * normal_length, 00931 m_vertices[f->v[j]].pos.z + m_vertices[f->v[j]].normal.z * normal_length ); 00932 } 00933 } 00934 glEnd(); 00935 00936 glColor3f(1.0, 1.0, 1.0); 00937 } 00938 00939 00940