glc_objtoworld.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, packaged on July 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 Lesser General Public License as published by
00011  the Free Software Foundation; either version 3 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 Lesser General Public License for more details.
00018 
00019  You should have received a copy of the GNU Lesser 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_OBJTOWORLD_H_
00028 #define GLC_OBJTOWORLD_H_
00029 
00030 #include <QFile>
00031 #include <QString>
00032 #include <QObject>
00033 #include <QHash>
00034 #include <QVector>
00035 #include <QStringList>
00036 
00037 #include "../maths/glc_vector3d.h"
00038 #include "../maths/glc_vector2df.h"
00039 #include "../maths/glc_vector3df.h"
00040 #include "../geometry/glc_mesh.h"
00041 
00042 #include "../glc_config.h"
00043 
00044 enum FaceType
00045 {
00046         notSet,
00047         coordinate,
00048         coordinateAndTexture,
00049         coordinateAndNormal,
00050         coordinateAndTextureAndNormal
00051 };
00052 
00053 class GLC_World;
00054 class GLC_ObjMtlLoader;
00055 class QGLContext;
00056 
00059 
00068 
00069 class GLC_LIB_EXPORT GLC_ObjToWorld : public QObject
00070 {
00071         Q_OBJECT
00072 
00073 public:
00074         // OBJ Vertice (Position index, Normal index and TexCoord index)
00075         struct ObjVertice
00076         {
00077                 ObjVertice()
00078                 : m_Values(3)
00079                 {
00080                         m_Values[0]= 0;
00081                         m_Values[1]= 0;
00082                         m_Values[2]= 0;
00083                 }
00084                 ObjVertice(int v1, int v2, int v3)
00085                 : m_Values(3)
00086                 {
00087                         m_Values[0]= v1;
00088                         m_Values[1]= v2;
00089                         m_Values[2]= v3;
00090                 }
00091 
00092                 QVector<int> m_Values;
00093         };
00094 
00095         // Material assignement
00096         struct MatOffsetSize
00097         {
00098                 MatOffsetSize()
00099                 : m_Offset(0)
00100                 , m_size(0)
00101                 {}
00102                 int m_Offset;
00103                 int m_size;
00104         };
00105 
00106         // Current OBJ Mesh
00107         struct CurrentObjMesh
00108         {
00109                 CurrentObjMesh(const QString materialName)
00110                 : m_pMesh(new GLC_Mesh())
00111                 , m_Positions()
00112                 , m_Normals()
00113                 , m_Texels()
00114                 , m_Index()
00115                 , m_pLastOffsetSize(new MatOffsetSize())
00116                 , m_Materials()
00117                 , m_NextFreeIndex(0)
00118                 , m_ObjVerticeIndexMap()
00119                 {
00120                         m_Materials.insert(materialName, m_pLastOffsetSize);
00121                 }
00122                 ~CurrentObjMesh()
00123                 {
00124                         QHash<QString, MatOffsetSize*>::iterator i= m_Materials.begin();
00125                         while (m_Materials.constEnd() != i)
00126                         {
00127                                 delete i.value();
00128                                 ++i;
00129                         }
00130                 }
00131                 GLC_Mesh* m_pMesh;
00132                 QList<float> m_Positions;
00133                 QList<float> m_Normals;
00134                 QList<float> m_Texels;
00136                 IndexList m_Index;
00137                 // Pointer to the last matOffsetSize
00138                 MatOffsetSize* m_pLastOffsetSize;
00139                 // QHash containing material id and associated offset and size
00140                 QHash<QString, MatOffsetSize*> m_Materials;
00142                 int m_NextFreeIndex;
00144                 QHash<ObjVertice, GLuint> m_ObjVerticeIndexMap;
00145         };
00146 
00148 
00150 
00151 
00152 public:
00153         GLC_ObjToWorld();
00154         virtual ~GLC_ObjToWorld();
00156 
00158 
00160 
00161 public:
00163         GLC_World* CreateWorldFromObj(QFile &file);
00164 
00166         inline QStringList listOfAttachedFileName() const{return m_ListOfAttachedFileName;}
00168 
00170 // Private services functions
00172 private:
00174         QString getMtlLibFileName(QString);
00175 
00177         void scanLigne(QString &);
00178 
00180         void changeGroup(QString);
00181 
00183         QList<float> extract3dVect(QString &);
00184 
00186         QList<float> extract2dVect(QString &);
00187 
00189         void extractFaceIndex(QString &);
00190 
00192         void setCurrentMaterial(QString &line);
00193 
00195         void extractVertexIndex(QString ligne, int &Coordinate, int &Normal, int &TextureCoordinate);
00196 
00198         void setObjType(QString &);
00199 
00201         GLC_Vector3df computeNormal(GLuint, GLuint, GLuint);
00202 
00204         void clear();
00205 
00207         void mergeLines(QString*, QTextStream*);
00208 
00210         void addCurrentObjMeshToWorld();
00211 
00212 
00213 
00215 // Qt Signals
00217         signals:
00218         void currentQuantum(int);
00219 
00221 // Private members
00223 private:
00225         GLC_World* m_pWorld;
00226 
00228         QString m_FileName;
00229 
00231         GLC_ObjMtlLoader* m_pMtlLoader;
00232 
00234         int m_CurrentLineNumber;
00235 
00237         CurrentObjMesh* m_pCurrentObjMesh;
00238 
00240         FaceType m_FaceType;
00241 
00243         QHash<QString, int> m_CurrentMeshMaterials;
00244 
00246         QString m_CurrentMaterialName;
00247 
00249         QStringList m_ListOfAttachedFileName;
00250 
00252         QList<float> m_Positions;
00253 
00255         QList<float> m_Normals;
00256 
00258         QList<float> m_Texels;
00259 };
00260 
00261 // To use ObjVertice as a QHash key
00262 inline bool operator==(const GLC_ObjToWorld::ObjVertice& vertice1, const GLC_ObjToWorld::ObjVertice& vertice2)
00263 { return (vertice1.m_Values == vertice2.m_Values);}
00264 
00265 inline uint qHash(const GLC_ObjToWorld::ObjVertice& vertice)
00266 { return qHash(QString::number(vertice.m_Values.at(0)) + QString::number(vertice.m_Values.at(1)) + QString::number(vertice.m_Values.at(2)));}
00267 
00268 
00269 #endif /*GLC_OBJTOWORLD_H_*/

SourceForge.net Logo

©2005-2011 Laurent Ribon