VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
mesh.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4// Created by Arnis Lektauers on 08.11.2025.
5//
6#pragma once
7
8#include <cstddef>
9
14
15namespace visutwin::canvas
16{
27
32 struct Primitive
33 {
35
37 int base = 0;
38
40 int baseVertex = 0;
41
43 int count = 0;
44
46 bool indexed = false;
47 };
48
54 {
55 // Maximum number of vertices that can be stored without reallocation
56 int maxVertices = 0;
57
58 // Maximum number of indices that can be stored without reallocation
59 int maxIndices = 0;
60
61 // Current number of vertices in use
62 int vertexCount = 0;
63
64 // Current number of indices in use
65 int indexCount = 0;
66 };
67
78 class Mesh : public RefCountedObject
79 {
80 public:
81 int aabbVer() const { return _aabbVer; }
82
83 const BoundingBox& aabb() const { return _aabb; }
84
85 void setAabb(const BoundingBox& bounds)
86 {
87 _aabb = bounds;
88 _aabbVer++;
89 }
90
91 void setVertexBuffer(const std::shared_ptr<VertexBuffer>& vb)
92 {
93 _vertexBuffer = vb;
94 _aabbVer++;
95 }
96
97 void setIndexBuffer(const std::shared_ptr<IndexBuffer>& ib, const size_t style = 0)
98 {
99 if (_indexBuffer.size() <= style) {
100 _indexBuffer.resize(style + 1);
101 }
102 _indexBuffer[style] = ib;
103 _aabbVer++;
104 }
105
106 void setPrimitive(const Primitive& p, const size_t style = 0)
107 {
108 if (_primitive.size() <= style) {
109 _primitive.resize(style + 1);
110 }
111 _primitive[style] = p;
112 _aabbVer++;
113 }
114
115 std::shared_ptr<VertexBuffer> getVertexBuffer() const { return _vertexBuffer; }
116
117 std::shared_ptr<IndexBuffer> getIndexBuffer(const size_t style = 0) const
118 {
119 return style < _indexBuffer.size() ? _indexBuffer[style] : nullptr;
120 }
121
122 Primitive getPrimitive(const size_t style = 0) const
123 {
124 return style < _primitive.size() ? _primitive[style] : Primitive{};
125 }
126
127 private:
128 void initGeometryData();
129
130 // Internal AABB version counter
131 int _aabbVer = 0;
132
133 // AABB representing object space bounds
134 BoundingBox _aabb;
135
136 // Geometry data for programmatic mesh modification
137 std::unique_ptr<GeometryData> _geometryData;
138
139 // Array of index buffers for different render styles
140 std::vector<std::shared_ptr<IndexBuffer>> _indexBuffer;
141
142 // The vertex buffer holding vertex data
143 std::shared_ptr<VertexBuffer> _vertexBuffer;
144
145 // Array of primitive objects defining how to interpret vertex/index data
146 std::vector<Primitive> _primitive;
147 };
148}
Axis-Aligned Bounding Box defined by center and half-extents.
Definition boundingBox.h:21
GPU-resident geometry defined by vertex/index buffers and one or more Primitives.
Definition mesh.h:79
void setVertexBuffer(const std::shared_ptr< VertexBuffer > &vb)
Definition mesh.h:91
void setPrimitive(const Primitive &p, const size_t style=0)
Definition mesh.h:106
Primitive getPrimitive(const size_t style=0) const
Definition mesh.h:122
void setAabb(const BoundingBox &bounds)
Definition mesh.h:85
void setIndexBuffer(const std::shared_ptr< IndexBuffer > &ib, const size_t style=0)
Definition mesh.h:97
std::shared_ptr< VertexBuffer > getVertexBuffer() const
Definition mesh.h:115
const BoundingBox & aabb() const
Definition mesh.h:83
std::shared_ptr< IndexBuffer > getIndexBuffer(const size_t style=0) const
Definition mesh.h:117
int aabbVer() const
Definition mesh.h:81
@ PRIMITIVE_POINTS
Definition mesh.h:19
@ PRIMITIVE_LINES
Definition mesh.h:20
@ PRIMITIVE_LINESTRIP
Definition mesh.h:22
@ PRIMITIVE_LINELOOP
Definition mesh.h:21
@ PRIMITIVE_TRISTRIP
Definition mesh.h:24
@ PRIMITIVE_TRIFAN
Definition mesh.h:25
@ PRIMITIVE_TRIANGLES
Definition mesh.h:23
Describes how vertex and index data should be interpreted for a draw call.
Definition mesh.h:33
PrimitiveType type
Definition mesh.h:34