Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GLRenderer.cpp
Go to the documentation of this file.
1 // -*- c-basic-offset: 4 -*-
23 #include <wx/wx.h>
24 #include <wx/platform.h>
25 
26 #ifdef __WXMAC__
27 #include <OpenGL/gl.h>
28 #include <OpenGL/glu.h>
29 #else
30 #ifdef __WXMSW__
31 #include <vigra/windows.h>
32 #endif
33 #include <GL/gl.h>
34 #include <GL/glu.h>
35 
36 #endif
37 
38 #include "hugin_config.h"
39 
40 #include "panoinc.h"
41 
42 #include "TextureManager.h"
43 #include "MeshManager.h"
44 #include "ViewState.h"
45 #include "GLRenderer.h"
46 #include "GLViewer.h"
47 #include "ToolHelper.h"
49 
50 GLRenderer::GLRenderer(const wxColour backgroundColour)
51 {
52  m_background_color = backgroundColour;
53 };
54 
56 {
58 }
59 
60 void GLRenderer::SetBackground(unsigned char red, unsigned char green, unsigned char blue)
61 {
62  glClearColor((float) red / 255.0, (float) green / 255.0, (float) blue / 255.0, 1.0);
63 }
64 
66 {
67 }
68 
70  MeshManager *mesh_man, VisualizationState *visualization_state,
71  PreviewToolHelper *tool_helper,const wxColour backgroundColour) : GLRenderer(backgroundColour)
72 {
73  m_pano = pano;
74  m_tex_man = tex_man;
75  m_mesh_man = mesh_man;
76  m_visualization_state = visualization_state;
77  m_tool_helper = tool_helper;
78 }
79 
81  MeshManager *mesh_man, PanosphereOverviewVisualizationState *visualization_state,
82  PanosphereOverviewToolHelper *tool_helper, const wxColour backgroundColour) : GLRenderer(backgroundColour)
83 {
84  m_pano = pano;
85  m_tex_man = tex_man;
86  m_mesh_man = mesh_man;
87  m_visualization_state = visualization_state;
88  m_tool_helper = tool_helper;
89 }
90 
92  MeshManager *mesh_man, PlaneOverviewVisualizationState *visualization_state,
93  PlaneOverviewToolHelper *tool_helper, const wxColour backgroundColour) : GLRenderer(backgroundColour)
94 {
95 
96  m_pano = pano;
97  m_tex_man = tex_man;
98  m_mesh_man = mesh_man;
99  m_visualization_state = visualization_state;
100  m_tool_helper = tool_helper;
101 }
102 
103 vigra::Diff2D GLPreviewRenderer::Resize(int in_width, int in_height)
104 {
105  width = in_width;
106  height = in_height;
107  glViewport(0, 0, width, height);
108  // notify viewstate about current canvas size,
109  // maybe needed for a changed image center
111  // we use the view_state rather than the panorama to allow interactivity.
113  width_o = options->getWidth();
114  height_o = options->getHeight();
115  double aspect_screen = double(width) / double (height),
116  aspect_pano = width_o / height_o;
117  glMatrixMode(GL_PROJECTION);
118  glLoadIdentity();
119  double scale;
120  if (aspect_screen < aspect_pano)
121  {
122  // the panorama is wider than the screen
123  scale = width_o / width;
124  } else {
125  // the screen is wider than the panorama
126  scale = height_o / height;
127  }
128  hugin_utils::FDiff2D center(0.5, 0.5);
130  {
131  // apply scale and translate only when not in layout mode
134  };
135  const hugin_utils::FDiff2D lookAt((center.x - 0.5) * width_o, (center.y - 0.5) * height_o);
136 
137  double x_offs = (scale * double(width) - width_o) / 2.0,
138  y_offs = (scale * double(height) - height_o) / 2.0;
139  // set up the projection, so we can use panorama coordinates.
140  glOrtho(-x_offs, width * scale - x_offs,
141  height * scale - y_offs, -y_offs,
142  -1.0, 1.0);
143  glTranslatef(-lookAt.x, -lookAt.y, 0);
144  // scissor to the panorama.
145  glScissor((x_offs-lookAt.x) / scale, (y_offs+lookAt.y) / scale,
146  width_o / scale, height_o / scale);
147  glMatrixMode(GL_MODELVIEW);
148  // tell the view state the region we are displaying.
149  // TODO add support for zooming and panning.
150  m_visualization_state->SetVisibleArea(vigra::Rect2D((center.x-0.5) * width_o, (center.y-0.5) * height_o,
151  (0.5 + center.x) * width_o, (0.5 + center.y) * height_o));
152  m_visualization_state->SetScale(1.0 / scale);
153 
154  // return the offset from the top left corner of the viewpoer to the top left
155  // corner of the panorama.
156  return vigra::Diff2D(int (x_offs / scale), int (y_offs / scale));
157 }
158 
160 {
161  glClear(GL_COLOR_BUFFER_BIT);
162  glEnable(GL_SCISSOR_TEST);
164  // background color of flat pano preview
165  glColor3f((float)m_background_color.Red()/255, (float)m_background_color.Green()/255, (float)m_background_color.Blue()/255);
166  glBegin(GL_QUADS);
167  glVertex2f(0.0, 0.0);
168  glVertex2f(width_o, 0.0);
169  glVertex2f(width_o, height_o);
170  glVertex2f(0.0, height_o);
171  glEnd();
172  glColor3f(1.0, 1.0, 1.0);
173  // draw things under the preview images
174  // draw each active image.
175  int imgs = m_pano->getNrOfImages();
176  // offset by a half a pixel
177  glPushMatrix();
178  glTranslatef(0.5, 0.5, 0.0);
179  glEnable(GL_TEXTURE_2D);
180  m_tex_man->Begin();
182  // The old preview shows the lowest numbered image on top, so do the same:
183  for (int img = imgs - 1; img != -1; img--)
184  {
185  // only draw active images
186  if (m_pano->getImage(img).getActive())
187  {
188  // the tools can cancel drawing of images.
190  {
191  // the texture manager may need to call the display list
192  // multiple times with blending, so we pass it the display list
193  // rather than switching to the texture and then calling the
194  // list ourselves.
197  }
198  }
199  }
200  m_tex_man->End();
201  // drawn things after the active image.
204  glPopMatrix();
205  // darken the cropped out range, except in layout mode
207  {
208  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
209  glEnable(GL_BLEND);
210  glColor4f(0.0, 0.0, 0.0, 0.5);
211  // construct a strip of quads, with each pair being one of the corners.
212  const vigra::Rect2D roi = m_visualization_state->getViewState()->GetOptions()->getROI();
213  glBegin(GL_QUAD_STRIP);
214  glVertex2f(0.0, 0.0); glVertex2i(roi.left(), roi.top());
215  glVertex2f(width_o, 0.0); glVertex2i(roi.right(), roi.top());
216  glVertex2f(width_o, height_o); glVertex2i(roi.right(), roi.bottom());
217  glVertex2f(0.0, height_o); glVertex2i(roi.left(), roi.bottom());
218  glVertex2f(0.0, 0.0); glVertex2i(roi.left(), roi.top());
219  glEnd();
220  // draw lines around cropped area.
221  // we want to invert the color to make it stand out.
222  glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
223  glColor3f(1.0, 1.0, 1.0);
224  glBegin(GL_LINE_LOOP);
225  glVertex2i(roi.left(), roi.top());
226  glVertex2i(roi.right(), roi.top());
227  glVertex2i(roi.right(), roi.bottom());
228  glVertex2i(roi.left(), roi.bottom());
229  glEnd();
230  glDisable(GL_BLEND);
231  };
232  glEnable(GL_TEXTURE_2D);
233 
234  glDisable(GL_SCISSOR_TEST);
235 }
236 
237 
239 {
240  glClearColor(0,0,0,1);
241  glClear(GL_COLOR_BUFFER_BIT);
242 
243  glMatrixMode(GL_MODELVIEW);
244 
245  glLoadIdentity();
246 
247  double R = m_visualization_state->getR();
248  double angx = m_visualization_state->getAngX();
249  double angy = m_visualization_state->getAngY();
250 
251  const bool insideView = m_visualization_state->isInsideView();
252  if (insideView)
253  {
254  gluLookAt(0, 0, 0, R * cos(angy) * cos(angx), R * sin(angy), R * cos(angy) * sin(angx), 0, 1, 0);
255  }
256  else
257  {
258  // view from outside
259  gluLookAt(R * cos(angy) * cos(angx), R * sin(angy), R * cos(angy) * sin(angx), 0, 0, 0, 0, 1, 0);
260  };
261 
262  // draw things under the preview images
263  // draw each active image.
264  int imgs = m_pano->getNrOfImages();
265  // offset by a half a pixel
266  glPushMatrix();
267 
268  if (!insideView)
269  {
270  //draw the rectangle around the sphere
271  glColor3f(0.5, 0.5, 0.5);
272 
273  double side = 150;
274  glBegin(GL_LINE_LOOP);
275  glVertex3f(-side, side, 0);
276  glVertex3f(side, side, 0);
277  glVertex3f(side, -side, 0);
278  glVertex3f(-side, -side, 0);
279  glEnd();
280 
281  //draw the axes, to give a sense of orientation
282  double axis = 200;
283  glBegin(GL_LINES);
284  glColor3f(1, 0, 0);
285  glVertex3f(-axis, 0, 0);
286  glVertex3f(axis, 0, 0);
287 
288  glColor3f(0, 1, 0);
289  glVertex3f(0, 0, 0);
290  glVertex3f(0, axis, 0);
291 
292  glColor3f(0, 0, 1);
293  glVertex3f(0, 0, 0);
294  glVertex3f(0, 0, axis);
295  glEnd();
296  };
297 
298  glEnable(GL_TEXTURE_2D);
299 
300  if (insideView)
301  {
302  // in inside view we only need to draw one side of the textures, the other side is not visible at all
303  // but we need also the BeforeDrawImages[Back/Front] because all PanosphereTools were written
304  // for the outside view where back/front textures are used
305  static_cast<PanosphereOverviewToolHelper*>(m_tool_helper)->BeforeDrawImagesBack();
306  m_tex_man->Begin();
308  // The old preview shows the lowest numbered image on top, so do the same:
309  for (int img = imgs - 1; img != -1; img--)
310  {
311  // only draw active images
312  if (m_pano->getImage(img).getActive())
313  {
314  // the tools can cancel drawing of images.
316  {
317  // the texture manager may need to call the display list
318  // multiple times with blending, so we pass it the display list
319  // rather than switching to the texture and then calling the
320  // list ourselves.
323  }
324  }
325  }
326  // drawn things after the active image.
328  m_tex_man->End();
329  static_cast<PanosphereOverviewToolHelper*>(m_tool_helper)->AfterDrawImagesBack();
331  }
332  else
333  {
334  //To avoid z-order fight of the images if depth buffer is used, depth buffer is disabled and meshes are drawn twice,
335  //first with back faces culled so that the inner face of the sphere is visible and below the outter face,
336  //and afterwards the meshes are drawn again with the front faces culled
337 
338  glEnable(GL_CULL_FACE);
339  glCullFace(GL_BACK);
340 
341  //event called only before drawing of the images with front faces culled (the inner face of the panosphere)
342  static_cast<PanosphereOverviewToolHelper*>(m_tool_helper)->BeforeDrawImagesBack();
343  //generic draw before images are drawn (called twice with front and back faces culled)
344  m_tex_man->Begin();
346 
347  // The old preview shows the lowest numbered image on top, so do the same:
348  for (int img = imgs - 1; img != -1; img--)
349  {
350  // only draw active images
351  if (m_pano->getImage(img).getActive())
352  {
353  // the tools can cancel drawing of images.
355  {
356  // the texture manager may need to call the display list
357  // multiple times with blending, so we pass it the display list
358  // rather than switching to the texture and then calling the
359  // list ourselves.
362  }
363  }
364  }
365 
366  m_tex_man->End();
369  static_cast<PanosphereOverviewToolHelper*>(m_tool_helper)->AfterDrawImagesBack();
370 
371  // #ifdef __WXGTK__
390  // #endif
391 
392  glMatrixMode(GL_MODELVIEW);
393  glCullFace(GL_FRONT);
394 
395  static_cast<PanosphereOverviewToolHelper*>(m_tool_helper)->BeforeDrawImagesFront();
396 
397  glEnable(GL_TEXTURE_2D);
398  m_tex_man->Begin();
400 
401  // The old preview shows the lowest numbered image on top, so do the same:
402  for (int img = imgs - 1; img != -1; img--)
403  {
404  // only draw active images
405  if (m_pano->getImage(img).getActive())
406  {
407  // the tools can cancel drawing of images.
409  {
410  // the texture manager may need to call the display list
411  // multiple times with blending, so we pass it the display list
412  // rather than switching to the texture and then calling the
413  // list ourselves.
416  }
417  }
418  }
419 
420  m_tex_man->End();
421  // drawn things after the active image.
424  static_cast<PanosphereOverviewToolHelper*>(m_tool_helper)->AfterDrawImagesFront();
425 
427 
428  glDisable(GL_CULL_FACE);
429  };
430  glPopMatrix();
431 }
432 
433 vigra::Diff2D GLPanosphereOverviewRenderer::Resize(int w, int h)
434 {
435 
436  width = w;
437  height = h;
438  glViewport(0, 0, width, height);
439  // we use the view_state rather than the panorama to allow interactivity.
441  width_o = options->getWidth();
442  height_o = options->getHeight();
443 
444  //since gluPerspective needs vertical field of view, depending on the aspect ratio we convert from vertical to horizontal FOV if needed
445  double fov = m_visualization_state->getFOV();
446  double fovy;
447  if (h > w) {
448  fovy = 2.0 * atan( tan(fov * M_PI / 360.0) * (float) h / (float) w) / M_PI * 180.0;
449  } else {
450  fovy = fov;
451  }
452 
453  float ratio = 1.0* w / h;
454 // aspect = ratio;
455  glMatrixMode(GL_PROJECTION);
456  glLoadIdentity();
457  glViewport(0, 0, w, h);
458  gluPerspective(fovy,ratio,1,1000000);
459 
460  m_visualization_state->SetVisibleArea(vigra::Rect2D(0, 0, options->getWidth(),
461  options->getHeight()));
462 
464  {
466  }
467  else
468  {
469  //calculate the scale depending on the section of the panosphere in the center of the screen
470  double R = m_visualization_state->getR();
471  double radius = m_visualization_state->getSphereRadius();
472  //height of the screen in screen pixels over the length of the panosphere in panorama pixels when spread out
473  double scrscale = (float)h / (2 * tan(fovy / 360.0 * M_PI) * (R - radius) / (2 * radius * M_PI) * (options->getWidth()));
474  m_visualization_state->SetScale(scrscale);
475  };
477 // DEBUG_DEBUG("renderer " << scrscale << " " << h << " " << R << " " << fovy);
478 // DEBUG_DEBUG("renderer scale " << scrscale);
479 
480 // return vigra::Diff2D(w / 2, h / 2);
481  return vigra::Diff2D(0,0);
482 
483 }
484 
486 {
487  // background color of mosaic plane
488  glClearColor((float)m_background_color.Red()/255, (float)m_background_color.Green()/255, (float)m_background_color.Blue()/255,1.0);
489 
490  glClear(GL_COLOR_BUFFER_BIT);
491 
492  glMatrixMode(GL_MODELVIEW);
493 
494  glLoadIdentity();
495 
496  double R = m_visualization_state->getR();
497 
498  double X = m_visualization_state->getX();
499  double Y = m_visualization_state->getY();
500 
501  gluLookAt(X,Y,R, X, Y, 0, 0, 1, 0);
502 
503  // draw things under the preview images
505  int imgs = m_pano->getNrOfImages();
506  glPushMatrix();
507 
508  glColor3f(0.5,0.5,0.5);
509  double side = 150;
510  glBegin(GL_LINE_LOOP);
511 
512  glVertex3f(-side,side,0);
513  glVertex3f(side,side,0);
514  glVertex3f(side,-side,0);
515  glVertex3f(-side,-side,0);
516 
517  glEnd();
518 
519  double axis = 200;
520  glBegin(GL_LINES);
521 
522  glColor3f(1,0,0);
523  glVertex3f(-axis,0,0);
524  glVertex3f(axis,0,0);
525 
526  glColor3f(0,1,0);
527  glVertex3f(0,0,0);
528  glVertex3f(0,axis,0);
529 
530  glColor3f(0,0,1);
531  glVertex3f(0,0,0);
532  glVertex3f(0,0,axis);
533 
534  glEnd();
535 
536 
537  glEnable(GL_TEXTURE_2D);
538 
539  m_tex_man->Begin();
540  // The old preview shows the lowest numbered image on top, so do the same:
541  for (int img = imgs - 1; img != -1; img--)
542  {
543  // only draw active images
544  if (m_pano->getImage(img).getActive())
545  {
546  // the tools can cancel drawing of images.
548  {
549  // the texture manager may need to call the display list
550  // multiple times with blending, so we pass it the display list
551  // rather than switching to the texture and then calling the
552  // list ourselves.
555  }
556  }
557  }
558 
559  m_tex_man->End();
561 
563 
564  glMatrixMode(GL_MODELVIEW);
565 
566  glEnable(GL_TEXTURE_2D);
567  m_tex_man->Begin();
568 
570  // The old preview shows the lowest numbered image on top, so do the same:
571  for (int img = imgs - 1; img != -1; img--)
572  {
573  // only draw active images
574  if (m_pano->getImage(img).getActive())
575  {
576  // the tools can cancel drawing of images.
578  {
579  // the texture manager may need to call the display list
580  // multiple times with blending, so we pass it the display list
581  // rather than switching to the texture and then calling the
582  // list ourselves.
585  }
586  }
587  }
588 
589  m_tex_man->End();
590  // drawn things after the active image.
593 
594  glPopMatrix();
595 }
596 
597 
598 vigra::Diff2D GLPlaneOverviewRenderer::Resize(int w, int h)
599 {
600  width = w;
601  height = h;
602  glViewport(0, 0, width, height);
603  // we use the view_state rather than the panorama to allow interactivity.
605  width_o = options->getWidth();
606  height_o = options->getHeight();
607 
608  glMatrixMode(GL_PROJECTION);
609  glLoadIdentity();
610 
611  double fov = m_visualization_state->getFOV();
612  double fovy;
613  if (h > w) {
614  fovy = 2.0 * atan( tan(fov * M_PI / 360.0) * (float) h / (float) w) / M_PI * 180.0;
615  } else {
616  fovy = fov;
617  }
618 
619  float ratio = 1.0* w / h;
620 // aspect = ratio;
621  glMatrixMode(GL_PROJECTION);
622  glLoadIdentity();
623  gluPerspective(fovy,ratio,1,1000000);
624 
625  m_visualization_state->SetVisibleArea(vigra::Rect2D(0, 0, options->getWidth(),
626  options->getHeight()));
627 
628  double R = m_visualization_state->getR();
629  double scrscale = (float) h / (2 * tan(fovy / 360.0 * M_PI) * R * options->getWidth() / MeshManager::PlaneOverviewMeshInfo::scale);
630  m_visualization_state->SetScale(scrscale);
631 // m_visualization_state->SetGLScale(gl_scale);
633 
634  return vigra::Diff2D(0,0);
635 }
636 
implementation of huginApp Class
void SetScale(float scale)
Definition: ViewState.cpp:378
void AfterDrawImageNumber(unsigned int image)
Definition: ToolHelper.cpp:233
hugin_utils::FDiff2D GetViewingCenter() const
Definition: ViewState.h:232
void AfterDrawImages()
Definition: ToolHelper.cpp:189
double X
unsigned int getHeight() const
get panorama height
GLRenderer(const wxColour backgroundColour)
ctor.
Definition: GLRenderer.cpp:50
void MarkToolsDirty()
Definition: GLViewer.cpp:479
void DisableTexture(bool maskOnly=false)
void SetCanvasSize(const vigra::Size2D &canvasSize)
Definition: ViewState.cpp:517
include file for the hugin project
ToolHelper * m_tool_helper
Definition: GLRenderer.h:75
double GetZoomLevel() const
Definition: ViewState.h:227
bool GetLayoutMode() const
return if layout mode is active
HuginBase::PanoramaOptions * GetOptions()
Definition: ViewState.cpp:273
bool BeforeDrawImageNumber(unsigned int image)
Definition: ToolHelper.cpp:212
vigra::Diff2D Resize(int width, int height)
Resize the viewport because the window&#39;s dimensions have changed.
Definition: GLRenderer.cpp:433
const vigra::Rect2D & getROI() const
Model for a panorama.
Definition: Panorama.h:152
int height
Definition: GLRenderer.h:76
HuginBase::Panorama * m_pano
Definition: GLRenderer.h:72
MeshManager * GetMeshManager()
Definition: ViewState.h:207
std::size_t getNrOfImages() const
number of images.
Definition: Panorama.h:205
The renderer handles drawing the opengl scene.
Definition: GLRenderer.h:47
GLPlaneOverviewRenderer(HuginBase::Panorama *pano, TextureManager *tex_man, MeshManager *mesh_man, PlaneOverviewVisualizationState *visualization_state, PlaneOverviewToolHelper *tool_helper, const wxColour backgroundColour)
Definition: GLRenderer.cpp:91
float height_o
Definition: GLRenderer.h:67
PlaneOverviewVisualizationState * m_visualization_state
Definition: GLRenderer.h:128
GLPreviewRenderer(HuginBase::Panorama *pano, TextureManager *tex_man, MeshManager *mesh_man, VisualizationState *visualization_state, PreviewToolHelper *tool_helper, const wxColour backgroundColour)
Definition: GLRenderer.cpp:69
virtual ~GLRenderer()
dtor.
Definition: GLRenderer.cpp:65
IMPEX double h[25][1024]
Definition: emor.cpp:169
wxColour m_background_color
Definition: GLRenderer.h:77
ViewState * getViewState()
Definition: ViewState.h:239
unsigned int getWidth() const
A MeshManager handles the graphics system representation of a remapping, by creating OpenGL display l...
Definition: MeshManager.h:37
void DrawImage(unsigned int image_number, unsigned int display_list)
void SetVisibleArea(vigra::Rect2D area)
Definition: ViewState.h:213
MeshManager * m_mesh_man
Definition: GLRenderer.h:74
void SetBackground(unsigned char red, unsigned char green, unsigned char blue)
Definition: GLRenderer.cpp:60
double Y
GLViewer * GetViewer()
Definition: ViewState.h:251
float width_o
Definition: GLRenderer.h:67
GLPanosphereOverviewRenderer(HuginBase::Panorama *pano, TextureManager *tex_man, MeshManager *mesh_man, PanosphereOverviewVisualizationState *visualization_state, PanosphereOverviewToolHelper *tool_helper, const wxColour backgroundColour)
Definition: GLRenderer.cpp:80
VisualizationState * m_visualization_state
Definition: GLRenderer.h:95
#define DEG_TO_RAD(x)
Definition: hugin_math.h:44
void SetPreviewBackgroundColor(const wxColour c)
sets the preview background color
Definition: GLRenderer.cpp:55
TextureManager * m_tex_man
Definition: GLRenderer.h:73
const SrcPanoImage & getImage(std::size_t nr) const
get a panorama image, counting starts with 0
Definition: Panorama.h:211
vigra::Diff2D Resize(int width, int height)
Resize the viewport because the window&#39;s dimensions have changed.
Definition: GLRenderer.cpp:598
Panorama image options.
#define M_PI
Definition: GaborFilter.cpp:34
void BeforeDrawImages()
Definition: ToolHelper.cpp:173
PanosphereOverviewVisualizationState * m_visualization_state
Definition: GLRenderer.h:111
unsigned int GetDisplayList(unsigned int image_number) const
HuginBase::PanoramaOptions * GetOptions()
Definition: ViewState.cpp:548
vigra::Diff2D Resize(int width, int height)
Resize the viewport because the window&#39;s dimensions have changed.
Definition: GLRenderer.cpp:103