Radium Engine  1.5.0
VolumetricMaterial.cpp
1 #include <Engine/Data/VolumetricMaterial.hpp>
2 
3 #include <Engine/Data/ShaderConfigFactory.hpp>
4 #include <Engine/Data/ShaderProgramManager.hpp>
5 #include <Engine/Data/Texture.hpp>
6 #include <Engine/Data/TextureManager.hpp>
7 #include <Engine/RadiumEngine.hpp>
8 #include <Engine/Rendering/RenderTechnique.hpp>
9 
10 #include <fstream>
11 
12 namespace Ra {
13 namespace Engine {
14 namespace Data {
15 
16 static const std::string materialName { "Volumetric" };
17 
18 nlohmann::json VolumetricMaterial::s_parametersMetadata = {};
19 
20 VolumetricMaterial::VolumetricMaterial( const std::string& name ) :
21  Material( name, materialName, Material::MaterialAspect::MAT_DENSITY ) {}
22 
24 
26  if ( !m_isDirty ) { return; }
27 
28  m_isDirty = false;
29  updateRenderingParameters();
30 }
31 
33  auto& renderParameters = getParameters();
34  m_sigma_a = renderParameters.getParameter<Core::Utils::Color>( "material.sigma_a" );
35  m_sigma_s = renderParameters.getParameter<Core::Utils::Color>( "material.sigma_s" );
36  m_g = renderParameters.getParameter<Scalar>( "material.g" );
37  m_scale = renderParameters.getParameter<Scalar>( "material.scale" );
38  m_stepsize = renderParameters.getParameter<Scalar>( "material.stepsize" );
39 }
40 
41 void VolumetricMaterial::updateRenderingParameters() {
42  auto& renderParameters = getParameters();
43  renderParameters.addParameter( "material.sigma_a", m_sigma_a );
44  renderParameters.addParameter( "material.sigma_s", m_sigma_s );
45  renderParameters.addParameter( "material.g", m_g );
46  renderParameters.addParameter( "material.scale", m_scale );
47  if ( m_stepsize < 0 ) {
48  auto dim = std::sqrt( Scalar( m_texture->width() * m_texture->width() +
49  m_texture->height() * m_texture->height() +
50  m_texture->depth() * m_texture->depth() ) );
51  renderParameters.addParameter( "material.stepsize", 1._ra / dim );
52  }
53  else { renderParameters.addParameter( "material.stepsize", m_stepsize ); }
54  renderParameters.addParameter( "material.density", m_texture );
55  renderParameters.addParameter( "material.modelToDensity", m_modelToMedium.matrix() );
56 }
57 
59  return false;
60 }
61 
63  // For resources access (glsl files) in a filesystem
64  auto resourcesRootDir { RadiumEngine::getInstance()->getResourcesDir() };
65  auto shaderProgramManager = RadiumEngine::getInstance()->getShaderProgramManager();
66 
67  shaderProgramManager->addShaderProgram(
68  { { "ComposeVolume" },
69  resourcesRootDir + "Shaders/2DShaders/Basic2D.vert.glsl",
70  resourcesRootDir + "Shaders/Materials/Volumetric/ComposeVolumeRender.frag.glsl" } );
71 
72  // adding the material glsl implementation file
73  shaderProgramManager->addNamedString(
74  "/Volumetric.glsl", resourcesRootDir + "Shaders/Materials/Volumetric/Volumetric.glsl" );
75 
76  // registering re-usable shaders
78  "Volumetric",
79  resourcesRootDir + "Shaders/Materials/Volumetric/Volumetric.vert.glsl",
80  resourcesRootDir + "Shaders/Materials/Volumetric/Volumetric.frag.glsl" );
82 
83  // registering re-usable shaders
85  "VolumetricOIT",
86  resourcesRootDir + "Shaders/Materials/Volumetric/Volumetric.vert.glsl",
87  resourcesRootDir + "Shaders/Materials/Volumetric/VolumetricOIT.frag.glsl" );
89 
90  // Ra::Engine::ShaderConfiguration zprepassconfig(
91  // "ZprepassRayMarching", "Shaders/Materials/RayMarching/RayMarching.vert.glsl",
92  // "Shaders/Materials/RayMarching/DepthAmbientRayMarching.frag.glsl" );
93  // Ra::Engine::ShaderConfigurationFactory::addConfiguration( zprepassconfig );
94 
95  // Ra::Engine::ShaderConfiguration transparentpassconfig(
96  // "LitOITRayMarching", "Shaders/Materials/RayMarching/RayMarching.vert.glsl",
97  // "Shaders/Materials/RayMarching/LitOITRayMarching.frag.glsl" );
98  // Ra::Engine::ShaderConfigurationFactory::addConfiguration( transparentpassconfig );
99 
100  // Registering technique
101  Rendering::EngineRenderTechniques::registerDefaultTechnique(
102  "Volumetric",
103 
104  []( Rendering::RenderTechnique& rt, bool /*isTransparent*/ ) {
105  /*
106  auto lightpassconfig =
107  Ra::Engine::ShaderConfigurationFactory::getConfiguration( "Volumetric" );
108  rt.setConfiguration( *lightpassconfig,
109  DefaultRenderingPasses::LIGHTING_OPAQUE );
110  */
111  auto passconfig = Data::ShaderConfigurationFactory::getConfiguration( "Volumetric" );
112  rt.setConfiguration( *passconfig,
113  Rendering::DefaultRenderingPasses::LIGHTING_VOLUMETRIC );
114  } );
115 
116  // Registering parameters metadata
117  ParameterSetEditingInterface::loadMetaData( materialName, s_parametersMetadata );
118 }
119 
121  Rendering::EngineRenderTechniques::removeDefaultTechnique( "Volumetric" );
122 }
123 
124 } // namespace Data
125 } // namespace Engine
126 } // namespace Ra
static void loadMetaData(const std::string &basename, nlohmann::json &destination)
Load the ParameterSet description.
size_t depth() const
Definition: Texture.hpp:197
size_t height() const
Definition: Texture.hpp:193
size_t width() const
Definition: Texture.hpp:189
Core::Utils::Color m_sigma_s
Scattering coefficient, default to Air (2.55, 3.21, 3.77)
Scalar m_scale
Scale factor applied to the absorption and scattering coefficients.
EIGEN_MAKE_ALIGNED_OPERATOR_NEW VolumetricMaterial(const std::string &name)
void updateGL() override
Update the OpenGL states used by the ShaderParameterProvider. These state could be the ones from an a...
Scalar m_g
phase function assymetry factor, default to 0
Core::Utils::Color m_sigma_a
Absorption coefficient, default to Air (0.0011, 0.0024, 0.014)
void updateFromParameters() override
Update the attributes of the ShaderParameterProvider to their actual values stored in the renderParam...
void setConfiguration(const Data::ShaderConfiguration &newConfig, Core::Utils::Index pass=DefaultRenderingPasses::LIGHTING_OPAQUE)
void addConfiguration(const ShaderConfiguration &config)
Core::Utils::optional< ShaderConfiguration > getConfiguration(const std::string &name)
Definition: Cage.cpp:3