ProjectionGridTool.cpp

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

Generated on 21 Oct 2014 for Hugintrunk by  doxygen 1.4.7