Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
Mesh.h
Go to the documentation of this file.
1/**
2 * @file Mesh.h
3 * @ingroup mnd_render
4 * @brief GPU mesh: Vertex Array Object wrapping a VBO and optional EBO.
5 *
6 * A Mesh holds the GPU-side geometry for a drawable object. It is created by
7 * Builder factory methods (or Mesh::CreateCube) and stored in a shared_ptr so
8 * that multiple MeshComponents can share the same geometry data.
9 *
10 * ## Relationship with Material
11 * Mesh and Material are intentionally decoupled — they are paired only at
12 * submit time inside a RenderCommand. This allows one mesh to be drawn with
13 * different materials (e.g. a highlight variant) without duplication.
14 *
15 * ## Draw call flow
16 * @code
17 * graphicsAPI.BindMesh(mesh); // glBindVertexArray → sets up attrib pointers
18 * mesh->Draw(); // glDrawElements or glDrawArrays
19 * @endcode
20 *
21 * @see Builder, MeshComponent, RenderCommand
22 */
23
24#pragma once
25#include <memory>
26#include <vector>
27
28#include "Types.h"
29#include "graphics/GLForward.h"
30#include "graphics/VertexLayout.h"
31
32namespace mnd
33{
34
35/**
36 * @brief Immutable GPU mesh (VAO + VBO + optional EBO).
37 *
38 * Non-copyable — each instance owns unique GL handles.
39 * Vertex data is uploaded to the GPU in the constructor and released from
40 * CPU memory immediately; the GL objects live until the Mesh is destroyed.
41 */
42class Mesh
43{
44public:
45 /**
46 * @brief Upload indexed geometry to the GPU.
47 * @param layout Describes how vertex bytes map to shader attributes.
48 * @param vertices Interleaved vertex data (position, normal, UV …).
49 * @param indices Triangle list indices into the vertex array.
50 */
51 Mesh(const VertexLayout &layout, const std::vector<float> &vertices, const std::vector<uint32_t> &indices);
52
53 /**
54 * @brief Upload non-indexed geometry to the GPU.
55 * @param layout Vertex attribute layout.
56 * @param vertices Interleaved vertex data; drawn with glDrawArrays.
57 */
58 Mesh(const VertexLayout &layout, const std::vector<float> &vertices);
59
60 Mesh(const Mesh &) = delete;
61 Mesh &operator=(const Mesh &) = delete;
62
63 /// Bind the VAO so subsequent draw calls use this mesh's geometry.
64 void Bind();
65
66 void Unbind();
67 /**
68 * @brief Issue the draw call for this mesh.
69 *
70 * Uses glDrawElements when an EBO is present; glDrawArrays otherwise.
71 * Must be preceded by Bind() (or GraphicsAPI::BindMesh()).
72 */
73 void Draw();
74
75 /// Creates a unit cube mesh (factory convenience, same as Builder::CreateCube).
76 static std::shared_ptr<Mesh> CreateCube();
77 static std::shared_ptr<Mesh> CreateSphere(f32 radius, int sectors, int stacks);
78 static std::shared_ptr<Mesh> CreateCapsule(f32 radius, f32 height, int sectors, int hemisphereStacks);
79
80 static std::shared_ptr<Mesh> CreateBox(const glm::vec3 &extents = glm::vec3(1.0f));
81
82private:
83 VertexLayout m_vertexLayout; ///< Attribute layout used when setting up the VAO.
84
85 GLuint m_VBO = 0; ///< Vertex Buffer Object — raw vertex data on the GPU.
86 GLuint m_EBO = 0; ///< Element Buffer Object — index data (0 if non-indexed).
87 GLuint m_VAO = 0; ///< Vertex Array Object — records attribute layout + buffer bindings.
88 size_t m_vertexCount = 0; ///< Number of vertices (used for glDrawArrays).
89 size_t m_indexCount = 0; ///< Number of indices (used for glDrawElements; 0 if non-indexed).
90};
91
92} // namespace mnd
unsigned int GLuint
Definition GLForward.h:11
Engine-wide primitive type aliases and GLM math imports.
Immutable GPU mesh (VAO + VBO + optional EBO).
Definition Mesh.h:43
static std::shared_ptr< Mesh > CreateSphere(f32 radius, int sectors, int stacks)
Definition Mesh.cpp:322
void Unbind()
Definition Mesh.cpp:317
void Draw()
Issue the draw call for this mesh.
Definition Mesh.cpp:198
static std::shared_ptr< Mesh > CreateCapsule(f32 radius, f32 height, int sectors, int hemisphereStacks)
Definition Mesh.cpp:396
Mesh(const Mesh &)=delete
static std::shared_ptr< Mesh > CreateCube()
Creates a unit cube mesh (factory convenience, same as Builder::CreateCube).
Definition Mesh.cpp:81
Mesh & operator=(const Mesh &)=delete
void Bind()
Bind the VAO so subsequent draw calls use this mesh's geometry.
Definition Mesh.cpp:193
static std::shared_ptr< Mesh > CreateBox(const glm::vec3 &extents=glm::vec3(1.0f))
Definition Mesh.cpp:209
float f32
32-bit IEEE float — the standard GL scalar type.
Definition Types.h:40
Complete description of how vertex data is packed in a VBO.