glc_3dviewinstance.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  http://glc-lib.sourceforge.net
00006 
00007  GLC-lib is free software; you can redistribute it and/or modify
00008  it under the terms of the GNU Lesser General Public License as published by
00009  the Free Software Foundation; either version 3 of the License, or
00010  (at your option) any later version.
00011 
00012  GLC-lib is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU Lesser General Public License for more details.
00016 
00017  You should have received a copy of the GNU Lesser General Public License
00018  along with GLC-lib; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 *****************************************************************************/
00022 
00024 
00025 #ifndef GLC_3DVIEWINSTANCE_H_
00026 #define GLC_3DVIEWINSTANCE_H_
00027 
00028 #include "../glc_global.h"
00029 #include "../glc_boundingbox.h"
00030 #include "../glc_object.h"
00031 #include "../maths/glc_matrix4x4.h"
00032 #include "../glc_state.h"
00033 #include "../geometry/glc_3drep.h"
00034 #include "../shading/glc_renderproperties.h"
00035 #include "../glc_context.h"
00036 
00037 #include <QMutex>
00038 
00039 #include "../glc_config.h"
00040 
00041 class GLC_Viewport;
00042 
00045 
00052 
00053 
00054 class GLC_LIB_EXPORT GLC_3DViewInstance : public GLC_Object
00055 {
00056 public:
00058         enum Viewable
00059         {
00060                 FullViewable= 120,
00061                 PartialViewable= 121,
00062                 NoViewable= 122
00063         };
00065 
00067 
00068 public:
00070         GLC_3DViewInstance();
00071 
00073         GLC_3DViewInstance(GLC_Geometry* pGeom);
00074 
00076         GLC_3DViewInstance(const GLC_3DRep&);
00077 
00079         GLC_3DViewInstance(const GLC_3DViewInstance& );
00080 
00082         GLC_3DViewInstance &operator=(const GLC_3DViewInstance&);
00083 
00085         ~GLC_3DViewInstance();
00086 
00088 
00090 
00092 
00093 public:
00095         inline bool isTransparent() const;
00096 
00098         inline bool hasTransparentMaterials() const;
00099 
00101         inline bool isEmpty() const
00102         {return m_3DRep.isEmpty();}
00103 
00105         inline bool isSelected(void) const
00106         {return m_RenderProperties.isSelected();}
00107 
00109         inline int numberOfGeometry() const
00110         {return m_3DRep.numberOfBody();}
00111 
00112 
00114         inline GLC_Geometry* geomAt(int index) const
00115         {
00116                 if (!m_3DRep.isEmpty()) return m_3DRep.geomAt(index);
00117                 else return NULL;
00118         }
00119 
00121         GLC_BoundingBox boundingBox();
00122 
00124         inline bool boundingBoxValidity() const
00125         {return (m_pBoundingBox != NULL) && m_IsBoundingBoxValid && m_3DRep.boundingBoxIsValid();}
00126 
00128         inline const GLC_Matrix4x4& matrix() const
00129         {return m_AbsoluteMatrix;}
00130 
00132         GLC_3DViewInstance deepCopy() const;
00133 
00135         GLC_3DViewInstance instanciate();
00136 
00138 
00139         inline GLenum polygonMode() const
00140         {return m_RenderProperties.polygonMode();}
00141 
00143         inline GLC_RenderProperties* renderPropertiesHandle()
00144         {return &m_RenderProperties;}
00145 
00147         inline bool isVisible() const
00148         {return m_IsVisible;}
00149 
00151         inline GLC_3DViewInstance::Viewable viewableFlag() const
00152         {return m_ViewableFlag;}
00153 
00155         inline bool isGeomViewable(int index) const
00156         {return m_ViewableGeomFlag.at(index);}
00157 
00159         inline unsigned int numberOfFaces() const
00160         {return m_3DRep.faceCount();}
00161 
00163         inline unsigned int numberOfVertex() const
00164         {return m_3DRep.vertexCount();}
00165 
00167         inline unsigned int numberOfMaterials() const
00168         {return m_3DRep.materialCount();}
00169 
00171         inline QSet<GLC_Material*> materialSet() const
00172         {return m_3DRep.materialSet();}
00173 
00175         inline int defaultLodValue() const
00176         {return m_DefaultLOD;}
00177 
00179         inline GLC_3DRep representation() const
00180         {return m_3DRep;}
00181 
00183         inline int numberOfBody() const
00184         {return m_3DRep.numberOfBody();}
00185 
00187         inline static int globalDefaultLod()
00188         {
00189                 return m_GlobalDefaultLOD;
00190         }
00191 
00193 
00195 
00197 
00198 public:
00199 
00201         bool addGeometry(GLC_Geometry* pGeom);
00202 
00204         inline void removeEmptyGeometry()
00205         {m_3DRep.clean();}
00206 
00208         inline void reverseGeometriesNormals()
00209         {m_3DRep.reverseNormals();}
00210 
00212         GLC_3DViewInstance& translate(double Tx, double Ty, double Tz);
00213 
00215         inline GLC_3DViewInstance& translate(const GLC_Vector3d& v)
00216         {
00217                 return translate(v.x(), v.y(), v.z());
00218         }
00219 
00221         GLC_3DViewInstance& multMatrix(const GLC_Matrix4x4 &MultMat);
00222 
00224         GLC_3DViewInstance& setMatrix(const GLC_Matrix4x4 &SetMat);
00225 
00227         GLC_3DViewInstance& resetMatrix(void);
00228 
00230 
00232         inline void setPolygonMode(GLenum Face, GLenum Mode)
00233         {m_RenderProperties.setPolygonMode(Face, Mode);}
00234 
00236         inline void select(bool primitive)
00237         {m_RenderProperties.select(primitive);}
00238 
00240         inline void unselect(void)
00241         {m_RenderProperties.unselect();}
00242 
00244         inline void setVisibility(bool visibility)
00245         {m_IsVisible= visibility;}
00246 
00248         inline void setId(const GLC_uint id)
00249         {
00250                 GLC_Object::setId(id);
00251                 glc::encodeRgbId(m_Uid, m_colorId);
00252         }
00253 
00255         inline void setDefaultLodValue(int lod)
00256         {
00257                 m_DefaultLOD= lod;
00258         }
00259 
00261         inline bool setViewable(GLC_3DViewInstance::Viewable flag);
00262 
00264         inline void setGeomViewable(int index, bool flag)
00265         {m_ViewableGeomFlag[index]= flag;}
00266 
00267 
00269         static void setGlobalDefaultLod(int);
00270 
00272         inline void setRenderProperties(const GLC_RenderProperties& renderProperties)
00273         {m_RenderProperties= renderProperties;}
00274 
00276         void setVboUsage(bool usage);
00277 
00279 
00281 
00283 
00284 public:
00286         void render(glc::RenderFlag renderFlag= glc::ShadingFlag, bool useLoad= false, GLC_Viewport* pView= NULL);
00287 
00289         void renderForBodySelection();
00290 
00292         int renderForPrimitiveSelection(GLC_uint);
00293 
00294 
00295 private:
00297         inline void OpenglVisProperties()
00298         {
00299                 // Polygons display mode
00300                 glPolygonMode(m_RenderProperties.polyFaceMode(), m_RenderProperties.polygonMode());
00301                 // Change the current matrix
00302                 GLC_Context::current()->glcMultMatrix(m_AbsoluteMatrix);
00303         }
00304 
00305 
00307 
00309 // private services functions
00311 private:
00313         void computeBoundingBox(void);
00314 
00316         void clear();
00317 
00319         int choseLod(const GLC_BoundingBox&, GLC_Viewport*, bool);
00320 
00322 // Private members
00324 private:
00325 
00327         GLC_3DRep m_3DRep;
00328 
00330         GLC_BoundingBox* m_pBoundingBox;
00331 
00333         GLC_Matrix4x4 m_AbsoluteMatrix;
00334 
00336         bool m_IsBoundingBoxValid;
00337 
00339         GLC_RenderProperties m_RenderProperties;
00340 
00342         bool m_IsVisible;
00343 
00345         GLubyte m_colorId[4];
00346 
00348         int m_DefaultLOD;
00349 
00351         Viewable m_ViewableFlag;
00352 
00354         QVector<bool> m_ViewableGeomFlag;
00355 
00357         static QMutex m_Mutex;
00358 
00360         static int m_GlobalDefaultLOD;
00361 
00362 
00363 };
00364 
00365 // Return true if the all instance's mesh are transparent
00366 bool GLC_3DViewInstance::isTransparent() const
00367 {
00368         if (m_3DRep.isEmpty()) return false;
00369         if (m_RenderProperties.renderingMode() == glc::OverwriteTransparency) return true;
00370         if (m_RenderProperties.renderingMode() == glc::OverwriteMaterial)
00371         {
00372                 return m_RenderProperties.overwriteMaterial()->isTransparent();
00373         }
00374         const int size= m_3DRep.numberOfBody();
00375         bool result= true;
00376         int i= 0;
00377         while((i < size) && result)
00378         {
00379                 result= result && m_3DRep.geomAt(i)->isTransparent();
00380                 ++i;
00381         }
00382         return result && m_RenderProperties.needToRenderWithTransparency();
00383 }
00384 
00385 // Return true if the instance contains mesh which contains transparent material
00386 bool GLC_3DViewInstance::hasTransparentMaterials() const
00387 {
00388         if (m_3DRep.isEmpty()) return false;
00389         if (m_RenderProperties.needToRenderWithTransparency()) return true;
00390         const int size= m_3DRep.numberOfBody();
00391         bool result= false;
00392         int i= 0;
00393         while ((i < size) && !result)
00394         {
00395                 result= result || m_3DRep.geomAt(i)->hasTransparentMaterials();
00396                 ++i;
00397         }
00398         return result;
00399 }
00401 bool GLC_3DViewInstance::setViewable(GLC_3DViewInstance::Viewable flag)
00402 {
00403         const int bodyCount= m_3DRep.numberOfBody();
00404         if (bodyCount != m_ViewableGeomFlag.size())
00405         {
00406                 m_ViewableGeomFlag.fill(true, bodyCount);
00407         }
00408         bool asChange= m_ViewableFlag != flag;
00409         if (asChange)
00410         {
00411                 m_ViewableFlag= flag;
00412                 if (flag != GLC_3DViewInstance::PartialViewable)
00413                 {
00414                         bool viewable= (flag == GLC_3DViewInstance::FullViewable);
00415 
00416                         for (int i= 0; i < bodyCount; ++i)
00417                         {
00418                                 m_ViewableGeomFlag[i]= viewable;
00419                         }
00420                 }
00421         }
00422         return asChange;
00423 }
00424 
00425 
00426 #endif /*GLC_3DVIEWINSTANCE_H_*/

SourceForge.net Logo

©2005-2011 Laurent Ribon