Geometry
Geometry is the core unit of renderable mesh data. It stores a collection of named buffer attributes (positions, normals, UVs, tangents and so on) alongside an optional index buffer. The renderer matches attributes to shader inputs by name, so custom attributes only need a matching declaration in shader code.
All attributes must share the same element count, which defines the geometry's vertex count. A geometry can be rendered using different primitive types (triangles, lines, line loops) and provide access to derived bounding volumes such as axis-aligned bounding boxes and bounding spheres, useful for frustum culling or collision queries.
Instances are usually created via the static Geometry::Create factory method and populated with attributes using Geometry::AddAttribute.
auto geometry = vglx::Geometry::Create();
geometry->AddAttribute(vglx::BufferAttribute::Create({
.name = vglx::BufferAttribute::kPosition,
.format = vglx::BufferAttribute::Format::Float32x3,
.rate = vglx::BufferAttribute::Rate::Vertex
}, {
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
}));
auto material = vglx::PhongMaterial::Create({.color = 0x049EF4});
my_scene->Add(vglx::Mesh::Create(geometry, material));Construction
Factories preferred
Creates a shared instance of an empty Geometry.
The geometry has no attributes or index data and must be populated before rendering.
static std::shared_ptr<Geometry> Geometry::Create();Types
Geometry::PrimitiveType enum
Primitive topology used when rendering this geometry.
Controls how the vertex and index data are interpreted by the renderer.
| Value | Description |
|---|---|
| Triangles | Render as triangle list. |
| Lines | Render as line list. |
| LineLoop | Render as a closed line loop. |
Properties
primitive PrimitiveType
Primitive topology used by this geometry (triangles by default).
PrimitiveType primitive {PrimitiveType::Triangles};Functions
AddAttribute() void
Adds a vertex attribute to this geometry.
The attribute must have a vertex rate, a unique name within the geometry and an element count that matches any previously added attributes. Attributes that violate these constraints are reported and rejected.
void Geometry::AddAttribute(std::shared_ptr<BufferAttribute> attribute);| Parameter | Description |
|---|---|
| attribute | Buffer attribute to add. |
Returns an axis-aligned bounding box that encloses the geometry.
The box is computed from the position attribute and cached. It is recomputed automatically when the position data changes.
Box3 Geometry::BoundingBox();Returns a bounding sphere that encloses the geometry.
The sphere is computed from the position attribute and cached. It is recomputed automatically when the position data changes.
Sphere Geometry::BoundingSphere();GetAttribute() std::shared_ptr<BufferAttribute>
Returns the vertex attribute with the given name.
std::shared_ptr<BufferAttribute> Geometry::GetAttribute(std::string_view name) const;| Parameter | Description |
|---|---|
| name | Attribute name to look up. |
GetAttributes() const std::vector<std::shared_ptr< BufferAttribute> >&
Returns the list of vertex attributes added to this geometry.
const std::vector<std::shared_ptr< BufferAttribute> >& Geometry::GetAttributes() const;GetIndexData() const std::vector<uint32_t>&
Returns a read-only reference to the index buffer.
If the geometry is non-indexed, this buffer is empty.
const std::vector<uint32_t>& Geometry::GetIndexData() const;GetIndexVersion() uint32_t
Returns the index version incremented when indices are replaced.
The renderer compares versions to decide when the index buffer needs to be re-uploaded.
uint32_t Geometry::GetIndexVersion() const;GetMaxIndex() uint32_t
Returns the largest index in the index buffer.
Used by the renderer to validate that indices reference valid vertices. For non-indexed geometry this value is zero.
uint32_t Geometry::GetMaxIndex() const;HasPositions() bool
Checks whether the geometry has a position attribute.
bool Geometry::HasPositions() const;SetIndices() void
Replaces the index buffer.
Indices reference elements in the geometry's vertex attributes. Passing an empty list makes the geometry non-indexed.
void Geometry::SetIndices(std::vector<uint32_t> index_data);| Parameter | Description |
|---|---|
| index_data | Index buffer (empty for non-indexed geometry). |
VertexCount() uint32_t
Returns the number of vertices in the geometry.
The vertex count is derived from the element count of the geometry's attributes. For a geometry with no attributes this value is zero.
uint32_t Geometry::VertexCount() const;