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 // initialize static class members
00040 bool ProjectionGridTool::texture_created = false;
00041 unsigned int ProjectionGridTool::texture_num = 0;
00042 
00043 ProjectionGridTool::ProjectionGridTool(ToolHelper* helper) : helper_g(helper)
00044 {
00045     mesh_info = NULL;
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     static_cast<PanosphereOverviewToolHelper*>(helper)->NotifyMe(PanosphereOverviewToolHelper::DRAW_OVER_IMAGES_BACK, this);
00110     static_cast<PanosphereOverviewToolHelper*>(helper)->NotifyMe(PanosphereOverviewToolHelper::DRAW_UNDER_IMAGES_BACK, this);
00111     static_cast<PanosphereOverviewToolHelper*>(helper)->NotifyMe(PanosphereOverviewToolHelper::DRAW_OVER_IMAGES_FRONT, this);
00112     static_cast<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     DEBUG_DEBUG("resources created");
00135     glColor4f(1.0f, 1.0f, 1.0f, 0.3f);
00136     glEnable( GL_TEXTURE_2D );
00137     glEnable(GL_BLEND);
00138     glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00139     if(helper->GetViewStatePtr()->GetSupportMultiTexture())
00140     {
00141         glActiveTexture(GL_TEXTURE1);
00142         glDisable(GL_TEXTURE_2D);
00143         glActiveTexture(GL_TEXTURE0);
00144         glBindTexture(GL_TEXTURE_2D, texture_num);
00145     }
00146     else
00147     {
00148         glBindTexture(GL_TEXTURE_2D, texture_num);
00149     };
00150 
00151     glMatrixMode(GL_TEXTURE);
00152     // using just a sphere instead of the remapped mesh for better quality
00153     glPushMatrix();
00154     glRotated(180,1,0,0);
00155     glMatrixMode(GL_MODELVIEW);
00156     GLUquadric* grid = gluNewQuadric();
00157     gluQuadricTexture(grid, GL_TRUE);
00158 
00159     glPushMatrix();
00160     glScalef(-1,1,1);
00161     glRotated(-90,1,0,0);
00162     gluSphere(grid, 101,40,20);
00163     glPopMatrix();
00164 
00165     glMatrixMode(GL_TEXTURE);
00166     glPopMatrix();
00167 
00168     glDisable(GL_BLEND);
00169     glMatrixMode(GL_MODELVIEW);
00170     DEBUG_DEBUG("end");
00171 }
00172 
00173 void PanosphereOverviewProjectionGridTool::AfterDrawImagesFrontEvent()
00174 {
00175     if (!texture_created)
00176     {
00177         if (!createTexture())
00178         {
00179             return;
00180         }
00181     }
00182 
00183     glColor4f(1,1,1,1);
00184     glEnable( GL_TEXTURE_2D );
00185     glEnable(GL_BLEND);
00186     glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00187     DEBUG_DEBUG("proj grid tex " << texture_num);
00188     if(helper->GetViewStatePtr()->GetSupportMultiTexture())
00189     {
00190         glActiveTexture(GL_TEXTURE0);
00191         glBindTexture(GL_TEXTURE_2D, texture_num);
00192     }
00193     else
00194     {
00195         glBindTexture(GL_TEXTURE_2D, texture_num);
00196     };
00197     glMatrixMode(GL_TEXTURE);
00198 
00199     //using just a sphere instead of the remapped mesh for better quality
00200     glPushMatrix();
00201     glRotated(180,1,0,0);
00202     glMatrixMode(GL_MODELVIEW);
00203 
00204     GLUquadric* grid = gluNewQuadric();
00205     gluQuadricTexture(grid, GL_TRUE);
00206     glPushMatrix();
00207     glScalef(-1,1,1);
00208     glRotated(-90,1,0,0);
00209     gluSphere(grid, 101,40,20);
00210     glPopMatrix();
00211 
00212     glMatrixMode(GL_TEXTURE);
00213     glPopMatrix();
00214 
00215     glDisable(GL_BLEND);
00216     glMatrixMode(GL_MODELVIEW);
00217 
00218 }
00219 
00220 void PreviewProjectionGridTool::createMesh()
00221 {
00222     HuginBase::SrcPanoImage image;
00223     image.setSize(vigra::Size2D(3600,1780));
00224     image.setHFOV(360);
00225     image.setProjection(HuginBase::BaseSrcPanoImage::EQUIRECTANGULAR);
00226     mesh_info = new MeshManager::MeshInfo(helper->GetPanoramaPtr(), &image, helper->GetVisualizationStatePtr(), false);
00227 }
00228 
00232 bool ProjectionGridTool::createTexture()
00233 {
00234     glGenTextures(1,(GLuint*) &texture_num);
00235 
00236     if(helper_g->GetViewStatePtr()->GetSupportMultiTexture())
00237     {
00238         glActiveTexture(GL_TEXTURE0);
00239         glBindTexture(GL_TEXTURE_2D, texture_num);
00240     }
00241     else
00242     {
00243         glBindTexture(GL_TEXTURE_2D, texture_num);
00244     };
00245 
00246 
00247     GLint texSize; 
00248     glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize);
00249     int width_p = 12;
00250     int height_p = 11;
00251     int width = std::min(1 << width_p,texSize);
00252     int height = std::min(1 << height_p, texSize >> 1);
00253 
00254     int hor_lines = 20;
00255     int ver_lines = 40;
00256     double line_width_per = 0.02;
00257     double dw = width / ver_lines;
00258     double dh = height / hor_lines;
00259     double line_width =  (dw < dh) ? line_width_per * dw : line_width_per * dh;
00260 
00261     GLubyte *image = new GLubyte[width * height * 4];
00262     GLubyte *pix_start = image;
00263 
00264     int horLineNr=0;
00265     for (int y = 0 ; y < height ; y++)
00266     {
00267         bool onHorLine=false;
00268         if((horLineNr+0.5)*dh-line_width/2.0<y)
00269         {
00270             if(y<=(horLineNr+0.5)*dh+line_width/2.0)
00271             {
00272                 onHorLine=true;
00273             }
00274             else
00275             {
00276                 horLineNr++;
00277             };
00278         };
00279         int verLineNr=0;
00280         for (int x = 0 ; x < width ; x++)
00281         {
00282             bool onVerLine=false;
00283             if((verLineNr+0.5)*dw-line_width/2.0<x)
00284             {
00285                 if(x<=(verLineNr+0.5)*dw+line_width/2.0)
00286                 {
00287                     onVerLine=true;
00288                 }
00289                 else
00290                 {
00291                     verLineNr++;
00292                 };
00293             };
00294 
00295             if(onHorLine || onVerLine)
00296             {
00297                 pix_start[0]=0;
00298                 pix_start[1]=255;
00299                 pix_start[2]=255;
00300                 pix_start[3]=255;
00301             }
00302             else
00303             {
00304                 pix_start[0]=255;
00305                 pix_start[1]=255;
00306                 pix_start[2]=255;
00307                 pix_start[3]=0;
00308             }
00309             pix_start += 4;
00310         }
00311     }
00312 
00313     bool has_error = false;
00314     GLint error;
00315     error = gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA8, width, height, GL_RGBA, GL_UNSIGNED_BYTE, image);
00316     delete [] image;
00317     static bool checked_anisotropic = false;
00318     static bool has_anisotropic;
00319     static float anisotropy;
00320     if (!checked_anisotropic)
00321     {
00322         // check if it is supported
00323         if (GLEW_EXT_texture_filter_anisotropic)
00324         {
00325             has_anisotropic = true;
00326             glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &anisotropy);
00327             DEBUG_INFO("Using anisotropic filtering at maximum value "
00328                       << anisotropy);
00329         } else {
00330             has_anisotropic = false;
00331             DEBUG_INFO("Anisotropic filtering is not available.");
00332         }
00333         checked_anisotropic = true;
00334     }
00335     if (has_anisotropic)
00336     {
00337         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT,
00338                         anisotropy);
00339     }
00340     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
00341     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00342 
00343     if (error)
00344     {
00345         DEBUG_ERROR("GLU Error when building mipmap levels: "
00346                   << gluErrorString(error) << ".");
00347         has_error = true;
00348     }
00349     error = glGetError();
00350     if (error != GL_NO_ERROR)
00351     {
00352         DEBUG_ERROR("GL Error when building mipmap levels: "
00353                   << gluErrorString(error) << ".");
00354         has_error = true;
00355     }
00356     DEBUG_INFO("Finsihed loading texture.");
00357     if (has_error) {
00358         return false;
00359     } else {
00360         texture_created = true;
00361         return true;
00362     }
00363 }

Generated on 23 Jul 2016 for Hugintrunk by  doxygen 1.4.7