Loading [MathJax]/extensions/TeX/AMSmath.js
Radium Engine  1.5.29
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
materials.cpp
1#include <catch2/catch_test_macros.hpp>
2
3#include <Engine/Data/BlinnPhongMaterial.hpp>
4#include <Engine/Data/LambertianMaterial.hpp>
5#include <Engine/Data/PlainMaterial.hpp>
6#include <Engine/Data/VolumetricMaterial.hpp>
7#include <Engine/RadiumEngine.hpp>
8
9#include <Headless/CLIViewer.hpp>
10#ifdef HEADLESS_HAS_EGL
11# include <Headless/OpenGLContext/EglOpenGLContext.hpp>
12#else
13# include <Headless/OpenGLContext/GlfwOpenGLContext.hpp>
14#endif
15
16#include "../unittestUtils.hpp"
17
18using namespace Ra::Headless;
19using namespace Ra::Engine::Data;
20
21struct PrintThemAll : public PrintAllHelper<RenderParameters::BindableTypes> {};
22
23TEST_CASE( "Engine/Data/Materials", "[unittests][Engine][Engine/Data][Materials]" ) {
24
25 // Get the Engine and materials initialized
26 glbinding::Version glVersion { 4, 4 };
27#ifdef HEADLESS_HAS_EGL
28 CLIViewer viewer { std::make_unique<EglOpenGLContext>( glVersion ) };
29#else
30 CLIViewer viewer { std::make_unique<GlfwOpenGLContext>( glVersion ) };
31#endif
32 const char* testName = "Materials testing";
33 auto code = viewer.init( 1, &testName );
34
35 SECTION( "Blinn-Phong material" ) {
36 LOG( Ra::Core::Utils::logINFO ) << "Testing Blinn-Phong material";
37 REQUIRE( code == 0 );
38 BlinnPhongMaterial bp( "testBlinnPhong" );
39
40 /* Testing default values */
41 REQUIRE( bp.getAlpha() == 1.0 );
42 REQUIRE( !bp.isColoredByVertexAttrib() );
43
44 bp.setColoredByVertexAttrib( true );
45 REQUIRE( bp.isColoredByVertexAttrib() == true );
46 /* Setting GL Parameters */
47 bp.updateGL();
48 auto& bpParameters = bp.getParameters();
49
50 bpParameters.visit( PrintThemAll {} );
51
52 REQUIRE( bpParameters.existsVariable<bool>( "material.hasPerVertexKd" ) );
53 REQUIRE( bpParameters.existsVariable<Scalar>( "material.alpha" ) );
54
55 auto& pvc = bpParameters.getVariable<bool>( "material.hasPerVertexKd" );
56 REQUIRE( pvc == bp.isColoredByVertexAttrib() );
57
58 auto& alp = bpParameters.getVariable<Scalar>( "material.alpha" );
59 REQUIRE( alp == bp.getAlpha() );
60
61 /* changing parameter values */
62 bpParameters.setVariable( "material.hasPerVertexKd", !pvc );
63 bpParameters.setVariable( "material.alpha", 0.5_ra );
64 REQUIRE( pvc != bp.isColoredByVertexAttrib() );
65 REQUIRE( alp != bp.getAlpha() );
66
67 /* Updating material parameters from GL parameters */
68 bp.updateFromParameters();
69 REQUIRE( pvc == bp.isColoredByVertexAttrib() );
70 REQUIRE( alp == bp.getAlpha() );
71 LOG( Ra::Core::Utils::logINFO ) << "Blinn-Phong material tested.\n";
72 }
73
74 SECTION( "Lambertian material" ) {
75 LOG( Ra::Core::Utils::logINFO ) << "Testing Lambertian material";
76 REQUIRE( code == 0 );
77 LambertianMaterial mat( "test LambertianMaterial" );
78
79 REQUIRE( !mat.isColoredByVertexAttrib() );
80
81 mat.setColoredByVertexAttrib( true );
82 REQUIRE( mat.isColoredByVertexAttrib() );
83
84 mat.updateGL();
85 auto& matParameters = mat.getParameters();
86
87 REQUIRE( matParameters.existsVariable<bool>( "material.perVertexColor" ) );
88 auto& pvc = matParameters.getVariable<bool>( "material.perVertexColor" );
89 REQUIRE( pvc == mat.isColoredByVertexAttrib() );
90
91 /* changing parameter values */
92 matParameters.setVariable( "material.perVertexColor", !pvc );
93 REQUIRE( pvc != mat.isColoredByVertexAttrib() );
94
95 /* Updating material parameters from GL parameters */
96 mat.updateFromParameters();
97 REQUIRE( pvc == mat.isColoredByVertexAttrib() );
98 LOG( Ra::Core::Utils::logINFO ) << "Lambertian material tested.\n";
99 }
100
101 SECTION( "Plain material" ) {
102 LOG( Ra::Core::Utils::logINFO ) << "Testing Plain material";
103 REQUIRE( code == 0 );
104 PlainMaterial mat( "test PlainMaterial" );
105
106 REQUIRE( !mat.isColoredByVertexAttrib() );
107
108 mat.setColoredByVertexAttrib( true );
109 REQUIRE( mat.isColoredByVertexAttrib() );
110
111 mat.updateGL();
112 auto& matParameters = mat.getParameters();
113
114 REQUIRE( matParameters.existsVariable<bool>( "material.perVertexColor" ) );
115 auto& pvc = matParameters.getVariable<bool>( "material.perVertexColor" );
116 REQUIRE( pvc == mat.isColoredByVertexAttrib() );
117
118 /* changing parameter values */
119 matParameters.setVariable( "material.perVertexColor", !pvc );
120 REQUIRE( pvc != mat.isColoredByVertexAttrib() );
121
122 /* Updating material parameters from GL parameters */
123 mat.updateFromParameters();
124 REQUIRE( pvc == mat.isColoredByVertexAttrib() );
125 LOG( Ra::Core::Utils::logINFO ) << "Plain material tested.\n";
126 }
127
128 SECTION( "Volumetric material" ) {
129 LOG( Ra::Core::Utils::logINFO ) << "Testing Volumetric material";
130 REQUIRE( code == 0 );
131 VolumetricMaterial mat( "test VolumetricMaterial" );
132 auto d = std::shared_ptr<float[]>( new float[1] );
133 d[0] = 1.f;
134 Texture density {
135 { "simpleDensity",
136 { gl::GL_CLAMP_TO_EDGE,
137 gl::GL_CLAMP_TO_EDGE,
138 gl::GL_CLAMP_TO_EDGE,
139 gl::GL_NEAREST,
140 gl::GL_NEAREST },
141 { gl::GL_TEXTURE_3D, 1, 1, 1, gl::GL_RED, gl::GL_RED, gl::GL_FLOAT, false, d } } };
142 density.initializeNow();
143 mat.setTexture( &density );
144
145 REQUIRE( mat.m_g == 0_ra );
146
147 mat.updateGL();
148 auto& matParameters = mat.getParameters();
149 REQUIRE( matParameters.existsVariable<Scalar>( "material.g" ) );
150
151 auto& g = matParameters.getVariable<Scalar>( "material.g" );
152 REQUIRE( g == 0_ra );
153
154 /* changing parameter values */
155 matParameters.setVariable( "material.g", 0.5_ra );
156 REQUIRE( g != mat.m_g );
157
158 /* Updating material parameters from GL parameters */
159 mat.updateFromParameters();
160 REQUIRE( g == mat.m_g );
161 LOG( Ra::Core::Utils::logINFO ) << "Volumetric material tested.\n";
162 }
163
164 SECTION( "Metadata verification" ) {
165 BlinnPhongMaterial bp( "testBlinnPhong" );
166 auto bpMetadata = bp.getParametersMetadata();
167 REQUIRE( bpMetadata.contains( "material.tex.ns" ) );
168 REQUIRE( bpMetadata["material.tex.ns"].contains( "type" ) );
169 REQUIRE( bpMetadata["material.tex.ns"]["type"] == "texture" );
170
171 LambertianMaterial lm( "test LambertianMaterial" );
172 auto lmMetadata = lm.getParametersMetadata();
173 REQUIRE( lmMetadata.contains( "material.tex.color" ) );
174 REQUIRE( lmMetadata["material.tex.color"].contains( "type" ) );
175 REQUIRE( lmMetadata["material.tex.color"]["type"] == "texture" );
176
177 PlainMaterial pm( "test PlainMaterial" );
178 auto pmMetadata = pm.getParametersMetadata();
179 REQUIRE( pmMetadata.contains( "material.tex.color" ) );
180 REQUIRE( pmMetadata["material.tex.color"].contains( "type" ) );
181 REQUIRE( pmMetadata["material.tex.color"]["type"] == "texture" );
182
183 VolumetricMaterial vm( "test VolumetricMaterial" );
184 auto vmMetadata = vm.getParametersMetadata();
185 REQUIRE( vmMetadata.contains( "material.sigma_a" ) );
186 REQUIRE( vmMetadata["material.sigma_a"].contains( "type" ) );
187 REQUIRE( vmMetadata["material.sigma_a"]["type"] == "array" );
188
189 nlohmann::json destination;
190 ParameterSetEditingInterface::loadMetaData( "UnknownMetadata", destination );
191 REQUIRE( destination.empty() );
192 }
193}
Implementation of the Lambertian Material BSDF.
static void loadMetaData(const std::string &basename, nlohmann::json &destination)
Load the ParameterSet description.
Represent a Texture of the engine.
Definition Texture.hpp:120
void initializeNow()
Generate the GPU representation of the texture right now. Need an active OpenGL context.
Definition Texture.cpp:72
(GPU) Data representation, along with manager
Types, list of types as Ra::Core::Utils::TypeList< ... >