Basic Image AlgorithmS Library 2.8.0

GLProjectionParametersBase.hh

00001 #ifndef __GLPROJECTIONPARAMSBASE_HH__
00002 #define __GLPROJECTIONPARAMSBASE_HH__
00003 
00004 #include <bias_config.h>
00005 
00006 #if !(defined(BIAS_HAVE_OPENGL)&&defined(BIAS_HAVE_GLEW))
00007 #error You need BIAS with OpenGL and Glew. Please recompile BIAS
00008 #endif //!(defined(BIAS_HAVE_OPENGL)&&defined(BIAS_HAVE_GLEW))
00009 
00010 #include <Gui/biasgl.h>
00011 #include <Geometry/ProjectionParametersBase.hh>
00012 #include <Base/Image/Image.hh>
00013 #include <Base/Geometry/Quaternion.hh>
00014 #include <Base/Debug/Debug.hh>
00015 #include <GLviewer/GLProjectionParametersInterface.hh>
00016 #include <GLviewer/OffscreenRendering.hh>
00017 #include <GLviewer/Scenes/SceneBase.hh>
00018 #include <GLviewer/OutputAttachments/GLProjectionOutputAttachment.hh>
00019 #include <deque>
00020 
00021 #define GetMyselfAsProjectionParameterBase GetMyselfAsProjectionParametersBase
00022 
00023 namespace BIAS {
00024 
00025   class BIASGLviewer_EXPORT SceneBGImage;
00026 
00027   /** @class GLProjectionParametersBase
00028       @brief GLPPB encapsulates the opengl rendering interface for the
00029       projectionparameter classes.
00030       GLPPB replaces the glprojection. In constrast to the good old glprojection
00031       every projection type (e.g. perspective or fisheye) implements its own
00032       class based on the GLPPB interface. This hopefully reduces the code
00033       complextity and the need for gigantic case differentiations.
00034       Unlike the glprojection it does not inherit anything from the
00035       related projection class. the specific implementations
00036       (e.g. GLprojectionsparametersperspective ) are derived from GLPPB and
00037       a projectionparameter class (e.g. projectionparametersperspective).
00038       the GLPPbase class should stay generic and must not contain methods
00039       or members only related to a specific projection type. (e.g. distortion)
00040       \ingroup g_glviewer
00041       \author MIP
00042   */
00043 
00044   class BIASGLviewer_EXPORT GLProjectionParametersBase
00045     :public GLProjectionParametersInterface, public BIAS::Debug
00046   {
00047   public:
00048     // standard constructor without parameter object
00049     GLProjectionParametersBase(  );
00050     virtual ~GLProjectionParametersBase( );
00051 
00052     virtual void SetDebugLevel(const long int lv);
00053     virtual void AddDebugLevel(const long int lv);
00054 
00055     /**
00056        the pure virtual GetMyselfAsProjectionParameterBase() forces the child
00057        classes to inherit from a projectionparameterbase class and
00058        allows to access the projectionparams in cases where only a GLPPB* is
00059        available. this avoids the undesired need to derive GLPPB from PPB.
00060     */
00061     virtual BIAS::ProjectionParametersBase* GetMyselfAsProjectionParametersBase() const{
00062       BIASERR("Base class called, should be overloaded!");
00063       return NULL;
00064     };
00065 
00066     /* Implement functions from GLProjectionParametersInterface*/
00067     virtual int SetExtrinsics(const BIAS::HomgPoint3D& C, const BIAS::RMatrix& R);
00068     virtual int SetExtrinsics(const BIAS::Vector3<double>& C, const BIAS::RMatrix& R);
00069     virtual int SetExtrinsics(const BIAS::HomgPoint3D& C,const BIAS::Quaternion<double>& Q);
00070     virtual int SetExtrinsics(const BIAS::Vector3<double>& C,const BIAS::Quaternion<double>& Q);
00071 
00072 
00073     /** Set extrinsic similar to GL's gluLookAt.
00074      * Defines the camera coordinate base through specified scenepoint
00075      * that is observed from given position (the eye point). And the up
00076      * direction in the image (which is the -y axis of image cooridinates).
00077      * Assuming orthonormal base, extrinsics are calculated.
00078      * Orthonormality is assured.
00079      * @{  */
00080     virtual int SetExtrinsics(float eyex, float eyey, float eyez,
00081                               float scenex, float sceney, float scenez,
00082                               float upx, float upy, float upz);  /** @} */
00083     virtual int SetExtrinsics(const BIAS::Vector3<double>& C,
00084                               const BIAS::Vector3<double>& scenePoint,
00085                               const BIAS::Vector3<double>& up);
00086 
00087     virtual void SetIntrinsics(BIAS::ProjectionParametersBase *p);
00088     virtual void Set(BIAS::ProjectionParametersBase *p);
00089 
00090     virtual int GetExtrinsics(BIAS::Vector3<double>& C,
00091                               BIAS::Vector3<double>& up) const;
00092 
00093     virtual int GetExtrinsics(BIAS::Vector3<double>& C,
00094                               BIAS::RMatrix& R) const;
00095 
00096     void SwitchToOffscreenRendering(bool offscreen = true);
00097 
00098     void AppendOutputAttachment(int type, std::vector<int> args=std::vector<int>());
00099     
00100     // workaround for underwater needing to change parameters from outside, fkellner
00101     GLProjectionOutputAttachment *GetAttachmentByType(int type);
00102 
00103 
00104     virtual bool PoseValid(){ return(GetMyselfAsProjectionParameterBase()->PoseValid()); }
00105 
00106     BIAS::HomgPoint3D UnProject(const unsigned int x, const unsigned int y);
00107     BIAS::HomgPoint3D UnProject(const BIAS::HomgPoint2D& p);
00108     BIAS::HomgPoint3D UnProject(BIAS::HomgPoint2D& point2D, double depth);
00109     BIAS::HomgPoint2D Project(BIAS::HomgPoint3D &point);
00110 
00111     /**
00112         Renders scenes passed as argument beginning with the smallest index.
00113         <b>Depth test is enabled per default with depth func set to GL_LESS<\b>.
00114         Therfore scenes rendered first will also appear in front if competing
00115         with other scenes over depth-values.
00116         If different behaviour is wanted change this within specific scene code!
00117         The background image scene must be passed through a special parameter
00118         and should not be contained in the 'scenes' vector.
00119     */
00120     virtual int Draw(std::vector<BIAS::SceneBase *>& scenes,
00121                      BIAS::SceneBGImage* backgroundImageScene = NULL,
00122                      BIAS::glfFramebufferObject* theTarget=NULL);
00123 #ifdef BIAS_HAVE_XML2
00124     virtual int XMLRead (const std::string &Filename)=0;
00125     virtual int XMLWrite(const std::string &Filename) const =0;
00126     virtual int Load    (const std::string& filename)=0;
00127     virtual int Save    (const std::string& filename) const =0;
00128 #endif
00129     //virtual void Assign(BIAS::GLProjectionParametersBase& p);
00130     virtual void Assign(BIAS::ProjectionParametersBase& p);
00131 
00132     /** Returns the absolute world coordinate frame position of camera */
00133 
00134 
00135     void Rescale(float ratio);
00136 
00137     /** Setting the near and far z-Clipping planes.
00138      * @{ */
00139     void SetZClippingPlanes(float n, float f);
00140     void SetNearClippingPlane(float n);
00141     void SetFarClippingPlane(float f);
00142     float GetNearClippingPlane() const { return (float)zNear_; }
00143     float GetFarClippingPlane() const { return (float)zFar_; }
00144     /** @} */
00145 
00146 
00147     /** evaluate depth.
00148         Point is given in pixel coords relative to upper left image corner
00149         with positive x axis directed to the right and the positive y-axis
00150         pointing down. Result is the depth on optical ray.
00151         \attention Point coordinates passed to this method must lie within
00152         used viewport and at the same time have the origin in the upper left
00153         corner with the y-axis pointing down the viewport side.
00154         \attention Result is only valid if rendering is up to date
00155         \attention make sure your rendering context is current
00156     */
00157     virtual double GetDepth(const unsigned int x, const unsigned int y);
00158     virtual double GetDepth(const BIAS::HomgPoint2D& p);
00159 
00160     /** Method loads the parameters into the projection and modelview Matrix.
00161      * \params forceUpdate do not check if camera state changed always
00162      *         recalculate.
00163      * \attention make sure your rendering context is current.
00164      * \attention Method is not equivalent to draw yet.
00165      */
00166     virtual int SetGLMatrices(bool forceUpdate = true) = 0;
00167 
00168     virtual int UpdateGLProjectionMatrix_(unsigned int x0, unsigned int y0,
00169                                           unsigned int width, unsigned int height)=0;
00170 
00171     /** Sets gl's Modelview matrix for rendering
00172      * relative to camera coordinate system (ignoring camera pose).
00173      * \attention changes gl state, take care of resets yourself.
00174      * \attention make sure your rendering context ist current
00175      */
00176     virtual void SetGLCameraToLocal();
00177 
00178     /**  Calculates depth map from current z-Buffer state. */
00179     int GetDepthMap(BIAS::Image<float>& depthMap, const float invalid=0.0);
00180     /** @brief helper function which converts a z map obtained with the last
00181         camera into a depth map for the last camera */
00182     virtual void TranslateZToDepth(BIAS::Image<float>& ztodepth,
00183                                    const float invalid = 0.0,
00184                                    const bool flip = true) const;
00185     /** @brief helper function which converts a z map obtained with the last
00186         camera into a depth map for the last camera */
00187     virtual void TranslateDepthToZ(BIAS::Image<float>& depthtoz,
00188                                    const float invalid = 0.0,
00189                                    const bool flip = true) const;
00190 virtual void TranslateZToMetricZ(BIAS::Image<float>& depthMap,
00191                                      const float invalid = 0.0,
00192                                      const bool flip = true) const;
00193 
00194     int GetZBuffer(BIAS::Image<float>& zBuffer);
00195     int GetMetricZMap(BIAS::Image<float>& metricZMap,
00196                       const float invalid = 0.0);
00197     void GetImage(BIAS::Image<unsigned char>& image);
00198     void GetGreyImage(BIAS::Image<unsigned char>& image);
00199 
00200     /** Rendering will call UpdateCameraRelatedState() on scenes. */
00201     void ForceSceneUpdate();
00202 
00203     /** Is true if in multipass rendering procedure is rendering
00204         for second or subsequent time. In single pass rendering
00205         it never should return true.
00206     */
00207     bool FirstRenderingPassFinished() {return FirstRenderingPassFinished_;}
00208 
00209     void SubtractFromBackgroundImage(bool subtract = true) {
00210       subtractFromBackgroundImage_ = subtract;
00211     }
00212 
00213     void SetAutoReshapeBehaviour(BIAS::GLProjectionParametersInterface::ViewingReshapeBehaviour newBehaviour) {
00214       if(offscreenRenderingEnabled_) {
00215         AutoViewingMatchBehaviour_ = Deny;
00216       }
00217       else
00218         AutoViewingMatchBehaviour_ = newBehaviour;
00219     }
00220 
00221     static int instanceCounter;
00222 
00223     /**
00224        \brief Takes x, y as window coordinates, checks whether the coordinates
00225        lie in the rendered image (in the viewrange of projection). If so
00226        \return the coordinates relative to the upper left image corner of the used
00227        viewport and true. else coordintes are unchanged and return value is
00228        false.
00229     */
00230     bool GetViewportCoordinates(int& x, int& y);
00231 
00232     BIAS::Matrix4x4<double> GetGLProjectionMatrixTransposed() {
00233       return GLProjectionMatrix_.Transpose();
00234     }
00235     BIAS::Matrix4x4<double> GetGLModelViewMatrixTransposed() {
00236       return GLModelViewMatrix_.Transpose();
00237     }
00238 
00239 
00240   /**
00241      setclearcolor is pure virtual.
00242      in some cases like for example distortion rendering
00243      the lower render layers (offscreen surfaces) must informed about changes of
00244      the clear color thus the child classes have to deal with the clear color on
00245      their own.
00246     */
00247     virtual void SetClearColor(const BIAS::Vector4<float>& cc) = 0;
00248 
00249     /** @brief set the clear depth for render targets  */
00250     void SetClearDepth(const float d){
00251         ClearDepth_=d;
00252     };
00253 
00254     virtual BIAS::Vector4<float> GetClearColor(){
00255       return ClearColor_;
00256     };
00257 
00258     virtual float GetClearDepth(){
00259       return ClearDepth_;
00260     };
00261 
00262     virtual void SetRenderTarget(BIAS::glfRenderTarget* RT);
00263     virtual BIAS::glfRenderTarget* GetRenderTarget();
00264 
00265 
00266   protected:
00267     
00268     int GetGLViewport_(int &lowerleftx,int &lowerlefty,
00269           int &sizex,int &sizey);
00270     int SetGLViewport_(int lowerleftx,int lowerlefty,
00271           int sizex,int sizey);
00272         
00273     virtual int BeginDraw_(BIAS::glfFramebufferObject* theTarget=NULL);
00274     virtual int EndDraw_(BIAS::glfFramebufferObject* theTarget=NULL);
00275     /** Implement in deriving classes */
00276     virtual int Draw_(std::vector<SceneBase *>& scenes,
00277                       BIAS::glfFramebufferObject* theTarget = NULL)=0;
00278 
00279     virtual void Rescale_(float scale) = 0;
00280     int          UpdateGLModelViewMatrix_();
00281     void         SetToUnknownState_();
00282     void         InitBeforeDraw_();
00283     void         InitRaw_();
00284 
00285     virtual double Z2Depth_(const int x,const  int y, const double z) const =0;
00286 
00287     /** used by GetDepthMap() which writes the current zbuffer in depthMap.
00288         AdjustRawZBuffer_ computes the distance from the camera to every pixel  */
00289     virtual void ZBufferToDepthMap_(BIAS::Image<float>& depthMap);
00290 
00291     /** Takes care of adapting internal parameters and gl viewport
00292         by specified behaviour scheme. External parameters update
00293         is not performed here.  */
00294     virtual int DrawWithMatchedParamsAndViewport_(int viewport[4],
00295                                                   int imageWidth, int imageHeight,
00296                                                   std::vector<SceneBase*>& scenes);
00297 
00298     /** Renders a rectangular part of actual image.
00299         Use if not full resolution is available for example with scrollbars.
00300         Does not update external parameter forces internal parameter update
00301         upon next call to Draw().
00302         \attention make sure your rendering context ist current
00303     */
00304     int DrawPartOfImage_(const unsigned int ulX,
00305                          const unsigned int ulY,
00306                          const unsigned int width,
00307                          const unsigned int height,
00308                          std::vector<SceneBase *> scenes);
00309 
00310 
00311     void ReadDepthBufferToTexture_(int texID, int width, int height);
00312     
00313     void PrepareOutputAttachments_();
00314 
00315   protected:
00316     unsigned int instanceNum_;
00317     BIAS::GLProjectionParametersInterface::ViewingReshapeBehaviour AutoViewingMatchBehaviour_;
00318 
00319     bool subtractFromBackgroundImage_;
00320     bool FirstRenderingPassFinished_;
00321     BIAS::SceneBGImage * backgroundImageScene_;
00322 
00323     /**Keeping track which part of image was drawn last time Draw() was called
00324        and where it was drawn.
00325        Set by Draw_();  */
00326     BIAS::Vector4<int> lastUsedGLViewport_;
00327 
00328     /** Remembering the last full viewport prevent unecessary calculations in
00329         AutoRescaleParams behaviour  */
00330     BIAS::Vector4<int> lastFullGLViewport_;
00331 
00332     /** When cutting the image this field is telling the projection
00333         the upper left corner of the drawn image part.  */
00334     BIAS::Vector2<unsigned int>  drawingUpperLeft_;
00335 
00336     /** Keeping track which image part was drawn last time Draw() was called.
00337         The width and height of the drawn image is contained in
00338         lastUsedGLViewport  */
00339     BIAS::Vector2<unsigned int>  lastDrawingUpperLeft_;
00340 
00341     double lastAutoRescaleRatio_;
00342 
00343     /** Changes to the parameters or GL state that lead to new calculation of
00344         GL_MODELVIEW matrix.  */
00345     bool ExtrinsicStateChanged_;
00346 
00347     /** Changes to the parameters or GL state that lead to new calculation of
00348         GL_PROJECTION matrix.   */
00349     bool IntrinsicStateChanged_;
00350 
00351     /** Abstract projection params have changed, scenes have to be informed
00352         about this.  */
00353     bool informScenesOfChange_;
00354     double zNear_, zFar_;
00355 
00356     /**
00357      * \attention BIAS::Matrix4x4 stores the matrix in row major order
00358      * where GL expects a matrix in column major order!
00359      * Since we want to use those matrices for rendering they are stored
00360      * the way they can directly be used in GL. Before operating on those
00361      * matrices and other Matrix4x4 objects some transpositions might
00362      * be neccessary! @{*/
00363     BIAS::Matrix4x4<double> GLProjectionMatrix_;
00364     BIAS::Matrix4x4<double> GLModelViewMatrix_;
00365     /**}@*/
00366 
00367     bool callInitBeforeDraw_;
00368 
00369     BIAS::Vector4<float> ClearColor_;
00370     float ClearDepth_;
00371 
00372 #ifdef BIAS_HAVE_CLASS_OFFSCREENRENDERING
00373     OffscreenRendering offscreenRendering_;
00374 #endif
00375 
00376 
00377     bool useStencilBuffer_;
00378 
00379     int cubeSideResolution_;
00380 
00381     bool distort_;
00382 
00383     BIAS::glfRenderTarget* currentRenderTarget_;
00384     
00385     std::deque<BIAS::GLProjectionOutputAttachment *> outputAttachments_;
00386     std::deque<std::vector<int> > outputAttachmentOptions_;
00387     std::deque<int> outputAttachmentIdentifiers_;
00388   };
00389 }
00390 #endif
00391 
00392 
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends