Radium Engine  1.5.20
Loading...
Searching...
No Matches
RenderParameters.hpp
1#pragma once
2
3#include <Engine/RaEngine.hpp>
4
5#include <vector>
6
7#include <nlohmann/json.hpp>
8
9#include <Core/Types.hpp>
10#include <Core/Utils/Color.hpp>
11#include <Core/Utils/EnumConverter.hpp>
12#include <Core/Utils/Log.hpp>
13#include <Core/Utils/StdOptional.hpp>
14
15#include <Core/Containers/VariableSet.hpp>
16
17#include <Engine/Data/ShaderProgram.hpp>
18
19namespace Ra {
20namespace Engine {
21namespace Data {
22
23class Texture;
24
34class RA_ENGINE_API RenderParameters final : public Core::VariableSet
35{
36 public:
41
43 using BindableTypes = Core::Utils::TypeList<bool,
45 int,
46 uint,
47 Scalar,
52 Core::Vector2,
53 Core::Vector3,
54 Core::Vector4,
55 Core::Matrix2,
56 Core::Matrix3,
57 Core::Matrix4,
60
71 template <typename T,
73 void setTexture( const std::string& name, T* tex, int texUnit = -1 ) {
74 setVariable( name, TextureInfo { tex, texUnit } );
75 }
76
77 using VariableSet::setVariable;
78
80 void setVariable( const std::string& name, RenderParameters& rp ) {
81 VariableSet::setVariable( name, std::ref( rp ) );
82 }
83 void setVariable( const std::string& name, const RenderParameters& rp ) {
84 VariableSet::setVariable( name, std::cref( rp ) );
85 }
86
92 void bind( const Data::ShaderProgram* shader ) const {
93 visit( StaticParameterBinder {}, shader );
94 }
95
96 private:
101 class StaticParameterBinder
102 {
103 public:
105 using types = BindableTypes;
106
109 void operator()( const std::string& name,
110 const Ra::Core::Utils::Color& p,
111 const Data::ShaderProgram* shader ) {
112 shader->setUniform( name.c_str(), Ra::Core::Utils::Color::VectorType( p ) );
113 }
114
117 void operator()( const std::string& name,
118 const RenderParameters::TextureInfo& p,
119 const Data::ShaderProgram* shader ) {
120 auto [tex, texUnit] = p;
121 if ( texUnit == -1 ) { shader->setUniformTexture( name.c_str(), tex ); }
122 else { shader->setUniform( name.c_str(), tex, texUnit ); }
123 }
124
129 template <typename T>
130 void operator()( const std::string& /*name*/,
132 const Data::ShaderProgram* shader ) {
133 p.get().bind( shader );
134 }
135
138 template <typename T>
139 void operator()( const std::string& name, const T& p, const Data::ShaderProgram* shader ) {
140 shader->setUniform( name.c_str(), p );
141 }
142 };
143};
144
149{
150 public:
151 virtual ~ParameterSetEditingInterface() = default;
152
157 virtual nlohmann::json getParametersMetadata() const = 0;
158
160 static void loadMetaData( const std::string& basename, nlohmann::json& destination );
161};
162
170class RA_ENGINE_API ShaderParameterProvider
171{
172 public:
173 virtual ~ShaderParameterProvider() = default;
174 virtual RenderParameters& getParameters() { return m_renderParameters; }
175 virtual const RenderParameters& getParameters() const { return m_renderParameters; }
176
183 virtual void updateGL() = 0;
184
188 virtual void updateFromParameters() {};
189
199 virtual std::list<std::string> getPropertyList() const { return {}; };
200
201 private:
203 RenderParameters m_renderParameters;
204};
205
206} // namespace Data
207} // namespace Engine
208} // namespace Ra
T c_str(T... args)
Heterogeneous container storing "Variables", that maps a name (std::string) to a value (of any type T...
Interface to define metadata (constraints, description, ...) for the editing of parameter set.
virtual nlohmann::json getParametersMetadata() const =0
Get a json containing metadata about the parameters.
Management of shader parameters with automatic binding of a named parameter to the corresponding glsl...
void setVariable(const std::string &name, RenderParameters &rp)
overload create ref wrapper for RenderParameters variable
void bind(const Data::ShaderProgram *shader) const
Bind the parameter uniform on the shader program.
void setTexture(const std::string &name, T *tex, int texUnit=-1)
Adding a texture parameter.
Core::Utils::TypeList< bool, Core::Utils::Color, int, uint, Scalar, TextureInfo, std::vector< int >, std::vector< uint >, std::vector< Scalar >, Core::Vector2, Core::Vector3, Core::Vector4, Core::Matrix2, Core::Matrix3, Core::Matrix4, std::reference_wrapper< RenderParameters >, std::reference_wrapper< const RenderParameters > > BindableTypes
List of bindable types, to be used with static visitors.
Shader program parameter provider.
virtual void updateFromParameters()
Update the attributes of the ShaderParameterProvider to their actual values stored in the renderParam...
virtual void updateGL()=0
Update the OpenGL states used by the ShaderParameterProvider.
virtual std::list< std::string > getPropertyList() const
Get the list of properties the provider might use in a shader.
void setUniform(const char *name, const T &value) const
Uniform setters.
void setUniformTexture(const char *name, Texture *tex) const
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3
T ref(T... args)