Skip to main content

OpenGLMesh Class

Representative of an OpenGLMesh. This class manages the OpenGL Vertex Array Object (VAO), the Vertex Buffer Object (VBO) and Element Buffer Object (EBO) handles. The raw mesh data is uploaded to the GPU, preparing it for subsequent rendering commands / draw calls. More...

Declaration

class helios::ext::opengl::rendering::model::OpenGLMesh { ... }

Base class

classMesh

Representative of a renderable 3D mesh. More...

Public Constructors Index

OpenGLMesh (const OpenGLMesh &)=delete

Rule of three. More...

OpenGLMesh (std::shared_ptr< const std::vector< helios::rendering::Vertex > > vertices, std::shared_ptr< const std::vector< unsigned int > > indices, std::shared_ptr< const helios::rendering::mesh::MeshConfig > meshConfig)

Constructs a new OpenGLMesh instance from raw mesh data. More...

OpenGLMesh (const helios::rendering::asset::shape::Shape &shape, std::shared_ptr< const helios::rendering::mesh::MeshConfig > meshConfig)

Creates a new OpenGLMesh instance from the specified Shape. More...

Public Destructor Index

~OpenGLMesh () override

Frees allocated resources bv this instance. If an instance of this class is destructed, `glDeleteBuffers`/`glDeleteVertexArrays` is called to free the associated resources. More...

Public Operators Index

OpenGLMesh &operator= (const OpenGLMesh &)=delete

Public Member Functions Index

const unsigned int &vao () const noexcept

Returns the OpenGL Vertex Array Object Handle. More...

const unsigned int &vbo () const noexcept

Returns the OpenGL Vertex Buffer Object handle. More...

const unsigned int &ebo () const noexcept

Returns the OpenGL Element Buffer Object handle for indexed rendering. More...

Protected Member Functions Index

voidinit () override

Initializes all buffer objects required by OpenGL from the provided mesh data. The current implementation generates all VAO, VBO and EBO handles, loads the vertex and index data to the GPU. It follows [Vri20, 162] in this regard. More...

Protected Member Attributes Index

const unsigned intvao_

Vertex Array Object handle. More...

const unsigned intvbo_

Vertex Buffer Object handle. Stores vertex attributes such as normals and texture coordinates. More...

const unsigned intebo_

Element Buffer Object handle. Stores the indices used for indexed drawing. More...

Protected Static Functions Index

static unsigned intgenerateGLVertexArray () noexcept

Helper function for generating a Vertex Array Object identifier. More...

static unsigned intgenerateGLBuffer () noexcept

Helper function for generating a Buffer Object identifier, to be used with vertex buffer or element buffer. More...

Description

Representative of an OpenGLMesh. This class manages the OpenGL Vertex Array Object (VAO), the Vertex Buffer Object (VBO) and Element Buffer Object (EBO) handles. The raw mesh data is uploaded to the GPU, preparing it for subsequent rendering commands / draw calls.

Definition at line 29 of file OpenGLMesh.ixx.

Public Constructors

OpenGLMesh()

helios::ext::opengl::rendering::model::OpenGLMesh::OpenGLMesh (const OpenGLMesh &)
delete

OpenGLMesh()

helios::ext::opengl::rendering::model::OpenGLMesh::OpenGLMesh (std::shared_ptr< const std::vector< helios::rendering::Vertex > > vertices, std::shared_ptr< const std::vector< unsigned int > > indices, std::shared_ptr< const helios::rendering::mesh::MeshConfig > meshConfig)
inline explicit

Constructs a new OpenGLMesh instance from raw mesh data.

Parameters
vertices

A shared pointer to a vector of const Vertex

indices

A shared pointer to a vector of indices

meshConfig

A shared ptr to the const MeshConfig used with this Mesh.

Exceptions
std::invalid_argument

if either "vertices", "indices" or meshConfig is a null shared pointer

Definition at line 147 of file OpenGLMesh.ixx.

147 explicit OpenGLMesh(
148 std::shared_ptr<const std::vector<helios::rendering::Vertex>> vertices,
149 std::shared_ptr<const std::vector<unsigned int>> indices,
150 std::shared_ptr<const helios::rendering::mesh::MeshConfig> meshConfig
151 ) :
152 Mesh(
153 std::move(vertices),
154 std::move(indices),
155 std::move(meshConfig)
156 ),
160
161 if (!vertices_ || !indices_ || !meshConfig_) {
162 const std::string msg = "Mesh constructor received a null shared pointer.";
163 logger_.error(msg);
164 throw std::invalid_argument(msg);
165 }
166 /**
167 * @todo this should not be part of the constructor,
168 * instead, lazy init in render pass, then reuse.
169 */
171 }

References ebo_, generateGLBuffer, generateGLVertexArray, helios::rendering::mesh::Mesh::indices, helios::rendering::mesh::Mesh::indices_, init, helios::rendering::mesh::Mesh::logger_, helios::rendering::mesh::Mesh::Mesh, helios::rendering::mesh::Mesh::meshConfig, helios::rendering::mesh::Mesh::meshConfig_, vao_, vbo_, helios::rendering::mesh::Mesh::vertices and helios::rendering::mesh::Mesh::vertices_.

OpenGLMesh()

helios::ext::opengl::rendering::model::OpenGLMesh::OpenGLMesh (const helios::rendering::asset::shape::Shape & shape, std::shared_ptr< const helios::rendering::mesh::MeshConfig > meshConfig)
inline explicit

Creates a new OpenGLMesh instance from the specified Shape.

Parameters
shape

A const reference to the Shape.

meshConfig

A shared ptr to the const MeshConfig used with this OpenGLMesh.

Exceptions
std::invalid_argument

if meshConfig is a null shared pointer, or if the shape contained null data

Definition at line 182 of file OpenGLMesh.ixx.

182 explicit OpenGLMesh(
184 std::shared_ptr<const helios::rendering::mesh::MeshConfig> meshConfig
185 ) :
186 Mesh(
187 shape,
188 std::move(meshConfig)
189 ),
193
194 if (!vertices_ || !indices_ || !meshConfig_) {
195 const std::string msg = "Mesh constructor received a null shared pointer.";
196 logger_.error(msg);
197 throw std::invalid_argument(msg);
198 }
199
200 /**
201 * @todo this should not be part of the constructor,
202 * instead, lazy init in render pass, then reuse.
203 */
205 }

References ebo_, generateGLBuffer, generateGLVertexArray, helios::rendering::mesh::Mesh::indices_, init, helios::rendering::mesh::Mesh::logger_, helios::rendering::mesh::Mesh::Mesh, helios::rendering::mesh::Mesh::meshConfig, helios::rendering::mesh::Mesh::meshConfig_, vao_, vbo_ and helios::rendering::mesh::Mesh::vertices_.

Public Destructor

~OpenGLMesh()

helios::ext::opengl::rendering::model::OpenGLMesh::~OpenGLMesh ()
inline

Frees allocated resources bv this instance. If an instance of this class is destructed, `glDeleteBuffers`/`glDeleteVertexArrays` is called to free the associated resources.

See Also

https://registry.khronos.org/OpenGL-Refpages/gl4/html/glDeleteBuffers.xhtml

See Also

https://registry.khronos.org/OpenGL-Refpages/gl4/html/glDeleteVertexArrays.xhtml

Definition at line 216 of file OpenGLMesh.ixx.

216 ~OpenGLMesh() override {
217 glDeleteVertexArrays(1, &vao_);
218 glDeleteBuffers(1, &vbo_);
219 glDeleteBuffers(1, &ebo_);
220 }

References ebo_, vao_ and vbo_.

Public Operators

operator=()

OpenGLMesh & helios::ext::opengl::rendering::model::OpenGLMesh::operator= (const OpenGLMesh &)
delete

Definition at line 135 of file OpenGLMesh.ixx.

Reference OpenGLMesh.

Public Member Functions

ebo()

const unsigned int & helios::ext::opengl::rendering::model::OpenGLMesh::ebo ()
inline nodiscard noexcept

Returns the OpenGL Element Buffer Object handle for indexed rendering.

Returns

EBO handle

Definition at line 245 of file OpenGLMesh.ixx.

245 [[nodiscard]] const unsigned int& ebo() const noexcept {
246 return ebo_;
247 }

Reference ebo_.

vao()

const unsigned int & helios::ext::opengl::rendering::model::OpenGLMesh::vao ()
inline nodiscard noexcept

Returns the OpenGL Vertex Array Object Handle.

Returns

VAO handle

Definition at line 227 of file OpenGLMesh.ixx.

227 [[nodiscard]] const unsigned int& vao() const noexcept {
228 return vao_;
229 }

Reference vao_.

Referenced by generateGLVertexArray.

vbo()

const unsigned int & helios::ext::opengl::rendering::model::OpenGLMesh::vbo ()
inline nodiscard noexcept

Returns the OpenGL Vertex Buffer Object handle.

Returns

VBO handle

Definition at line 236 of file OpenGLMesh.ixx.

236 [[nodiscard]] const unsigned int& vbo() const noexcept {
237 return vbo_;
238 }

Reference vbo_.

Referenced by generateGLBuffer.

Protected Member Functions

init()

void helios::ext::opengl::rendering::model::OpenGLMesh::init ()
inline protected virtual

Initializes all buffer objects required by OpenGL from the provided mesh data. The current implementation generates all VAO, VBO and EBO handles, loads the vertex and index data to the GPU. It follows [Vri20, 162] in this regard.

See Also

[Vri20, 162]

Definition at line 86 of file OpenGLMesh.ixx.

86 void init() override {
87 glBindVertexArray(vao_);
88 glBindBuffer(GL_ARRAY_BUFFER, vbo_);
89
90 glBufferData(
91 GL_ARRAY_BUFFER,
92 vertices_->size() * sizeof(helios::rendering::Vertex),
93 &(*vertices_)[0],
94 GL_STATIC_DRAW
95 );
96
97 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo_);
98 glBufferData(
99 GL_ELEMENT_ARRAY_BUFFER,
100 indices_->size() * sizeof(unsigned int),
101 &(*indices_)[0],
102 GL_STATIC_DRAW
103 );
104
105 // vertex position
106 glEnableVertexAttribArray(0);
107 glVertexAttribPointer(
108 0, 3, GL_FLOAT,
109 GL_FALSE, sizeof(helios::rendering::Vertex), nullptr
110 );
111
112 // vertex normals
113 glEnableVertexAttribArray(1);
114 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(helios::rendering::Vertex),
115 reinterpret_cast<void*>(offsetof(helios::rendering::Vertex, normal))
116 );
117
118 // vertex texture coords
119 glEnableVertexAttribArray(2);
120 glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(helios::rendering::Vertex),
121 reinterpret_cast<void*>(offsetof(helios::rendering::Vertex, texCoords))
122 );
123
124 glBindVertexArray(0);
125 }

References ebo_, helios::rendering::mesh::Mesh::indices_, vao_, vbo_ and helios::rendering::mesh::Mesh::vertices_.

Referenced by OpenGLMesh and OpenGLMesh.

Protected Member Attributes

ebo_

const unsigned int helios::ext::opengl::rendering::model::OpenGLMesh::ebo_
protected

Element Buffer Object handle. Stores the indices used for indexed drawing.

Definition at line 51 of file OpenGLMesh.ixx.

51 const unsigned int ebo_;

Referenced by ebo, init, OpenGLMesh, OpenGLMesh and ~OpenGLMesh.

vao_

const unsigned int helios::ext::opengl::rendering::model::OpenGLMesh::vao_
protected

Vertex Array Object handle.

Definition at line 37 of file OpenGLMesh.ixx.

37 const unsigned int vao_;

Referenced by init, OpenGLMesh, OpenGLMesh, vao and ~OpenGLMesh.

vbo_

const unsigned int helios::ext::opengl::rendering::model::OpenGLMesh::vbo_
protected

Vertex Buffer Object handle. Stores vertex attributes such as normals and texture coordinates.

Definition at line 44 of file OpenGLMesh.ixx.

44 const unsigned int vbo_;

Referenced by init, OpenGLMesh, OpenGLMesh, vbo and ~OpenGLMesh.

Protected Static Functions

generateGLBuffer()

unsigned int helios::ext::opengl::rendering::model::OpenGLMesh::generateGLBuffer ()
inline noexcept protected static

Helper function for generating a Buffer Object identifier, to be used with vertex buffer or element buffer.

Returns

Vertex Array Object handle.

Definition at line 72 of file OpenGLMesh.ixx.

72 static unsigned int generateGLBuffer() noexcept {
73 unsigned int vbo;
74 glGenBuffers(1, &vbo);
75 return vbo;
76 }

Reference vbo.

Referenced by OpenGLMesh and OpenGLMesh.

generateGLVertexArray()

unsigned int helios::ext::opengl::rendering::model::OpenGLMesh::generateGLVertexArray ()
inline noexcept protected static

Helper function for generating a Vertex Array Object identifier.

Returns

Vertex Array Object handle.

Definition at line 59 of file OpenGLMesh.ixx.

59 static unsigned int generateGLVertexArray() noexcept {
60 unsigned int vao;
61 glGenVertexArrays(1, &vao);
62 return vao;
63 }

Reference vao.

Referenced by OpenGLMesh and OpenGLMesh.


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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.