Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
GLProjectionParametersBase.cpp
1 #include "GLProjectionParametersBase.hh"
2 #ifdef __APPLE__
3 # include <OpenGL/glu.h>
4 #else // __APPLE_
5 # include <GL/glu.h>
6 #endif // __APPLE__
7 #include <OpenGLFramework/Base/glfScreen.hh>
8 #include <OpenGLFramework/Base/glfException.hh>
9 #include <GLviewer/OutputAttachments/OutputWowDisplay.hh>
10 #include <GLviewer/OutputAttachments/OutputHaze.hh>
11 #include <GLviewer/OutputAttachments/OutputWowDisplay2.hh>
12 #include <GLviewer/OutputAttachments/OutputUnderwater.hh>
13 #include <GLviewer/OutputAttachments/OutputLensDistortion.hh>
14 #include <Utils/GlewInitWrapper.hh>
15 #include <Base/Common/CompareFloatingPoint.hh>
16 using namespace BIAS;
17 using namespace std;
18 
19 #define RAD_TO_DEG 57.29577951
20 //#define DEBUG_RESCALE
21 
23 
26 {
27  //instances
30  //bools
31  callInitBeforeDraw_ =true;
33  useStencilBuffer_ =false;
35 
38  outputAttachments_.clear();
39 
40  currentRenderTarget_ = NULL;
41  backgroundImageScene_ = NULL;
42  //others
44  ClearDepth_=1.0;
45  //init func
46  InitRaw_();
47 }
48 
51 {}
52 
53 
55 SetDebugLevel(const long int lv)
56 {
58 }
59 
60 
62 AddDebugLevel(const long int lv)
63 {
65 #ifdef BIAS_HAVE_CLASS_DISTORTIONRENDERING
66  distortionRendering_.AddDebugLevel(lv);
67 #endif
68 #ifdef BIAS_HAVE_CLASS_FISHEYERENDERING
69  fisheyeRendering_.AddDebugLevel(lv);
70 #endif
71 }
72 
75  SetExtrinsics(p.GetC(),p.GetR());
76  SetIntrinsics(&p);
77 }
78 
81  BIASERR("Call derived class!");
82 }
83 
86  BIASERR("Call derived class!");
87 }
88 
91 {
92  if(RT == NULL)
93  {
95  }
96  else
97  {
99  }
100 }
101 
104 {
105  // choose render target: offscreen rendering overrides 'currentRenderTarget_'
107 
108  #ifdef BIAS_HAVE_CLASS_OFFSCREENRENDERING
110  renderTarget = offscreenRendering_.GetFBO();
111  }
112  #endif
113 
114  return renderTarget;
115 }
116 
117 
120 {
121  zNear_ = 0.1;
122  zFar_ = 3000.0;
125 }
126 
129 {
131  drawingUpperLeft_[0] = 0;
132  drawingUpperLeft_[1] = 0;
133  lastDrawingUpperLeft_[0] = 0;
134  lastDrawingUpperLeft_[1] = 0;
135 
136  for(unsigned int i=0; i<3; i++) {
137  lastUsedGLViewport_[i] = 0;
138  lastFullGLViewport_[i] = 0;
139  }
142 }
143 
144 
147  float m[16] =
148  {1.0, 0.0, 0.0, 0.0,
149  0.0, -1.0, 0.0, 0.0,
150  0.0, 0.0, -1.0, 0.0,
151  0.0, 0.0, 0.0, 1.0 };
152  glMatrixMode(GL_MODELVIEW);
153  glLoadMatrixf(m);
154 }
155 
156 
158 Draw(std::vector<SceneBase *>& scenes,
159  SceneBGImage* backgroundImageScene,
160  BIAS::glfFramebufferObject* theTarget )
161 {
162  { GLenum err = glGetError(); if (err != GL_NO_ERROR) { BIASERR("My OpenGL Error 3: " << gluErrorString(err)); } }
163 
164  unsigned int imgWidth, imgHeight;
165 
166 
167  //backup pointer to backgroundscene
168  backgroundImageScene_ = backgroundImageScene;
169 
170  //set render target
171  if(theTarget != NULL) SetRenderTarget(theTarget);
172 
174  BIASERR("failure updating ModelViewMatrix\n");
175  return -1;
176  }
177 
178  GetMyselfAsProjectionParameterBase()->GetImageSize(imgWidth, imgHeight);
179  BIASDOUT(GLP_VIEWPORT,"Size from ProjectionParameters: "<<imgWidth<<"x"<<imgHeight);
180  BIASASSERT(imgWidth!=0 && imgHeight!=0);
181 
182 #ifdef BIAS_HAVE_CLASS_OFFSCREENRENDERING
184  SetGLViewport_(0,0,imgWidth,imgHeight);
185  BIASDOUT(GLP_VIEWPORT,"Setting viewport for offscreen to: "<<imgWidth<<"x"<<imgHeight);
186  }
187  try{
188  if(offscreenRenderingEnabled_ && !offscreenRendering_.IsInitialized()) {
189  unsigned int imgWidth, imgHeight;
190  GetMyselfAsProjectionParameterBase()->GetImageSize(imgWidth, imgHeight);
191  BIASDOUT(GLP_VIEWPORT,"Imagesize in BeginDraw:"<<imgWidth<<"x"<<imgHeight);
192  if(offscreenRendering_.Init(imgWidth, imgHeight)!=0) {
193  BIASERR("offscreen rendering could not be initialized\n");
194  return -1;
195  }
196  }
197  }
198  catch (glfException& e){
199  BIASERR("GL Error in Offscreenrender initialization in Draw() : " << e.GetMessageString());
200  return -1;
201  }
202 #endif // BIAS_HAVE_CLASS_OFFSCREENRENDERING
203  int viewport[4];
204  GetGLViewport_(viewport[0],viewport[1],viewport[2],viewport[3]);
205 
206  BIASDOUT(GLP_VIEWPORT,"Getting Viewport for control in Draw:"<<viewport[0]
207  <<","<<viewport[1]<<","<<viewport[2]<<","<<viewport[3]);
208 
209  // this is necessary, otherwise the modelview matrix is not set correctly
210  if(UpdateGLProjectionMatrix_(0, 0, imgWidth, imgHeight)!=0) {
211  BIASERR("error updating Projection matrix!");
212  return -1;
213  }
214  //prepare attachments
216 
217  //call either Draw_() or DrawWithMatchedParamsAndViewport_(...)
218  if(imgWidth == (unsigned)viewport[2] &&
219  imgHeight == (unsigned)viewport[3] &&
221  {
222  lastDrawingUpperLeft_[0] = viewport[0]; //why upperleft? isn't this the lower left?
223  lastDrawingUpperLeft_[1] = viewport[1];
224 
225  if (outputAttachments_.empty()) {
226  return Draw_(scenes);
227  } else {
228  int ret = 0;
229  for (unsigned int i=0;i<outputAttachments_.size();i++) {
230  ret = Draw_(scenes, outputAttachments_[i]->GetOutputFBO() );
231  if (ret!=0)
232  return ret;
233  }
234  }
235  }
236  else{
237  try{
238  return DrawWithMatchedParamsAndViewport_(viewport,
239  imgWidth, imgHeight,
240  scenes);
241  }catch (glfException& e){
242  BIASERR("GL Error in Draw() : " << e.GetMessageString());
243  return -1;
244  }
245  }
246  return 0;
247 }
248 
250 GetGLViewport_(int &lowerleftx,int &lowerlefty,
251  int &sizex,int &sizey){
252  int viewport[4];
253  glGetIntegerv(GL_VIEWPORT, viewport);
254  lowerleftx = viewport[0];
255  lowerlefty = viewport[1];
256  sizex = viewport[2];
257  sizey = viewport[3];
258  return 0;
259 }
260 
262 SetGLViewport_(int lowerleftx,int lowerlefty,
263  int sizex,int sizey){
264  glViewport(lowerleftx,lowerlefty,sizex,sizey);
265  lastUsedGLViewport_[0] = lowerleftx;
266  lastUsedGLViewport_[1] = lowerlefty;
267  lastUsedGLViewport_[2] = sizex;
268  lastUsedGLViewport_[3] = sizey;
269  return 0;
270 }
271 
272 /*
273  * Implementing in this method:
274  * (mainly about lastUsedViewport_) Draw_ will store
275  * the found viewport set in GL into the member lastUsedViewport_.
276  * This should help speeding up decisions in this method.
277  * Moreover its necessary to determine the valid window part for
278  * user interaction and calculation of image to viewport coordinates.
279  * If changing the viewport in this method it should be reset to the
280  * argument values before return.
281  */
284  int imageWidth,int imageHeight,
285  std::vector<SceneBase*>& scenes)
286 {
287  int res = 0;
288  /*
289  cout << "entering GLPPB::DrawWithMatchedParamsAndViewport_() with image ("
290  << imageWidth << " x " << imageHeight << ")" << endl;
291  */
293  {
294  case Deny:
295  BIASERR("viewport dimensions do not match parameters!"<<endl
296  <<"image="<<imageWidth<<"x"<<imageHeight<<" viewport="
297  <<viewport[0]<<";"<<viewport[1]<<" -> "
298  <<viewport[2]<<";"<<viewport[3]<<endl);
299  break;
300  case AutoRescaleParams:
301  {
302  lastDrawingUpperLeft_[0] = 0;
303  lastDrawingUpperLeft_[1] = 0;
304  if(lastFullGLViewport_[2] == viewport[2] &&
305  lastFullGLViewport_[3] == viewport[3] &&
306  lastUsedGLViewport_[2] > 0 &&
307  lastUsedGLViewport_[3] > 0 )
308  {
311 
312  res = Draw_(scenes);
313 #ifdef DEBUG_RESCALE
314  cout<<"Same Viewport:"<<lastUsedGLViewport_[0]<<","<<
315  lastUsedGLViewport_[1]<<","<<lastUsedGLViewport_[2]<<","<<
316  lastUsedGLViewport_[3]<<endl;
317 #endif
318  }
319  else
320  {
321  for(unsigned int i=0; i<4; i++)
322  lastFullGLViewport_[i] = viewport[i];
323 
324 #ifdef DEBUG_RESCALE
325  cout<<"new Viewport:"<<viewport[0]<<","<<
326  viewport[1]<<","<<viewport[2]<<","<<
327  viewport[3]<<endl;
328 #endif
329 
330  //determine whether enlarging or shrinking the image
331  //in this case scale greater one means enlarging the image
332  double decisionScaleWidth = ((double)viewport[0] - (double)viewport[2]) / (double)imageWidth;
333  double decisionScaleHeight = ((double)viewport[1]- (double)viewport[3]) / (double)imageHeight;
334  double decisionScale;
335  if(decisionScaleWidth<decisionScaleHeight)
336  decisionScale = decisionScaleWidth;
337  else
338  decisionScale = decisionScaleHeight;
339 
340  if(Equal(decisionScale,1.0))
341  {
342  //documentation!! What does it do??
343  SetGLViewport_(viewport[0], viewport[1]+(viewport[3]-imageHeight),
344  imageWidth, imageHeight);
345  res = Draw_(scenes);
346  SetGLViewport_(viewport[0], viewport[1],viewport[2], viewport[3]);
347  }
348  else
349  {
350  unsigned int newImageWidth=0, newImageHeight=0;
351  int ggT = imageWidth;
352  int b = imageHeight;
353  int r;
354  while (b > 0)
355  {
356  r = ggT % b;
357  ggT = b;
358  b = r;
359  }
360 
361  if(ggT == 1 && decisionScale < 1)
362  {
363  BIASERR("viewport too small for rescale");
364  return -1;
365  }
366  else
367  {
368  int smallestImageWidth = imageWidth / ggT;
369  int smallestImageHeight = imageHeight / ggT;
370 
371  decisionScaleWidth = (double)viewport[2]/(double)smallestImageWidth;
372  decisionScaleHeight= (double)viewport[3]/(double)smallestImageHeight;
373  if(decisionScaleWidth<decisionScaleHeight)
374  decisionScale = decisionScaleWidth;
375  else
376  decisionScale = decisionScaleHeight;
377  int flooredDecisionScale = (int)floor(decisionScale);
378  if(flooredDecisionScale == 1)
379  {
380  newImageWidth = smallestImageWidth;
381  newImageHeight = smallestImageHeight;
382  }
383  if(flooredDecisionScale > 1)
384  {
385  newImageWidth = (int) ((double)smallestImageWidth * flooredDecisionScale);/*decisionScale*/
386  newImageHeight = (int)((double)smallestImageHeight * flooredDecisionScale);/*decisionScale*/
387  }
388  if(flooredDecisionScale < 1)
389  {
390  BIASERR("this should never happen!");
391  return -100;
392  }
393  /*
394  if(decisionScale == 1.0)
395  {
396  newImageWidth = smallestImageWidth;
397  newImageHeight = smallestImageHeight;
398  }
399  if(decisionScale > 1)
400  {
401  newImageWidth = (int) ((double)smallestImageWidth * decisionScale);
402  newImageHeight = (int)((double)smallestImageHeight * decisionScale);
403  }
404  if(decisionScale < 1)
405  {
406  BIASERR("this should never happen!");
407  return -100;
408  }
409  */
410  }//else ggT > 1
411 
412  // ratio < 1 means image has been enlarged
413  double ratio = double(imageWidth)/ double(newImageWidth);
414  GLProjectionParametersBase::Rescale(ratio); //sets intrinsicschanged = true;
415  if(UpdateGLProjectionMatrix_(0, 0, newImageWidth, newImageHeight)!=0)
416  {
417  BIASERR("UpdateGLProjectionMatrix_ failed");
418  return -1;
419  }
420  //unter linke ecke: viewport[0] und [1]
421  SetGLViewport_(viewport[0], viewport[1]+(viewport[3]-newImageHeight),
422  newImageWidth, newImageHeight);
423 #ifdef DEBUG_RESCALE
424  cout<<"setting the vp to: "<<viewport[0]<<" "
425  <<viewport[1]+(viewport[3]-newImageHeight)<<" "
426  <<newImageWidth<<" "<<newImageHeight<<endl;
427 #endif
428  res = Draw_(scenes);
429  }
430  }
431  break;
432  }
433  case AdaptToCroppedImage:
434 
435  if(lastFullGLViewport_[2] == viewport[2] &&
436  lastFullGLViewport_[3] == viewport[3])
437  {
438  res = Draw_(scenes);
439  }
440  else
441  {
442  BIASWARN("AdaptToCroppedImage disabled on last Vision-N merge, reactivate if required!");
443  // double decisionScaleWidth = (double)viewport[2] / (double)imageWidth;
444  // double decisionScaleHeight = (double)viewport[3] / (double)imageHeight;
445  // double decisionScale;
446 
447  // if(decisionScaleWidth>decisionScaleHeight)
448  // decisionScale = 1.0/decisionScaleWidth;
449  // else
450  // decisionScale = 1.0/decisionScaleHeight;
451 
452 
453  // //force new image dimension (don't use rescale - it fails because of rounding errors)
454  // SetImageSize(viewport[2],viewport[3]);
455  // SetIdealImageSize(viewport[2],viewport[3]);
456  // //scale principal point
457  // double cx,cy;
458  // GetPrincipal(cx,cy);
459  // SetPrincipal(cx/decisionScale,cy/decisionScale);
460  // //scale focal length
461  // double f;
462  // //TODO adjust skew ...
463  // GetFocalLength(f);
464  // SetFocalLengthAndAspect(f/decisionScale,(double)viewport[2]/(double)viewport[3]);
465  // GLProjectionParametersBase::Rescale(1.0); //create new K
466 
467  // for(unsigned int i=0; i<4; i++)
468  // lastFullGLViewport_[i] = viewport[i];
469 
470  // SetGLViewport_(lastFullGLViewport_[0],
471  // lastFullGLViewport_[1],
472  // lastFullGLViewport_[2],
473  // lastFullGLViewport_[3]);
474  // IntrinsicStateChanged_=true;
475  // informScenesOfChange_ = true;
476  // if (UpdateGLProjectionMatrix_(lastFullGLViewport_[0],
477  // lastFullGLViewport_[1],
478  // lastFullGLViewport_[2],
479  // lastFullGLViewport_[3])!=0) BIASABORT;
480  // InitShaders_();
481  // res = Draw_(scenes);
482  }
483  break;
484  case AutoCutImage:
485  {
486  int visibleWidth = imageWidth - drawingUpperLeft_[0];
487  int visibleHeight = imageHeight - drawingUpperLeft_[1];
488 
489  int newWidth, newHeight, newLLY;
490  if(visibleWidth < viewport[2])
491  {
492  //viewport is bigger then image -> adapt viewport + move viewport
493  newWidth = visibleWidth;
494  }
495  else
496  {
497  //viewport is smaller then image -> adapt image
498  newWidth = viewport[2];
499  }
500 
501  if(visibleHeight < viewport[3])
502  {
503  //viewport is bigger then image->adapt viewport
504  newHeight = visibleHeight;
505  newLLY = viewport[3] - visibleHeight;
506  }
507  else
508  {
509  //viewport is smaller then image -> adapt image
510  newHeight = viewport[3];
511  newLLY = viewport[1];
512  }
513  SetGLViewport_(viewport[0], newLLY,
514  newWidth, newHeight);
515  lastDrawingUpperLeft_[0] = drawingUpperLeft_[0];
516  lastDrawingUpperLeft_[1] = drawingUpperLeft_[1];
517  res = DrawPartOfImage_(drawingUpperLeft_[0],
518  drawingUpperLeft_[1],
519  newWidth, newHeight,
520  scenes);
521  SetGLViewport_(viewport[0], viewport[1],
522  viewport[2], viewport[3]);
523  } break;
524 
525  case AutoRescaleSimple:
526  {
527  lastDrawingUpperLeft_[0] = 0;
528  lastDrawingUpperLeft_[1] = 0;
529 
530  if(lastFullGLViewport_[2] == viewport[2] &&
531  lastFullGLViewport_[3] == viewport[3] &&
532  lastUsedGLViewport_[2] > 0 && lastUsedGLViewport_[3] > 0 ) {
533  /*
534  cout<<"(ARS) same Viewport: "
535  <<lastUsedGLViewport_[0]<<", "<<lastUsedGLViewport_[1]<<", "
536  <<lastUsedGLViewport_[2]<<", "<<lastUsedGLViewport_[3]<<endl;
537  */
538 
541 
542 
543  res = Draw_(scenes);
544 
545  }
546  else {
547  /*
548  cout<<"(ARS) new Viewport: "
549  <<viewport[0]<<", "<<viewport[1]<<", "
550  <<viewport[2]<<", "<<viewport[3]<<endl;
551  */
552  for(unsigned int i=0; i<4; i++) {
553  lastFullGLViewport_[i] = viewport[i];
554  }
555 
556  //determine whether enlarging or shrinking the image
557  //in this case scale greater one means enlarging the image
558  double decisionScaleWidth = (double)viewport[2] / (double)imageWidth;
559  double decisionScaleHeight = (double)viewport[3] / (double)imageHeight;
560  /*
561  cout<<"\nGLPPB::DrawWithMatchedParamsAndViewport_():\n" << setprecision(10)
562  << "\tdecisionScaleWidth = viewport[2] / imageWidth: " << decisionScaleWidth
563  << " = " << viewport[2] << " / " << imageWidth << endl;
564  cout<< setprecision(10)
565  << "\tdecisionScaleHeight = viewport[3] / imageHeight: " << decisionScaleHeight
566  << " = " << viewport[3] << " / " << imageHeight << endl;
567  */
568  double decisionScale;
569  if(decisionScaleWidth<decisionScaleHeight)
570  decisionScale = decisionScaleWidth;
571  else
572  decisionScale = decisionScaleHeight;
573 
574  if( Equal(decisionScale,1.0) ) {
575  /*
576  cout<<"\nGLPPB::DrawWithMatchedParamsAndViewport_(): no rescale" << setprecision(10)
577  << "\n\tdecisionScale = " << decisionScale
578  << "\n\tviewport ("
579  <<viewport[0]<<", "<<viewport[1]<<", "
580  <<viewport[2]<<", "<<viewport[3]<<")"
581  <<"\n\timage (" << imageWidth << " x " << imageHeight << ")" <<endl;
582  */
583  SetGLViewport_(viewport[0], viewport[1]+(viewport[3]-imageHeight),
584  imageWidth, imageHeight);
585 
586  res = Draw_(scenes);
587 
588  SetGLViewport_(viewport[0], viewport[1],viewport[2], viewport[3]);
589  /*
590  cout << "drawing unscaled: setting vp to: "
591  <<viewport[0]<<", "<<viewport[1]<<", "
592  <<viewport[2]<<", "<<viewport[3]<<endl;
593  */
594  }
595  else { // !Equal(decisionScale,1.0)
596  double tmpImageHeight, tmpImageWidth;
597  tmpImageWidth = (double)imageWidth * decisionScale;
598  tmpImageHeight = (double)imageHeight * decisionScale;
599  double ratio = (double)imageWidth / tmpImageWidth;
600  /*
601  cout << "GLPPB::DrawWithMatchedParamsAndViewport_() rescale: "
602  << "\n\tratio = " << ratio << ", decisionScale = " << decisionScale
603  << "\n\timage (" << imageWidth << " x " << imageHeight << ")"
604  << "\n\tnew image (" << newImageWidth << " x " << newImageHeight << ")" << endl;
605  */
606  GLProjectionParametersBase::Rescale(ratio); //sets intrinsicschanged = true;
607 
608  int newImageWidth = (int)rint(tmpImageWidth);
609  int newImageHeight = (int)rint(tmpImageHeight);
610  if( UpdateGLProjectionMatrix_( 0, 0,
611  newImageWidth, newImageHeight) != 0 ) {
612  return -1;
613  }
614  //untere linke ecke: viewport[0] und [1]
615  SetGLViewport_(viewport[0], viewport[1]+(viewport[3]-newImageHeight),
616  newImageWidth, newImageHeight);
617  /*
618  cout<<"drawing scaled: setting vp to: "
619  <<viewport[0]<<", "<<viewport[1]+(viewport[3]-newImageHeight)<<", "
620  <<newImageWidth<<", "<<newImageHeight<<endl;
621  */
622  res = Draw_(scenes);
623  } // end if (Equal(decisionScale,1.0))
624  }
625  break;
626  }
628  {
629  //BIASWARN("AutoSimplePerspectiveCam disabled on last Vision-N merge, reactivate if required!");
630  //assuming simple perspective camera with principle point on image center
632  = dynamic_cast<ProjectionParametersPerspective*>(GetMyselfAsProjectionParameterBase());
633 
634  if(ppp){
635 
636  for(unsigned int i=0; i<4; i++)
637  lastFullGLViewport_[i] = viewport[i];
638 
639  /*double flength = ppp->GetFocalLength();
640  double ysize = ppp->GetImageHeight();
641  double normedImageSizeHalf = ysize/2.0;
642  double angleDEG = 2.0*atan(normedImageSizeHalf/ppp->GetFocalLength());*/
643 
644  //TODO: Retrieve the FoV from the projection without degenerating
645  // for now the FoV is fixed to 1.5
646  ppp->SetSimplePerspective(1.5, viewport[2], viewport[3]);
647 
648  if(UpdateGLProjectionMatrix_(0,0, viewport[2], viewport[3])!=0)
649  return -1;
650  lastDrawingUpperLeft_[0] = 0;
651  lastDrawingUpperLeft_[1] = 0;
652 
653  SetGLViewport_(0,0, viewport[2], viewport[3]);
654 
655  Draw_(scenes);
656  }
657  else
658  BIASWARN("AutoSimplePerspectiveCam only for perspective cameras, guess why?!");
659 
660  }
661  break;
662  default: cerr<<"unknown matching behaviour\n"; return -1; break;
663  }
664  return res;
665 }
666 
668 DrawPartOfImage_(const unsigned int ulX,
669  const unsigned int ulY,
670  const unsigned int width,
671  const unsigned int height,
672  std::vector<SceneBase *> scenes)
673 {
675  if(UpdateGLModelViewMatrix_()!=0)
676  return -1;
677 
678  if(UpdateGLProjectionMatrix_(ulX, ulY, width, height)!=0)
679  return -1;
680 
681 #ifdef BIAS_DEBUG
682  int viewport[4];
683  glGetIntegerv(GL_VIEWPORT, viewport);
684 
685  //image size and viewport resolution have to be the same
686  if((int)width!=viewport[2] || (int)height!=viewport[3]) {
687  cerr<<"viewport dimensions do not suffice!\n";
688  return -1;
689  }
690 #endif
691 
692  int res = Draw_(scenes);
693  IntrinsicStateChanged_ = true;//Draw() must calc new projection matrix
694  //the abstract parameters did not change so do not inform scenes!
695  return res;
696 }
697 
698 
701 {
703 }
704 
707 {
708 #ifdef DEBUG_RESCALE
709  BIAS::Vector4<int> tmp;
710  glGetIntegerv(GL_VIEWPORT, tmp.GetData());
711  cout << "\nentering BeginDraw_(): GLViewport is " << tmp[0]
712  << ", " << tmp[1]
713  << ", " << tmp[2]
714  << ", " << tmp[3] << endl;
715  BIASASSERT(tmp[1] != 1);
716 #endif
717 
718  if(callInitBeforeDraw_) {
719  try{
720  InitBeforeDraw_(); // call glewInit() and offscreen render init
721  callInitBeforeDraw_ = false;
722  }
723  catch (glfException& e){
724  BIASERR("Error in InitBeforeDraw in BeginDraw_() : " << e.GetMessageString());
725  return -1;
726  }
727  }
728 
729  // bind render target
730  glfRenderTarget* renderTarget;
731  if (theTarget == NULL) {
732  renderTarget = GetRenderTarget();
733  }
734  else{
735  renderTarget = theTarget;
736  }
737 
738  try{
739  renderTarget->Bind();
740  //added
742  renderTarget->ClearDepthBuffer(ClearDepth_);
743  if(useStencilBuffer_) {
744  renderTarget->ClearStencilBuffer();
745  //cout << "STENCIL USAGE IS ON!!!" << endl;
746  }
747  }
748  catch (glfException& e){
749  BIASERR("GL Error in BeginDraw_() : " << e.GetMessageString());
750  //return -1;
751  }
752  //push attributes to the attribute stack
753  glPushAttrib(GL_TRANSFORM_BIT | GL_ENABLE_BIT |
754  GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
755 
756  //blending, enabled in RenderContextBase with AllowBlending(bool)
757  if(glIsEnabled(GL_BLEND)) {
758  glBlendEquationSeparate(GL_FUNC_ADD, GL_MAX);
759  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
760  }
761 
762  //enable depth test
763  glEnable(GL_DEPTH_TEST);
764  glDepthFunc(GL_LEQUAL);
765 
766  //enable writing into the depth buffer
767  glDepthMask(GL_TRUE);
768 
769  //get glviewport and set to lastUsedGLViewport_
772 
773 #ifdef DEBUG_RESCALE
774  cout << "BeginDraw_(): get GLViewport: " << lastUsedGLViewport_[0]
775  << ", " << lastUsedGLViewport_[1]
776  << ", " << lastUsedGLViewport_[2]
777  << ", " << lastUsedGLViewport_[3] << endl;
778 #endif
779  //if intrinsics chnaged update GLProjectionMatrix
783  BIASERR("UpdateGLProjectionMatrix_ failed!");
784  BIASABORT;
785  }
786 
787  //projection and model view matrix
788  glMatrixMode(GL_PROJECTION);
789  glPushMatrix();
790  glLoadMatrixd(GLProjectionMatrix_.GetData());
791 
792  glMatrixMode(GL_MODELVIEW);
793  glPushMatrix();
794  glLoadMatrixd(GLModelViewMatrix_.GetData());
795 
796  if(useStencilBuffer_) glEnable(GL_STENCIL_TEST);
797 
798  return 0;
799 }
800 
803 {
804  GLF_THROW_ON_OPENGL_ERROR
805 #ifdef DEBUG_RESCALE
806  BIAS::Vector4<int> tmp;
807  glGetIntegerv(GL_VIEWPORT, tmp.GetData());
808  cout << "entering EndDraw_(): GLViewport is " << tmp[0]
809  << ", " << tmp[1]
810  << ", " << tmp[2]
811  << ", " << tmp[3] << endl;
812 #endif
814  //rendering set background image
815  if(backgroundImageScene_ != NULL) {
816  glEnable(GL_DEPTH_TEST);
817 #ifdef BIAS_HAVE_GLEW
818  // checkin for GL1.4 fails on Intel GMA965, but blending works, so it would be wise
819  // to check for something more detailed 20091103 evers
820  //if(GLEW_VERSION_1_4) {
821  if(glIsEnabled(GL_BLEND) == GL_TRUE ) {
823  BIASDOUT(GLP_ACTIVITY_HIGH, "rendering bg image !");
824 
825  glBlendEquationSeparate(GL_FUNC_ADD, GL_MAX);
826  glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
827  GLF_THROW_ON_OPENGL_ERROR
828  }
829  else {
830  BIASDOUT(GLP_ACTIVITY_HIGH,
831  "preparing subtraction from background image !");
832  glDisable(GL_DEPTH_TEST);
833  glBlendEquationSeparate(GL_FUNC_SUBTRACT, GL_MAX);
834  glBlendColor(0.5, 0.5, 0.5, 1.0);
835  glBlendFunc(GL_ONE, GL_CONSTANT_COLOR);
836 
837  glMatrixMode(GL_PROJECTION);
838  glPushMatrix();
839  glLoadIdentity();
840  glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
841  glMatrixMode(GL_MODELVIEW);
842  glPushMatrix();
843  glLoadIdentity();
844  glColor3f(0.5, 255.5, 0.5);
845  glBegin(GL_QUADS);
846  glVertex3f(-1, 1, -1);
847  glVertex3f(1, 1, -1);
848  glVertex3f(1, -1, -1);
849  glVertex3f(-1, -1, -1);
850  glEnd();
851 
852  glPopMatrix(); //model view
853  glMatrixMode(GL_PROJECTION);
854  glPopMatrix();
855 
856  glBlendEquationSeparate(GL_FUNC_ADD, GL_MAX);
857  // glBlendColor(0.5, 0.5, 0.5, 1.0);
858  glBlendFunc(GL_CONSTANT_COLOR, GL_ONE);
859  }
860  }
861  else {
862  BIASDOUT(GLP_ACTIVITY_HIGH,"Blending Disabled: skipping blending/subtraction");
863  }
864  // }//is gl version 1.4 supported?
865  /*else {
866  BIASERR("Blending or difference image calculation is"
867  " only available with gl version 1.4 or higher");
868  }*/
869 #endif
870  BIASDOUT(GLP_ACTIVITY_HIGH, "backgroundImageScene->Render() now !");
871  glDisable(GL_STENCIL_TEST);
873  }
874 
875  glMatrixMode(GL_PROJECTION);
876  glPopMatrix();
877  glMatrixMode(GL_MODELVIEW);
878  glPopMatrix();
879 
880  //pop attribs set in BeginDraw_()
881  //GL_TRANSFORM_BIT|GL_ENABLE_BIT|GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT
882  glPopAttrib();
883 
884 #ifdef BIAS_HAVE_CLASS_OFFSCREENRENDERING
887 #endif
888 
889  if (theTarget != NULL) {
890  try{
891  for (unsigned int i=0;i<outputAttachments_.size();i++)
892  outputAttachments_[ i ]->Draw();
893  }catch (glfException& e){
894  BIASERR("GL Error in BeginDraw_() : " << e.GetMessageString());
895  return -1;
896  }
897  }
898 #ifdef DEBUG_RESCALE
899 
900  glGetIntegerv(GL_VIEWPORT, tmp.GetData());
901  cout << "leaving EndDraw_(): GLViewport is " << tmp[0]
902  << ", " << tmp[1]
903  << ", " << tmp[2]
904  << ", " << tmp[3] << endl << endl;
905 #endif
906  GLF_THROW_ON_OPENGL_ERROR
907  return 0;
908 }
909 
912 {
913  if (!GetMyselfAsProjectionParameterBase()->PoseValid())
914  {
915  BIASERR("pose invalid?!");
916  return -1;
917  }
918 
919  //This have to be absolut values!
921  GetExtrinsics(C,R);
922 
923  unsigned int i;
924  for(i=0; i<3; i++) {
925  R[i][1] = -R[i][1];
926  R[i][2] = -R[i][2];
927  }
928 
929  BIAS::Matrix4x4<double> newGLModelViewMatrix(R.Transpose(),
930  (-1.0)*R.Transpose()*C);
931  //see docu of GLModelViewMatrices_ in RenderProjection.hh
932  GLModelViewMatrix_ = newGLModelViewMatrix.Transpose();
933 
934  ExtrinsicStateChanged_ = false;
935  return 0;
936 }
937 
939 GetViewportCoordinates(int& x, int& y)
940 {
941  //determine if x,y lie in viewport
942  int xInLastViewport = x - lastUsedGLViewport_[0];
943  int yInLastViewport = y - lastUsedGLViewport_[1];
944  if(xInLastViewport<0 ||
945  yInLastViewport<0 ||
946  xInLastViewport>lastUsedGLViewport_[2] ||
947  yInLastViewport>lastUsedGLViewport_[3])
948  {
949  cerr<<"argument point does not lie in last viewport\n";
950  return false;
951  }
952  x = xInLastViewport;
953  y = yInLastViewport;
954  return true;
955 }
956 
958 SetZClippingPlanes(float n, float f)
959 {
962 }
963 
966 {
967  zNear_ = n;
968  IntrinsicStateChanged_ = true;
969 }
970 
973 {
974  zFar_ = f;
975  IntrinsicStateChanged_ = true;
976 }
977 
978 
981 {
982  double depth = 0.0; depth = GetDepth(p);
983  if(depth==0.0) {
984  BIASWARNONCE("Depth is 0!");
985  Vector3<double> opticalray, pos;
986  GetMyselfAsProjectionParameterBase()->UnProjectToRay(p, pos, opticalray);
987  if(opticalray.NormL2()==0.0) {
988  BIASWARNONCE("returned ray has zero length!");
989  return HomgPoint3D(0,0,0,0);
990  }
991  return HomgPoint3D(opticalray[0], opticalray[1], opticalray[2], 0);
992  }
993 
994  return GetMyselfAsProjectionParameterBase()->UnProjectToPoint(p, depth);
995 }
996 
998 UnProject(const unsigned int x, const unsigned int y)
999 {
1000  //cout<<"[GLProjectionPArametersBase] Unproject "<<x<<","<<y<<endl;
1001  return UnProject(HomgPoint2D(x,y,1.0));
1002 }
1003 
1005 UnProject(BIAS::HomgPoint2D& point2D, double depth){
1006 
1007  return GetMyselfAsProjectionParameterBase()->UnProjectToPoint(point2D,depth);
1008 }
1009 
1010 
1013  return GetMyselfAsProjectionParameterBase()->Project(point);
1014 }
1015 
1016 
1019  BIAS::Vector3<double>& Up) const
1020 {
1021  ProjectionParametersBase* pPPB = GetMyselfAsProjectionParameterBase();
1022  C = pPPB->GetC();
1023  RMatrix R = pPPB->GetR();
1024  for(unsigned int i=0; i<3; i++)
1025  Up[i] = -R[i][1];
1026 
1027  return 0;
1028 }
1029 
1032  BIAS::RMatrix& R) const
1033 {
1034  BIAS::ProjectionParametersBase* pPPB=NULL;
1035  pPPB=GetMyselfAsProjectionParameterBase();
1036  if(pPPB == NULL) //this should never fail. xcept somebody made mistake...
1037  return -1;
1038  C = pPPB->GetC();
1039  R = pPPB->GetR();
1040  return 0;
1041 }
1042 
1043  /* Implement functions from GLProjectionParametersInterface*/
1046  const BIAS::Quaternion<double>& Q){
1047  BIAS::ProjectionParametersBase* pPPB=GetMyselfAsProjectionParameterBase();
1048  if(pPPB != NULL){
1049  ExtrinsicStateChanged_ = true;
1050  informScenesOfChange_ = true;
1051  Vector3<double> c(C[0],C[1],C[2]);
1052  pPPB->SetQC(Q,c);
1053  return 0;
1054  }
1055  else
1056  return -1;
1057 }
1058 
1061  const BIAS::RMatrix& R)
1062 {
1064  if(!pPPB) { cout<<"setextr failed"<<endl; return -1;}
1065  pPPB->SetC(C);
1066  pPPB->SetR(R);
1067  ExtrinsicStateChanged_ = true;
1068  informScenesOfChange_ = true;
1069  return 0;
1070 }
1071 
1074  const BIAS::Quaternion<double>& Q)
1075  {
1076  ProjectionParametersBase* pPPB = GetMyselfAsProjectionParameterBase();
1077  if(!pPPB) { cout<<"setextr failed"<<endl; return -1;}
1078  pPPB->SetC(C);
1079  pPPB->SetQ(Q);
1080  ExtrinsicStateChanged_ = true;
1081  informScenesOfChange_ = true;
1082  return 0;
1083  }
1084 
1087  const BIAS::RMatrix& R)
1088 {
1089  Vector3<double> tmp(C[0],C[1],C[2]);
1090  return SetExtrinsics(tmp,R);
1091 }
1092 
1094 SetExtrinsics(float eyex, float eyey, float eyez,
1095  float scenex, float sceney, float scenez,
1096  float upx, float upy, float upz)
1097 {
1098  return SetExtrinsics( BIAS::Vector3<double>(eyex,eyey,eyez),
1099  BIAS::Vector3<double>(scenex,sceney,scenez),
1100  BIAS::Vector3<double>(upx,upy,upz));
1101 }
1102 
1105  const BIAS::Vector3<double>& scenePoint,
1106  const BIAS::Vector3<double>& up)
1107 {
1108  // cout <<"SetExtrinsics(C,ScenePoint,up)"<<endl;
1109  // cout <<C<<","<<scenePoint<<","<<up<<endl;
1110  //calculate R and C
1111  RMatrix R;
1112  Vector3<double> A=(scenePoint-C);
1113  if(A.NormL2() == 0.0) {
1114  BIASERR("Camera center and scene point must not be the same!");
1115  C.Sub(Vector3<double>(0,0,-1),A);
1116  return -1;
1117  }
1118  A.Normalize();
1119  Vector3<double> V= -1.0*up;
1120  if(V.NormL2() == 0.0) {
1121  cerr<<"Up vector must not be the null vector \n";
1122  return -1;
1123  }
1124  V.Normalize();
1125  Vector3<double> H;
1126  V.CrossProduct(A,H);
1127  if(H.NormL2() == 0.0) {
1128  cerr<<"Up vector and viewing direction must not be parallel \n";
1129  return -1;
1130  }
1131  H.Normalize();
1132  //make sure the rotation matrix is orthonormal :
1133  A.CrossProduct(H,V);
1134  V.Normalize();
1135 
1136  for(unsigned int i=0; i<3; i++) {
1137  R[i][0] = H[i];
1138  R[i][1] = V[i];
1139  R[i][2] = A[i];
1140  }
1141 
1142  return SetExtrinsics(C, R);
1143 }
1144 
1145 
1146 /* has this been deleted by CAU ?
1147 void GLProjectionParametersBase::
1148 SetR(const BIAS::RMatrixBase& R)
1149 {
1150  Quaternion<double> Q;
1151  R.GetQuaternion(Q);
1152  SetQ(Q);
1153 }
1154 */
1155 
1158 {
1159  if(Less(p[0], 0.0) || Less(p[1], 0.0)) {
1160  BIASERR("point not in image");
1161  return 0.0;
1162  }
1163 
1164  unsigned int ul[2] = {(unsigned int)floor(p[0]), (unsigned int)floor(p[1])};
1165  double depthUL = GetDepth(ul[0], ul[1]);
1166  if(depthUL == 0.0) return 0.0;
1167 
1168  unsigned int ur[2] = {(unsigned int)ceil(p[0]), (unsigned int)floor(p[1])};
1169  double depthUR = GetDepth(ur[0], ur[1]);
1170  if(depthUR == 0.0) return 0.0;
1171 
1172  unsigned int ll[2] = {(unsigned int)floor(p[0]), (unsigned int)ceil(p[1])};
1173  double depthLL = GetDepth(ll[0], ll[1]);
1174  if(depthLL == 0.0) return 0.0;
1175 
1176  unsigned int lr[2] = {(unsigned int)ceil(p[0]), (unsigned int)ceil(p[1])};
1177  double depthLR = GetDepth(lr[0], lr[1]);
1178  if(depthLR == 0.0) return 0.0;
1179 
1180  double dx = p[0] - ul[0];
1181  double dy = p[1] - ul[1];
1182 
1183  return (depthUL +
1184  dy*(depthLL - depthUL) +
1185  dx*(depthUR - depthUL) +
1186  dy*dx*(depthUL + depthLR - depthLL - depthUR));
1187 }
1188 
1189 
1191 GetDepth(const unsigned int x, const unsigned int y)
1192 {
1193  GetRenderTarget()->Bind();
1194  float depthRead = 0.0;
1195  GLint accessX = 0;
1196  GLint accessY = 0;
1197  accessX = GLint(lastUsedGLViewport_[0]+x);
1198  accessY = lastUsedGLViewport_[1]+ lastUsedGLViewport_[3]-1-y;
1199  glReadPixels(accessX,
1200  accessY,
1201  1, 1,
1202  GL_DEPTH_COMPONENT,
1203  GL_FLOAT, &depthRead);
1204  // cout<<"[GLProjectionParametersBase] depthread (z):"<<depthRead<<endl;
1205  return Z2Depth_(x,y,depthRead);
1206 }
1207 
1210 {
1211  float* z = depthMap.GetImageData();
1212  //calculate distances on optical rays
1213  for ( int y=0; y<lastUsedGLViewport_[3] ; y++)
1214  {
1215  for ( int x=0; x<lastUsedGLViewport_[2]; x++)
1216  {
1217  *z = Z2Depth_(x,y,*z);
1218  z++;
1219  }
1220  }
1221 }
1222 
1223 
1225 Rescale(float scale)
1226 {
1227  Rescale_(scale);
1229 
1230 }
1231 
1233 GetDepthMap(BIAS::Image<float>& depthMap, const float invalid)
1234 {
1235  int res = GetZBuffer(depthMap);
1236  if(res!=0) return res;
1237  TranslateZToDepth(depthMap, invalid);
1238  return 0;
1239 }
1240 
1241 
1244  const float invalid,
1245  const bool flip) const {
1246  if(flip) depthMap.Flip();
1247  BIASASSERT(zFar_!=zNear_);
1248  float** z = depthMap.GetImageDataArray();
1249  //calculate distances on optical rays
1250  for ( unsigned y=0; y<depthMap.GetHeight() ; y++) {
1251  for ( unsigned x=0; x<depthMap.GetWidth(); x++) {
1252  if (z[y][x] >= 1.0) z[y][x] = invalid;
1253  else if(z[y][x] != invalid){
1254  z[y][x]=Z2Depth_(x,y,z[y][x]);
1255  }
1256  }
1257  }
1258 }
1259 
1260 
1261  /**
1262  \attention this is only valid for perspective cameras so far!
1263  */
1266  const float invalid,
1267  const bool flip) const {
1268  if(flip) depthMap.Flip(); //is an in out variable!
1269  float** z = depthMap.GetImageDataArray();
1270  Vector3<double> ray;
1271  //calculate distances on optical rays
1272  for (unsigned int y=0; y<depthMap.GetHeight() ; y++)
1273  {
1274  for (unsigned int x=0; x<depthMap.GetWidth(); x++)
1275  {
1276  if (z[y][x] >= 1.0) z[y][x] = invalid;
1277  else if(z[y][x] != invalid)
1278  {
1279  z[y][x] = zNear_*zFar_ / (zFar_ - (z[y][x])*(zFar_-zNear_));
1280  }
1281  }
1282  }
1283 }
1284 
1287  const float invalid,
1288  const bool flip) const
1289 {
1290  if(flip) depthtoz.Flip();
1291  float** depth = depthtoz.GetImageDataArray();
1292  Vector3<double> ray, pos;
1293  //calculate distances on optical rays
1294  for ( int y=0; y<lastUsedGLViewport_[3] ; y++)
1295  {
1296  for ( int x=0; x<lastUsedGLViewport_[2]; x++)
1297  {
1298  if(depth[y][x] != invalid)
1299  {
1300  //determine length of optical ray and assign to *z
1301  //if image is distorted will use undistortion
1302  GetMyselfAsProjectionParameterBase()->
1303  UnProjectToRay(HomgPoint2D(x,y,1.0), pos, ray);
1304  ray.Normalize();
1305  ray.MultIP(depth[y][x]);
1306  depth[y][x] = -((zNear_*zFar_)/ray[2]-zFar_)/(zFar_-zNear_);
1307  }
1308  }
1309  }
1310 }
1311 
1312 
1314 GetMetricZMap(BIAS::Image<float>& depthMap, const float invalid)
1315 {
1316  int res = GetZBuffer(depthMap);
1317  if(res!=0) return res;
1318  TranslateZToMetricZ(depthMap, invalid);
1319  return 0;
1320 }
1321 
1324 {
1325  GetRenderTarget()->Bind();
1326  glFinish();
1327  int depthbits;
1328  glGetIntegerv(GL_DEPTH_BITS,&depthbits);
1329  if(depthbits==0)
1330  {
1331  cerr<<"no depth buffer available\n";
1332  return -1;
1333  }
1334 
1335  if (lastUsedGLViewport_[2] ==0 || lastUsedGLViewport_[3] ==0)
1336  {
1337  BIASERR("You never rendered, no screenshot now.");
1338  return -1;
1339  }
1340 
1341  if( (lastUsedGLViewport_[2] != (int)zBuffer.GetWidth()) ||
1342  (lastUsedGLViewport_[3] != (int)zBuffer.GetHeight()) ||
1343  zBuffer.GetChannelCount()!=1)
1344  {
1345  zBuffer.Release();
1346  zBuffer.Init(lastUsedGLViewport_[2],
1347  lastUsedGLViewport_[3], 1);
1348  }
1349  //get z buffer
1350  glReadPixels(lastUsedGLViewport_[0],
1354  GL_DEPTH_COMPONENT,
1355  GL_FLOAT, zBuffer.GetImageData());
1356  // zBuffer.Flip();
1357  // set default render target.
1359  return 0;
1360 }
1361 
1363 ReadDepthBufferToTexture_(int texID, int width, int height)
1364 {
1365  glBindTexture(GL_TEXTURE_2D, texID);
1366  glCopyTexImage2D(GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT, 0,0, width, height, 0);
1367 }
1368 
1371 {
1372  GetRenderTarget()->Bind();
1373  glFinish();
1374  int redbits, greenbits;
1375  glGetIntegerv(GL_RED_BITS, &redbits);
1376  glGetIntegerv(GL_GREEN_BITS, &greenbits);
1377  int colorType = image.GetChannelCount();
1378  if (lastUsedGLViewport_[2] ==0||lastUsedGLViewport_[3] ==0)
1379  {
1380  BIASERR("You never rendered, no screenshot now.");
1381  return;
1382  }
1383 
1384  if((int)image.GetWidth() != lastUsedGLViewport_[2] ||
1385  (int)image.GetHeight()!= lastUsedGLViewport_[3] ||
1386  (colorType == 1 && greenbits>0) ||
1387  (colorType == 3 && greenbits==0) ||
1388  (colorType != 1 && colorType != 3 ) )
1389  {
1390  if(greenbits>0)
1391  colorType = 3;
1392  else
1393  {
1394  if(redbits>0)
1395  colorType = 1;
1396  else
1397  {
1398  cerr<<"red framebuffer bits are zero!";
1399  BIASABORT;
1400  }
1401  }
1402  image.Release();
1403  image.Init(lastUsedGLViewport_[2],
1404  lastUsedGLViewport_[3], colorType);
1405  }
1406  glFinish();
1407  // by default OpenGL assumes that rows are 4-byte-aligned,
1408  // which is wrong for BIAS::Image.
1409  glPixelStorei(GL_PACK_ALIGNMENT, 1);
1410  switch(colorType)
1411  {
1412  case 3:
1413  glReadPixels(lastUsedGLViewport_[0],
1417  GL_RGB,
1418  GL_UNSIGNED_BYTE, image.GetImageData());
1419  break;
1420  case 1:
1421  glReadPixels(lastUsedGLViewport_[0],
1425  GL_RED,
1426  GL_UNSIGNED_BYTE, image.GetImageData());
1427  break;
1428  }
1429 
1430  int err;
1431  if((err=glGetError()) != GL_NO_ERROR)
1432  cerr<<"Error getting image "<<gluErrorString(err)<<endl;
1433  image.Flip();
1434 
1436 }
1437 
1440 {
1441  GetRenderTarget()->Bind();
1442  int redbits, greenbits;
1443  glGetIntegerv(GL_RED_BITS, &redbits);
1444  glGetIntegerv(GL_GREEN_BITS, &greenbits);
1445 
1446  int colorType = image.GetChannelCount();
1447  if (lastUsedGLViewport_[2] ==0
1448  ||lastUsedGLViewport_[3] ==0)
1449  {
1450  BIASERR("You never rendered, no screenshot now.");
1451  return;
1452  }
1453 
1454  if((int)image.GetWidth() != lastUsedGLViewport_[2] ||
1455  (int)image.GetHeight() != lastUsedGLViewport_[3] ||
1456  (colorType != 1) )
1457  {
1458  if(!image.IsEmpty()) image.Release();
1459  image.Init(lastUsedGLViewport_[2],
1460  lastUsedGLViewport_[3], 1);
1461  }
1462 
1463 
1464  //wait till all gl stuff is done
1465  glFinish();
1466 
1467  //decision made by gl color type
1468  if(greenbits>0)
1469  colorType = 3;
1470  else
1471  if(redbits>0)
1472  colorType = 1;
1473  else
1474  {
1475  BIASERR("red framebuffer bits are zero!\n");
1476  BIASABORT;
1477  }
1478 
1479  switch(colorType)
1480  {
1481  case 3:
1482  glPushAttrib(GL_PIXEL_MODE_BIT);
1483  glPixelTransferf(GL_RED_SCALE, 0.299);
1484  glPixelTransferf(GL_GREEN_SCALE, 0.587);
1485  glPixelTransferf(GL_BLUE_SCALE, 0.114);
1486  glReadPixels(lastUsedGLViewport_[0],
1490  GL_LUMINANCE,
1491  GL_UNSIGNED_BYTE, image.GetImageData());
1492  glPopAttrib(); //GL_PIXEL_MODE_BIT
1493  break;
1494  case 1:
1495  glReadPixels(lastUsedGLViewport_[0],
1499  GL_RED,
1500  GL_UNSIGNED_BYTE, image.GetImageData());
1501  break;
1502  }
1503 
1504  if(glGetError() != GL_NO_ERROR)
1505  cerr<<"Error getting image "<<gluErrorString(glGetError())<<endl;
1506 
1507  image.Flip();
1508 
1509  // set default render target.
1511 }
1512 
1515 {
1516  informScenesOfChange_= true;
1517 }
1518 
1519 
1522 
1523  // BIASDOUT(GLP_ACTIVITY_BASIC,"Switching Instance:"<<instanceNum_<<" to offscreen");
1524 #ifndef BIAS_HAVE_CLASS_OFFSCREENRENDERING
1525  if(offscreen) {
1526  cerr<<"offscreen rendering cannot be activated due to missing or "<<
1527  "inactive GLEW/GL/CG features!\n";
1528  }
1530 #else
1531  offscreenRenderingEnabled_=offscreen;
1533 #endif
1534 }
1535 
1536 void GLProjectionParametersBase::AppendOutputAttachment(int type, std::vector<int> args) {
1537  if (!outputAttachments_.empty()) {
1538  BIASERR("adding output attachments after first call to draw not supported");
1539  return;
1540  }
1542  outputAttachmentIdentifiers_.push_front(type);
1543  outputAttachmentOptions_.push_front(args);
1544  } else {
1545  outputAttachmentIdentifiers_.push_back(type);
1546  outputAttachmentOptions_.push_back(args);
1547  }
1548 }
1549 
1550 // workaround for underwater needing to change parameters from outside, fkellner
1552  for (int i=0;i<int(outputAttachmentIdentifiers_.size());i++) {
1553  if (outputAttachmentIdentifiers_[i] == type) {
1554  return outputAttachments_[i];
1555  }
1556  }
1557  return NULL;
1558 }
1559 
1562  if ( outputAttachments_.empty() && !outputAttachmentIdentifiers_.empty()) {
1563  // attach extra out
1565 
1566  for (int i=int(outputAttachmentIdentifiers_.size())-1;i>=0;i--) {
1567 
1568  BIAS::glfRenderTarget *attachmentTarget;
1569  if (i == int(outputAttachmentIdentifiers_.size())-1) { // last one renders to screen
1570  attachmentTarget = GetRenderTarget();
1571  } else {
1572  attachmentTarget = outputAttachments_[i+1]->GetOutputFBO();
1573  }
1574 
1575  switch (outputAttachmentIdentifiers_[i]) {
1577  if (outputAttachmentOptions_[i].size()!=2) {
1578  outputAttachments_[i] = new OutputWowDisplay(attachmentTarget);
1579  }
1580  else {
1581  outputAttachments_[i] = new OutputWowDisplay(attachmentTarget,
1583  outputAttachmentOptions_[i][1]);
1584  }
1585  BIASDOUT(GLP_ACTIVITY_BASIC,"Adding output attachment: WoWVX");
1586  break;
1588  BIASDOUT(GLP_ACTIVITY_BASIC,"Adding output attachment: Haze");
1589  outputAttachments_[i] = new OutputHaze(attachmentTarget);
1590  break;
1592  BIASDOUT(GLP_ACTIVITY_BASIC,"Adding output attachment: WoWVX2");
1593  outputAttachments_[i] = new OutputWowDisplay2(attachmentTarget);
1594  break;
1596  BIASDOUT(GLP_ACTIVITY_BASIC,"Adding output attachment: Underwater");
1597  outputAttachments_[i] = new OutputUnderwater(attachmentTarget);
1598  break;
1600  BIASDOUT(GLP_ACTIVITY_BASIC,"Adding output attachment: Lens Distortion");
1601  outputAttachments_[i] = new OutputLensDistortion(attachmentTarget);
1602  dynamic_cast<OutputLensDistortion*>(outputAttachments_[i])->CreateLookupTable(this->GetMyselfAsProjectionParametersBase());
1603  break;
1604  default:
1605  BIASERR("Unknown Output Attachment, bailing out..");
1606  break;
1607  }
1608  outputAttachments_[i]->SetClearColor(ClearColor_);
1609  }
1610  }
1611 }
virtual BIAS::Vector3< double > GetC() const
Get projection center.
void Release()
reimplemented from ImageBase
Definition: Image.cpp:1579
bool Less(const T left, const T right, const T eps=std::numeric_limits< T >::epsilon())
comparison function for floating point values
virtual void TranslateZToDepth(BIAS::Image< float > &ztodepth, const float invalid=0.0, const bool flip=true) const
helper function which converts a z map obtained with the last camera into a depth map for the last ca...
BIAS::Vector2< unsigned int > lastDrawingUpperLeft_
Keeping track which image part was drawn last time Draw() was called.
virtual int EndDraw_(BIAS::glfFramebufferObject *theTarget=NULL)
virtual void Render()
this is the entry point seen from GLProjectionParametersBase.
Definition: SceneBase.cpp:26
BIAS::Vector4< int > lastFullGLViewport_
Remembering the last full viewport prevent unecessary calculations in AutoRescaleParams behaviour...
exemplary output attachment, creating a bluish haze effect on the rendered output ...
Definition: OutputHaze.hh:23
int GetZBuffer(BIAS::Image< float > &zBuffer)
int DrawPartOfImage_(const unsigned int ulX, const unsigned int ulY, const unsigned int width, const unsigned int height, std::vector< SceneBase * > scenes)
Renders a rectangular part of actual image.
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
int GetGLViewport_(int &lowerleftx, int &lowerlefty, int &sizex, int &sizey)
BIAS::HomgPoint2D Project(BIAS::HomgPoint3D &point)
void AddDebugLevel(const long int lv)
Definition: Debug.hh:355
exemplary output attachment, creating a underwater effect
std::deque< BIAS::GLProjectionOutputAttachment * > outputAttachments_
Scene that renders a background image behind all other scenes.
Definition: SceneBGImage.hh:42
virtual void SetR(const BIAS::RMatrix &R)
Set orientation from rotation matrix R.
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
Definition: ImageBase.hh:245
virtual void Assign(BIAS::ProjectionParametersBase &p)
int GetDepthMap(BIAS::Image< float > &depthMap, const float invalid=0.0)
Calculates depth map from current z-Buffer state.
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
virtual void SetRenderTarget(BIAS::glfRenderTarget *RT)
bool GetViewportCoordinates(int &x, int &y)
Takes x, y as window coordinates, checks whether the coordinates lie in the rendered image (in the vi...
void AppendOutputAttachment(int type, std::vector< int > args=std::vector< int >())
bool ExtrinsicStateChanged_
Changes to the parameters or GL state that lead to new calculation of GL_MODELVIEW matrix...
rescale internal projection parameters preserving aspect ratio and scale by integer values only...
virtual int BeginDraw_(BIAS::glfFramebufferObject *theTarget=NULL)
int SetGLViewport_(int lowerleftx, int lowerlefty, int sizex, int sizey)
bool IntrinsicStateChanged_
Changes to the parameters or GL state that lead to new calculation of GL_PROJECTION matrix...
int GetMetricZMap(BIAS::Image< float > &metricZMap, const float invalid=0.0)
BIAS::Vector4< int > lastUsedGLViewport_
Keeping track which part of image was drawn last time Draw() was called and where it was drawn...
virtual int GetExtrinsics(BIAS::Vector3< double > &C, BIAS::Vector3< double > &up) const
Exception class used for run-time errors in the OpenGLFramework.
Definition: glfException.hh:79
void SwitchToOffscreenRendering(bool offscreen=true)
unsigned int GetWidth() const
Definition: ImageBase.hh:312
virtual int Draw(std::vector< BIAS::SceneBase * > &scenes, BIAS::SceneBGImage *backgroundImageScene=NULL, BIAS::glfFramebufferObject *theTarget=NULL)
Renders scenes passed as argument beginning with the smallest index.
virtual void AddDebugLevel(const long int lv)
const std::string & GetMessageString() const
Returns the description of the error including the file name and line number where the error occured...
3D rotation matrix
Definition: RMatrix.hh:49
virtual void SetQ(const BIAS::Quaternion< double > &Q)
Set orientation from unit quaternion Q.
exemplary output attachment to create 2D+Depth output for Wow Displays
void CrossProduct(const Vector3< T > &argvec, Vector3< T > &destvec) const
cross product of two vectors destvec = this x argvec
Definition: Vector3.hh:594
GLProjectionOutputAttachment * GetAttachmentByType(int type)
Render part of the image or the whole image, does not change projection parameters.
virtual void Set(BIAS::ProjectionParametersBase *p)
static GlewInitWrapper * GetInstance()
BIAS::GLProjectionParametersInterface::ViewingReshapeBehaviour AutoViewingMatchBehaviour_
virtual void SetDebugLevel(const long int lv)
static glfScreen & GetInstance()
Returns the global screen instance.
Definition: glfScreen.cpp:30
virtual void Bind() const =0
Makes this render target the currently used render target.
BIAS::Vector2< unsigned int > drawingUpperLeft_
When cutting the image this field is telling the projection the upper left corner of the drawn image ...
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
virtual int Draw_(std::vector< SceneBase * > &scenes, BIAS::glfFramebufferObject *theTarget=NULL)=0
Implement in deriving classes.
void SetDebugLevel(const long int lv)
Definition: Debug.hh:318
virtual double Z2Depth_(const int x, const int y, const double z) const =0
unsigned int GetHeight() const
Definition: ImageBase.hh:319
virtual BIAS::RMatrix GetR() const
Get orientation as rotation matrix R.
virtual void SetGLCameraToLocal()
Sets gl&#39;s Modelview matrix for rendering relative to camera coordinate system (ignoring camera pose)...
Matrix4x4 Transpose() const
Definition: Matrix4x4.hh:315
virtual void SetIntrinsics(BIAS::ProjectionParametersBase *p)
T * GetData()
get the pointer to the data array of the matrix (for faster direct memeory access) ...
Definition: Matrix.hh:185
std::deque< std::vector< int > > outputAttachmentOptions_
virtual BIAS::glfRenderTarget * GetRenderTarget()
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
Calculate new simple perspective camera parameters keeping the current field of view in &quot;y direction&quot;...
void SetZClippingPlanes(float n, float f)
Setting the near and far z-Clipping planes.
void ClearDepthBuffer(float depth=1.0f)
Clears the depth buffer of the render target with the given value.
void GetImage(BIAS::Image< unsigned char > &image)
virtual void Bind() const
Makes this render target the currently used render target.
Definition: glfScreen.cpp:36
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
Definition: Image.cpp:421
bool Equal(const T left, const T right, const T eps)
comparison function for floating point values See http://www.boost.org/libs/test/doc/components/test_...
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
Definition: Image.hh:137
void Sub(const T &scalar, Vector3< T > &dest) const
Substraction with a scalar, storing results in destination vector.
Definition: Vector3.hh:705
void ForceSceneUpdate()
Rendering will call UpdateCameraRelatedState() on scenes.
Matrix3x3< T > Transpose() const
returns transposed matrix tested 12.06.2002
Definition: Matrix3x3.cpp:167
const T * GetData() const
get the data pointer the member function itself is const (before {..}) because it doesn&#39;t change the...
Definition: Vector4.hh:177
void Rescale(float ratio)
Returns the absolute world coordinate frame position of camera.
void MultIP(const T &scalar)
Definition: Vector3.hh:327
virtual void TranslateZToMetricZ(BIAS::Image< float > &depthMap, const float invalid=0.0, const bool flip=true) const
bool informScenesOfChange_
Abstract projection params have changed, scenes have to be informed about this.
Do not do anything! Do not render either!
int Flip()
flips the image vertically (row order is inverted) In place function return 0 in case of success...
Definition: ImageBase.cpp:834
void ClearStencilBuffer(int s=0)
Clears the stencil buffer of the render target with the given value.
virtual void ZBufferToDepthMap_(BIAS::Image< float > &depthMap)
used by GetDepthMap() which writes the current zbuffer in depthMap.
virtual BIAS::ProjectionParametersBase * GetMyselfAsProjectionParametersBase() const
the pure virtual GetMyselfAsProjectionParameterBase() forces the child classes to inherit from a proj...
void ClearColorBuffer(float red=0.0f, float green=0.0f, float blue=0.0f, float alpha=0.0f)
Clears the color buffer of the render target with the given color.
virtual void SetQC(const BIAS::Quaternion< double > &Q, const BIAS::Vector3< double > &C)
Set pose from unit quaternion Q and projection center C.
void SetSimplePerspective(const double FoV=M_PI/2, const unsigned int width=512, const unsigned int height=512)
Sets the parameters for a simple perspective camera with imagesize 512x512, focal length 512 (fov = 9...
Class for extra rendering pass in GLProjection (for now only used in glutGLviewer) ...
BIAS::HomgPoint3D UnProject(const unsigned int x, const unsigned int y)
Camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
virtual int UpdateGLProjectionMatrix_(unsigned int x0, unsigned int y0, unsigned int width, unsigned int height)=0
Interface for render targets.
rescale internal projection parameters preeerving aspectration but use non-integer values...
virtual int DrawWithMatchedParamsAndViewport_(int viewport[4], int imageWidth, int imageHeight, std::vector< SceneBase * > &scenes)
Takes care of adapting internal parameters and gl viewport by specified behaviour scheme...
void GetGreyImage(BIAS::Image< unsigned char > &image)
virtual void SetC(const BIAS::Vector3< double > &C)
Set projection center.
Vector3< T > & Normalize()
normalize this vector to length 1
Definition: Vector3.hh:663
virtual double GetDepth(const unsigned int x, const unsigned int y)
evaluate depth.
double NormL2() const
the L2 norm sqrt(a^2 + b^2 + c^2)
Definition: Vector3.hh:633
class BIASGeometryBase_EXPORT HomgPoint2D
virtual void TranslateDepthToZ(BIAS::Image< float > &depthtoz, const float invalid=0.0, const bool flip=true) const
helper function which converts a z map obtained with the last camera into a depth map for the last ca...
virtual void Rescale_(float scale)=0
class BIASGeometryBase_EXPORT HomgPoint3D
virtual int SetExtrinsics(const BIAS::HomgPoint3D &C, const BIAS::RMatrix &R)
exemplary output attachment to create 2D+Depth output for Wow Displays
void ReadDepthBufferToTexture_(int texID, int width, int height)
const StorageType ** GetImageDataArray() const
overloaded GetImageDataArray() from ImageBase
Definition: Image.hh:153