Skip to content

Geometry

Represents renderable mesh data as a set of named vertex attributes.

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.

cpp
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


Geometry::Create() std::shared_ptr<Geometry>

Creates a shared instance of an empty Geometry.

The geometry has no attributes or index data and must be populated before rendering.

cpp
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.

ValueDescription
TrianglesRender as triangle list.
LinesRender as line list.
LineLoopRender as a closed line loop.

Properties

primitive PrimitiveType

Primitive topology used by this geometry (triangles by default).

cpp
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.

cpp
void Geometry::AddAttribute(std::shared_ptr<BufferAttribute> attribute);
ParameterDescription
attributeBuffer attribute to add.

BoundingBox() Box3

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.

cpp
Box3 Geometry::BoundingBox();

BoundingSphere() Sphere

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.

cpp
Sphere Geometry::BoundingSphere();

GetAttribute() std::shared_ptr<BufferAttribute>

Returns the vertex attribute with the given name.

cpp
std::shared_ptr<BufferAttribute> Geometry::GetAttribute(std::string_view name) const;
ParameterDescription
nameAttribute name to look up.

GetAttributes() const std::vector<std::shared_ptr< BufferAttribute> >&

Returns the list of vertex attributes added to this geometry.

cpp
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.

cpp
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.

cpp
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.

cpp
uint32_t Geometry::GetMaxIndex() const;

HasPositions() bool

Checks whether the geometry has a position attribute.

cpp
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.

cpp
void Geometry::SetIndices(std::vector<uint32_t> index_data);
ParameterDescription
index_dataIndex 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.

cpp
uint32_t Geometry::VertexCount() const;

Released under the MIT License.