Visual Computing Library  devel
Loading...
Searching...
No Matches
material.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_SPACE_CORE_MATERIAL_H
24#define VCL_SPACE_CORE_MATERIAL_H
25
26#include "color.h"
27#include "image.h"
28#include "texture_descriptor.h"
29
30#include <vclib/base.h>
31
32namespace vcl {
33
45{
46public:
50 enum class AlphaMode {
57 };
58
62 enum class TextureType {
64 0,
67 NORMAL,
68 OCCLUSION,
70 EMISSIVE,
71 COUNT
72 };
73
74 inline static const std::
75 array<std::string, toUnderlying(TextureType::COUNT)>
76 TEXTURE_TYPE_NAMES = {
77 "baseColorTex",
78 "metallicRoughnessTex",
79 "normalTex",
80 "occlusionTex",
81 "emissiveTex"};
82
83private:
84 inline static const uint N_TEXTURE_TYPE =
85 static_cast<uint>(TextureType::COUNT);
86
87 std::string mName;
88
89 // essential PBR properties
90 Color mBaseColor = Color::White;
91
92 float mMetallic = 0.0f;
93
94 float mRoughness = 1.0f;
95
96 // optional PBR properties
97 Color mEmissiveColor = Color::Black;
98
100
101 float mAlphaCutoff = 0.5f; // only used when mAlphaMode is MASK
102
103 float mNormalScale = 1.0f;
104
105 float mOcclusionStrength = 1.0f;
106
107 std::array<TextureDescriptor, N_TEXTURE_TYPE> mTextureDescriptors;
108
109 bool mDoubleSided = false;
110
111public:
130
135 const std::string& name() const { return mName; }
136
141 std::string& name() { return mName; }
142
148 const Color& baseColor() const { return mBaseColor; }
149
154 Color& baseColor() { return mBaseColor; }
155
160 float metallic() const { return mMetallic; }
161
166 float& metallic() { return mMetallic; }
167
172 float roughness() const { return mRoughness; }
173
178 float& roughness() { return mRoughness; }
179
185 const Color& emissiveColor() const { return mEmissiveColor; }
186
191 Color& emissiveColor() { return mEmissiveColor; }
192
197 AlphaMode alphaMode() const { return mAlphaMode; }
198
203 AlphaMode& alphaMode() { return mAlphaMode; }
204
210 float alphaCutoff() const { return mAlphaCutoff; }
211
216 float& alphaCutoff() { return mAlphaCutoff; }
217
224 bool doubleSided() const { return mDoubleSided; }
225
230 bool& doubleSided() { return mDoubleSided; }
231
236 float normalScale() const { return mNormalScale; }
237
242 float& normalScale() { return mNormalScale; }
243
248 float occlusionStrength() const { return mOcclusionStrength; }
249
254 float& occlusionStrength() { return mOcclusionStrength; }
255
261 {
262 using enum TextureType;
263 return mTextureDescriptors[toUnderlying(BASE_COLOR)];
264 }
265
272 {
273 using enum TextureType;
274 return mTextureDescriptors[toUnderlying(BASE_COLOR)];
275 }
276
283 const TextureDescriptor& textureDescriptor(uint type) const
284 {
285 assert(type < N_TEXTURE_TYPE);
286 return mTextureDescriptors[type];
287 }
288
295 {
296 return textureDescriptor(toUnderlying(type));
297 }
298
307 {
308 assert(type < N_TEXTURE_TYPE);
309 return mTextureDescriptors[type];
310 }
311
319 {
320 return textureDescriptor(toUnderlying(type));
321 }
322
327 void serialize(std::ostream& os) const
328 {
329 vcl::serialize(os, mName);
330 mBaseColor.serialize(os);
331 vcl::serialize(os, mMetallic, mRoughness);
332 mEmissiveColor.serialize(os);
333 vcl::serialize(os, mAlphaMode, mAlphaCutoff);
334 vcl::serialize(os, mNormalScale);
335 vcl::serialize(os, mOcclusionStrength);
336 vcl::serialize(os, mTextureDescriptors);
337 vcl::serialize(os, mDoubleSided);
338 }
339
344 void deserialize(std::istream& is)
345 {
346 vcl::deserialize(is, mName);
347 mBaseColor.deserialize(is);
348 vcl::deserialize(is, mMetallic, mRoughness);
349 mEmissiveColor.deserialize(is);
350 vcl::deserialize(is, mAlphaMode, mAlphaCutoff);
351 vcl::deserialize(is, mNormalScale);
352 vcl::deserialize(is, mOcclusionStrength);
353 vcl::deserialize(is, mTextureDescriptors);
354 vcl::deserialize(is, mDoubleSided);
355 }
356
363 bool operator==(const Material& other) const = default;
364
375 static Image::ColorSpace textureTypeToColorSpace(TextureType type)
376 {
377 switch (type) {
379 case TextureType::EMISSIVE: return Image::ColorSpace::SRGB;
383 default: return Image::ColorSpace::LINEAR;
384 }
385 }
386};
387
388/* Concepts */
389
400template<typename T>
401concept MaterialConcept = std::derived_from< // same type or derived type
402 std::remove_cvref_t<T>,
403 Material>;
404
405} // namespace vcl
406
407#endif // VCL_SPACE_CORE_MATERIAL_H
A class representing a box in N-dimensional space.
Definition box.h:46
The Color class represents a 32 bit color.
Definition color.h:48
Represents a Physically-Based Rendering (PBR) material.
Definition material.h:45
bool operator==(const Material &other) const =default
Defaulted equality comparison operator.
TextureDescriptor & textureDescriptor(TextureType type)
Gets a mutable reference to the texture descriptor for a given texture type.
Definition material.h:318
void deserialize(std::istream &is)
Deserializes the material's data from an input stream.
Definition material.h:344
float & roughness()
Gets a mutable reference to the roughness factor of the material.
Definition material.h:178
std::string & name()
Gets a mutable reference to the name of the material.
Definition material.h:141
const TextureDescriptor & baseColorTextureDescriptor() const
Gets the texture descriptor for the base color texture.
Definition material.h:260
TextureType
Defines the types of textures used in the PBR material model.
Definition material.h:62
@ NORMAL
The tangent-space normal map. Stored in linear color space.
@ COUNT
Utility value to get the number of texture types.
@ BASE_COLOR
The base color (albedo) texture. Stored in sRGB color space.
@ EMISSIVE
The emissive color texture. Stored in sRGB color space.
const TextureDescriptor & textureDescriptor(uint type) const
Gets the texture descriptor for a given texture type index.
Definition material.h:283
const std::string & name() const
Gets the name of the material.
Definition material.h:135
bool & doubleSided()
Gets a mutable reference to the double-sided property.
Definition material.h:230
const TextureDescriptor & textureDescriptor(TextureType type) const
Gets the texture descriptor for a given texture type.
Definition material.h:294
float & alphaCutoff()
Gets a mutable reference to the alpha cutoff value.
Definition material.h:216
Color & emissiveColor()
Gets a mutable reference to the emissive color of the material.
Definition material.h:191
const Color & emissiveColor() const
Gets the emissive color of the material. This is the color emitted by the material,...
Definition material.h:185
void serialize(std::ostream &os) const
Serializes the material's data to an output stream.
Definition material.h:327
AlphaMode & alphaMode()
Gets a mutable reference to the alpha rendering mode.
Definition material.h:203
static Image::ColorSpace textureTypeToColorSpace(TextureType type)
Determines the appropriate color space for a given texture type.
Definition material.h:375
Material()
Default constructor.
Definition material.h:129
float metallic() const
Gets the metallic factor of the material.
Definition material.h:160
Color & baseColor()
Gets a mutable reference to the base color of the material.
Definition material.h:154
float & metallic()
Gets a mutable reference to the metallic factor of the material.
Definition material.h:166
float occlusionStrength() const
Gets the strength of the ambient occlusion effect.
Definition material.h:248
float alphaCutoff() const
Gets the alpha cutoff value. This value is used only when the alpha mode is ALPHA_MASK.
Definition material.h:210
float & occlusionStrength()
Gets a mutable reference to the occlusion strength.
Definition material.h:254
AlphaMode
Defines the alpha rendering mode of the material.
Definition material.h:50
float normalScale() const
Gets the scalar multiplier for the normal map.
Definition material.h:236
TextureDescriptor & baseColorTextureDescriptor()
Gets a mutable reference to the texture descriptor for the base color texture.
Definition material.h:271
TextureDescriptor & textureDescriptor(uint type)
Gets a mutable reference to the texture descriptor for a given texture type index.
Definition material.h:306
float & normalScale()
Gets a mutable reference to the normal scale.
Definition material.h:242
AlphaMode alphaMode() const
Gets the alpha rendering mode.
Definition material.h:197
const Color & baseColor() const
Gets the base color of the material. This is used as a tint if a base color texture is present.
Definition material.h:148
float roughness() const
Gets the roughness factor of the material.
Definition material.h:172
bool doubleSided() const
Checks if the material is double-sided. If true, both front and back faces of polygons with this mate...
Definition material.h:224
Describes the properties of a texture, such as its source path and rendering parameters.
Definition texture_descriptor.h:42
A concept representing a Material.
Definition material.h:401