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


blort
Author(s): Thomas Mörwald , Michael Zillich , Andreas Richtsfeld , Johann Prankl , Markus Vincze , Bence Magyar
autogenerated on Wed Aug 26 2015 15:24:12