27 #include <QLoggingCategory> 33 Q_DECLARE_LOGGING_CATEGORY(stelOBJ)
43 XYZ, XZY, YXZ, YZX, ZXY, ZYX
72 enum Illum { I_NONE=-1, I_DIFFUSE=0, I_DIFFUSE_AND_AMBIENT=1, I_SPECULAR=2, I_TRANSLUCENT=9 } illum;
76 Ka(-1.0f,-1.0f,-1.0f),Kd(-1.0f,-1.0f,-1.0f),Ks(-1.0f,-1.0f,-1.0f),Ke(-1.0f,-1.0f,-1.0f),
113 typedef QMap<QString,QStringList> ParamsMap;
123 static QVector<Material> loadFromFile(
const QString& filename);
128 static bool parseBool(
const QStringList& params,
bool &out);
132 static bool parseFloat(
const QStringList ¶ms,
float &out);
136 static bool parseVec2d(
const QStringList ¶ms,
Vec2d &out);
171 typedef QVector<MaterialGroup> MaterialGroupList;
178 : isDefaultObject(
false),
201 void postprocess(
const StelOBJ& obj,
Vec3d& centroid);
202 friend class StelOBJ;
205 typedef QVector<Vec3f> V3Vec;
206 typedef QVector<Vec2f> V2Vec;
207 typedef QVector<Vertex> VertexList;
208 typedef QVector<unsigned int> IndexList;
209 typedef QVector<unsigned short> ShortIndexList;
210 typedef QVector<Material> MaterialList;
211 typedef QMap<QString, int> MaterialMap;
212 typedef QVector<Object> ObjectList;
213 typedef QMap<QString, int> ObjectMap;
224 inline unsigned int getFaceCount()
const {
return m_indices.size() / 3; }
247 bool load(
const QString& filename,
const VertexOrder vertexOrder = VertexOrder::XYZ);
253 bool load(QIODevice& device,
const QString& basePath,
const VertexOrder vertexOrder = VertexOrder::XYZ);
264 inline bool canUseShortIndices()
const {
return m_vertices.size() < std::numeric_limits<unsigned short>::max(); }
277 void scale(
double factor);
281 void transform(
const QMatrix4x4& mat,
bool onlyPosition =
false);
287 V3Vec*
normal = Q_NULLPTR,
300 typedef QVector<QStringRef> ParseParams;
301 typedef QHash<Vertex, int> VertexCache;
303 struct CurrentParserState
305 int currentMaterialIdx;
312 VertexList m_vertices;
316 MaterialList m_materials;
317 MaterialMap m_materialMap;
318 ObjectList m_objects;
319 ObjectMap m_objectMap;
327 inline Object* getCurrentObject(CurrentParserState& state);
329 inline MaterialGroup* getCurrentMaterialGroup(CurrentParserState& state);
331 inline int getCurrentMaterialIndex(CurrentParserState& state);
333 inline static bool parseBool(
const ParseParams& params,
bool& out,
int paramsStart=1);
335 inline static bool parseInt(
const ParseParams& params,
int& out,
int paramsStart=1);
337 inline static bool parseString(
const ParseParams ¶ms, QString &out,
int paramsStart=1);
338 inline static QString getRestOfString(
const QString &strip,
const QString& line);
340 inline static bool parseFloat(
const ParseParams& params,
float& out,
int paramsStart=1);
345 inline static bool parseVec3(
const ParseParams& params, T& out,
int paramsStart=1);
350 inline static bool parseVec2(
const ParseParams& params, T& out,
int paramsStart=1);
351 inline bool parseFace(
const ParseParams& params,
const V3Vec& posList,
const V3Vec& normList,
const V2Vec& texList,
352 CurrentParserState &state, VertexCache& vertCache);
354 inline void addObject(
const QString& name, CurrentParserState& state);
357 void generateNormals();
360 void generateTangents();
367 void performPostProcessing(
bool genNormals);
373 #if (QT_VERSION >= QT_VERSION_CHECK(5,4,0)) 378 uint h1 = qHash(reinterpret_cast<const quint64*>(key.
position)[0], seed);
379 uint h2 = qHash(reinterpret_cast<const quint64*>(key.
position)[1], seed);
381 return ((h1 << 16) | (h1 >> 16)) ^ h2 ^ seed;
385 #endif // _STELOBJ_HPP_ GLfloat bitangent[3]
The vertex bitangent.
Vec3f centroid
The centroid of this object at load time.
bool isLoaded() const
Returns true if this object contains valid data from a load() method.
AABBox boundingbox
The AABB of this group at load time.
GLfloat tangent[4]
The vertex tangent.
QString name
The name of the object. May be empty.
GLfloat normal[3]
The vertex normal.
QString map_Ke
The emissive texture map file path.
Vec3f centroid
The centroid of this group at load time.
int startIndex
The starting index in the index list.
GLfloat texCoord[2]
The UV texture coordinate.
int materialIndex
The index of the material that this group uses.
QVector3D Ka
Ambient coefficient. Contains all -1 if not set by .mtl.
bool operator==(const Vertex &b) const
Checks if the 2 vertices correspond to the same data using memcmp.
QString name
Name of the material as defined in the .mtl, default empty.
const ObjectMap & getObjectMap() const
Returns the object map (mapping the object names to their indices in the object list) ...
const MaterialList & getMaterialList() const
Returns the list of materials.
void rebuildNormals()
Rebuilds vertex normals as the average of face normals.
void clearVertexData()
Clears the internal vertex list to save space, meaning getVertexList() returns an empty list! The oth...
int indexCount
The amount of indices after the start index which belong to this material group.
bool isDefaultObject
True if this object was automatically generated because no 'o' statements were before the first 'f' s...
QVector3D Ks
Specular coefficient. Contains all -1 if not set by .mtl.
void transform(const QMatrix4x4 &mat, bool onlyPosition=false)
Applies the given transformation matrix to the vertex data.
unsigned int getFaceCount() const
Returns the number of faces.
Defines a material loaded from an .mtl file.
QString map_bump
The bump/normal texture map file path.
QString map_Ka
The ambient texture map file path.
int objectIndex
The index of the object this group belongs to.
GLfloat position[3]
The XYZ position.
ShortIndexList getShortIndexList() const
Converts the index list (as returned by getIndexList()) to use unsigned short instead of integer...
QVector3D Kd
Diffuse coefficient. Contains all -1 if not set by .mtl.
bool canUseShortIndices() const
Returns if unsigned short indices can be used instead of unsigned int indices, to save some memory...
Illum
MTL Illumination models, see the developer doc for info.
const ObjectList & getObjectList() const
Returns the list of objects.
ParamsMap additionalParams
Contains all other material parameters that are not recognized by this class, but can still be access...
void splitVertexData(V3Vec *position, V2Vec *texCoord=Q_NULLPTR, V3Vec *normal=Q_NULLPTR, V3Vec *tangent=Q_NULLPTR, V3Vec *bitangent=Q_NULLPTR) const
Splits the vertex data into separate arrays.
QString map_Kd
The diffuse texture map file path.
const VertexList & getVertexList() const
Returns an vertex list, suitable for loading into OpenGL arrays.
const Vec3f & getCentroid() const
Returns the global centroid of all vertices of the OBJ.
const AABBox & getAABBox() const
Returns the global AABB of all vertices of the OBJ.
Representation of a custom subset of a Wavefront .obj file, including only triangle data and material...
A Vertex struct holds the vertex itself (position), corresponding texture coordinates, normals, tangents and bitangents It does not use Vec3f etc.
Represents a bunch of faces following after each other that use the same material.
Represents an OBJ object as defined with the 'o' statement.
bool load(const QString &filename, const VertexOrder vertexOrder=VertexOrder::XYZ)
Loads an .obj file by name.
MaterialGroupList groups
The list of material groups in this object.
QString map_Ks
The specular texture map file path.
float d
Alpha value (1 means opaque).
AABBox boundingbox
The AABB of this object at load time.
An axis-aligned bounding-box class.
void scale(double factor)
Scales the vertex positions according to the given factor.
StelOBJ()
Constructs an empty StelOBJ. Use load() to load data from a .obj file.
QVector3D Ke
Emissive coefficient. Contains all -1 if not set by .mtl.
This header contains useful classes for common geometric operations that are useful for 3D rendering...
const IndexList & getIndexList() const
Returns an index list, suitable for use with OpenGL element arrays.
float Ns
Specular shininess (exponent), should be > 0. Default 8.0.
VertexOrder
Possible vertex orderings with load()
QString map_height
The height map texture file path.
void clear()
Resets all data contained in this StelOBJ.