CloudyDay
 All Classes Functions Variables Enumerations
Atmosphereprecompute.h
1 
2 // Copyright (c) 2011-2012, Daniel Müller <dm@g4t3.de>
3 // Computer Graphics Systems Group at the Hasso-Plattner-Institute, Germany
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of the Computer Graphics Systems Group at the
14 // Hasso-Plattner-Institute (HPI), Germany nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 
30 #pragma once
31 #include "earth.h"
32 #include <osg/Referenced>
33 #include <osg/GL>
34 #include <osg/ref_ptr>
35 #include <osg/Vec3f>
36 
37 #include <map>
38 #include <vector>
39 #include <string>
40 
41 
42 namespace osg
43 {
44  class Texture2D;
45  class Texture3D;
46  class Image;
47  class Uniform;
48  class GraphicsContext;
49  class Geode;
50  class StateSet;
51  class Program;
52  class Camera;
53  class Group;
54 }
55 
56 namespace osgViewer
57 {
58  class CompositeViewer;
59 }
60 
61 namespace osgCloudyDay
62 {
63  class AtmospherePrecompute : public osg::Referenced
64 {
65 public:
66 
67  typedef struct PrecomputedTextureConfig
68  {
69  int transmittanceWidth;
70  int transmittanceHeight;
71 
72  int skyHeight;
73  int skyWidth;
74 
75  int resR;
76  int resMu;
77  int resMuS;
78  int resNu;
79 
80  int transmittanceIntegralSamples;
81  int inscatterIntegralSamples;
82  int irradianceIntegralSamples;
83  int inscatterSphericalIntegralSamples;
84 
85  } t_preTexCfg;
86 
87  typedef struct PhysicalModelConfig
88  {
89  float avgGroundReflectance;
90 
91  float HR; // Reighley const denisty Atmosphere height in km
92  osg::Vec3f betaR;
93 
94  float HM; // Mie const denisty Atmosphere height in km
95  osg::Vec3f betaMSca;
96  osg::Vec3f betaMEx;
97  float mieG;
98 
99  } t_modelCfg;
100 
101  t_modelCfg &getModelConfig()
102  {
103  return m_modelCfg;
104  }
105 
106 protected:
107 
108  t_preTexCfg &getTextureConfig()
109  {
110  return m_preTexCfg;
111  }
112 
113 protected:
114 
115  typedef std::map<GLint, osg::Texture2D*> t_tex2DsByUnit;
116  typedef std::map<GLint, osg::Texture3D*> t_tex3DsByUnit;
117 
118  typedef std::vector<osg::Uniform*> t_uniforms;
119 
120 public:
121 
122  AtmospherePrecompute();
123  virtual ~AtmospherePrecompute();
124 
125  osg::Texture2D *getTransmittanceTexture();
126  osg::Texture2D *getIrradianceTexture();
127  osg::Texture3D *getInscatterTexture();
128 
129  const bool compute(const bool ifDirtyOnly = true);
130  void dirty();
131 
132  const float defaultAltitude();
133  osg::Uniform *cmnUniform();
134 
135  void substituteMacros(std::string &source);
136 
137 protected:
138 
139  osg::Texture2D *getDeltaETexture();
140  osg::Texture3D *getDeltaSRTexture();
141  osg::Texture3D *getDeltaSMTexture();
142  osg::Texture3D *getDeltaJTexture();
143 
144  osg::GraphicsContext *setupContext();
145 
146  osg::Geode *genQuad() const;
147 
148  osg::Texture2D *setupTexture2D(
149  const char *name // used as sampler identifier
150  , const GLenum internalFormat
151  , const GLenum pixelFormat
152  , const GLenum dataType
153  , const int width
154  , const int height
155  , osg::Image *image = NULL);
156 
157  osg::Texture3D *setupTexture3D(
158  const char *name // used as sampler identifier
159  , const GLenum internalFormat
160  , const GLenum pixelFormat
161  , const GLenum dataType
162  , const int width
163  , const int height
164  , const int depth
165  , osg::Image *image = NULL);
166 
167  osg::Image *getLayerFrom3DImage(
168  osg::Image *source
169  , const int layer);
170 
171  void setupLayerUniforms(
172  osg::StateSet *stateSet
173  , const int depth
174  , const int layer);
175 
176  osg::Program *setupProgram(
177  const std::string &fragmentShaderSource);
178 
179  osg::Camera *setupCamera(
180  const int viewportWidth
181  , const int viewportHeight
182  , osg::Geode *geode
183  , const int orderNum);
184 
185 
186  osg::Group *setupGroup(
187  osgViewer::CompositeViewer *viewer);
188 
189  void cleanUp(
190  osgViewer::CompositeViewer *viewer);
191 
192  void assignUniforms(
193  osg::StateSet *stateSet
194  , t_uniforms &uniforms);
195 
196  void assignSamplers(
197  osg::StateSet *stateSet
198  , t_tex2DsByUnit &samplers2D
199  , t_tex3DsByUnit &samplers3D);
200 
201  void dirtyTargets(t_tex2DsByUnit &targets2D);
202  void dirtyTargets(t_tex3DsByUnit &targets3D);
203 
204  void render2D(
205  osgViewer::CompositeViewer *viewer
206  , osg::Geode *geode
207  , t_tex2DsByUnit &targets2D
208  , t_tex2DsByUnit &samplers2D
209  , t_tex3DsByUnit &samplers3D
210  , t_uniforms &uniforms
211  , const char* fragmentShaderSource);
212 
213  void render3D(
214  osgViewer::CompositeViewer *viewer
215  , osg::Geode *geode
216  , t_tex3DsByUnit &targets3D
217  , t_tex2DsByUnit &samplers2D
218  , t_tex3DsByUnit &samplers3D
219  , t_uniforms &uniforms
220  , const char* fragmentShaderSource);
221 
222 protected:
223 
224  bool m_dirty;
225 
226  t_preTexCfg m_preTexCfg;
227  t_modelCfg m_modelCfg;
228 
229  osg::ref_ptr<osg::Texture2D> m_transmittanceTexture;
230  osg::ref_ptr<osg::Texture2D> m_deltaETexture;
231  osg::ref_ptr<osg::Texture3D> m_deltaSRTexture;
232  osg::ref_ptr<osg::Texture3D> m_deltaSMTexture;
233  osg::ref_ptr<osg::Texture2D> m_irradianceTexture;
234  osg::ref_ptr<osg::Texture3D> m_inscatterTexture;
235  osg::ref_ptr<osg::Texture3D> m_deltaJTexture;
236 
237  osg::ref_ptr<osg::Image> m_transmittanceImage;
238  osg::ref_ptr<osg::Image> m_irradianceImage;
239  osg::ref_ptr<osg::Image> m_inscatterImage;
240 };
241 }