Overte C++ Documentation
graphics/src/graphics/Stage.h
1 //
2 // Stage.h
3 // libraries/graphics/src/graphics
4 //
5 // Created by Sam Gateau on 2/24/2015.
6 // Copyright 2014 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 #ifndef hifi_model_Stage_h
12 #define hifi_model_Stage_h
13 
14 #include <gpu/Pipeline.h>
15 
16 #include "Light.h"
17 #include "Skybox.h"
18 
19 namespace graphics {
20 
21 typedef glm::dvec3 Vec3d;
22 typedef glm::dvec4 Vec4d;
23 typedef glm::dmat4 Mat4d;
24 typedef glm::mat4 Mat4;
25 
26 class EarthSunModel {
27 public:
28 
29  void setScale(float scale);
30  float getScale() const { return _scale; }
31 
32  void setLatitude(float lat);
33  float getLatitude() const { return _latitude; }
34  void setLongitude(float lon);
35  float getLongitude() const { return _longitude; }
36  void setAltitude(float altitude);
37  float getAltitude() const { return _altitude; }
38 
39 
40  void setSurfaceOrientation(const Quat& orientation);
41  const Quat& getSurfaceOrientation() const { valid(); return _surfaceOrientation; }
42 
43  const Vec3d& getSurfacePos() const { valid(); return _surfacePos; }
44 
45  const Mat4d& getSurfaceToWorldMat() const { valid(); return _surfaceToWorldMat; }
46  const Mat4d& getWoldToSurfaceMat() const { valid(); return _worldToSurfaceMat; }
47 
48  const Mat4d& getEyeToSurfaceMat() const { valid(); return _eyeToSurfaceMat; }
49  const Mat4d& getSurfaceToEyeMat() const { valid(); return _surfaceToEyeMat; }
50 
51  const Mat4d& getEyeToWorldMat() const { valid(); return _eyeToWorldMat; }
52  const Mat4d& getWorldToEyeMat() const { valid(); return _worldToEyeMat; }
53 
54 
55  //or set the surfaceToEye mat directly
56  void setEyeToSurfaceMat( const Mat4d& e2s);
57 
58  const Vec3d& getEyePos() const { valid(); return _eyePos; }
59  const Vec3d& getEyeDir() const { valid(); return _eyeDir; }
60 
61  void setSunLongitude(float lon);
62  float getSunLongitude() const { return _sunLongitude; }
63 
64  void setSunLatitude(float lat);
65  float getSunLatitude() const { return _sunLatitude; }
66 
67  const Vec3d& getWorldSunDir() const { valid(); return _sunDir; }
68  const Vec3d& getSurfaceSunDir() const { valid(); return _surfaceSunDir; }
69 
70 
71  EarthSunModel() { valid(); }
72 
73 protected:
74  float _scale = 1000.0f; //Km
75  float _earthRadius = 6360.0;
76 
77  Quat _surfaceOrientation;
78 
79  float _longitude = 0.0f;
80  float _latitude = 0.0f;
81  float _altitude = 0.01f;
82  mutable Vec3d _surfacePos;
83  mutable Mat4d _worldToSurfaceMat;
84  mutable Mat4d _surfaceToWorldMat;
85  void updateWorldToSurface() const;
86 
87  mutable Mat4d _surfaceToEyeMat;
88  mutable Mat4d _eyeToSurfaceMat;
89  mutable Vec3d _eyeDir;
90  mutable Vec3d _eyePos;
91  void updateSurfaceToEye() const;
92 
93  mutable Mat4d _worldToEyeMat;
94  mutable Mat4d _eyeToWorldMat;
95 
96  float _sunLongitude = 0.0f;
97  float _sunLatitude = 0.0f;
98  mutable Vec3d _sunDir;
99  mutable Vec3d _surfaceSunDir;
100  void updateSun() const;
101 
102  mutable bool _invalid = true;
103  void invalidate() const { _invalid = true; }
104  void valid() const { if (_invalid) { updateAll(); _invalid = false; } }
105  void updateAll() const;
106 
107  static Mat4d evalWorldToGeoLocationMat(double longitude, double latitude, double altitude, double scale);
108 };
109 
110 // Sun sky stage generates the rendering primitives to display a scene realistically
111 // at the specified location and time around earth
112 class SunSkyStage {
113 public:
114 
115  SunSkyStage();
116  ~SunSkyStage();
117 
118  // time of the day (local to the position) expressed in decimal hour in the range [0.0, 24.0]
119  void setDayTime(float hour);
120  float getDayTime() const { return _dayTime; }
121 
122  // time of the year expressed in day in the range [0, 365]
123  void setYearTime(unsigned int day);
124  unsigned int getYearTime() const { return _yearTime; }
125 
126  // Origin orientation used to modify the cardinal axis alignement used.
127  // THe default is north along +Z axis and west along +X axis. this orientation gets added
128  // to the transform stack producing the sun light direction.
129  void setOriginOrientation(const Quat& orientation);
130  const Quat& getOriginOrientation() const { return _earthSunModel.getSurfaceOrientation(); }
131 
132  // Location used to define the sun & sky is a longitude and latitude [rad] and a earth surface altitude [km]
133  void setOriginLatitude(float latitude);
134  void setOriginLongitude(float longitude);
135  void setOriginSurfaceAltitude(float surfaceAltitude);
136  void setOriginLocation(float longitude, float latitude, float surfaceAltitude);
137  float getOriginLatitude() const { return _earthSunModel.getLatitude(); }
138  float getOriginLongitude() const { return _earthSunModel.getLongitude(); }
139  float getOriginSurfaceAltitude() const { return _earthSunModel.getAltitude(); }
140 
141  // Enable / disable the effect of the time and location on the sun direction and color
142  void setSunModelEnable(bool isEnabled);
143  bool isSunModelEnabled() const { return _sunModelEnable; }
144 
145  // Sun properties
146  void setSunColor(const Vec3& color) { _sunLight->setColor(color); }
147  const Vec3& getSunColor() const { return getSunLight()->getColor(); }
148  void setSunIntensity(float intensity) { _sunLight->setIntensity(intensity); }
149  float getSunIntensity() const { return getSunLight()->getIntensity(); }
150  void setSunAmbientIntensity(float intensity) { _sunLight->setAmbientIntensity(intensity); }
151  float getSunAmbientIntensity() const { return getSunLight()->getAmbientIntensity(); }
152  void setSunAmbientSphere(const gpu::SHPointer& sphere);
153  void setSunAmbientMap(const gpu::TexturePointer& map);
154 
155  // The sun direction is expressed in the world space
156  void setSunDirection(const Vec3& direction);
157  const Vec3& getSunDirection() const { return getSunLight()->getDirection(); }
158 
159  LightPointer getSunLight() const { valid(); return _sunLight; }
160 
161  enum BackgroundMode {
162  NO_BACKGROUND = 0,
163  SKY_DEFAULT,
164  SKY_BOX,
165  SKY_DEFAULT_AMBIENT_TEXTURE,
166  SKY_DEFAULT_TEXTURE,
167 
168  NUM_BACKGROUND_MODES,
169  };
170  void setBackgroundMode(BackgroundMode mode);
171  BackgroundMode getBackgroundMode() const { return _backgroundMode; }
172 
173  // Skybox
174  void setSkybox(const SkyboxPointer& skybox);
175  const SkyboxPointer& getSkybox() const { valid(); return _skybox; }
176 
177 protected:
178  BackgroundMode _backgroundMode = SKY_DEFAULT;
179 
180  LightPointer _sunLight;
181  mutable SkyboxPointer _skybox;
182 
183  float _dayTime = 12.0f;
184  int _yearTime = 0;
185  mutable EarthSunModel _earthSunModel;
186  bool _sunModelEnable = true;
187 
188  mutable bool _invalid = true;
189  void invalidate() const { _invalid = true; }
190  void valid() const { if (_invalid) { updateGraphicsObject(); _invalid = false; } }
191  void updateGraphicsObject() const;
192 };
193 
194 typedef std::shared_ptr< SunSkyStage > SunSkyStagePointer;
195 
196 };
197 
198 #endif
Provides the Mat4 scripting interface.
Definition: Mat4.h:44
Provides the Quat scripting interface.
Definition: Quat.h:61
Provides the Vec3 scripting interface.
Definition: Vec3.h:80