ProjectionGridTool.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00024 #include <GL/glew.h>
00025 #ifdef __WXMAC__
00026 #include <OpenGL/gl.h>
00027 #include <OpenGL/glu.h>
00028 #else
00029 #include <GL/gl.h>
00030 #include <GL/glu.h>
00031 #endif
00032 #ifdef __APPLE__
00033   #include <GLUT/glut.h>
00034 #endif
00035 
00036 
00037 #include "ProjectionGridTool.h"
00038 
00039 ProjectionGridTool::ProjectionGridTool(ToolHelper* helper) : helper_g(helper)
00040 {
00041     mesh_info = NULL;
00042     texture_created = false;
00043 }
00044 
00045 ProjectionGridTool::~ProjectionGridTool()
00046 {
00047     if (mesh_info)
00048     {
00049         delete mesh_info;
00050     }
00051 }
00052 
00053 void PreviewProjectionGridTool::Activate()
00054 {
00055     helper->NotifyMe(ToolHelper::DRAW_OVER_IMAGES, this);
00056     helper->NotifyMe(ToolHelper::DRAW_UNDER_IMAGES, this);
00057 }
00058 
00059 void PreviewProjectionGridTool::BeforeDrawImagesEvent()
00060 {
00061 }
00062 
00063 void PreviewProjectionGridTool::AfterDrawImagesEvent()
00064 {
00065     DEBUG_DEBUG("begin");
00066     if (!texture_created)
00067     {
00068         if (!createTexture())
00069         {
00070             return;
00071         }
00072     }
00073     if (!mesh_info)
00074     {
00075         createMesh();
00076     }
00077 
00078     mesh_info->Update();
00079     glColor4f(1,1,1,1);
00080     glEnable( GL_TEXTURE_2D );
00081     glEnable(GL_BLEND);
00082     glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00083     if(helper->GetViewStatePtr()->GetSupportMultiTexture())
00084     {
00085         glActiveTexture(GL_TEXTURE1);
00086         glDisable(GL_TEXTURE_2D);
00087         glActiveTexture(GL_TEXTURE0);
00088         glBindTexture(GL_TEXTURE_2D, texture_num);
00089     }
00090     else
00091     {
00092         glBindTexture(GL_TEXTURE_2D, texture_num);
00093     };
00094     glMatrixMode(GL_TEXTURE);
00095     glPushMatrix();
00096     glMatrixMode(GL_MODELVIEW);
00097     mesh_info->CallList();
00098     glMatrixMode(GL_TEXTURE);
00099     glPopMatrix();
00100     glMatrixMode(GL_MODELVIEW);
00101     glDisable(GL_BLEND);
00102 }
00103 
00104 void PanosphereOverviewProjectionGridTool::Activate()
00105 {
00106     static_cast<PanosphereOverviewToolHelper*>(helper)->NotifyMe(PanosphereOverviewToolHelper::DRAW_OVER_IMAGES_BACK, this);
00107     static_cast<PanosphereOverviewToolHelper*>(helper)->NotifyMe(PanosphereOverviewToolHelper::DRAW_UNDER_IMAGES_BACK, this);
00108     static_cast<PanosphereOverviewToolHelper*>(helper)->NotifyMe(PanosphereOverviewToolHelper::DRAW_OVER_IMAGES_FRONT, this);
00109     static_cast<PanosphereOverviewToolHelper*>(helper)->NotifyMe(PanosphereOverviewToolHelper::DRAW_UNDER_IMAGES_FRONT, this);
00110 }
00111 
00112 void PanosphereOverviewProjectionGridTool::BeforeDrawImagesBackEvent()
00113 {
00114 }
00115 
00116 void PanosphereOverviewProjectionGridTool::BeforeDrawImagesFrontEvent()
00117 {
00118 }
00119 
00120 void PanosphereOverviewProjectionGridTool::AfterDrawImagesBackEvent()
00121 {
00122     DEBUG_DEBUG("begin");
00123     if (!texture_created)
00124     {
00125         if (!createTexture())
00126         {
00127             return;
00128         }
00129     }
00130 
00131     if (!mesh_info)
00132     {
00133         createMesh();
00134     }
00135 
00136     DEBUG_DEBUG("resources created");
00137     glColor4f(1.0f, 1.0f, 1.0f, 0.3f);
00138     glEnable( GL_TEXTURE_2D );
00139     glEnable(GL_BLEND);
00140     glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00141     if(helper->GetViewStatePtr()->GetSupportMultiTexture())
00142     {
00143         glActiveTexture(GL_TEXTURE1);
00144         glDisable(GL_TEXTURE_2D);
00145         glActiveTexture(GL_TEXTURE0);
00146         glBindTexture(GL_TEXTURE_2D, texture_num);
00147     }
00148     else
00149     {
00150         glBindTexture(GL_TEXTURE_2D, texture_num);
00151     };
00152 
00153     glMatrixMode(GL_TEXTURE);
00154     // using just a sphere instead of the remapped mesh for better quality
00155     glPushMatrix();
00156     glRotated(180,1,0,0);
00157     glMatrixMode(GL_MODELVIEW);
00158     GLUquadric* grid = gluNewQuadric();
00159     gluQuadricTexture(grid, GL_TRUE);
00160 
00161     glPushMatrix();
00162     glScalef(-1,1,1);
00163     glRotated(-90,1,0,0);
00164     gluSphere(grid, 101,40,20);
00165     glPopMatrix();
00166 
00167     glMatrixMode(GL_TEXTURE);
00168     glPopMatrix();
00169 
00170     glDisable(GL_BLEND);
00171     glMatrixMode(GL_MODELVIEW);
00172     DEBUG_DEBUG("end");
00173 }
00174 
00175 void PanosphereOverviewProjectionGridTool::AfterDrawImagesFrontEvent()
00176 {
00177     if (!texture_created)
00178     {
00179         if (!createTexture())
00180         {
00181             return;
00182         }
00183     }
00184 
00185     if (!mesh_info)
00186     {
00187         createMesh();
00188     }
00189 
00190     glColor4f(1,1,1,1);
00191     glEnable( GL_TEXTURE_2D );
00192     glEnable(GL_BLEND);
00193     glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00194     DEBUG_DEBUG("proj grid tex " << texture_num);
00195     if(helper->GetViewStatePtr()->GetSupportMultiTexture())
00196     {
00197         glActiveTexture(GL_TEXTURE0);
00198         glBindTexture(GL_TEXTURE_2D, texture_num);
00199     }
00200     else
00201     {
00202         glBindTexture(GL_TEXTURE_2D, texture_num);
00203     };
00204     glMatrixMode(GL_TEXTURE);
00205 
00206     //using just a sphere instead of the remapped mesh for better quality
00207     glPushMatrix();
00208     glRotated(180,1,0,0);
00209     glMatrixMode(GL_MODELVIEW);
00210 
00211     GLUquadric* grid = gluNewQuadric();
00212     gluQuadricTexture(grid, GL_TRUE);
00213     glPushMatrix();
00214     glScalef(-1,1,1);
00215     glRotated(-90,1,0,0);
00216     gluSphere(grid, 101,40,20);
00217     glPopMatrix();
00218 
00219     glMatrixMode(GL_TEXTURE);
00220     glPopMatrix();
00221 
00222     glDisable(GL_BLEND);
00223     glMatrixMode(GL_MODELVIEW);
00224 
00225 }
00226 
00227 void PreviewProjectionGridTool::createMesh()
00228 {
00229     HuginBase::SrcPanoImage image;
00230     image.setSize(vigra::Size2D(3600,1780));
00231     image.setHFOV(360);
00232     image.setProjection(HuginBase::BaseSrcPanoImage::EQUIRECTANGULAR);
00233     mesh_info = new MeshManager::MeshInfo(helper->GetPanoramaPtr(), &image, helper->GetVisualizationStatePtr(), false);
00234 }
00235 
00236 void PanosphereOverviewProjectionGridTool::createMesh()
00237 {
00238     DEBUG_DEBUG("Create mesh projection grid");
00239     HuginBase::SrcPanoImage image;
00240     image.setSize(vigra::Size2D(3600,1780));
00241     image.setHFOV(360);
00242     image.setProjection(HuginBase::BaseSrcPanoImage::EQUIRECTANGULAR);
00243     mesh_info = new MeshManager::PanosphereOverviewMeshInfo(helper->GetPanoramaPtr(), &image, helper->GetVisualizationStatePtr(), false);
00244     DEBUG_DEBUG("End create mesh projection grid");
00245 }
00246 
00250 bool ProjectionGridTool::createTexture()
00251 {
00252     glGenTextures(1,(GLuint*) &texture_num);
00253 
00254     if(helper_g->GetViewStatePtr()->GetSupportMultiTexture())
00255     {
00256         glActiveTexture(GL_TEXTURE0);
00257         glBindTexture(GL_TEXTURE_2D, texture_num);
00258     }
00259     else
00260     {
00261         glBindTexture(GL_TEXTURE_2D, texture_num);
00262     };
00263 
00264 
00265     GLint texSize; 
00266     glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize);
00267     int width_p = 12;
00268     int height_p = 11;
00269     int width = std::min(1 << width_p,texSize);
00270     int height = std::min(1 << height_p, texSize >> 1);
00271 
00272     int hor_lines = 20;
00273     int ver_lines = 40;
00274     double line_width_per = 0.02;
00275     double dw = width / ver_lines;
00276     double dh = height / hor_lines;
00277     double line_width =  (dw < dh) ? line_width_per * dw : line_width_per * dh;
00278 
00279     unsigned char *image = new unsigned char[width * height * 4];
00280     unsigned char *pix_start = image;
00281 
00282     int horLineNr=0;
00283     for (int y = 0 ; y < height ; y++)
00284     {
00285         bool onHorLine=false;
00286         if((horLineNr+0.5)*dh-line_width/2.0<y)
00287         {
00288             if(y<=(horLineNr+0.5)*dh+line_width/2.0)
00289             {
00290                 onHorLine=true;
00291             }
00292             else
00293             {
00294                 horLineNr++;
00295             };
00296         };
00297         int verLineNr=0;
00298         for (int x = 0 ; x < width ; x++)
00299         {
00300             bool onVerLine=false;
00301             if((verLineNr+0.5)*dw-line_width/2.0<x)
00302             {
00303                 if(x<=(verLineNr+0.5)*dw+line_width/2.0)
00304                 {
00305                     onVerLine=true;
00306                 }
00307                 else
00308                 {
00309                     verLineNr++;
00310                 };
00311             };
00312 
00313             if(onHorLine || onVerLine)
00314             {
00315                 pix_start[0]=0;
00316                 pix_start[1]=255;
00317                 pix_start[2]=255;
00318                 pix_start[3]=255;
00319             }
00320             else
00321             {
00322                 pix_start[0]=255;
00323                 pix_start[1]=255;
00324                 pix_start[2]=255;
00325                 pix_start[3]=0;
00326             }
00327             pix_start += 4;
00328         }
00329     }
00330 
00331     bool has_error = false;
00332     GLint error;
00333     error = gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA8, width, height, GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char *) image);
00334     delete [] image;
00335     static bool checked_anisotropic = false;
00336     static bool has_anisotropic;
00337     static float anisotropy;
00338     if (!checked_anisotropic)
00339     {
00340         // check if it is supported
00341         if (GLEW_EXT_texture_filter_anisotropic)
00342         {
00343             has_anisotropic = true;
00344             glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &anisotropy);
00345             DEBUG_INFO("Using anisotropic filtering at maximum value "
00346                       << anisotropy);
00347         } else {
00348             has_anisotropic = false;
00349             DEBUG_INFO("Anisotropic filtering is not available.");
00350         }
00351         checked_anisotropic = true;
00352     }
00353     if (has_anisotropic)
00354     {
00355         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT,
00356                         anisotropy);
00357     }
00358     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
00359     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00360 
00361     if (error)
00362     {
00363         DEBUG_ERROR("GLU Error when building mipmap levels: "
00364                   << gluErrorString(error) << ".");
00365         has_error = true;
00366     }
00367     error = glGetError();
00368     if (error != GL_NO_ERROR)
00369     {
00370         DEBUG_ERROR("GL Error when building mipmap levels: "
00371                   << gluErrorString(error) << ".");
00372         has_error = true;
00373     }
00374     DEBUG_INFO("Finsihed loading texture.");
00375     if (has_error) {
00376         return false;
00377     } else {
00378         texture_created = true;
00379         return true;
00380     }
00381 }

Generated on 5 Feb 2016 for Hugintrunk by  doxygen 1.4.7