Skip to content

Geometry

Represents GPU-ready mesh data with vertex and index buffers.

Geometry is the core unit of renderable mesh data in VGLX. It stores raw vertex and index buffers along with attribute metadata that describes how the data should be interpreted by the renderer (positions, normals, UVs, tangents, per-instance data, and so on).

A geometry can be rendered using different primitive types (triangles, lines, line loops) and provides 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 methods and configured with vertex attributes using Geometry::SetAttribute.

cpp
auto geometry = vglx::Geometry::Create({
  // 3 vertices, XYZ position only:
  0.5f, -0.5f, 0.0f,
  0.0f,  0.5f, 0.0f,
 -0.5f, -0.5f, 0.0f,
});

geometry->SetAttribute({vglx::Geometry::VertexAttributeType::Position, 3});

auto material = vglx::PhongMaterial::Create(0x049EF4);

my_scene->Add(vglx::Mesh::Create(geometry, material));

Construction

Constructors


Geometry()

Constructs a geometry from vertex and index data.

The vertex buffer is populated from vertex_data, and the optional index buffer from index_data. Attribute metadata must be configured separately using SetAttribute.

cpp
Geometry::Geometry(const std::vector<float> & vertex_data, const std::vector<unsigned int> & index_data);
ParameterDescription
vertex_dataInterleaved vertex data stored as floats.
index_dataOptional index buffer (empty for non-indexed geometry).

Factories preferred


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

Creates a shared instance of an empty Geometry.

The resulting geometry has no vertex or index data and must be populated and configured before rendering.

cpp
static std::shared_ptr<Geometry> Geometry::Create();

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

Creates a shared instance of Geometry from vertex and index data.

cpp
static std::shared_ptr<Geometry> Geometry::Create(const std::vector<float> & vertex_data, const std::vector<unsigned int> & index_data={});
ParameterDescription
vertex_dataInterleaved vertex data stored as floats.
index_dataOptional index data; leave empty for non-indexed geometry.

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.

Geometry::VertexAttributeType enum

Enumerates supported vertex attribute semantics.

Each value identifies the purpose of a vertex attribute stream, such as positions, normals, UVs, or per-instance transforms. Attributes are described via Geometry::VertexAttribute and stored in the attributes array.

ValueDescription
PositionVertex position in object space.
NormalVertex normal in object space.
UVTexture coordinates.
TangentTangent vector for normal mapping.
ColorPer-vertex color.
InstanceColorPer-instance color.
InstanceTransformPer-instance transform matrix.
NoneSentinel value indicating no attribute.

Geometry::VertexAttribute struct

Describes a single vertex attribute stream.

Each attribute entry specifies the semantic type and the number of components per vertex item_size (for example, 3 for a vec3 position). Attributes are stored in a fixed-size array indexed by Geometry::VertexAttributeType.

ParameterDescription
item_size unsigned intNumber of float components per vertex.
type VertexAttributeTypeAttribute semantic.

AttributesType typedef

Convenience alias for the attribute array type.

cpp
using vglx::Geometry::AttributesType = std::array<vglx::Geometry::VertexAttribute, AttributesLength>

Properties

AttributesLength int

Total number of supported attribute slots.

cpp
int AttributesLength {std::to_underlying(VertexAttributeType::None)};

primitive PrimitiveType

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

cpp
PrimitiveType primitive {PrimitiveType::Triangles};

renderer_id unsigned int

Renderer-specific identifier for the underlying GPU resource.

cpp
unsigned int renderer_id {0};

Functions

Attributes() constAttributesType&

Returns the array of vertex attribute descriptors.

The array is indexed by Geometry::VertexAttributeType values in the range [0, AttributesLength).

cpp
const AttributesType& Geometry::Attributes() const;

BoundingBox() Box3

Returns an axis-aligned bounding box enclosing the geometry.

The bounding box is computed lazily from the vertex positions and cached internally. Subsequent calls reuse the cached result until the geometry data changes.

cpp
Box3 Geometry::BoundingBox();

BoundingSphere() Sphere

Returns a bounding sphere enclosing the geometry.

The bounding sphere is computed lazily from the vertex positions and cached internally. Subsequent calls reuse the cached result until the geometry data changes.

cpp
Sphere Geometry::BoundingSphere();

HasAttribute() bool

Checks whether an attribute of the given type is present.

cpp
bool Geometry::HasAttribute(VertexAttributeType type) const;
ParameterDescription
typeAttribute semantic to query.

IndexCount() size_t

Returns the number of indices stored in the index buffer.

For non-indexed geometry, this value is zero.

cpp
size_t Geometry::IndexCount() const;

IndexData() const std::vector<unsigned int> &

Returns a read-only reference to the index buffer.

If the geometry is non-indexed, this buffer may be empty.

cpp
const std::vector<unsigned int> & Geometry::IndexData() const;

SetAttribute() void

Registers a vertex attribute on this geometry.

The attribute is stored in the internal attribute array and used by the renderer to interpret the vertex buffer layout. Multiple attributes can be configured as long as their combined stride matches the vertex data.

cpp
void Geometry::SetAttribute(const Geometry::VertexAttribute& attribute);
ParameterDescription
attributeVertex attribute descriptor to add or update.

Stride() size_t

Returns the vertex stride in number of floats.

The stride is computed from the active vertex attributes and describes how many floats form a single vertex in the interleaved buffer.

cpp
size_t Geometry::Stride() const;

VertexCount() size_t

Returns the number of vertices in the geometry.

The vertex count is derived from the size of the vertex buffer and the configured stride.

cpp
size_t Geometry::VertexCount() const;

VertexData() const std::vector<float> &

Returns a read-only reference to the vertex buffer.

The data is stored as a flat array of floats, typically interleaved according to the configured attributes.

cpp
const std::vector<float> & Geometry::VertexData() const;

Released under the MIT License.