GLRenderer.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00023 #include <wx/wx.h>
00024 #include <wx/platform.h>
00025 
00026 #ifdef __WXMAC__
00027 #include <OpenGL/gl.h>
00028 #include <OpenGL/glu.h>
00029 #else
00030 #ifdef __WXMSW__
00031 #include <vigra/windows.h>
00032 #endif
00033 #include <GL/gl.h>
00034 #include <GL/glu.h>
00035 
00036 #endif
00037 
00038 #include <config.h>
00039 
00040 #include "panoinc.h"
00041 
00042 #include "TextureManager.h"
00043 #include "MeshManager.h"
00044 #include "ViewState.h"
00045 #include "GLRenderer.h"
00046 #include "GLViewer.h"
00047 #include "ToolHelper.h"
00048 #include <panodata/PanoramaOptions.h>
00049 
00050 GLRenderer::GLRenderer(const wxColour backgroundColour)
00051 {
00052     m_background_color = backgroundColour;
00053 };
00054 
00055 void GLRenderer::SetPreviewBackgroundColor(const wxColour c)
00056 {
00057     m_background_color = c;
00058 }
00059 
00060 void GLRenderer::SetBackground(unsigned char red, unsigned char green, unsigned char blue)
00061 {
00062     glClearColor((float) red / 255.0, (float) green / 255.0, (float) blue / 255.0, 1.0);
00063 }
00064 
00065 GLRenderer::~GLRenderer()
00066 {
00067 }
00068 
00069 GLPreviewRenderer::GLPreviewRenderer(HuginBase::Panorama *pano, TextureManager *tex_man,
00070                        MeshManager *mesh_man, VisualizationState *visualization_state,
00071                        PreviewToolHelper *tool_helper,const wxColour backgroundColour) : GLRenderer(backgroundColour)
00072 {
00073     m_pano = pano;
00074     m_tex_man = tex_man;
00075     m_mesh_man = mesh_man;
00076     m_visualization_state = visualization_state;
00077     m_tool_helper = tool_helper;
00078 }
00079 
00080 GLPanosphereOverviewRenderer::GLPanosphereOverviewRenderer(HuginBase::Panorama *pano, TextureManager *tex_man,
00081                        MeshManager *mesh_man, PanosphereOverviewVisualizationState *visualization_state,
00082                        PanosphereOverviewToolHelper *tool_helper, const wxColour backgroundColour) : GLRenderer(backgroundColour)
00083 {
00084     m_pano = pano;
00085     m_tex_man = tex_man;
00086     m_mesh_man = mesh_man;
00087     m_visualization_state = visualization_state;
00088     m_tool_helper = tool_helper;
00089 }
00090 
00091 GLPlaneOverviewRenderer::GLPlaneOverviewRenderer(HuginBase::Panorama *pano, TextureManager *tex_man,
00092                        MeshManager *mesh_man, PlaneOverviewVisualizationState *visualization_state,
00093                        PlaneOverviewToolHelper *tool_helper, const wxColour backgroundColour) : GLRenderer(backgroundColour)
00094 {
00095 
00096     m_pano = pano;
00097     m_tex_man = tex_man;
00098     m_mesh_man = mesh_man;
00099     m_visualization_state = visualization_state;
00100     m_tool_helper = tool_helper;
00101 }
00102 
00103 vigra::Diff2D GLPreviewRenderer::Resize(int in_width, int in_height)
00104 {
00105   width = in_width;
00106   height = in_height;
00107   glViewport(0, 0, width, height);
00108   // we use the view_state rather than the panorama to allow interactivity.
00109   HuginBase::PanoramaOptions *options = m_visualization_state->getViewState()->GetOptions();
00110   width_o = options->getWidth();
00111   height_o = options->getHeight();
00112   double aspect_screen = double(width) / double (height),
00113         aspect_pano = width_o / height_o;
00114   glMatrixMode(GL_PROJECTION);
00115   glLoadIdentity();  
00116   double scale;
00117   if (aspect_screen < aspect_pano)
00118   {
00119       // the panorama is wider than the screen
00120       scale = width_o / width;
00121   } else {
00122       // the screen is wider than the panorama
00123       scale = height_o / height;
00124   }
00125   double x_offs = (scale * double(width) - width_o) / 2.0,
00126          y_offs = (scale * double(height) - height_o) / 2.0;
00127   // set up the projection, so we can use panorama coordinates.
00128   glOrtho(-x_offs, width * scale - x_offs,
00129           height * scale - y_offs, -y_offs,
00130           -1.0, 1.0);
00131   // scissor to the panorama.
00132   glScissor(x_offs / scale, y_offs / scale,
00133             width_o / scale, height_o / scale);
00134   glMatrixMode(GL_MODELVIEW);
00135   // tell the view state the region we are displaying.
00136   // TODO add support for zooming and panning.
00137   m_visualization_state->SetVisibleArea(vigra::Rect2D(0, 0, options->getWidth(),
00138                                              options->getHeight()));
00139   m_visualization_state->SetScale(1.0 / scale);
00140 
00141   // return the offset from the top left corner of the viewpoer to the top left
00142   // corner of the panorama.
00143   return vigra::Diff2D(int (x_offs / scale), int (y_offs / scale));
00144 }
00145 
00146 void GLPreviewRenderer::Redraw()
00147 {
00148     glClear(GL_COLOR_BUFFER_BIT);
00149     glEnable(GL_SCISSOR_TEST);
00150     m_tex_man->DisableTexture();
00151     // background color of flat pano preview
00152     glColor3f((float)m_background_color.Red()/255, (float)m_background_color.Green()/255, (float)m_background_color.Blue()/255);
00153     glBegin(GL_QUADS);
00154         glVertex2f(0.0, 0.0);
00155         glVertex2f(width_o, 0.0);
00156         glVertex2f(width_o, height_o);
00157         glVertex2f(0.0, height_o);
00158     glEnd();
00159     glColor3f(1.0, 1.0, 1.0);
00160     // draw things under the preview images
00161     // draw each active image.
00162     int imgs = m_pano->getNrOfImages();
00163     // offset by a half a pixel
00164     glPushMatrix();
00165     glTranslatef(0.5, 0.5, 0.0);
00166     glEnable(GL_TEXTURE_2D);
00167     m_tex_man->Begin();
00168     m_tool_helper->BeforeDrawImages();
00169     // The old preview shows the lowest numbered image on top, so do the same:
00170     for (int img = imgs - 1; img != -1; img--)
00171     {
00172         // only draw active images
00173         if (m_pano->getImage(img).getActive())
00174         {
00175             // the tools can cancel drawing of images.
00176             if (m_tool_helper->BeforeDrawImageNumber(img))
00177             {
00178                 // the texture manager may need to call the display list
00179                 // multiple times with blending, so we pass it the display list
00180                 // rather than switching to the texture and then calling the
00181                 // list ourselves.
00182                 m_tex_man->DrawImage(img, m_mesh_man->GetDisplayList(img));
00183                 m_tool_helper->AfterDrawImageNumber(img);
00184             }
00185         }
00186     }
00187     m_tex_man->End();
00188     // drawn things after the active image.
00189     m_tool_helper->AfterDrawImages();
00190     m_tex_man->DisableTexture();
00191     glPopMatrix();
00192     // darken the cropped out range
00193     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00194     glEnable(GL_BLEND);
00195     glColor4f(0.0, 0.0, 0.0, 0.5);
00196     // construct a strip of quads, with each pair being one of the corners.
00197     const vigra::Rect2D roi = m_visualization_state->getViewState()->GetOptions()->getROI();
00198     glBegin(GL_QUAD_STRIP);
00199         glVertex2f(0.0,     0.0);      glVertex2i(roi.left(),  roi.top());
00200         glVertex2f(width_o, 0.0);      glVertex2i(roi.right(), roi.top());
00201         glVertex2f(width_o, height_o); glVertex2i(roi.right(), roi.bottom());
00202         glVertex2f(0.0,     height_o); glVertex2i(roi.left(),  roi.bottom());
00203         glVertex2f(0.0,     0.0);      glVertex2i(roi.left(),  roi.top());
00204     glEnd();
00205     // draw lines around cropped area.
00206     // we want to invert the color to make it stand out.
00207     glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
00208     glColor3f(1.0, 1.0, 1.0);
00209     glBegin(GL_LINE_LOOP);
00210         glVertex2i(roi.left(),  roi.top());
00211         glVertex2i(roi.right(), roi.top());
00212         glVertex2i(roi.right(), roi.bottom());
00213         glVertex2i(roi.left(),  roi.bottom());
00214     glEnd();
00215     glDisable(GL_BLEND);
00216     glEnable(GL_TEXTURE_2D);
00217     
00218     glDisable(GL_SCISSOR_TEST);
00219 }
00220 
00221 
00222 void GLPanosphereOverviewRenderer::Redraw()
00223 {
00224     glClearColor(0,0,0,1);
00225         glClear(GL_COLOR_BUFFER_BIT);
00226 
00227         glMatrixMode(GL_MODELVIEW);
00228 
00229         glLoadIdentity();
00230 
00231     double R = m_visualization_state->getR();
00232     double angx = m_visualization_state->getAngX();
00233     double angy = m_visualization_state->getAngY();
00234         
00235         gluLookAt(R * cos(angy) * cos(angx), R * sin(angy), R * cos(angy) * sin(angx), 0, 0, 0, 0, 1, 0);
00236     //for look from inside
00237 //      gluLookAt(0,0,0,R * cos(angy) * cos(angx), R * sin(angy), R * cos(angy) * sin(angx), 0, 1, 0);
00238 
00239     // draw things under the preview images
00240     // draw each active image.
00241     int imgs = m_pano->getNrOfImages();
00242     // offset by a half a pixel
00243     glPushMatrix();
00244 
00245     //draw the rectangle around the sphere
00246     glColor3f(0.5, 0.5, 0.5);
00247     
00248     double side = 150;
00249     glBegin(GL_LINE_LOOP);
00250 
00251         glVertex3f(-side,side,0);
00252         glVertex3f(side,side,0);
00253         glVertex3f(side,-side,0);
00254         glVertex3f(-side,-side,0);
00255 
00256     glEnd();
00257 
00258     //draw the axes, to give a sense of orientation
00259     double axis = 200;
00260     glBegin(GL_LINES);
00261 
00262         glColor3f(1,0,0);
00263         glVertex3f(-axis,0,0);
00264         glVertex3f(axis,0,0);
00265 
00266         glColor3f(0,1,0);
00267         glVertex3f(0,0,0);
00268         glVertex3f(0,axis,0);
00269 
00270         glColor3f(0,0,1);
00271         glVertex3f(0,0,0);
00272         glVertex3f(0,0,axis);
00273 
00274     glEnd();
00275 
00276 
00277     glEnable(GL_TEXTURE_2D);
00278 
00279     //To avoid z-order fight of the images if depth buffer is used, depth buffer is disabled and meshes are drawn twice,
00280     //first with back faces culled so that the inner face of the sphere is visible and below the outter face, 
00281     //and afterwards the meshes are drawn again with the front faces culled
00282     
00283     glEnable(GL_CULL_FACE);
00284     glCullFace(GL_BACK);
00285 
00286     //event called only before drawing of the images with front faces culled (the inner face of the panosphere)
00287     static_cast<PanosphereOverviewToolHelper*>(m_tool_helper)->BeforeDrawImagesBack();
00288     //generic draw before images are drawn (called twice with front and back faces culled)
00289     m_tex_man->Begin();
00290     m_tool_helper->BeforeDrawImages();
00291 
00292     // The old preview shows the lowest numbered image on top, so do the same:
00293     for (int img = imgs - 1; img != -1; img--)
00294     {
00295         // only draw active images
00296         if (m_pano->getImage(img).getActive())
00297         {
00298             // the tools can cancel drawing of images.
00299             if (m_tool_helper->BeforeDrawImageNumber(img))
00300             {
00301                 // the texture manager may need to call the display list
00302                 // multiple times with blending, so we pass it the display list
00303                 // rather than switching to the texture and then calling the
00304                 // list ourselves.
00305                 m_tex_man->DrawImage(img, m_mesh_man->GetDisplayList(img));
00306                 m_tool_helper->AfterDrawImageNumber(img);
00307             }
00308         }
00309     }
00310 
00311     m_tex_man->End();
00312     m_tool_helper->AfterDrawImages();
00313     m_tex_man->DisableTexture();
00314     static_cast<PanosphereOverviewToolHelper*>(m_tool_helper)->AfterDrawImagesBack();
00315 
00316 //    #ifdef __WXGTK__
00335 //    #endif
00336 
00337     glMatrixMode(GL_MODELVIEW);
00338     glCullFace(GL_FRONT);
00339 
00340     static_cast<PanosphereOverviewToolHelper*>(m_tool_helper)->BeforeDrawImagesFront();
00341 
00342     glEnable(GL_TEXTURE_2D);
00343     m_tex_man->Begin();
00344     m_tool_helper->BeforeDrawImages();
00345 
00346     // The old preview shows the lowest numbered image on top, so do the same:
00347     for (int img = imgs - 1; img != -1; img--)
00348     {
00349         // only draw active images
00350         if (m_pano->getImage(img).getActive())
00351         {
00352             // the tools can cancel drawing of images.
00353             if (m_tool_helper->BeforeDrawImageNumber(img))
00354             {
00355                 // the texture manager may need to call the display list
00356                 // multiple times with blending, so we pass it the display list
00357                 // rather than switching to the texture and then calling the
00358                 // list ourselves.
00359                 m_tex_man->DrawImage(img, m_mesh_man->GetDisplayList(img));
00360                 m_tool_helper->AfterDrawImageNumber(img);
00361             }
00362         }
00363     }
00364 
00365     m_tex_man->End();
00366     // drawn things after the active image.
00367     m_tool_helper->AfterDrawImages();
00368     m_tex_man->DisableTexture();
00369     static_cast<PanosphereOverviewToolHelper*>(m_tool_helper)->AfterDrawImagesFront();
00370 
00371     m_tex_man->DisableTexture();
00372 
00373     glDisable(GL_CULL_FACE);
00374 
00375     glPopMatrix();
00376 }
00377 
00378 vigra::Diff2D GLPanosphereOverviewRenderer::Resize(int w, int h)
00379 {
00380 
00381     width = w;
00382     height = h;
00383     glViewport(0, 0, width, height);
00384     // we use the view_state rather than the panorama to allow interactivity.
00385     HuginBase::PanoramaOptions *options = m_visualization_state->GetOptions();
00386     width_o = options->getWidth();
00387     height_o = options->getHeight();
00388 
00389     //since gluPerspective needs vertical field of view, depending on the aspect ratio we convert from vertical to horizontal FOV if needed
00390     double fov = m_visualization_state->getFOV();
00391     double fovy;
00392     if (h > w) {
00393         fovy = 2.0 * atan( tan(fov * M_PI / 360.0) * (float) h / (float) w) / M_PI * 180.0;
00394     } else {
00395         fovy = fov;
00396     }
00397 
00398         float ratio = 1.0* w / h;
00399 //      aspect = ratio;
00400         glMatrixMode(GL_PROJECTION);
00401         glLoadIdentity();
00402         glViewport(0, 0, w, h);
00403         gluPerspective(fovy,ratio,1,1000000);
00404 
00405     m_visualization_state->SetVisibleArea(vigra::Rect2D(0, 0, options->getWidth(),
00406                                              options->getHeight()));
00407 
00408     //calculate the scale depending on the section of the panosphere in the center of the screen
00409     double R = m_visualization_state->getR();
00410     double radius = m_visualization_state->getSphereRadius();
00411     //height of the screen in screen pixels over the length of the panosphere in panorama pixels when spread out
00412     double scrscale = (float) h  / (2 * tan(fovy / 360.0 * M_PI) * (R - radius) / (2 * radius * M_PI) * (options->getWidth()));
00413     m_visualization_state->SetScale(scrscale);
00414     m_visualization_state->GetViewer()->MarkToolsDirty();
00415 //    DEBUG_DEBUG("renderer " << scrscale << " " << h << " " << R << " " << fovy);
00416 //    DEBUG_DEBUG("renderer scale " << scrscale);
00417 
00418 //    return vigra::Diff2D(w / 2, h / 2);
00419     return vigra::Diff2D(0,0);
00420 
00421 }
00422 
00423 void GLPlaneOverviewRenderer::Redraw()
00424 {
00425     // background color of mosaic plane
00426     glClearColor((float)m_background_color.Red()/255, (float)m_background_color.Green()/255, (float)m_background_color.Blue()/255,1.0);
00427 
00428         glClear(GL_COLOR_BUFFER_BIT);
00429 
00430         glMatrixMode(GL_MODELVIEW);
00431 
00432         glLoadIdentity();
00433 
00434     double R = m_visualization_state->getR();
00435 
00436         double X = m_visualization_state->getX();
00437         double Y = m_visualization_state->getY();
00438 
00439         gluLookAt(X,Y,R, X, Y, 0, 0, 1, 0);
00440 
00441     // draw things under the preview images
00442     m_tool_helper->BeforeDrawImages();
00443     int imgs = m_pano->getNrOfImages();
00444     glPushMatrix();
00445 
00446     glColor3f(0.5,0.5,0.5);
00447     double side = 150;
00448     glBegin(GL_LINE_LOOP);
00449 
00450         glVertex3f(-side,side,0);
00451         glVertex3f(side,side,0);
00452         glVertex3f(side,-side,0);
00453         glVertex3f(-side,-side,0);
00454 
00455     glEnd();
00456 
00457     double axis = 200;
00458     glBegin(GL_LINES);
00459 
00460         glColor3f(1,0,0);
00461         glVertex3f(-axis,0,0);
00462         glVertex3f(axis,0,0);
00463 
00464         glColor3f(0,1,0);
00465         glVertex3f(0,0,0);
00466         glVertex3f(0,axis,0);
00467 
00468         glColor3f(0,0,1);
00469         glVertex3f(0,0,0);
00470         glVertex3f(0,0,axis);
00471 
00472     glEnd();
00473 
00474 
00475     glEnable(GL_TEXTURE_2D);
00476 
00477     m_tex_man->Begin();
00478     // The old preview shows the lowest numbered image on top, so do the same:
00479     for (int img = imgs - 1; img != -1; img--)
00480     {
00481         // only draw active images
00482         if (m_pano->getImage(img).getActive())
00483         {
00484             // the tools can cancel drawing of images.
00485             if (m_tool_helper->BeforeDrawImageNumber(img))
00486             {
00487                 // the texture manager may need to call the display list
00488                 // multiple times with blending, so we pass it the display list
00489                 // rather than switching to the texture and then calling the
00490                 // list ourselves.
00491                 m_tex_man->DrawImage(img, m_mesh_man->GetDisplayList(img));
00492                 m_tool_helper->AfterDrawImageNumber(img);
00493             }
00494         }
00495     }
00496 
00497     m_tex_man->End();
00498     m_tool_helper->AfterDrawImages();
00499 
00500     m_tex_man->DisableTexture();
00501 
00502     glMatrixMode(GL_MODELVIEW);
00503     
00504     glEnable(GL_TEXTURE_2D);
00505     m_tex_man->Begin();
00506 
00507     m_tool_helper->BeforeDrawImages();
00508     // The old preview shows the lowest numbered image on top, so do the same:
00509     for (int img = imgs - 1; img != -1; img--)
00510     {
00511         // only draw active images
00512         if (m_pano->getImage(img).getActive())
00513         {
00514             // the tools can cancel drawing of images.
00515             if (m_tool_helper->BeforeDrawImageNumber(img))
00516             {
00517                 // the texture manager may need to call the display list
00518                 // multiple times with blending, so we pass it the display list
00519                 // rather than switching to the texture and then calling the
00520                 // list ourselves.
00521                 m_tex_man->DrawImage(img, m_mesh_man->GetDisplayList(img));
00522                 m_tool_helper->AfterDrawImageNumber(img);
00523             }
00524         }
00525     }
00526 
00527     m_tex_man->End();
00528     // drawn things after the active image.
00529     m_tool_helper->AfterDrawImages();
00530     m_tex_man->DisableTexture();
00531 
00532     glPopMatrix();
00533 }
00534 
00535 
00536 vigra::Diff2D GLPlaneOverviewRenderer::Resize(int w, int h)
00537 {
00538     width = w;
00539     height = h;
00540     glViewport(0, 0, width, height);
00541     // we use the view_state rather than the panorama to allow interactivity.
00542     HuginBase::PanoramaOptions *options = m_visualization_state->getViewState()->GetOptions();
00543     width_o = options->getWidth();
00544     height_o = options->getHeight();
00545 
00546     glMatrixMode(GL_PROJECTION);
00547     glLoadIdentity();  
00548 
00549     double fov = m_visualization_state->getFOV();
00550     double fovy;
00551     if (h > w) {
00552         fovy = 2.0 * atan( tan(fov * M_PI / 360.0) * (float) h / (float) w) / M_PI * 180.0;
00553     } else {
00554         fovy = fov;
00555     }
00556 
00557         float ratio = 1.0* w / h;
00558 //      aspect = ratio;
00559         glMatrixMode(GL_PROJECTION);
00560         glLoadIdentity();
00561         gluPerspective(fovy,ratio,1,1000000);
00562 
00563     m_visualization_state->SetVisibleArea(vigra::Rect2D(0, 0, options->getWidth(),
00564                                              options->getHeight()));
00565 
00566     double R = m_visualization_state->getR();
00567     double scrscale = (float) h / (2 * tan(fovy / 360.0 * M_PI) * R   * options->getWidth() / MeshManager::PlaneOverviewMeshInfo::scale);
00568     m_visualization_state->SetScale(scrscale);
00569 //    m_visualization_state->SetGLScale(gl_scale);
00570     m_visualization_state->GetViewer()->MarkToolsDirty();
00571 
00572     return vigra::Diff2D(0,0);
00573 }
00574 

Generated on 29 Jun 2016 for Hugintrunk by  doxygen 1.4.7