glc_colladatoworld.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  *****************************************************************************/
00023 
00024 #ifndef GLC_COLLADATOWORLD_H_
00025 #define GLC_COLLADATOWORLD_H_
00026 
00027 #include <QObject>
00028 #include <QString>
00029 #include <QFile>
00030 #include <QXmlStreamReader>
00031 #include <QHash>
00032 #include <QColor>
00033 
00034 #include "../shading/glc_material.h"
00035 #include "../geometry/glc_mesh.h"
00036 #include "../sceneGraph/glc_structoccurence.h"
00037 
00038 #include "../glc_config.h"
00039 
00040 class GLC_World;
00041 class QGLContext;
00042 
00045 
00046 
00047 class GLC_LIB_EXPORT GLC_ColladaToWorld : public QObject
00048 {
00049 private:
00050         Q_OBJECT
00051 
00052         // The 3 supported semantic
00053         enum Semantic
00054         { // Values are very important !
00055                 VERTEX= 0,
00056                 NORMAL= 1,
00057                 TEXCOORD= 2
00058         };
00059 
00060         // input data info
00061         struct InputData
00062         {
00063                 int m_Offset;
00064                 QString m_Source;
00065                 Semantic m_Semantic;
00066         };
00067 public:
00068         // Collada Vertice (Position index, Normal index and TexCoord index)
00069         struct ColladaVertice
00070         {
00071                 ColladaVertice()
00072                 : m_Values(3)
00073                 {
00074                         m_Values[0]= 0;
00075                         m_Values[1]= 0;
00076                         m_Values[2]= 0;
00077                 }
00078 
00079                 QVector<int> m_Values;
00080         };
00081 private:
00082 
00083         // Material assignement
00084         struct MatOffsetSize
00085         {
00086                 int m_Offset;
00087                 int m_size;
00088         };
00089 
00090         // Accessor a source data (bulk)
00091         struct Accessor
00092         {
00093                 Accessor()
00094                 : m_Count(0)
00095                 , m_Offset(0)
00096                 , m_Stride(1)
00097                 {}
00098                 unsigned int m_Count;
00099                 unsigned int m_Offset;
00100                 unsigned int m_Stride;
00101         };
00102 
00103         // The loading mesh info
00104         struct MeshInfo
00105         {
00106                 MeshInfo()
00107                 : m_pMesh(NULL)
00108                 , m_Datas(3)
00109                 , m_Mapping()
00110                 , m_Index()
00111                 , m_FreeIndex(0)
00112                 {}
00113 
00114                 ~MeshInfo() {delete m_pMesh;}
00115                 // Mesh of the mesh info
00116                 GLC_Mesh* m_pMesh;
00117                 // Bulk data vector (Position, normal, texel)
00118                 QVector<QList<float> > m_Datas;
00119                 // Mapping between collada vertice and index
00120                 QHash<ColladaVertice, GLuint> m_Mapping;
00121                 // Triangle index
00122                 IndexList m_Index;
00123                 // Next index Position
00124                 GLuint m_FreeIndex;
00125                 // QHash containing material id and associated offset and size
00126                 QHash<QString, MatOffsetSize> m_Materials;
00127         };
00128 
00129         // The collada Node
00130         struct ColladaNode
00131         {
00132                 ColladaNode(const QString id, ColladaNode* pParent)
00133                 : m_Id(id)
00134                 , m_Matrix()
00135                 , m_InstanceGeometryIDs()
00136                 , m_InstanceOffNodeIds()
00137                 , m_ChildNodes()
00138                 , m_pParent(pParent)
00139                 {}
00140                 // Destrucot not needed
00141                 // The node id
00142                 QString m_Id;
00143                 // Position matrix
00144                 GLC_Matrix4x4 m_Matrix;
00145                 // Instance geometry id
00146                 QList<QString> m_InstanceGeometryIDs;
00147                 // Instance off another node
00148                 QList<QString> m_InstanceOffNodeIds;
00149                 // Child Node
00150                 QList<ColladaNode*> m_ChildNodes;
00151                 // Parent Node
00152                 ColladaNode* m_pParent;
00153         };
00154 
00155         typedef QHash<const QString, GLC_Material*> MaterialHash;
00156         typedef QHash<const QString, QList<float> > BulkDataHash;
00157         typedef QHash<const QString, Accessor> DataAccessorHash;
00159 
00161 
00162 public:
00164         GLC_ColladaToWorld();
00165 
00167         virtual ~GLC_ColladaToWorld();
00169 
00171 
00173 
00174 public:
00176         GLC_World* CreateWorldFromCollada(QFile &);
00177 
00179         inline QStringList listOfAttachedFileName() const
00180         {return m_ListOfAttachedFileName.toList();}
00181 
00183 
00185 // Qt Signals
00187         signals:
00188         void currentQuantum(int);
00189 
00191 
00193 
00194 private:
00196         void goToElement(const QString&);
00197 
00199         void goToEndElement(const QString&);
00200 
00201         // Return the content of an element
00202         QString getContent(const QString&);
00203 
00205         QString readAttribute(const QString&, bool required= false);
00206 
00209         void checkForXmlError(const QString&);
00210 
00212         void throwException(const QString&);
00213 
00215         void clear();
00216 
00218         void loadLibraryImage();
00219 
00221         void loadImage();
00222 
00224         void loadLibraryMaterials();
00225 
00227         void loadMaterial();
00228 
00230         void loadLibraryEffects();
00231 
00233         void loadEffect();
00234 
00236         void loadProfileCommon();
00237 
00239         void loadNewParam();
00240 
00242         void loadSurface(const QString&);
00243 
00245         void loadSampler2D(const QString&);
00246 
00248         void loadTechnique();
00249 
00251         void loadMaterialTechnique(const QString&);
00252 
00254         void loadCommonColorOrTexture(const QString&);
00255 
00257         void loadTransparent();
00258 
00260         void loadTransparency(const QString&);
00261 
00263         void loadShininess(const QString&);
00264 
00266         QColor readXmlColor();
00267 
00269         void loadLibraryGeometries();
00270 
00272         void loadGeometry();
00273 
00275         void loadMesh();
00276 
00278         void loadVertexBulkData();
00279 
00281         void loadTechniqueCommon();
00282 
00284         void loadAccessor();
00285 
00287         void loadVertices();
00288 
00290         void loadPolylist();
00291 
00293         void loadPolygons();
00294 
00296         void addPolylistToCurrentMesh(const QList<InputData>&, const QList<int>&, const QList<int>&, const QString&);
00297 
00299         void computeNormalOfCurrentPrimitiveOfCurrentMesh(int offset);
00300 
00302         void loadTriangles();
00303 
00305         void addTrianglesToCurrentMesh(const QList<InputData>&, const QList<int>&, const QString&);
00306 
00308         void loadLibraryNodes();
00309 
00311         void loadLibraryContollers();
00312 
00314         void loadVisualScenes();
00315 
00317         void loadInstanceGeometry(ColladaNode*);
00318 
00320         void loadInstanceNode(ColladaNode*);
00321 
00323         void loadInstanceController(ColladaNode*);
00324 
00326         void loadController();
00327 
00329         ColladaNode* loadNode(ColladaNode*);
00330 
00332         void translateNode(ColladaNode*);
00333 
00335         void scaleNode(ColladaNode*);
00336 
00338         void rotateNode(ColladaNode*);
00339 
00341         void composeMatrixNode(ColladaNode*);
00342 
00344         void loadScene();
00345 
00347         void linkTexturesToMaterials();
00348 
00350         void createMesh();
00351 
00353         void createSceneGraph();
00354 
00356         GLC_StructOccurence* createOccurenceFromNode(ColladaNode*);
00357 
00359         void updateProgressBar();
00360 
00361 
00362 
00364 
00365 // Private members
00367 private:
00369         GLC_World* m_pWorld;
00370 
00372         QXmlStreamReader* m_pStreamReader;
00373 
00375         QString m_FileName;
00376 
00378         QFile* m_pFile;
00379 
00381         QHash<QString, QString> m_ImageFileHash;
00382 
00384         QHash<QString, QString> m_MaterialLibHash;
00385 
00387         QHash<QString, QString> m_SurfaceImageHash;
00388 
00390         QHash<QString, QString> m_Sampler2DSurfaceHash;
00391 
00393         MaterialHash m_MaterialEffectHash;
00394 
00396         GLC_Material* m_pCurrentMaterial;
00397 
00399         MaterialHash m_TextureToMaterialHash;
00400 
00402         BulkDataHash m_BulkDataHash;
00403 
00405         DataAccessorHash m_DataAccessorHash;
00406 
00408         QHash<QString, QString> m_VerticesSourceHash;
00409 
00411         MeshInfo* m_pMeshInfo;
00412 
00414         QHash<const QString, MeshInfo*> m_GeometryHash;
00415 
00417         QHash<const QString, ColladaNode*> m_ColladaNodeHash;
00418 
00420         QList<ColladaNode*> m_TopLevelColladaNode;
00421 
00423         QHash<const QString, QString> m_MaterialInstanceMap;
00424 
00426         QHash<const QString, GLC_3DRep*> m_3DRepHash;
00427 
00429         QHash<const QString, GLC_StructInstance*> m_StructInstanceHash;
00430 
00432         QString m_CurrentId;
00433 
00435         qint64 m_FileSize;
00436 
00438         int m_CurrentOffset;
00439 
00441         QSet<QString> m_ListOfAttachedFileName;
00442 
00444         bool m_TransparentIsRgbZero;
00445 
00446 };
00447 
00448 // To use ColladaVertice as a QHash key
00449 inline bool operator==(const GLC_ColladaToWorld::ColladaVertice& vertice1, const GLC_ColladaToWorld::ColladaVertice& vertice2)
00450 { return (vertice1.m_Values == vertice2.m_Values);}
00451 
00452 inline uint qHash(const GLC_ColladaToWorld::ColladaVertice& vertice)
00453 { return qHash(QString::number(vertice.m_Values.at(0)) + QString::number(vertice.m_Values.at(1)) + QString::number(vertice.m_Values.at(2)));}
00454 
00455 #endif /* GLC_COLLADATOWORLD_H_ */

SourceForge.net Logo

©2005-2011 Laurent Ribon