Skip to main content

OpenGLMeshRenderer Class

OpenGL implementation of the MeshRenderer interface. More...

Declaration

class helios::ext::opengl::rendering::OpenGLMeshRenderer { ... }

Base class

classMeshRenderer

Abstract base class for mesh rendering operations. More...

Friends Index

classOpenGLDevice

Public Destructor Index

~OpenGLMeshRenderer () override

Destructor that unbinds any currently bound VAO. More...

Public Member Functions Index

voidrender (const helios::rendering::mesh::MeshRenderCommand &command, const helios::rendering::shader::UniformValueMap &frameUniformValues) noexcept

Renders a mesh using the provided render command and frame-level uniforms. More...

Private Member Functions Index

voidinit ()

Initializes the renderer. More...

voidbeginRenderPass (helios::rendering::RenderPass &renderPass) const noexcept

Resets cached rendering state at the beginning of a render pass. More...

Private Member Attributes Index

const helios::ext::opengl::rendering::shader::OpenGLShader *lastShader_ = nullptr

Cached pointer to the last used shader for state optimization. More...

unsigned intlastVao_ = 0

Cached VAO ID for state optimization. More...

Description

OpenGL implementation of the MeshRenderer interface.

`OpenGLMeshRenderer` provides the concrete rendering logic for mesh geometry using the OpenGL graphics API. It translates abstract `MeshRenderCommand` objects into OpenGL-specific draw calls, handling shader binding, uniform application, and VAO state management.

## State Caching

This class implements state caching to minimize redundant OpenGL state changes:

  • Shader programs are only activated when they differ from the previously used shader.
  • VAO bindings are only updated when rendering a mesh with a different VAO.

## Usage

```cpp OpenGLMeshRenderer renderer; MeshRenderCommand command = createRenderCommand(); UniformValueMap frameUniforms = {{"viewMatrix", viewMat}, {"projMatrix", projMat}}; renderer.render(command, frameUniforms); ```

## Ownership

This class is typically owned by `OpenGLDevice` and should not be instantiated directly in most use cases.

info

The renderer expects all shaders and meshes to be OpenGL-specific implementations (`OpenGLShader` and `OpenGLMesh`). Using incompatible types will trigger assertions.

See Also

helios::rendering::mesh::MeshRenderer

See Also

helios::rendering::mesh::MeshRenderCommand

See Also

OpenGLDevice

Definition at line 60 of file OpenGLMeshRenderer.ixx.

Friends

OpenGLDevice

friend class OpenGLDevice

Definition at line 64 of file OpenGLMeshRenderer.ixx.

64 friend class OpenGLDevice;

Reference OpenGLDevice.

Referenced by OpenGLDevice.

Public Destructor

~OpenGLMeshRenderer()

helios::ext::opengl::rendering::OpenGLMeshRenderer::~OpenGLMeshRenderer ()
inline

Destructor that unbinds any currently bound VAO.

Ensures clean OpenGL state upon destruction.

Definition at line 116 of file OpenGLMeshRenderer.ixx.

117
118 glBindVertexArray(0);
119 }

Public Member Functions

render()

void helios::ext::opengl::rendering::OpenGLMeshRenderer::render (const helios::rendering::mesh::MeshRenderCommand & command, const helios::rendering::shader::UniformValueMap & frameUniformValues)
inline noexcept virtual

Renders a mesh using the provided render command and frame-level uniforms.

This method performs the following steps: 1. Extracts the shader and mesh from the render command's prototype. 2. Activates the shader if it differs from the previously used shader (state caching). 3. Applies frame-level, object-level, and material-level uniform values. 4. Binds the mesh's VAO if it differs from the previously bound VAO (state caching). 5. Issues a `glDrawElements` call with the appropriate primitive type.

Parameters
command

The render command containing the render prototype with mesh, material, and per-object uniform data.

frameUniformValues

Frame-level uniform values shared across all objects (e.g., view and projection matrices).

info

If the render command does not contain a valid render prototype, the method returns without issuing any draw calls.

See Also

MeshRenderCommand

See Also

UniformValueMap

Definition at line 143 of file OpenGLMeshRenderer.ixx.

143 void render(
145 const helios::rendering::shader::UniformValueMap& frameUniformValues) noexcept {
146
147 if (const auto renderPrototype_ptr = command.renderPrototype()) {
148 const auto& baseShader = renderPrototype_ptr->material().shader();
149 const auto& baseMesh = renderPrototype_ptr->mesh();
150
151 const auto* shader = static_cast<const helios::ext::opengl::rendering::shader::OpenGLShader*>(&baseShader);
152 assert(shader && "Unexpected failure when casting to OpenGLShader.");
153
154 if (shader != lastShader_) {
155 shader->use();
156 lastShader_ = shader;
157 }
158
159 shader->applyUniformValues(frameUniformValues);
160 shader->applyUniformValues(command.objectUniformValues());
161 shader->applyUniformValues(command.materialUniformValues());
162
163 const auto* mesh = static_cast<const helios::ext::opengl::rendering::model::OpenGLMesh*>(&baseMesh);
164 assert(mesh && "Unexpected failure when casting to OpenGLMesh.");
165
166 const auto [primitiveType] = mesh->meshConfig();
167
168 if (mesh->vao() != lastVao_) {
169 glBindVertexArray(mesh->vao());
170 lastVao_ = mesh->vao();
171 }
172
173 glDrawElements(helios::ext::opengl::rendering::OpenGLEnumMapper::toOpenGL(primitiveType), mesh->indexCount(), GL_UNSIGNED_INT, nullptr);
174 }
175
176 };

Reference helios::ext::opengl::rendering::OpenGLEnumMapper::toOpenGL.

Private Member Functions

beginRenderPass()

void helios::ext::opengl::rendering::OpenGLMeshRenderer::beginRenderPass (helios::rendering::RenderPass & renderPass)
inline noexcept

Resets cached rendering state at the beginning of a render pass.

This ensures proper shader and VAO binding even when the render queue contents change between frames. Called internally by `OpenGLDevice` before processing the render queue.

Parameters
renderPass

The render pass being started (currently unused, reserved for future use).

Definition at line 100 of file OpenGLMeshRenderer.ixx.

100 void beginRenderPass(helios::rendering::RenderPass& renderPass) const noexcept {
101 // Reset cached state at the beginning of each render pass
102 // to ensure proper shader and VAO binding even when render queue contents change
103
104 lastShader_ = nullptr;
105 lastVao_ = 0;
106 }

init()

void helios::ext::opengl::rendering::OpenGLMeshRenderer::init ()
inline

Initializes the renderer.

Currently a no-op placeholder for future initialization logic.

Definition at line 86 of file OpenGLMeshRenderer.ixx.

86 void init() {
87
88 }

Private Member Attributes

lastShader_

const helios::ext::opengl::rendering::shader::OpenGLShader* helios::ext::opengl::rendering::OpenGLMeshRenderer::lastShader_ = nullptr
mutable

Cached pointer to the last used shader for state optimization.

Used to avoid redundant shader program activations. Reset at the beginning of each render pass.

Definition at line 72 of file OpenGLMeshRenderer.ixx.

72 mutable const helios::ext::opengl::rendering::shader::OpenGLShader* lastShader_ = nullptr;

lastVao_

unsigned int helios::ext::opengl::rendering::OpenGLMeshRenderer::lastVao_ = 0
mutable

Cached VAO ID for state optimization.

Used to avoid redundant VAO bindings. Reset at the beginning of each render pass.

Definition at line 79 of file OpenGLMeshRenderer.ixx.

79 mutable unsigned int lastVao_ = 0;

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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.