Radium Engine  1.5.20
Loading...
Searching...
No Matches
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
12namespace Ra {
13namespace Engine {
14namespace Data {
15
16static const std::string materialName { "Volumetric" };
17
18nlohmann::json VolumetricMaterial::s_parametersMetadata = {};
19
21 Material( name, materialName, Material::MaterialAspect::MAT_DENSITY ) {}
22
24
26 if ( !isDirty() ) { return; }
27
28 updateRenderingParameters();
29 setClean();
30}
31
33 auto& renderParameters = getParameters();
34 m_sigma_a = renderParameters.getVariable<Core::Utils::Color>( "material.sigma_a" );
35 m_sigma_s = renderParameters.getVariable<Core::Utils::Color>( "material.sigma_s" );
36 m_g = renderParameters.getVariable<Scalar>( "material.g" );
37 m_scale = renderParameters.getVariable<Scalar>( "material.scale" );
38 m_stepsize = renderParameters.getVariable<Scalar>( "material.stepsize" );
39}
40
41void VolumetricMaterial::updateRenderingParameters() {
42 auto& renderParameters = getParameters();
43 renderParameters.setVariable( "material.sigma_a", m_sigma_a );
44 renderParameters.setVariable( "material.sigma_s", m_sigma_s );
45 renderParameters.setVariable( "material.g", m_g );
46 renderParameters.setVariable( "material.scale", m_scale );
47 if ( m_stepsize < 0 ) {
48 auto dim = std::sqrt( Scalar( m_texture->getWidth() * m_texture->getWidth() +
49 m_texture->getHeight() * m_texture->getHeight() +
50 m_texture->getDepth() * m_texture->getDepth() ) );
51 renderParameters.setVariable( "material.stepsize", 1._ra / dim );
52 }
53 else { renderParameters.setVariable( "material.stepsize", m_stepsize ); }
54 renderParameters.setTexture( "material.density", m_texture );
55 renderParameters.setVariable( "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,
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
Base class for materials.
Definition Material.hpp:24
bool isDirty()
Return dirty state.
Definition Material.hpp:118
void setClean()
Set dirty state to false.
Definition Material.hpp:124
MaterialAspect
Identifies the type of the material.
Definition Material.hpp:31
static void loadMetaData(const std::string &basename, nlohmann::json &destination)
Load the ParameterSet description.
size_t getWidth() const
Definition Texture.hpp:179
size_t getDepth() const
Definition Texture.hpp:185
size_t getHeight() const
Definition Texture.hpp:182
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.
VolumetricMaterial(const std::string &name)
void updateGL() override
Update the OpenGL states used by the ShaderParameterProvider.
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)
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3
T sqrt(T... args)