Rendering Engine 0.2.0
Modular Graphics Rendering Engine | v0.2.0
Loading...
Searching...
No Matches
rendering_engine::Material Class Reference

Represents a material instance with parameter values, texture bindings, and rendering configuration. More...

#include <material.hpp>

Public Member Functions

 Material (IRenderer *renderer, MaterialSettings matSettings)
 Constructs a new Material instance.
const MaterialSettings GetMaterialSettings () const
 Returns the material's static settings (domain, blend mode, shading model, etc.).
void InitializeRenderResources ()
 Initializes backend-specific GPU resources associated with this material.
void ReleaseRenderResources ()
 Releases any GPU resources associated with this material.
PackedMaterialData PackMaterialParameters ()
 Packs the current float/vector parameters into a binary buffer and layout metadata.
std::vector< std::string > GetTextures () const
 Returns the list of texture names used by this material.
IMaterialRenderResourcesGetMaterialRenderResources () const
 Returns the backend-specific GPU handle of the material.
void SetFloat (const std::string &name, float value)
 Sets a float parameter for the material.
void SetVec3 (const std::string &name, glm::vec3 value)
 Sets a vec3 parameter for the material.
void SetVec4 (const std::string &name, glm::vec4 value)
 Sets a vec4 parameter for the material.
void AddTexture (const std::string &textureName)
 Adds a texture name to the material's list of used textures.

Detailed Description

Represents a material instance with parameter values, texture bindings, and rendering configuration.

This class encapsulates both the CPU-side representation of a material (its parameters and textures), and a pointer to its backend-specific GPU resource handle.

Definition at line 29 of file material.hpp.

Constructor & Destructor Documentation

◆ Material()

rendering_engine::Material::Material ( IRenderer * renderer,
MaterialSettings matSettings )
explicit

Constructs a new Material instance.

Parameters
rendererPointer to the rendering engine interface.
matSettingsSettings that define the material behavior.

Definition at line 10 of file material.cpp.

11 :
12 mRenderer(renderer),
13 mMaterialSettings(matSettings),
14 mGpuHandle(nullptr)
15{}

Member Function Documentation

◆ AddTexture()

void rendering_engine::Material::AddTexture ( const std::string & textureName)

Adds a texture name to the material's list of used textures.

Parameters
textureNameName of the texture (e.g., texture ID or resource name).

Definition at line 96 of file material.cpp.

97{
98 mTextures.push_back(textureName);
99}

◆ GetMaterialRenderResources()

IMaterialRenderResources * rendering_engine::Material::GetMaterialRenderResources ( ) const

Returns the backend-specific GPU handle of the material.

Definition at line 76 of file material.cpp.

77{
78 return mGpuHandle.get();
79}

◆ GetMaterialSettings()

const MaterialSettings rendering_engine::Material::GetMaterialSettings ( ) const

Returns the material's static settings (domain, blend mode, shading model, etc.).

Definition at line 16 of file material.cpp.

17{
18 return mMaterialSettings;
19}

◆ GetTextures()

std::vector< std::string > rendering_engine::Material::GetTextures ( ) const

Returns the list of texture names used by this material.

Definition at line 71 of file material.cpp.

72{
73 return mTextures;
74}

◆ InitializeRenderResources()

void rendering_engine::Material::InitializeRenderResources ( )

Initializes backend-specific GPU resources associated with this material.

Must be called after construction and before rendering.

Definition at line 20 of file material.cpp.

21{
22 mGpuHandle = std::unique_ptr<IMaterialRenderResources>(mRenderer->ProvideMaterialRenderResources());
23 mGpuHandle->Initialize(this);
24}

◆ PackMaterialParameters()

PackedMaterialData rendering_engine::Material::PackMaterialParameters ( )

Packs the current float/vector parameters into a binary buffer and layout metadata.

This buffer can be uploaded to the GPU as a uniform buffer or push constants.

Returns
PackedMaterialData Struct containing binary data and parameter layout info.

Definition at line 31 of file material.cpp.

32{
33 PackedMaterialData result;
34 size_t currentOffset = 0;
35
36 // floats
37 for (const auto& [name, value] : mFloatParameters)
38 {
39 size_t alignment = std140_align(MaterialParameterLayoutEntry::Type::Float);
40 currentOffset = (currentOffset + alignment - 1) & ~(alignment - 1);
41 result.layout.push_back({ name, currentOffset, 4, MaterialParameterLayoutEntry::Type::Float });
42 result.buffer.resize(currentOffset + 4);
43 std::memcpy(result.buffer.data() + currentOffset, &value, 4);
44 currentOffset += 4;
45 }
46 // vec3
47 for (const auto& [name, value] : mVec3Parameters)
48 {
49 size_t alignment = std140_align(MaterialParameterLayoutEntry::Type::Vec3);
50 currentOffset = (currentOffset + alignment - 1) & ~(alignment - 1);
51 result.layout.push_back({ name, currentOffset, 12, MaterialParameterLayoutEntry::Type::Vec3 });
52 result.buffer.resize(currentOffset + 16); // 16 bytes for std140 vec3
53 std::memcpy(result.buffer.data() + currentOffset, &value, 12);
54 // pad last 4 bytes with zero
55 std::memset(result.buffer.data() + currentOffset + 12, 0, 4);
56 currentOffset += 16;
57 }
58 // vec4
59 for (const auto& [name, value] : mVec4Parameters)
60 {
61 size_t alignment = std140_align(MaterialParameterLayoutEntry::Type::Vec4);
62 currentOffset = (currentOffset + alignment - 1) & ~(alignment - 1);
63 result.layout.push_back({ name, currentOffset, 16, MaterialParameterLayoutEntry::Type::Vec4 });
64 result.buffer.resize(currentOffset + 16);
65 std::memcpy(result.buffer.data() + currentOffset, &value, 16);
66 currentOffset += 16;
67 }
68 return result;
69}
@ Vec3
@ Float
@ Vec4

◆ ReleaseRenderResources()

void rendering_engine::Material::ReleaseRenderResources ( )

Releases any GPU resources associated with this material.

Definition at line 25 of file material.cpp.

26{
27 mGpuHandle->Shutdown();
28 mGpuHandle.release();
29 mGpuHandle = nullptr;
30}

◆ SetFloat()

void rendering_engine::Material::SetFloat ( const std::string & name,
float value )

Sets a float parameter for the material.

Parameters
nameName of the parameter.
valueFloat value to set.

Definition at line 81 of file material.cpp.

82{
83 mFloatParameters[name] = value;
84}

◆ SetVec3()

void rendering_engine::Material::SetVec3 ( const std::string & name,
glm::vec3 value )

Sets a vec3 parameter for the material.

Parameters
nameName of the parameter.
valueglm::vec3 value to set.

Definition at line 86 of file material.cpp.

87{
88 mVec3Parameters[name] = value;
89}

◆ SetVec4()

void rendering_engine::Material::SetVec4 ( const std::string & name,
glm::vec4 value )

Sets a vec4 parameter for the material.

Parameters
nameName of the parameter.
valueglm::vec4 value to set.

Definition at line 91 of file material.cpp.

92{
93 mVec4Parameters[name] = value;
94}

The documentation for this class was generated from the following files: