CalcMySky  v0.3.1
AtmosphereRenderer.hpp
1 #ifndef INCLUDE_ONCE_5DB905D2_61C0_44DB_8F35_67B31BD78315
2 #define INCLUDE_ONCE_5DB905D2_61C0_44DB_8F35_67B31BD78315
3 
4 #include <cmath>
5 #include <array>
6 #include <deque>
7 #include <memory>
8 #include <glm/glm.hpp>
9 #include <QObject>
10 #include <QOpenGLTexture>
11 #include <QOpenGLShaderProgram>
12 #include <QOpenGLFunctions_3_3_Core>
13 #include "../common/types.hpp"
14 #include "../common/AtmosphereParameters.hpp"
16 
18 {
19  using ShaderProgPtr=std::unique_ptr<QOpenGLShaderProgram>;
20  using TexturePtr=std::unique_ptr<QOpenGLTexture>;
21  using ScattererName=QString;
22  QOpenGLFunctions_3_3_Core& gl;
23 public:
24 
41  AtmosphereRenderer(QOpenGLFunctions_3_3_Core& gl,
42  QString const& pathToData,
43  ShowMySky::Settings* tools,
44  std::function<void(QOpenGLShaderProgram& shprog)> const& drawSurface);
48  void setDrawSurfaceCallback(std::function<void(QOpenGLShaderProgram& shprog)> const& drawSurface) override;
49  int initDataLoading(QByteArray viewDirVertShaderSrc, QByteArray viewDirFragShaderSrc,
50  std::vector<std::pair<std::string,GLuint>> viewDirBindAttribLocations) override;
51  void setViewDirShaders(QByteArray viewDirVertShaderSrc, QByteArray viewDirFragShaderSrc,
52  std::vector<std::pair<std::string,GLuint>> viewDirBindAttribLocations) override;
53  LoadingStatus stepDataLoading() override;
54  int initPreparationToDraw() override;
56  QString currentActivity() const override { return currentActivity_; }
57  bool isLoading() const override { return totalLoadingStepsToDo_ > 0; }
58  bool isReadyToRender() const override { return state_ == State::ReadyToRender; }
59  bool canGrabRadiance() const override;
60  bool canSetSolarSpectrum() const override;
62  GLuint getLuminanceTexture() override { return luminanceRenderTargetTexture_.textureId(); };
63 
64  void draw(double brightness, bool clear) override;
65  void resizeEvent(int width, int height) override;
66  QVector4D getPixelLuminance(QPoint const& pixelPos) override;
67  SpectralRadiance getPixelSpectralRadiance(QPoint const& pixelPos) override;
68  std::vector<float> getWavelengths() override;
69  void setSolarSpectrum(std::vector<float> const& solarIrradianceAtTOA) override;
70  void resetSolarSpectrum() override;
71  Direction getViewDirection(QPoint const& pixelPos) override;
72 
73  void setScattererEnabled(QString const& name, bool enable) override;
74  int initShaderReloading() override;
75  LoadingStatus stepShaderReloading() override;
76  AtmosphereParameters const& atmosphereParameters() const { return params_; }
77 
78 private: // variables
79  ShowMySky::Settings* tools_;
80  std::function<void(QOpenGLShaderProgram&)> drawSurfaceCallback;
81  AtmosphereParameters params_;
82  QString pathToData_;
83  int totalLoadingStepsToDo_=-1, loadingStepsDone_=0, currentLoadingIterationStepCounter_=0;
84  QString currentActivity_;
85 
86  QByteArray viewDirVertShaderSrc_, viewDirFragShaderSrc_;
87  std::vector<std::pair<std::string,GLuint>> viewDirBindAttribLocations_;
88 
89  GLuint vao_=0, vbo_=0, luminanceRadianceFBO_=0, viewDirectionFBO_=0;
90  GLuint eclipseSingleScatteringPrecomputationFBO_=0;
91  GLuint eclipseDoubleScatteringPrecomputationFBO_=0;
92  // Lower and upper altitude slices from the 4D texture
93  std::vector<TexturePtr> eclipsedDoubleScatteringTextures_;
94  std::vector<TexturePtr> multipleScatteringTextures_;
95  std::vector<TexturePtr> transmittanceTextures_;
96  std::vector<TexturePtr> irradianceTextures_;
97  std::vector<TexturePtr> lightPollutionTextures_;
98  std::vector<GLuint> radianceRenderBuffers_;
99  std::map<ScattererName,std::vector<TexturePtr>> singleScatteringInterpolationGuidesTextures01_; // VZA-dotViewSun dimensions
100  std::map<ScattererName,std::vector<TexturePtr>> singleScatteringInterpolationGuidesTextures02_; // VZA-SZA dimensions
101  GLuint viewDirectionRenderBuffer_=0;
102  // Indexed as singleScatteringTextures_[scattererName][wavelengthSetIndex]
103  std::map<ScattererName,std::vector<TexturePtr>> singleScatteringTextures_;
104  std::map<ScattererName,std::vector<TexturePtr>> eclipsedSingleScatteringPrecomputationTextures_;
105  TexturePtr eclipsedDoubleScatteringPrecomputationScratchTexture_;
106  std::vector<TexturePtr> eclipsedDoubleScatteringPrecomputationTargetTextures_;
107  QOpenGLTexture luminanceRenderTargetTexture_;
108  QSize viewportSize_;
109  double altCoordToLoad_=0;
110 
111  std::vector<ShaderProgPtr> lightPollutionPrograms_;
112  std::vector<ShaderProgPtr> zeroOrderScatteringPrograms_;
113  std::vector<ShaderProgPtr> eclipsedZeroOrderScatteringPrograms_;
114  std::vector<ShaderProgPtr> multipleScatteringPrograms_;
115  // Indexed as singleScatteringPrograms_[renderMode][scattererName][wavelengthSetIndex]
116  using ScatteringProgramsMap=std::map<ScattererName,std::vector<ShaderProgPtr>>;
117  std::vector<std::unique_ptr<ScatteringProgramsMap>> singleScatteringPrograms_;
118  std::vector<std::unique_ptr<ScatteringProgramsMap>> eclipsedSingleScatteringPrograms_;
119  std::vector<ShaderProgPtr> eclipsedDoubleScatteringPrecomputedPrograms_;
120  std::vector<ShaderProgPtr> eclipsedDoubleScatteringPrecomputationPrograms_;
121  // Indexed as eclipsedSingleScatteringPrecomputationPrograms_[scattererName][wavelengthSetIndex]
122  std::unique_ptr<ScatteringProgramsMap> eclipsedSingleScatteringPrecomputationPrograms_;
123  std::unique_ptr<QOpenGLShader> precomputationProgramsVertShader_;
124  std::unique_ptr<QOpenGLShader> viewDirVertShader_, viewDirFragShader_;
125  ShaderProgPtr viewDirectionGetterProgram_;
126  std::map<ScattererName,bool> scatterersEnabledStates_;
127 
128  std::vector<QVector4D> solarIrradianceFixup_;
129 
130  int numAltIntervalsIn4DTexture_;
131 
132  enum class State
133  {
134  NotReady,
135  LoadingData,
136  ReloadingShaders,
137  ReloadingTextures,
138  ReadyToRender,
139  } state_ = State::NotReady;
140 
141 private: // methods
142  DEFINE_EXPLICIT_BOOL(CountStepsOnly);
143  void loadTextures(CountStepsOnly countStepsOnly);
144  void reloadScatteringTextures(CountStepsOnly countStepsOnly);
145  void setupRenderTarget();
146  void loadShaders(CountStepsOnly countStepsOnly);
147  void setupBuffers();
148  void clearResources();
149  void finalizeLoading();
150  void drawSurface(QOpenGLShaderProgram& prog);
151 
152  double altitudeUnitRangeTexCoord() const;
153  double cameraMoonDistance() const;
154  glm::dvec3 sunDirection() const;
155  glm::dvec3 moonPosition() const;
156  glm::dvec3 moonPositionRelativeToSunAzimuth() const;
157  glm::dvec3 cameraPosition() const;
158  glm::ivec2 loadTexture2D(QString const& path);
159  enum class Texture4DType
160  {
161  ScatteringTexture,
162  InterpolationGuides,
163  };
164  void loadTexture4D(QString const& path, float altitudeCoord, Texture4DType texType = Texture4DType::ScatteringTexture);
165  void loadEclipsedDoubleScatteringTexture(QString const& path, float altitudeCoord);
166 
167  void precomputeEclipsedSingleScattering();
168  void precomputeEclipsedDoubleScattering();
169  void renderZeroOrderScattering();
170  void renderSingleScattering();
171  void renderMultipleScattering();
172  void renderLightPollution();
173  void prepareRadianceFrames(bool clear);
174 };
175 
176 #endif
AtmosphereRenderer(QOpenGLFunctions_3_3_Core &gl, QString const &pathToData, ShowMySky::Settings *tools, std::function< void(QOpenGLShaderProgram &shprog)> const &drawSurface)
Definition: AtmosphereRenderer.cpp:2105
GLuint getLuminanceTexture() override
Get OpenGL name of the luminance render target.
Definition: AtmosphereRenderer.hpp:62
Scene settings for AtmosphereRenderer.
Definition: Settings.hpp:9
Definition: AtmosphereRenderer.hpp:34
Definition: AtmosphereParameters.hpp:10
void setDrawSurfaceCallback(std::function< void(QOpenGLShaderProgram &shprog)> const &drawSurface) override
Set the callback that will draw the screen surface.
Definition: AtmosphereRenderer.cpp:2116
int initDataLoading(QByteArray viewDirVertShaderSrc, QByteArray viewDirFragShaderSrc, std::vector< std::pair< std::string, GLuint >> viewDirBindAttribLocations) override
Initialize step-by-step process of loading of data.
Definition: AtmosphereRenderer.cpp:2121
bool isLoading() const override
Tell whether the renderer is in process of loading data.
Definition: AtmosphereRenderer.hpp:57
bool isReadyToRender() const override
Tell whether the renderer is ready for a draw call.
Definition: AtmosphereRenderer.hpp:58
Status of data loading process.
Definition: AtmosphereRenderer.hpp:66
LoadingStatus stepShaderReloading() override
Perform a single step of reloading of shaders.
Definition: AtmosphereRenderer.cpp:2350
LoadingStatus stepPreparationToDraw() override
Perform a single step of preparation to draw.
Definition: AtmosphereRenderer.cpp:1960
SpectralRadiance getPixelSpectralRadiance(QPoint const &pixelPos) override
Get spectral radiance of a pixel.
Definition: AtmosphereRenderer.cpp:1386
int initPreparationToDraw() override
Initialize step-by-step process of preparation to render.
Definition: AtmosphereRenderer.cpp:1936
Definition: AtmosphereRenderer.hpp:17
int initShaderReloading() override
Initialize step-by-step process of reloading of shaders.
Definition: AtmosphereRenderer.cpp:2334
LoadingStatus stepDataLoading() override
Perform a single step of data loading.
Definition: AtmosphereRenderer.cpp:2205
QVector4D getPixelLuminance(QPoint const &pixelPos) override
Get luminance of a pixel.
Definition: AtmosphereRenderer.cpp:1371
bool canRenderPrecomputedEclipsedDoubleScattering() const override
Tell whether precomputed eclipsed double scattering can be rendered.
Definition: AtmosphereRenderer.cpp:1484
void draw(double brightness, bool clear) override
Do the drawing.
Definition: AtmosphereRenderer.cpp:1986
void resizeEvent(int width, int height) override
Update render target size.
Definition: AtmosphereRenderer.cpp:2288
void setScattererEnabled(QString const &name, bool enable) override
Enable or disable a single-scattering layer.
Definition: AtmosphereRenderer.cpp:2329
Direction getViewDirection(QPoint const &pixelPos) override
Get view direction of a pixel.
Definition: AtmosphereRenderer.cpp:1444
QString currentActivity() const override
Get a string describing current activity.
Definition: AtmosphereRenderer.hpp:56
bool canSetSolarSpectrum() const override
Tell whether solar spectrum can be changed.
Definition: AtmosphereRenderer.cpp:1479
void resetSolarSpectrum() override
Reset solar spectrum to the default.
Definition: AtmosphereRenderer.cpp:1438
void setSolarSpectrum(std::vector< float > const &solarIrradianceAtTOA) override
Set a custom solar spectrum.
Definition: AtmosphereRenderer.cpp:1426
std::vector< float > getWavelengths() override
Get the wavelengths used in computations.
Definition: AtmosphereRenderer.cpp:1416
bool canGrabRadiance() const override
Tell whether radiance of a pixel can be queried.
Definition: AtmosphereRenderer.cpp:1471
void setViewDirShaders(QByteArray viewDirVertShaderSrc, QByteArray viewDirFragShaderSrc, std::vector< std::pair< std::string, GLuint >> viewDirBindAttribLocations) override
Replace the current view direction shaders with a new set.
Definition: AtmosphereRenderer.cpp:2150