glc_viewport.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003  This file is part of the GLC-lib library.
00004  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
00005  Version 2.0.0 Beta 1, packaged on April 2010.
00006 
00007  http://glc-lib.sourceforge.net
00008 
00009  GLC-lib is free software; you can redistribute it and/or modify
00010  it under the terms of the GNU General Public License as published by
00011  the Free Software Foundation; either version 2 of the License, or
00012  (at your option) any later version.
00013 
00014  GLC-lib is distributed in the hope that it will be useful,
00015  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  GNU General Public License for more details.
00018 
00019  You should have received a copy of the GNU General Public License
00020  along with GLC-lib; if not, write to the Free Software
00021  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023 *****************************************************************************/
00024 
00026 
00027 #ifndef GLC_VIEWPORT_H_
00028 #define GLC_VIEWPORT_H_
00029 #include <QGLWidget>
00030 #include <QPair>
00031 #include <QHash>
00032 #include "glc_camera.h"
00033 #include "glc_imageplane.h"
00034 #include "../glc_boundingbox.h"
00035 #include "glc_frustum.h"
00036 #include "../maths/glc_plane.h"
00037 #include "../sceneGraph/glc_3dviewcollection.h"
00038 
00039 #include "../glc_config.h"
00040 
00041 class GLC_3DViewInstance;
00042 
00045 
00054 
00055 class GLC_LIB_EXPORT GLC_Viewport
00056 {
00057 
00059 
00061 
00062 public:
00064 
00071         GLC_Viewport(QGLWidget *GLWidget);
00072 
00074         virtual ~GLC_Viewport();
00075 
00077 
00079 
00081 
00082 public:
00084         inline GLC_Camera* cameraHandle() const
00085         {return m_pViewCam;}
00086 
00088         inline int viewHSize() const
00089         { return m_nWinHSize;}
00090 
00092         inline int viewVSize() const
00093         { return m_nWinVSize;}
00094 
00096         inline double aspectRatio() const
00097         { return static_cast<double>(m_nWinHSize) / static_cast<double>(m_nWinVSize);}
00098 
00100         GLC_Vector3d mapPosMouse( GLdouble Posx, GLdouble Posy) const;
00101 
00103         inline double viewAngle() const
00104         { return m_dFov;}
00105 
00107         inline double viewTangent() const
00108         { return m_ViewTangent;}
00109 
00110 
00112         inline double nearClippingPlaneDist(void) const
00113         { return m_dCamDistMin;}
00114 
00116         inline double farClippingPlaneDist(void) const
00117         { return m_dCamDistMax;}
00118 
00120         inline QColor backgroundColor(void) const
00121         { return m_BackgroundColor;}
00122 
00124         inline GLsizei selectionSquareSize() const
00125         {return m_SelectionSquareSize;}
00126 
00128         inline GLC_Matrix4x4 projectionMatrix() const
00129         {return m_ProjectionMatrix;}
00130 
00132         inline GLC_Matrix4x4 compositionMatrix() const;
00133 
00135         inline GLC_3DViewCollection* widget3dCollectionHandle()
00136         {return &m_3DWidget;}
00137 
00139         inline bool useOrtho()const
00140         {return m_UseOrtho;}
00141 
00143 
00145 
00147 
00148 public:
00150 
00151         void initGl();
00152 
00154         void glExecuteCam(void);
00155 
00157         void updateProjectionMat(void);
00158 
00160         void forceAspectRatio(double);
00161 
00163         void updateAspectRatio();
00164 
00166         const GLC_Frustum& frustum() const
00167         {return m_Frustum;}
00168 
00170         GLC_Frustum selectionFrustum(int, int) const;
00171 
00173         GLC_Point3d unProject(int, int) const;
00174 
00176 
00177         QList<GLC_Point3d> unproject(const QList<int>&)const;
00178 
00180         inline QGLWidget* qGLWidgetHandle()
00181         {return m_pQGLWidget;}
00182 
00184 
00186 
00188 
00189 private:
00190 
00192         void glExecuteImagePlane();
00193 
00194 public:
00195 
00197         void render3DWidget();
00198 
00200 
00202 
00204 
00205 public:
00206 
00208         void setWinGLSize(int HSize, int VSize);
00209 
00211 
00212         GLC_uint renderAndSelect(int x, int y);
00213 
00215         GLC_uint selectOnPreviousRender(int x, int y);
00216 
00218 
00219         GLC_uint selectBody(GLC_3DViewInstance*, int x, int y);
00220 
00222 
00223         QPair<int, GLC_uint> selectPrimitive(GLC_3DViewInstance*, int x, int y);
00224 
00226         QSet<GLC_uint> selectInsideSquare(int x1, int y1, int x2, int y2);
00227 
00229         void loadBackGroundImage(const QString& imageFile);
00230 
00232         void loadBackGroundImage(const QImage& image);
00233 
00235         void deleteBackGroundImage();
00236 
00238         inline void setViewAngle(double TargetFov)
00239         {
00240                 m_dFov= TargetFov;
00241                 m_ViewTangent= tan(glc::toRadian(m_dFov));
00242                 updateProjectionMat();
00243         }
00244 
00246         bool setDistMin(double DistMin);
00247 
00249         bool setDistMax(double DistMax);
00250 
00252 
00253         void setDistMinAndMax(const GLC_BoundingBox& bBox);
00254 
00256         void setBackgroundColor(QColor setColor);
00257 
00259         inline void setSelectionSquareSize(GLsizei size)
00260         {m_SelectionSquareSize= size;}
00261 
00263 
00264         inline bool updateFrustum(GLC_Matrix4x4* pMat= NULL);
00265 
00267         void addClipPlane(GLenum planeGlEnum, GLC_Plane* pPlane);
00268 
00270         void removeClipPlane(GLenum planeGlEnum);
00271 
00273         void useClipPlane(bool flag);
00274 
00276         inline void add3DWidget(GLC_3DViewInstance& widget)
00277         {m_3DWidget.add(widget);}
00278 
00280         inline void clearBackground(const QColor& c) const
00281         {m_pQGLWidget->qglClearColor(c);}
00282 
00284         void setToOrtho(bool useOrtho);
00286 
00287 
00290 //{@
00292 
00293         void reframe(const GLC_BoundingBox& box);
00294 
00296 
00297 
00299 // private services functions
00301 private:
00303         GLC_uint meaningfulIdInsideSquare(GLint x, GLint y, GLsizei width, GLsizei height);
00304 
00306         QSet<GLC_uint> listOfIdInsideSquare(GLint x, GLint y, GLsizei width, GLsizei height);
00307 
00308 
00310 // Private Members
00312 private:
00313 
00315         GLC_Camera *m_pViewCam;
00316 
00317         double m_dCamDistMax;           
00318         double m_dCamDistMin;           
00319         double m_dFov;                          
00320         double m_ViewTangent;           
00321 
00322 
00324         GLC_ImagePlane* m_pImagePlane;
00325 
00326         // OpenGL View Definition
00327         int m_nWinHSize;                        
00328         int m_nWinVSize;                        
00329 
00331         double m_AspectRatio;
00332 
00334         QGLWidget* m_pQGLWidget;
00335 
00337         QColor m_BackgroundColor;
00338 
00340         GLsizei m_SelectionSquareSize;
00341 
00343         GLC_Matrix4x4 m_ProjectionMatrix;
00344 
00346         GLC_Frustum m_Frustum;
00347 
00349         QHash<GLenum, GLC_Plane*> m_ClipPlane;
00350 
00352         bool m_UseClipPlane;
00353 
00355         GLC_3DViewCollection m_3DWidget;
00356 
00358         bool m_UseOrtho;
00359 };
00360 
00361 GLC_Matrix4x4 GLC_Viewport::compositionMatrix() const
00362 {
00363         // Get the viewport projection matrix
00364         GLC_Matrix4x4 projectionMatrix= m_ProjectionMatrix;
00365         // Get the camera modelView matrix
00366         GLC_Matrix4x4 modelViewMatrix= m_pViewCam->modelViewMatrix();
00367         // Composition matrix
00368         GLC_Matrix4x4 compMatrix= projectionMatrix * modelViewMatrix;
00369 
00370         return compMatrix;
00371 }
00372 
00373 bool GLC_Viewport::updateFrustum(GLC_Matrix4x4* pMat)
00374 {
00375         if (NULL == pMat)
00376         {
00377                 return m_Frustum.update(compositionMatrix());
00378         }
00379         else
00380         {
00381                 return m_Frustum.update(*pMat);
00382         }
00383 }
00384 #endif //GLC_VIEWPORT_H_

SourceForge.net Logo

©2005 Laurent Ribon