Basic Image AlgorithmS Library 2.8.0

RenderContextBase.cpp

00001 #include <GLviewer/RenderContextBase.hh>
00002 #include <GLviewer/Scenes/SceneBGImage.hh>
00003 
00004 #include <Gui/biasgl.h>
00005 
00006 #include <iostream>
00007 
00008 using namespace BIAS;
00009 using namespace BIAS;
00010 using namespace std;
00011 
00012 
00013 //#define MIN_ZNEAR 1.0
00014 
00015 //#define TURN_OF_LOCKING
00016 
00017 #ifdef BIAS_HAVE_PTHREADS
00018 MutexSingleton* RenderContextBase::contextLock_ = new MutexSingleton();
00019 #endif
00020 
00021 RenderContextBase::
00022 RenderContextBase() : ClearColor_(0.0, 0.0, 0.0, 0.0), centerViewport_(false)
00023 {
00024   camera_ = NULL;
00025   control_ = NULL;
00026   backgroundImageScene_ = NULL;
00027 
00028   initialized_=false;
00029   AutoClipping_ = false;
00030   blendingAllowed_=false;
00031   AutoMinZNear_ = 0.1; //old default.
00032   culling_=false;
00033   UseStencil_ = false;
00034   bSelectionMode_ = false;
00035 }
00036 
00037 #ifdef BIAS_HAVE_PTHREADS
00038 int RenderContextBase::LockContextSwitch()
00039 {
00040   int res = contextLock_->Lock();
00041   MakeGLContextCurrent();
00042   return res;
00043 }
00044 
00045 int RenderContextBase::ReleaseContextSwitch()
00046 {
00047   return contextLock_->Unlock();
00048 }
00049 #endif
00050 
00051 void RenderContextBase::
00052 PrintGLStatus()
00053 {
00054   int redbits, greenbits, bluebits, depthbits, alphabits,stencilbits;
00055   GLboolean doublebuffered;
00056   glGetIntegerv(GL_DEPTH_BITS,&depthbits);
00057   glGetIntegerv(GL_RED_BITS, &redbits);
00058   glGetIntegerv(GL_GREEN_BITS, &greenbits);
00059   glGetIntegerv(GL_BLUE_BITS, &bluebits);
00060   glGetIntegerv(GL_ALPHA_BITS, &alphabits);
00061   glGetIntegerv(GL_STENCIL_BITS, &stencilbits);
00062   glGetBooleanv(GL_DOUBLEBUFFER, &doublebuffered);
00063   int viewport[4];
00064   glGetIntegerv(GL_VIEWPORT, viewport);
00065   cout <<"GL buffer resolution Red:     "<<redbits<<" bits"<<endl;
00066   cout <<"GL buffer resolution Green:   "<<greenbits <<" bits"<<endl;
00067   cout <<"GL buffer resolution Blue:    "<<bluebits <<" bits"<<endl;
00068   cout <<"GL buffer resolution Alpha:   "<<alphabits <<" bits"<<endl;
00069   cout <<"GL buffer resolution Depth:   "<<depthbits <<" bits"<<endl;
00070   cout <<"GL buffer resolution Stencil: "<<stencilbits <<" bits"<<endl;
00071   cout <<"GL doublebuffered:            "<<(doublebuffered == GL_TRUE)<<endl;
00072   cout <<"GL Viewport:                  "<<viewport[0];
00073   cout<<","<<viewport[1]<<","<<viewport[2]<<","<<viewport[3]<<endl;
00074 }
00075 
00076 int RenderContextBase::
00077 GetImage(BIAS::Image<unsigned char>& Image)
00078 {
00079   if(camera_==NULL) {
00080     BIASERR("No camera set!\n");
00081     return -1;
00082   }
00083   camera_->GetImage(Image);
00084   return 0;
00085 }
00086 
00087 int RenderContextBase::
00088 GetGreyImage(BIAS::Image<unsigned char>& Image)
00089 {
00090  if(camera_==NULL) {
00091    BIASERR("No camera set!\n");
00092    return -1;
00093  }
00094  camera_->GetGreyImage(Image);
00095  return 0;
00096 }
00097 
00098 int RenderContextBase::
00099 GetDepthMap(BIAS::Image<float>& Image, const float invalid)
00100 {
00101   if(camera_==NULL) {
00102     BIASERR("No camera set!\n");
00103     return -1;
00104   } else
00105     return camera_->GetDepthMap(Image, invalid);
00106 }
00107 
00108 int RenderContextBase::
00109 GetZBuffer(BIAS::Image<float>& Image)
00110 {
00111   if(camera_==NULL) {
00112     BIASERR("No camera set!\n");
00113     return -1;
00114   } else
00115     return camera_->GetZBuffer(Image);
00116 }
00117 
00118 int RenderContextBase::
00119 GetMetricZMap(BIAS::Image<float>& Image, const float invalid)
00120 {
00121   if (camera_ == NULL)
00122     {
00123         BIASERR("No camera set!\n");
00124       return -1;
00125     }
00126   else
00127     return camera_->GetMetricZMap(Image, invalid);
00128 }
00129 
00130 
00131 void RenderContextBase::
00132 Init_()
00133 {
00134   int  depthbits;
00135   SetBlending(blendingAllowed_);
00136   SetBackfaceCulling(culling_);
00137 
00138   glEnable(GL_DEPTH_TEST);
00139   glDepthFunc(GL_LESS);
00140 
00141   glGetIntegerv(GL_DEPTH_BITS,&depthbits);
00142   if(depthbits == 0)
00143     BIASERR("No depth buffer was found most functionality is unavailable!");
00144 
00145   initialized_ = true;
00146 }
00147 
00148 bool RenderContextBase::
00149 SetBlending(bool blend){
00150   blendingAllowed_ = blend;
00151   int alphabits;
00152   glGetIntegerv(GL_ALPHA_BITS, &alphabits);
00153   if(alphabits>0 && blend)
00154     glEnable(GL_BLEND);
00155   else{
00156     blendingAllowed_=false;
00157     glDisable(GL_BLEND);
00158   }
00159   return blendingAllowed_;
00160 }
00161 
00162 bool RenderContextBase::
00163 SetBackfaceCulling(bool cull){
00164   culling_ = cull;
00165   if(cull)
00166     glEnable(GL_CULL_FACE);
00167   else
00168     glDisable(GL_CULL_FACE);
00169   return culling_;
00170 }
00171 
00172 int RenderContextBase::
00173 Render()
00174 {
00175   GLenum err = glGetError();
00176   if (err != GL_NO_ERROR) {
00177     BIASERR("OpenGL Error before rendering: " << gluErrorString(err));
00178     }
00179 
00180 
00181 #ifndef TURN_OF_LOCKING
00182 #ifdef BIAS_HAVE_PTHREADS
00183   LockContextSwitch();
00184 #endif
00185 #endif
00186 
00187   // MakeGLContextCurrent();
00188   if(camera_ == NULL) {
00189     BIASERR("No GLProjection object set!\n");
00190 #ifdef BIAS_HAVE_PTHREADS
00191     ReleaseContextSwitch();
00192 #endif
00193     return -1;
00194   }
00195 
00196   if(!initialized_) Init_();
00197 
00198 
00199   glClearColor(ClearColor_[0], ClearColor_[1],
00200                ClearColor_[2], ClearColor_[3]);
00201 
00202   // cout<<"ClearColor:"<<ClearColor_[0]<<","<<ClearColor_[1]<<","<<ClearColor_[2]<<","<<ClearColor_[3]<<endl;
00203   if(UseStencil_)
00204     {
00205      glClearStencil(0);
00206      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
00207    }
00208    else
00209    {
00210       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00211    }
00212 
00213   // near and far clipping plane is automatically derived and set.
00214   if (AutoClipping_)
00215   {
00216     Vector3<double> C, min, max, radiusOfSphere, centerOfBox;
00217 
00218     double zFar  = 0.0;
00219     double zNear = 0.0;
00220     bool autoClippingSucceeded = false;
00221 
00222     for (unsigned int i = 0; i < scenes_.size(); i++)
00223     {
00224       double newNear = 0.0;
00225       double newFar  = 0.0;
00226 
00227       scenes_[i]->GetBoundingBox(min, max);
00228 
00229       //cout<<"min :"<<min<<" max: "<<max<<endl;
00230 
00231       // has the scene a bounding box.
00232       if (min.Length() == 0.0 && max.Length() == 0.0)
00233       {
00234         continue;
00235       }
00236 
00237       //this is the new way, droogs!
00238       C = camera_->GetMyselfAsProjectionParametersBase()->GetC();
00239       //  cout<<"C:"<<C<<endl;
00240 
00241       // compute radius of the sphere around the bounding box.
00242       centerOfBox = max - min;
00243       centerOfBox = centerOfBox / 2.0;
00244       radiusOfSphere = centerOfBox;
00245       centerOfBox = min + centerOfBox;
00246 
00247       Vector3<double> direction;
00248       direction = (centerOfBox - C);
00249       newNear = (direction.Length() - radiusOfSphere.Length());
00250       newFar  = (direction.Length() + radiusOfSphere.Length());
00251 
00252       if (zNear == 0.0) zNear = newNear;
00253 
00254       // set new clipping planes.
00255       if (newNear < zNear)
00256       {
00257         zNear = newNear;
00258       }
00259 
00260       if (newFar > zFar)
00261       {
00262         zFar = newFar;
00263       }
00264       autoClippingSucceeded = true;
00265     }
00266 
00267 
00268     if (autoClippingSucceeded) {
00269       // set clipping planes to nearest and farest point +-10%
00270       zFar  *= 1.1;
00271       zNear *= 0.9;
00272 
00273       if (zNear < AutoMinZNear_) zNear = AutoMinZNear_; //MIN_ZNEAR
00274       if (zFar  < zNear*1.01) zFar = zNear*1.01;
00275 
00276       //cout<<"Context ALTERTED znear,zfar to:"<<zNear<<","<<zFar<<endl;
00277       //cout<<"Setting clipping planes to "<<zNear<<";"<<zFar<<endl;
00278       camera_->SetFarClippingPlane(zFar);
00279       camera_->SetNearClippingPlane(zNear);
00280     } else {
00281       BIASWARNONCE("autoclipping active but no bounding boxes could be retrieved.");
00282     }
00283   }//end if(autoclipping)
00284 
00285   //finally draw scenes with camera
00286   int res =  camera_->Draw(scenes_, backgroundImageScene_);
00287 
00288   // draw views
00289   for (int i = 0; i < (int)views_.size(); i++) {
00290     views_[i]->Render();
00291   }
00292 
00293   // TODO: should this be called once or for each view?
00294   for (unsigned int i=0; i<additionalListeners_.size(); i++)
00295     additionalListeners_[i]->FinishedDraw();
00296 
00297 #ifndef TURN_OF_LOCKING
00298 #ifdef BIAS_HAVE_PTHREADS
00299   ReleaseContextSwitch();
00300 #endif
00301 #endif
00302   err = glGetError();
00303   if (err != GL_NO_ERROR) {
00304     BIASERR("OpenGL Error after rendering: " << gluErrorString(err));
00305   }
00306 
00307   return res;
00308 }
00309 
00310 int RenderContextBase::
00311 GetSceneCount()
00312 {
00313   return scenes_.size();
00314 }
00315 
00316 void RenderContextBase::AppendScene(SceneBase* scene, bool push_front)
00317 {
00318   scene->SetGLProjectionParametersInterface(camera_);
00319 
00320   if (push_front)
00321     scenes_.insert(scenes_.begin(),scene);
00322   else
00323     scenes_.push_back(scene);
00324 
00325 }
00326 
00327 void RenderContextBase::RemoveScene(SceneBase* scene, bool noWarning)
00328 {
00329   int index = -1;
00330   int numberOfScenes = (int)scenes_.size();
00331   for (int i = 0; i < numberOfScenes; i++)
00332     if (scenes_[i] == scene)
00333       index = i;
00334 
00335   if (index != -1)
00336     scenes_.erase(scenes_.begin() + index);
00337   else
00338     if (!noWarning) BIASWARN("Cannot remove scene. Unknown scene pointer");
00339 }
00340 
00341 void RenderContextBase::
00342 InsertSceneAt(SceneBase* scene, unsigned int index)
00343 {
00344   vector<SceneBase*>::iterator itor = scenes_.begin();
00345 
00346   if (index <= scenes_.size())
00347   {
00348     unsigned int i = 0;
00349     while (i < index)
00350     {
00351       itor++;
00352       i++;
00353     }
00354     scenes_.insert( itor, 1, scene );
00355   }
00356   else
00357   {
00358     scenes_.push_back(scene);
00359   }
00360 }
00361 
00362 void RenderContextBase::
00363 DeleteBackgroundImageScene()
00364 {
00365   delete backgroundImageScene_;
00366   SetBackgroundImageScene(NULL);
00367 }
00368 
00369 
00370 void RenderContextBase::
00371 DeleteSceneAt(unsigned int index)
00372 {
00373   vector<SceneBase*>::iterator itor = scenes_.begin();
00374 
00375   if (index >= scenes_.size())
00376   {
00377     BIASERR("Index out of range");
00378   }
00379 
00380   unsigned int i = 0;
00381   while (i < index)
00382   {
00383     itor++;
00384     i++;
00385   }
00386   scenes_.erase(itor);
00387 }
00388 
00389 void RenderContextBase::
00390 DeleteScenes()
00391 {
00392   for(unsigned int i=0; i<scenes_.size(); i++)
00393     delete scenes_[i];
00394   scenes_.clear();
00395 }
00396 
00397 void RenderContextBase::
00398 ClearScenes()
00399 {
00400   scenes_.clear();
00401 }
00402 
00403 void BIAS::RenderContextBase::
00404 RemoveAdditionalListener(ControlInterface& listener)
00405 {
00406   std::vector<ControlInterface*>::iterator begin, end, pos;
00407   begin = additionalListeners_.begin();
00408   end = additionalListeners_.end();
00409   ControlInterface* test = &listener;
00410   for(pos = begin; test != *pos && pos != end; pos++) {}
00411   if(*pos == test)
00412     additionalListeners_.erase(pos);
00413 }
00414 
00415 bool BIAS::RenderContextBase::
00416 GetSceneCenter(Vector3<double>& center, Vector3<double>& radius)
00417 {
00418   center = Vector3<double>(0.0,0.0,0.0);
00419   radius = Vector3<double>(0.0,0.0,0.0);
00420 
00421   Vector3<double> min, max;
00422   Vector3<double> newCenter, newRadius;
00423   unsigned int foundBoundingBox = 0;
00424 
00425   for (unsigned int i = 0; i < scenes_.size(); i++)
00426   {
00427     scenes_[i]->GetBoundingBox(min, max);
00428 
00429     // has the scene a bounding box.
00430     if (min.Length() == 0.0 && max.Length() == 0.0)
00431       continue;
00432 
00433     foundBoundingBox++;
00434 
00435     // compute radius of the sphere around the bounding box.
00436     newRadius   = ((max - min) / 2.0);
00437     newCenter   = newRadius + min;
00438 
00439     if (newRadius.Length() > radius.Length())
00440       radius = newRadius;
00441 
00442     if (foundBoundingBox >= 2)
00443     {
00444       // compute new center and radius of sphere which includes all
00445       // spheres
00446       newCenter   = (newCenter - center) / 2.0;
00447       newRadius   = newCenter + radius;
00448       newCenter   = newCenter + center;
00449     }
00450 
00451     // set new radius and center for next iteration
00452     radius = newRadius;
00453     center = newCenter;
00454   }
00455 
00456 
00457   // if radius is zero then no bounding box was retrieved
00458   if (radius.Length() == 0.0)
00459     return false;
00460   else
00461     return true;
00462 }
00463 
00464 
00465 void RenderContextBase::
00466 SetGLProjectionParametersInterface(GLProjectionParametersInterface* newCamera)
00467 {
00468   if(newCamera == NULL) {
00469     BIASERR("RenderContextBase: specified camera is NULL");
00470     return;
00471   }
00472   camera_ = newCamera;
00473   camera_->SetClearColor(ClearColor_);
00474   for(unsigned int i=0; i<scenes_.size(); i++)
00475     scenes_[i]->SetGLProjectionParametersInterface(camera_);
00476   if(control_ != NULL)
00477     control_->SetGLProjectionParametersInterface(camera_);
00478 }
00479 
00480 
00481 bool RenderContextBase::
00482 MouseWheelUsed(double stepsize){
00483 
00484     bool callCaught = false;
00485     callCaught = control_->MouseWheelUsed(stepsize);
00486     for (int i = 0; i < (int)additionalListeners_.size(); i++)
00487     {
00488         bool res = additionalListeners_[i]->MouseWheelUsed(stepsize);
00489         callCaught = callCaught || res;
00490     }
00491     return callCaught;
00492 }
00493 
00494 
00495 void RenderContextBase::
00496 SetBackgroundImageScene(SceneBGImage* backgroundImageScene)
00497 {
00498   BIAS::Vector4<float> cc(255,255,255,0);
00499   SetClearColor(cc);
00500   backgroundImageScene_ = backgroundImageScene;
00501 }
00502 
00503 void RenderContextBase::
00504 SetControl(ControlInterface* newControl)
00505 {
00506   control_ = newControl;
00507   if(control_ != NULL && camera_ != NULL) {
00508     control_->SetGLProjectionParametersInterface(camera_);
00509   }
00510 
00511 }
00512 
00513 bool RenderContextBase::
00514 RightMouseDoubleClicked(int x, int y)
00515 {
00516 
00517   bool res = false;
00518   if(control_!=NULL)
00519     res = control_->RightMouseDoubleClicked(x, y);
00520 
00521   for (unsigned i = 0; i < additionalListeners_.size(); i++)
00522   {
00523     res = res || additionalListeners_[i]->RightMouseDoubleClicked(x, y);
00524   }
00525 
00526   return res;
00527 }
00528 
00529 bool RenderContextBase::LeftMouseDown(int x, int y)
00530 {
00531   bool callCaught = false;
00532   if(control_!=NULL) {
00533     callCaught = control_->LeftMouseDown(x,y);
00534   }
00535   for (int i = 0; i < (int)additionalListeners_.size(); i++)
00536   {
00537     bool res = additionalListeners_[i]->LeftMouseDown(x, y);
00538     callCaught = callCaught || res;
00539   }
00540   return callCaught;
00541 }
00542 
00543 bool RenderContextBase::LeftMouseUp(int x, int y)
00544 {
00545   bool callCaught = false;
00546   if(control_!=NULL) {
00547     callCaught = control_->LeftMouseUp(x,y);
00548   }
00549 
00550   for (int i = 0; i < (int)additionalListeners_.size(); i++)
00551   {
00552     bool res = additionalListeners_[i]->LeftMouseUp(x, y);
00553     callCaught = callCaught || res;
00554   }
00555 
00556   return callCaught;
00557 }
00558 
00559 bool RenderContextBase::
00560 LeftMouseDoubleClicked(int x, int y, int m)
00561 {
00562     bool callCaught = false;
00563 
00564     //if the selection mode is enabled, call selectAt for all scenes
00565     if(bSelectionMode_){
00566         // Get from 2D mouse position 3D point in scene.
00567         if(camera_->GetViewportCoordinates(x, y)) {
00568             BIAS::HomgPoint3D p=camera_->UnProject(x, y);
00569             if (p[3] != 0.0)
00570             {
00571                 BIAS::Vector3<double> p3(p[0],p[1],p[2]);
00572                 for(unsigned i=0;i<scenes_.size();i++)
00573                     scenes_[i]->SelectObjectAt(p3);
00574             }
00575         }
00576   }
00577 
00578   if(control_!=NULL) {
00579     callCaught = control_->LeftMouseDoubleClicked(x, y, m);
00580   }
00581 
00582   for (unsigned int i = 0; i < additionalListeners_.size(); i++)
00583     {
00584       bool res = additionalListeners_[i]->LeftMouseDoubleClicked(x, y, m);
00585       callCaught = callCaught || res;
00586     }
00587 
00588   return callCaught;
00589 }
00590 
00591 bool RenderContextBase::
00592 LeftMouseSingleClicked(int x, int y)
00593 {
00594     bool callCaught = false;
00595     if(control_!=NULL)
00596         callCaught = control_->LeftMouseSingleClicked(x, y);
00597 
00598     for (unsigned i = 0; i < additionalListeners_.size(); i++)
00599     {
00600         bool res = additionalListeners_[i]->LeftMouseSingleClicked(x, y);
00601         callCaught = callCaught || res;
00602     }
00603 
00604   return callCaught;
00605 }
00606 
00607 bool RenderContextBase::
00608 RightMouseSingleClicked(int x, int y)
00609 {
00610   bool callCaught = false;
00611   if(control_!=NULL)
00612     callCaught = control_->RightMouseSingleClicked(x, y);
00613 
00614 
00615   for (unsigned i = 0; i < additionalListeners_.size(); i++)
00616   {
00617     bool res = additionalListeners_[i]->RightMouseSingleClicked(x, y);
00618     callCaught = callCaught || res;
00619   }
00620 
00621   return callCaught;
00622 }
00623 
00624 bool RenderContextBase::
00625 LeftMouseMoved(int x1, int x2, int y1, int y2)
00626 {
00627   bool callCaught = false;
00628   if(control_!=NULL)
00629     callCaught = control_->LeftMouseMoved(x1, x2, y1, y2);
00630 
00631   for (unsigned i = 0; i < additionalListeners_.size(); i++)
00632   {
00633     bool res = additionalListeners_[i]->LeftMouseMoved(x1, x2, y1, y2);
00634     callCaught = callCaught || res;
00635   }
00636 
00637   return callCaught;
00638 }
00639 
00640 bool RenderContextBase::
00641 RightMouseMoved(int x1, int x2, int y1, int y2)
00642 {
00643   bool callCaught = false;
00644   if(control_!=NULL)
00645     callCaught = control_->RightMouseMoved(x1, x2, y1, y2);
00646 
00647   if(scenes_.size() == 0 || callCaught)
00648     return callCaught;
00649 
00650   for (unsigned i = 0; i < additionalListeners_.size(); i++)
00651   {
00652     bool res = additionalListeners_[i]->RightMouseMoved(x1, x2, y1, y2);
00653     callCaught = callCaught || res;
00654   }
00655   return callCaught;
00656 }
00657 
00658 
00659 bool RenderContextBase::RightMouseDown(int x, int y)
00660 {
00661   bool callCaught = false;
00662   if(control_!=NULL) {
00663         callCaught = control_->RightMouseDown(x,y);
00664   }
00665   for (int i = 0; i < (int)additionalListeners_.size(); i++)
00666   {
00667         bool res = additionalListeners_[i]->RightMouseDown(x, y);
00668         callCaught = callCaught || res;
00669   }
00670   return callCaught;
00671 }
00672 
00673 bool RenderContextBase::RightMouseUp(int x, int y)
00674 {
00675   bool callCaught = false;
00676   if(control_!=NULL) {
00677     callCaught = control_->RightMouseUp(x,y);
00678   }
00679 
00680   for (int i = 0; i < (int)additionalListeners_.size(); i++)
00681   {
00682         bool res = additionalListeners_[i]->RightMouseUp(x, y);
00683         callCaught = callCaught || res;
00684   }
00685 
00686   return callCaught;
00687 }
00688 
00689 bool RenderContextBase::MouseLeftWindow()
00690 {
00691   bool callCaught = false;
00692   if(control_!=NULL)
00693     callCaught = control_->MouseLeftWindow();
00694   for (unsigned i = 0; i < additionalListeners_.size(); i++)
00695    {
00696     bool res = additionalListeners_[i]->MouseLeftWindow();
00697     callCaught = callCaught || res;
00698    }
00699   return callCaught;
00700 }
00701 
00702 bool RenderContextBase::
00703 MiddleMouseMoved(int x1, int x2, int y1, int y2)
00704 {
00705   bool callCaught = false;
00706   if(control_!=NULL)
00707     callCaught = control_->MiddleMouseMoved(x1, x2, y1, y2);
00708 
00709   if(scenes_.size() == 0 || callCaught)
00710     return callCaught;
00711 
00712   for (unsigned i = 0; i < additionalListeners_.size(); i++)
00713   {
00714     bool res = additionalListeners_[i]->MiddleMouseMoved(x1, x2, y1, y2);
00715     callCaught = callCaught || res;
00716   }
00717   return callCaught;
00718 }
00719 
00720 bool RenderContextBase::MiddleMouseDown(int x, int y)
00721 {
00722   bool callCaught = false;
00723   if(control_!=NULL) {
00724         callCaught = control_->MiddleMouseDown(x,y);
00725   }
00726   for (int i = 0; i < (int)additionalListeners_.size(); i++)
00727   {
00728         bool res = additionalListeners_[i]->MiddleMouseDown(x, y);
00729         callCaught = callCaught || res;
00730   }
00731   return callCaught;
00732 }
00733 
00734 bool RenderContextBase::MiddleMouseUp(int x, int y)
00735 {
00736   bool callCaught = false;
00737   if(control_!=NULL) {
00738     callCaught = control_->MiddleMouseUp(x,y);
00739   }
00740 
00741   for (int i = 0; i < (int)additionalListeners_.size(); i++)
00742   {
00743         bool res = additionalListeners_[i]->MiddleMouseUp(x, y);
00744         callCaught = callCaught || res;
00745   }
00746 
00747   return callCaught;
00748 }
00749 
00750 bool RenderContextBase::
00751 MouseMoved(int x, int y)
00752 {
00753   bool callCaught = false;
00754   if(control_!=NULL){
00755     callCaught = control_->MouseMoved(x, y);
00756    // cout<<"RCB Mouse Moved:"<<x<<" "<<y<<endl;
00757   }
00758 
00759   for (unsigned i = 0; i < additionalListeners_.size(); i++)
00760   {
00761     bool res = additionalListeners_[i]->MouseMoved(x, y);
00762     callCaught = callCaught || res;
00763   }
00764   return callCaught;
00765 }
00766 
00767 
00768 bool RenderContextBase::
00769 LeftAndRightMouseMoved(int x1, int x2, int y1, int y2)
00770 {
00771   bool callCaught = false;
00772   if(control_!=NULL)
00773     callCaught = control_->LeftAndRightMouseMoved(x1, x2, y1, y2);
00774 
00775   if(scenes_.size() == 0 || callCaught)
00776     return callCaught;
00777 
00778   for (unsigned i = 0; i < additionalListeners_.size(); i++)
00779   {
00780     bool res = additionalListeners_[i]->LeftAndRightMouseMoved(x1, x2, y1, y2);
00781     callCaught = callCaught || res;
00782   }
00783   return callCaught;
00784 }
00785 
00786 bool RenderContextBase::
00787 SpecialKeyPressed(int key)
00788 {
00789     bool callCaught = false;
00790     if(control_!=NULL) {
00791         callCaught = control_->SpecialKeyPressed(key);
00792     }
00793 
00794     for (unsigned int i = 0; i < additionalListeners_.size(); i++)
00795     {
00796     bool res = additionalListeners_[i]->SpecialKeyPressed(key);
00797     callCaught = callCaught || res;
00798   }
00799 
00800   return callCaught;
00801 }
00802 
00803 bool RenderContextBase::
00804 SpecialKeyUp(int key)
00805 {
00806         bool callCaught = false;
00807         if(control_!=NULL) {
00808                 callCaught = control_->SpecialKeyUp(key);
00809         }
00810 
00811         for (unsigned int i = 0; i < additionalListeners_.size(); i++)
00812         {
00813     bool res = additionalListeners_[i]->SpecialKeyUp(key);
00814     callCaught = callCaught || res;
00815   }
00816 
00817   return callCaught;
00818 }
00819 
00820 
00821 
00822 bool RenderContextBase::
00823 StandardKeyPressed(unsigned char key,int x,int y)
00824 {
00825         bool callCaught = false;
00826   if(control_!=NULL) {
00827         callCaught = control_->StandardKeyPressed(key,x,y);
00828   }
00829 
00830   for (unsigned int i = 0; i < additionalListeners_.size(); i++)
00831   {
00832         bool res = additionalListeners_[i]->StandardKeyPressed(key,x,y);
00833         callCaught = callCaught || res;
00834   }
00835   return callCaught;
00836 }
00837 
00838 bool RenderContextBase::
00839 StandardKeyUp(unsigned char key,int x,int y)
00840 {
00841         bool callCaught = false;
00842   if(control_!=NULL) {
00843         callCaught = control_->StandardKeyUp(key,x,y);
00844   }
00845 
00846   for (unsigned int i = 0; i < additionalListeners_.size(); i++)
00847   {
00848         bool res = additionalListeners_[i]->StandardKeyUp(key,x,y);
00849         callCaught = callCaught || res;
00850   }
00851   return callCaught;
00852 }
00853 
00854 // static int calls = 0;
00855 bool RenderContextBase::
00856 TimerExpired()
00857 {
00858   bool callCaught = false;
00859   if(control_!=NULL) {
00860     callCaught = control_->TimerExpired();
00861   }
00862 
00863   for( unsigned int i = 0; i<additionalListeners_.size(); i++) {
00864     bool res =  additionalListeners_[i]->TimerExpired();
00865     callCaught = callCaught || res;
00866   }
00867   return callCaught;
00868 }
00869 
00870 void RenderContextBase::
00871 CenterViewport(bool center) {
00872   centerViewport_ = center;
00873 }
00874 
00875 bool RenderContextBase::
00876 WindowReshape(int width, int height)
00877 {
00878   MakeGLContextCurrent();
00879 
00880   int viewport[4];
00881   glGetIntegerv(GL_VIEWPORT, viewport);
00882 
00883   if(centerViewport_) {
00884     int newviewport[4];
00885     if(viewport[2]>width) {
00886       newviewport[0]=0;
00887       newviewport[2]=width;
00888     } else {
00889       int viewportHalfWidth = viewport[2]/2;
00890       int windowHalfWidth = width/2;
00891       newviewport[0]=windowHalfWidth-viewportHalfWidth;
00892       newviewport[2]=viewport[2];
00893     }
00894 
00895     if(viewport[3]>height) {
00896       newviewport[1]=0;
00897       newviewport[3]=width;
00898     } else {
00899       int viewportHalfHeight = viewport[3]/2;
00900       int windowHalfHeight = height/2;
00901       newviewport[1]=windowHalfHeight-viewportHalfHeight;
00902       newviewport[3]=viewport[3];
00903     }
00904 
00905     glViewport(newviewport[0], newviewport[1], newviewport[2], newviewport[3]);
00906   } else {
00907    /* cout<<"---------------------------------------------------------------"<<endl;
00908     cout<<"viewport was: "<<viewport[0]<<" "<<viewport[1]<<" "<<viewport[2]<<" "<<viewport[3]<<endl;
00909     cout<<"setting view port in windowreshape to: "<<width<<" "<<height<<endl;*/
00910     glViewport(0,0, width, height);
00911   }
00912 
00913   if(control_!=NULL) {
00914     control_->WindowReshape(width, height);
00915   }
00916 
00917   // unsigned int i = 0;
00918   //   while(i<scenes_.size()) {
00919   //     scenes_[i]->WindowReshape(width, height);
00920   //     i++;
00921   //   }
00922 
00923   return true;
00924 }
00925 
00926 void RenderContextBase::
00927 AppendView(View* view)
00928 {
00929   BIASASSERT(view != NULL);
00930   views_.push_back(view);
00931 }
00932 
00933 void RenderContextBase::EnableStencilUsage(bool enable)
00934 {
00935   UseStencil_ = enable;
00936 }
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends