Stellarium 0.14.3
OBJ.hpp
1 /*
2  * Stellarium Scenery3d Plug-in
3  *
4  * Copyright (C) 2011 Simon Parzer, Peter Neubauer, Georg Zotti, Andrei Borza
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20 
23 //-----------------------------------------------------------------------------
24 // Copyright (c) 2007 dhpoware. All Rights Reserved.
25 //
26 // Permission is hereby granted, free of charge, to any person obtaining a
27 // copy of this software and associated documentation files (the "Software"),
28 // to deal in the Software without restriction, including without limitation
29 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
30 // and/or sell copies of the Software, and to permit persons to whom the
31 // Software is furnished to do so, subject to the following conditions:
32 //
33 // The above copyright notice and this permission notice shall be included in
34 // all copies or substantial portions of the Software.
35 //
36 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
37 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
39 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
40 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
41 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
42 // IN THE SOFTWARE.
43 //-----------------------------------------------------------------------------
44 
45 #ifndef _OBJ_HPP_
46 #define _OBJ_HPP_
47 
48 #include <QFile>
49 #include <QOpenGLBuffer>
50 #include <QOpenGLVertexArrayObject>
51 
52 #include "StelTexture.hpp"
53 #include "VecMath.hpp"
54 #include "AABB.hpp"
55 
56 class Heightmap;
57 
63 class OBJ
64 {
65 public:
68  enum vertexOrder { XYZ, XZY, YXZ, YZX, ZXY, ZYX };
69 
71  struct Material
72  {
75  enum Illum { I_NONE=-1, I_DIFFUSE=0, I_DIFFUSE_AND_AMBIENT=1, I_SPECULAR=2, I_TRANSLUCENT=9 };
76  Illum illum;
77 
80  ambient[0] = -1.f;
81  ambient[1] = -1.f;
82  ambient[2] = -1.f;
83  diffuse[0] = 0.8f;
84  diffuse[1] = 0.8f;
85  diffuse[2] = 0.8f;
86  specular[0] = 0.0f;
87  specular[1] = 0.0f;
88  specular[2] = 0.0f;
89  emission[0] = 0.0f;
90  emission[1] = 0.0f;
91  emission[2] = 0.0f;
92  shininess = 8.0f;
93  name = "<invalid>";
94  alpha = -1.0f;
95  alphatest = false;
96  backfacecull = true;
97  illum = I_NONE;
98  hasSpecularity = false;
99  hasTransparency = false;
100  }
101 
103  void finalize();
104 
106  QString name;
108  QVector3D ambient;
109  QVector3D diffuse;
110  QVector3D specular;
111  QVector3D emission;
113  float shininess;
115  float alpha;
117  bool alphatest;
124 
126  QString textureName;
128  StelTextureSP texture;
130  QString bumpMapName;
132  StelTextureSP bump_texture;
134  QString heightMapName;
136  StelTextureSP height_texture;
140  StelTextureSP emissive_texture;
141  };
142 
143 
146  struct Vertex
147  {
148  GLfloat position[3];
149  GLfloat texCoord[2];
150  GLfloat normal[3];
151  GLfloat tangent[4];
152  GLfloat bitangent[3];
153 
154  static const Vertex EmptyVertex;
155  };
156 
159  struct StelModel
160  {
161  int startIndex, triangleCount;
162  //materials are managed by OBJ
163  const Material* pMaterial;
164  //AABB is managed by this
165  AABB bbox;
166  //The centroid location of all vertices of this model
167  Vec3f centroid;
168  };
169 
171  OBJ();
173  ~OBJ();
174 
176  void clean();
178  bool load(const QString& filename, const enum vertexOrder order, bool rebuildNormals = false);
181  void transform(QMatrix4x4 mat);
183  Material &getMaterial(int i);
185  const StelModel& getStelModel(int i) const;
186 
189  void finalizeForRendering();
190 
193  void transparencyDepthSort(const Vec3f& position);
194 
196  int getNumberOfIndices() const;
197  int getNumberOfStelModels() const;
198  int getNumberOfTriangles() const;
199  int getNumberOfVertices() const;
200  int getNumberOfMaterials() const;
201 
203  const Vertex& getVertex(int i) const;
205  const Vertex* getVertexArray() const;
207  int getVertexSize() const;
208 
210  bool isLoaded() const;
211  bool hasPositions() const;
212  bool hasTextureCoords() const;
213  bool hasNormals() const;
214  bool hasTangents() const;
215  bool hasStelModels() const;
216 
218  //const BoundingBox* getBoundingBox() const;
219  const AABB& getBoundingBox();
220 
221  void renderAABBs();
222 
224  size_t memoryUsage();
225 
227  void uploadTexturesGL();
229  void uploadBuffersGL();
230 
232  void bindGL();
234  void unbindGL();
235 
237  static void setupGL();
240  static inline GLenum getIndexBufferType() { return indexBufferType; }
241  static inline size_t getIndexBufferTypeSize() { return indexBufferTypeSize; }
242 
244  OBJ& operator=(const OBJ& other);
245 private:
246  struct FaceAttributes
247  {
248  int materialIndex;
249  int objectIndex;
250  };
251 
252  typedef QVector<FaceAttributes> AttributeVector;
253  typedef QVector<Vec3f> VF3Vector;
254  typedef QVector<Vec2f> VF2Vector;
255  typedef Vec3f VPos;
256  typedef QVector<Vec3f> PosVector;
257  typedef QMap<QString,int> MatCacheT;
258  typedef QMap<int, QVector<int> > VertCacheT;
259 
260  void addFaceAttrib(AttributeVector& attributeArray, uint index, int material, int object);
261  void addTrianglePos(const PosVector& vertexCoords, VertCacheT &vertexCache, unsigned int index, int v0, int v1, int v2);
262  void addTrianglePosNormal(const PosVector &vertexCoords, const VF3Vector& normals, VertCacheT &vertexCache, unsigned int index,
263  int v0, int v1, int v2,
264  int vn0, int vn1, int vn2);
265  void addTrianglePosTexCoord(const PosVector &vertexCoords, const VF2Vector &textureCoords, VertCacheT &vertexCache, unsigned int index,
266  int v0, int v1, int v2,
267  int vt0, int vt1, int vt2);
268  void addTrianglePosTexCoordNormal(PosVector &vertexCoords, const VF2Vector &textureCoords, const VF3Vector &normals, VertCacheT &vertexCache, unsigned int index,
269  int v0, int v1, int v2,
270  int vt0, int vt1, int vt2,
271  int vn0, int vn1, int vn2);
272  int addVertex(VertCacheT &vertexCache, int hash, const Vertex* pVertex);
274  void buildStelModels(const AttributeVector &attributeArray);
276  void generateNormals();
278  void generateTangents();
280  void importFirstPass(QFile& pFile, MatCacheT &materialCache);
282  void importSecondPass(FILE *pFile, const enum vertexOrder order, const MatCacheT &materialCache);
284  bool importMaterials(const QString& filename, MatCacheT& materialCache);
285  QString absolutePath(QString path);
287  void findBounds();
289  void bindBuffersGL();
291  void unbindBuffersGL();
292 
294  QFile* getFile(const QString& filename);
295 
297  QString parseTextureString(const char *buffer) const;
298 
300  bool m_loaded;
301  bool m_hasPositions;
302  bool m_hasTextureCoords;
303  bool m_hasNormals;
304  bool m_hasTangents;
305  bool m_hasStelModels;
306  static bool vertexArraysSupported;
307 
309  static GLenum indexBufferType;
311  static size_t indexBufferTypeSize;
312  int m_firstTransparentIndex;
313 
315  unsigned int m_numberOfVertexCoords;
316  unsigned int m_numberOfTextureCoords;
317  unsigned int m_numberOfNormals;
318  unsigned int m_numberOfTriangles;
319  unsigned int m_numberOfMaterials;
320  unsigned int m_numberOfStelModels;
321 
323  AABB pBoundingBox;
324 
326  QString m_basePath;
327 
329  QVector<StelModel> m_stelModels;
330  QVector<Material> m_materials;
331  QVector<Vertex> m_vertexArray;
332  QVector<unsigned int> m_indexArray;
333 
335  QOpenGLBuffer m_vertexBuffer;
336  QOpenGLBuffer m_indexBuffer;
337  QOpenGLVertexArrayObject* m_vertexArrayObject;
338 
340  friend class Heightmap;
341 };
342 
343 inline OBJ::Material& OBJ::getMaterial(int i) { return m_materials[i]; }
344 
345 inline const OBJ::StelModel& OBJ::getStelModel(int i) const { return m_stelModels[i]; }
346 
347 inline int OBJ::getNumberOfIndices() const { return m_numberOfTriangles * 3; }
348 
349 inline int OBJ::getNumberOfStelModels() const { return m_numberOfStelModels; }
350 
351 inline int OBJ::getNumberOfTriangles() const { return m_numberOfTriangles; }
352 
353 inline int OBJ::getNumberOfVertices() const { return static_cast<int>(m_vertexArray.size()); }
354 
355 inline int OBJ::getNumberOfMaterials() const {return m_numberOfMaterials; }
356 
357 inline const OBJ::Vertex& OBJ::getVertex(int i) const { return m_vertexArray[i]; }
358 
359 inline const OBJ::Vertex* OBJ::getVertexArray() const { return &m_vertexArray[0]; }
360 
361 inline int OBJ::getVertexSize() const { return static_cast<int>(sizeof(Vertex)); }
362 
363 inline bool OBJ::isLoaded() const{ return m_loaded; }
364 
365 inline bool OBJ::hasNormals() const{ return m_hasNormals; }
366 
367 inline bool OBJ::hasPositions() const { return m_hasPositions; }
368 
369 inline bool OBJ::hasTangents() const { return m_hasTangents; }
370 
371 inline bool OBJ::hasTextureCoords() const { return m_hasTextureCoords; }
372 
373 inline bool OBJ::hasStelModels() const { return m_hasStelModels; }
374 
375 //inline const OBJ::BoundingBox* OBJ::getBoundingBox() const { return pBoundingBox; }
376 inline const AABB &OBJ::getBoundingBox() {return pBoundingBox; }
377 
378 inline QString OBJ::absolutePath(QString path) { return m_basePath + path; }
379 
380 #endif
Structure for a Mesh, will be used with Stellarium to render Holds the starting index, the number of triangles and a pointer to the MTL.
Definition: OBJ.hpp:159
float alpha
Transparency [0..1].
Definition: OBJ.hpp:115
A vertex struct holds the vertex itself (position), corresponding texture coordinates, normals, tangents and bitangents It does not use Vec3f etc.
Definition: OBJ.hpp:146
QString emissiveMapName
Name of emissive texture.
Definition: OBJ.hpp:138
static void setupGL()
Set up some stuff that requires a valid OpenGL context.
QVector3D ambient
Ka, Kd, Ks, Ke.
Definition: OBJ.hpp:108
QString name
Material name.
Definition: OBJ.hpp:106
This represents a heightmap for viewer-ground collision.
Definition: Heightmap.hpp:27
bool isLoaded() const
Returns flags.
Definition: OBJ.hpp:363
size_t memoryUsage()
Returns an estimate of the memory usage of this instance (not fully accurate, but good enough) ...
QString heightMapName
Height map name.
Definition: OBJ.hpp:134
QString bumpMapName
Bump map name.
Definition: OBJ.hpp:130
bool load(const QString &filename, const enum vertexOrder order, bool rebuildNormals=false)
Loads the given obj file and, if specified rebuilds normals.
~OBJ()
Destructor.
void transform(QMatrix4x4 mat)
Transform all the vertices through multiplication with a 4x4 matrix.
bool hasSpecularity
Quick check if this material has specularity.
Definition: OBJ.hpp:121
int getNumberOfIndices() const
Getters for various datastructures.
Definition: OBJ.hpp:347
const AABB & getBoundingBox()
Returns the bounding box for this OBJ.
Definition: OBJ.hpp:376
bool alphatest
If to perform binary alpha testing. Default off.
Definition: OBJ.hpp:117
QString textureName
Texture name.
Definition: OBJ.hpp:126
bool hasTransparency
Quick check if this material has "real" transparency (i.e. needs blending)
Definition: OBJ.hpp:123
OBJ & operator=(const OBJ &other)
Copy assignment operator. No deep copies are performed, but QVectors have copy-on-write semantics...
An axis-aligned bounding-box class.
Definition: AABB.hpp:42
void bindGL()
Binds the necessary GL objects, making the OBJ ready for drawing. Uses a VAO if the platform supports...
OBJ()
Initializes values.
void unbindGL()
Unbinds this object's GL objects.
const Vertex * getVertexArray() const
Returns the vertex array.
Definition: OBJ.hpp:359
void transparencyDepthSort(const Vec3f &position)
Sorts the transparent StelModels according to their distance to the specified position.
bool backfacecull
If to perform backface culling. Default on.
Definition: OBJ.hpp:119
void finalizeForRendering()
This should be called after textures are loaded, and will re-order the StelModels to be grouped by th...
const StelModel & getStelModel(int i) const
Returns a StelModel.
Definition: OBJ.hpp:345
void clean()
Cleanup, will be called inside the destructor.
Material()
Creates a material with the default values.
Definition: OBJ.hpp:79
int getVertexSize() const
Returns the vertex size.
Definition: OBJ.hpp:361
void finalize()
Needs to be called after everything is loaded.
Illum
MTL Illumination models, do not use externally anymore.
Definition: OBJ.hpp:75
A basic Wavefront .OBJ format model loader.
Definition: OBJ.hpp:63
static GLenum getIndexBufferType()
Returns the OpenGL index buffer type supported on this hardware.
Definition: OBJ.hpp:240
QSharedPointer< StelTexture > StelTextureSP
void uploadTexturesGL()
Uploads the textures to GL (requires valid context)
Material & getMaterial(int i)
Returns a Material.
Definition: OBJ.hpp:343
Encapsulates all information that the describes the surface appearance of a StelModel.
Definition: OBJ.hpp:71
float shininess
Shininess [0..128].
Definition: OBJ.hpp:113
vertexOrder
OBJ files can have vertices encoded in different order.
Definition: OBJ.hpp:68
void uploadBuffersGL()
Uploads the vertex and index data to GL buffers (requires valid context)
const Vertex & getVertex(int i) const
Returns a vertex reference.
Definition: OBJ.hpp:357