Stellarium  0.90.0
S3DScene.hpp
1 /*
2  * Stellarium Scenery3d Plug-in
3  *
4  * Copyright (C) 2011-16 Simon Parzer, Peter Neubauer, Georg Zotti, Andrei Borza, Florian Schaukowitsch
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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
19  */
20 
21 #ifndef _S3DSCENE_HPP_
22 #define _S3DSCENE_HPP_
23 
24 #include "StelOBJ.hpp"
25 #include "StelTextureTypes.hpp"
26 #include "StelOpenGLArray.hpp"
27 #include "SceneInfo.hpp"
28 #include "Heightmap.hpp"
29 
30 #include <cfloat>
31 
32 Q_DECLARE_LOGGING_CATEGORY(s3dscene)
33 
34 class S3DScene
35 {
36 public:
38  struct Material : public StelOBJ::Material
39  {
40  Material() : traits(), bAlphatest(false), bBackface(false), fAlphaThreshold(0.5),
41  vis_fadeIn(-DBL_MAX, -DBL_MAX),vis_fadeOut(DBL_MAX,DBL_MAX),vis_fadeValue(1.0)
42  {
43 
44  }
45 
46  Material(const StelOBJ::Material& stelMat)
47  : StelOBJ::Material(stelMat), traits(), bAlphatest(false), bBackface(false), fAlphaThreshold(0.5),
48  vis_fadeIn(-DBL_MAX, -DBL_MAX),vis_fadeOut(DBL_MAX,DBL_MAX),vis_fadeValue(1.0)
49  {
50  if(additionalParams.contains("bAlphatest"))
51  parseBool(additionalParams.value("bAlphatest"), bAlphatest);
52  if(additionalParams.contains("bBackface"))
53  parseBool(additionalParams.value("bBackface"), bBackface);
54  if(additionalParams.contains("fAlphaThreshold"))
55  parseFloat(additionalParams.value("fAlphaThreshold"), fAlphaThreshold);
56  if(additionalParams.contains("vis_fadeIn"))
57  {
58  traits.hasTimeFade = true;
59  parseVec2d(additionalParams.value("vis_fadeIn"), vis_fadeIn);
60  }
61  if(additionalParams.contains("vis_fadeOut"))
62  {
63  traits.hasTimeFade = true;
64  parseVec2d(additionalParams.value("vis_fadeOut"), vis_fadeOut);
65  }
66  }
67 
68  struct Traits
69  {
70  //bool hasAmbientTexture; //! True when tex_Ka is a valid texture
71  bool hasDiffuseTexture;
72  //bool hasSpecularTexture; //! True when tex_Ks is a valid texture
78  bool hasTimeFade;
80  bool isFading;
81  } traits;
82 
83  //the actual texture instances
84  StelTextureSP tex_Kd;
85  StelTextureSP tex_Ke;
86  StelTextureSP tex_bump;
87  StelTextureSP tex_height;
88 
89  bool bAlphatest;
90  bool bBackface;
91  float fAlphaThreshold;
92 
93  Vec2d vis_fadeIn; // JD of begin of first visibility and begin of full visibility.
94  Vec2d vis_fadeOut; // JD of end of full visibility and end of last visibility.
97 
99  void loadTexturesAsync();
102  void fixup();
104  bool updateFadeInfo(double currentJD);
105  };
106 
107  typedef QVector<Material> MaterialList;
108  //for now, this does not use custom extensions...
109  typedef StelOBJ::ObjectList ObjectList;
110 
111  explicit S3DScene(const SceneInfo& info);
112 
113  void setModel(const StelOBJ& model);
114  void setGround(const StelOBJ& ground);
115 
116  const SceneInfo& getSceneInfo() const { return info; }
117 
118  MaterialList& getMaterialList() { return materials; }
119  const Material& getMaterial(int index) const { return materials.at(index); }
120  const ObjectList& getObjects() const { return objects; }
121 
126  void moveViewer(const Vec3d& moveView);
127 
129  void setViewerPosition(const Vec3d& pos);
131  void setViewerPositionOnHeightmap(const Vec2d& pos);
133  inline const Vec3d& getViewerPosition() const { return position; }
135  inline Vec2d getViewer2DPosition() const { return Vec2d(position[0], position[1]); }
137  inline const Vec3d& getEyePosition() const { return eyePosition; }
138  inline double getEyeHeight() const { return eye_height; }
139  inline void setEyeHeight(double height) { eye_height = height; recalcEyePos();}
140  inline const AABBox& getSceneAABB() const { return sceneAABB; }
141  float getGroundHeightAtViewer() const;
142  inline void setViewDirection(const Vec3d& viewDir) { viewDirection = viewDir; }
143  inline const Vec3d& getViewDirection() const { return viewDirection; }
144 
145  Vec3d getGridPosition() const;
146  void setGridPosition(const Vec3d& gridPos);
147 
149  bool glLoad();
151  bool isGLReady() const { return glReady; }
152  // Basic wrappers alround StelOpenGLArray
153  inline void glBind() { glArray.bind(); }
154  inline void glRelease() { glArray.release(); }
155  inline void glDraw(int offset, int count) const { glArray.draw(offset,count); }
156 
157 private:
158  inline void recalcEyePos() { eyePosition = position; eyePosition[2]+=eye_height; }
159  MaterialList materials;
160  ObjectList objects;
161 
162 
163  bool glReady;
164 
165  SceneInfo info;
166  QMatrix4x4 zRot2Grid;
167 
168  AABBox sceneAABB;
169 
170  // current view direction vector
171  Vec3d viewDirection;
172  // current foot position in model
173  Vec3d position;
174  // current eye height (relative to foot position)
175  double eye_height;
176  // current eye position in model (stored to avoid repeated re-calculations)
177  Vec3d eyePosition;
178 
179  //the model data is only retained before loaded into GL
180  StelOBJ modelData;
181  Heightmap heightmap;
182  StelOpenGLArray glArray;
183 
184  static void finalizeTexture(StelTextureSP& tex);
185 };
186 
187 #endif // _S3DSCENE_HPP_
Vec2d getViewer2DPosition() const
Returns the viewer foot position, without the height.
Definition: S3DScene.hpp:135
bool hasSpecularity
True when tex_height is a valid texture.
Definition: S3DScene.hpp:76
Encapsulates vertex data stored in the OpenGL server memory, which can be used for fast drawing comma...
Contains all the metadata necessary for a Scenery3d scene, and can be loaded from special ...
Definition: SceneInfo.hpp:37
This represents a heightmap for viewer-ground collision.
Definition: Heightmap.hpp:27
Define the StelTextureSP type.
bool hasEmissiveTexture
True when tex_Kd is a valid texture.
Definition: S3DScene.hpp:73
bool isFullyTransparent
True when the material has time-dependent display.
Definition: S3DScene.hpp:79
bool hasTransparency
True when both Ks and Ns are non-zero.
Definition: S3DScene.hpp:77
bool hasTimeFade
True when the material exhibits "true" transparency, that is when the tex_Kd has an alpha channel or ...
Definition: S3DScene.hpp:78
Defines a material loaded from an .mtl file.
Definition: StelOBJ.hpp:68
float vis_fadeValue
Updated by S3DRenderer when necessary, otherwise always 1.0.
Definition: S3DScene.hpp:96
bool hasHeightTexture
True when tex_bump is a valid texture.
Definition: S3DScene.hpp:75
Extension of StelOBJ::Material which provides Scenery3d specific stuff.
Definition: S3DScene.hpp:38
bool isFading
True when the material is (currently) completely invisible.
Definition: S3DScene.hpp:80
const Vec3d & getViewerPosition() const
Get current viewer foot position.
Definition: S3DScene.hpp:133
const Vec3d & getEyePosition() const
Get current eye position (= foot position + eye height)
Definition: S3DScene.hpp:137
Representation of a custom subset of a Wavefront .obj file, including only triangle data and material...
Definition: StelOBJ.hpp:37
bool isGLReady() const
Returns true if the scene is ready for GL rendering (glLoad succeded)
Definition: S3DScene.hpp:151
QSharedPointer< StelTexture > StelTextureSP
Use shared pointer to simplify memory managment.
An axis-aligned bounding-box class.
Definition: GeomMath.hpp:31
bool hasBumpTexture
True when tex_Ke is a valid texture.
Definition: S3DScene.hpp:74