Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
OpenGLMaterial.cpp
1/*
2Copyright (C) 2016-2021,2025,2026 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16#include "aeongames/Material.hpp"
17#include "aeongames/Texture.hpp"
18#include "aeongames/Mesh.hpp"
19
20#include "OpenGLFunctions.hpp"
21#include "OpenGLMaterial.hpp"
22#include "OpenGLRenderer.hpp"
23#include "OpenGLPipeline.hpp"
24
25namespace AeonGames
26{
27 OpenGLMaterial::OpenGLMaterial ( OpenGLRenderer& aOpenGLRenderer, const Material& aMaterial ) :
28 mOpenGLRenderer{aOpenGLRenderer},
29 mMaterial{&aMaterial},
30 mUniformBuffer{}
31 {
32 if ( aMaterial.GetUniformBuffer().size() )
33 {
34 mUniformBuffer.Initialize ( static_cast<GLsizei> ( aMaterial.GetUniformBuffer().size() ), GL_STATIC_DRAW, aMaterial.GetUniformBuffer().data() );
35 }
36
37 // Preload linked textures
38 for ( auto& i : aMaterial.GetSamplers() )
39 {
40 const Texture* texture = std::get<1> ( i ).Get<Texture>();
41 mOpenGLRenderer.LoadTexture ( *texture );
42 }
43 }
44
45 OpenGLMaterial::~OpenGLMaterial ( )
46 {
47 // Unload linked textures
48 for ( auto& i : mMaterial->GetSamplers() )
49 {
50 const Texture* texture = std::get<1> ( i ).Get<Texture>();
51 mOpenGLRenderer.UnloadTexture ( *texture );
52 }
53 if ( mMaterial->GetUniformBuffer().size() )
54 {
55 mUniformBuffer.Finalize();
56 }
57 }
58
60 mOpenGLRenderer{aOpenGLMaterial.mOpenGLRenderer},
61 mMaterial{aOpenGLMaterial.mMaterial},
62 mUniformBuffer{std::move ( aOpenGLMaterial.mUniformBuffer ) }
63 {
64 }
65
66 void OpenGLMaterial::Bind ( const OpenGLPipeline& aPipeline ) const
67 {
68 const auto& samplers = mMaterial->GetSamplers();
69 for ( GLenum i = 0; i < samplers.size(); ++i )
70 {
71 aPipeline.GetSamplerLocation ( std::get<0> ( samplers[i] ) );
72
73 glBindTextureUnit ( aPipeline.GetSamplerLocation ( std::get<0> ( samplers[i] ) ),
74 mOpenGLRenderer.GetTextureId ( *std::get<1> ( samplers[i] ).Cast<Texture>() ) );
75 OPENGL_CHECK_ERROR_THROW;
76 }
77
79
80 if ( mMaterial->GetUniformBuffer().size() )
81 {
82 const OpenGLUniformBlock* uniform_block = aPipeline.GetUniformBlock ( Mesh::MATERIAL );
83 if ( uniform_block != nullptr )
84 {
85 glBindBufferBase ( GL_UNIFORM_BUFFER, uniform_block->binding, mUniformBuffer.GetBufferId() );
86 OPENGL_CHECK_ERROR_THROW;
87 }
88 }
89 }
90}
Represents a surface material with uniform properties and texture samplers.
Definition Material.hpp:38
DLL const std::vector< uint8_t > & GetUniformBuffer() const
Get the raw uniform buffer.
Definition Material.cpp:45
DLL const std::vector< std::tuple< uint32_t, ResourceId > > & GetSamplers() const
Get all sampler bindings.
Definition Material.cpp:245
@ MATERIAL
Material binding.
Definition Mesh.hpp:72
void Finalize()
Release the buffer resources.
OpenGLMaterial(OpenGLRenderer &aOpenGLRenderer, const Material &aMaterial)
Construct from a renderer and material resource.
void Bind(const OpenGLPipeline &aPipeline) const
Binds the material to the specified pipeline.
OpenGL shader program pipeline with attribute and uniform reflection.
const OpenGLUniformBlock * GetUniformBlock(uint32_t name) const
Get a uniform block by its name hash, or nullptr if not found.
const GLuint GetSamplerLocation(uint32_t name_hash) const
Get the uniform location of a sampler by its name hash.
OpenGL rendering backend implementing the Renderer interface.
void UnloadTexture(const Texture &aTexture) final
Unloads a texture from GPU memory.
Represents a 2D texture image resource.
Definition Texture.hpp:33
<- This is here just for the literals
Definition AABB.hpp:31
STL namespace.
Describes an OpenGL uniform block with its binding and member variables.
GLint binding
Binding point index.