Infinity Engine v0.6.20
C++ API Documentation
Loading...
Searching...
No Matches
Infinity::Types::Rendering::IndexBuffer Class Reference

Container for mesh index data defining vertex connectivity and topology. More...

#include <IndexBuffer.hpp>

Inheritance diagram for Infinity::Types::Rendering::IndexBuffer:
[legend]
Collaboration diagram for Infinity::Types::Rendering::IndexBuffer:
[legend]

Public Types

enum class  IndexType { UInt16 , UInt32 }
 Index data format determining maximum vertex count. More...
 
enum class  Topology { Triangles , Lines , TriangleStrip , Quads }
 Primitive topology defining how indices form geometry. More...
 

Public Member Functions

 IndexBuffer ()
 Default constructor.
 
 IndexBuffer (const size_t size, IndexType type=IndexType::UInt16, Topology topology=Topology::Triangles)
 Constructs an index buffer with pre-allocated capacity.
 
 IndexBuffer (const Containers::Array< uint16_t > &indices, Topology topology=Topology::Triangles)
 Constructs an index buffer from a 16-bit index array.
 
 IndexBuffer (const Containers::Array< uint32_t > &indices, Topology topology=Topology::Triangles)
 Constructs an index buffer from a 32-bit index array.
 
virtual ~IndexBuffer ()
 Destructor.
 
std::unique_ptr< Core::Baseclone () const override
 
void reserve (size_t capacity)
 Pre-allocates storage for a specified number of indices.
 
void clear ()
 Removes all indices from the buffer.
 
uint32_t operator[] (size_t index) const
 Accesses an index value (unchecked).
 
uint32_t at (size_t index) const
 Accesses an index value (checked).
 
void setIndex (size_t index, uint32_t value)
 Sets the value of an existing index.
 
void pushIndex (uint16_t index)
 Appends a single 16-bit index.
 
void pushIndex (uint32_t index)
 Appends a single 32-bit index.
 
void pushTriangle (uint16_t v0, uint16_t v1, uint16_t v2)
 Appends three 16-bit indices forming a triangle.
 
void pushTriangle (uint32_t v0, uint32_t v1, uint32_t v2)
 Appends three 32-bit indices forming a triangle.
 
void pushQuad (uint16_t v0, uint16_t v1, uint16_t v2, uint16_t v3)
 Appends indices for a quad (as two triangles).
 
void pushQuad (uint32_t v0, uint32_t v1, uint32_t v2, uint32_t v3)
 Appends indices for a quad (as two triangles).
 
void pushLine (uint16_t v0, uint16_t v1)
 Appends two 16-bit indices forming a line segment.
 
void pushLine (uint32_t v0, uint32_t v1)
 Appends two 32-bit indices forming a line segment.
 
void pushIndices (const Containers::Array< uint16_t > &indices)
 Appends multiple 16-bit indices from an array.
 
void pushIndices (const Containers::Array< uint32_t > &indices)
 Appends multiple 32-bit indices from an array.
 
Core::span< const uint16_t > asUInt16Buffer () const
 Gets a const span view of the data as 16-bit indices.
 
Core::span< uint16_t > asUInt16Buffer ()
 Gets a mutable span view of the data as 16-bit indices.
 
Core::span< const uint32_t > asUInt32Buffer () const
 Gets a const span view of the data as 32-bit indices.
 
Core::span< uint32_t > asUInt32Buffer ()
 Gets a mutable span view of the data as 32-bit indices.
 
size_t count () const
 Gets the number of indices in the buffer.
 
const Infinity::Types::TypeIDtypeId () const override
 Gets the TypeID for IndexBuffer.
 
- Public Member Functions inherited from Infinity::Types::Core::Data
 Data ()
 
 Data (const Data &other)
 
 Data (Data &&other)
 
Dataoperator= (const Data &other)
 
Dataoperator= (Data &&other)
 
virtual ~Data ()
 Virtual destructor.
 
bool hasProperty (const std::string &key) const
 
template<typename T >
void setProperty (const std::string &key, T &&value)
 Sets a typed property value.
 
template<typename T >
void setProperty (const std::string &key, const T &value)
 Sets a typed property value.
 
void setProperty (const std::string &key, PropertyValue &&value)
 Sets a typed property value.
 
const PropertyValuegetProperty (const std::string &key) const
 Gets a property value without type checking.
 
template<typename T >
const T & getProperty (const std::string &key) const
 Gets a typed property value.
 
void removeProperty (const std::string &key)
 
- Public Member Functions inherited from Infinity::Types::Core::Base
virtual ~Base ()
 Virtual destructor.
 
virtual std::istream & legibleDataRead (std::istream &in) override
 Deserializes the object from a single-line text representation.
 
virtual std::ostream & legibleDataWrite (std::ostream &out) const override
 Serializes the object to a single-line text representation.
 

Public Attributes

IndexType type = IndexType::UInt16
 Index data format.
 
Topology topology = Topology::Triangles
 Primitive topology.
 
Containers::Array< uint8_t > data
 Raw index data storage.
 
- Public Attributes inherited from Infinity::Types::Core::Data
std::unordered_map< std::string, PropertyValueproperties
 Property storage for arbitrary metadata.
 

Detailed Description

Container for mesh index data defining vertex connectivity and topology.

IndexBuffer stores indices that define how vertices from a VertexBuffer are connected to form geometric primitives (triangles, lines, etc.). It provides efficient mesh representation by allowing vertices to be reused across multiple primitives, reducing memory usage and improving rendering performance.

The index buffer supports:

  • Two index formats: 16-bit (up to 65,535 vertices) or 32-bit (billions of vertices)
  • Multiple topologies: Triangles, Lines, TriangleStrip, Quads
  • Convenient primitive methods: pushTriangle(), pushQuad(), pushLine()
  • Dynamic construction: Build indices incrementally during mesh generation
  • Type-safe access: Automatic handling of 16-bit vs 32-bit indices

Index ordering determines face winding and visibility. Counter-clockwise (CCW) winding is the default front-facing convention in most rendering systems.

Common use cases:

  • Procedural mesh generation (terrain, shapes, buildings)
  • Mesh optimization and vertex sharing
  • LOD (Level of Detail) mesh construction
  • Dynamic mesh modification
  • Wireframe and debug visualization
Note
Use UInt16 for meshes with < 65,536 vertices (more memory efficient).
Use UInt32 for large meshes exceeding 65,535 vertices.
Triangle topology is most common for solid geometry rendering.
All index values must be valid vertex indices in the associated VertexBuffer.

Example usage:

// Create index buffer for a quad (2 triangles)
IndexBuffer indices;
indices.pushTriangle(0, 1, 2); // First triangle (CCW)
indices.pushTriangle(0, 2, 3); // Second triangle (CCW)
// Or use quad helper
IndexBuffer quadIndices;
quadIndices.pushQuad(0, 1, 2, 3); // Automatically creates 2 triangles
// Terrain mesh with grid topology
for (uint32_t z = 0; z < height - 1; ++z) {
for (uint32_t x = 0; x < width - 1; ++x) {
uint32_t topLeft = z * width + x;
uint32_t topRight = topLeft + 1;
uint32_t bottomLeft = (z + 1) * width + x;
uint32_t bottomRight = bottomLeft + 1;
// Two triangles per quad
terrainIndices.pushTriangle(topLeft, bottomLeft, topRight);
terrainIndices.pushTriangle(topRight, bottomLeft, bottomRight);
}
}
// Wireframe rendering (lines topology)
for (size_t i = 0; i < triangleCount; ++i) {
uint16_t v0 = triangles[i * 3 + 0];
uint16_t v1 = triangles[i * 3 + 1];
uint16_t v2 = triangles[i * 3 + 2];
wireframe.pushLine(v0, v1);
wireframe.pushLine(v1, v2);
wireframe.pushLine(v2, v0);
}
// Pre-allocate for known size
size_t triangleCount = 1000;
IndexBuffer optimized;
optimized.reserve(triangleCount * 3); // 3 indices per triangle
// Use with ProceduralSystem
system->setIn("mesh_indices", std::move(indices));
Container for mesh index data defining vertex connectivity and topology.
Definition IndexBuffer.hpp:89
@ Lines
Each 2 indices form an independent line segment.
void pushTriangle(uint16_t v0, uint16_t v1, uint16_t v2)
Appends three 16-bit indices forming a triangle.
void pushQuad(uint16_t v0, uint16_t v1, uint16_t v2, uint16_t v3)
Appends indices for a quad (as two triangles).
@ UInt16
16-bit indices (0-65,535 vertices, more memory efficient)
@ UInt32
32-bit indices (0-4,294,967,295 vertices, for large meshes)

Member Enumeration Documentation

◆ IndexType

Index data format determining maximum vertex count.

Enumerator
UInt16 

16-bit indices (0-65,535 vertices, more memory efficient)

UInt32 

32-bit indices (0-4,294,967,295 vertices, for large meshes)

◆ Topology

Primitive topology defining how indices form geometry.

Enumerator
Triangles 

Each 3 indices form an independent triangle (most common)

Lines 

Each 2 indices form an independent line segment.

TriangleStrip 

Connected triangles sharing edges (memory efficient)

Quads 

Each 4 indices form a quadrilateral (converted to 2 triangles)

Constructor & Destructor Documentation

◆ IndexBuffer() [1/4]

Infinity::Types::Rendering::IndexBuffer::IndexBuffer ( )

Default constructor.

Creates an empty index buffer with 16-bit indices and triangle topology.

◆ IndexBuffer() [2/4]

Infinity::Types::Rendering::IndexBuffer::IndexBuffer ( const size_t  size,
IndexType  type = IndexType::UInt16,
Topology  topology = Topology::Triangles 
)

Constructs an index buffer with pre-allocated capacity.

Parameters
sizeNumber of indices to pre-allocate space for.
typeIndex format (UInt16 or UInt32).
topologyPrimitive topology.

◆ IndexBuffer() [3/4]

Infinity::Types::Rendering::IndexBuffer::IndexBuffer ( const Containers::Array< uint16_t > &  indices,
Topology  topology = Topology::Triangles 
)

Constructs an index buffer from a 16-bit index array.

Parameters
indicesArray of 16-bit indices to copy.
topologyPrimitive topology.

◆ IndexBuffer() [4/4]

Infinity::Types::Rendering::IndexBuffer::IndexBuffer ( const Containers::Array< uint32_t > &  indices,
Topology  topology = Topology::Triangles 
)

Constructs an index buffer from a 32-bit index array.

Parameters
indicesArray of 32-bit indices to copy.
topologyPrimitive topology.

◆ ~IndexBuffer()

virtual Infinity::Types::Rendering::IndexBuffer::~IndexBuffer ( )
virtual

Destructor.

Member Function Documentation

◆ asUInt16Buffer() [1/2]

Core::span< uint16_t > Infinity::Types::Rendering::IndexBuffer::asUInt16Buffer ( )

Gets a mutable span view of the data as 16-bit indices.

Returns
Mutable span of uint16_t indices.
Warning
Only valid when type is UInt16.

◆ asUInt16Buffer() [2/2]

Core::span< const uint16_t > Infinity::Types::Rendering::IndexBuffer::asUInt16Buffer ( ) const

Gets a const span view of the data as 16-bit indices.

Returns
Const span of uint16_t indices.
Warning
Only valid when type is UInt16.

◆ asUInt32Buffer() [1/2]

Core::span< uint32_t > Infinity::Types::Rendering::IndexBuffer::asUInt32Buffer ( )

Gets a mutable span view of the data as 32-bit indices.

Returns
Mutable span of uint32_t indices.
Warning
Only valid when type is UInt32.

◆ asUInt32Buffer() [2/2]

Core::span< const uint32_t > Infinity::Types::Rendering::IndexBuffer::asUInt32Buffer ( ) const

Gets a const span view of the data as 32-bit indices.

Returns
Const span of uint32_t indices.
Warning
Only valid when type is UInt32.

◆ at()

uint32_t Infinity::Types::Rendering::IndexBuffer::at ( size_t  index) const

Accesses an index value (checked).

Parameters
indexPosition of the index to retrieve.
Returns
Index value as uint32_t (automatically converted from storage format).
Note
Currently doesn't perform bounds checking despite the name.

◆ clear()

void Infinity::Types::Rendering::IndexBuffer::clear ( )

Removes all indices from the buffer.

Resets the buffer to empty state while preserving type and topology.

◆ clone()

std::unique_ptr< Core::Base > Infinity::Types::Rendering::IndexBuffer::clone ( ) const
overridevirtual

Reimplemented from Infinity::Types::Core::Data.

◆ count()

size_t Infinity::Types::Rendering::IndexBuffer::count ( ) const

Gets the number of indices in the buffer.

Returns
Total count of indices.
Note
For Triangles topology: count / 3 = triangle count.
For Lines topology: count / 2 = line segment count.

◆ operator[]()

uint32_t Infinity::Types::Rendering::IndexBuffer::operator[] ( size_t  index) const

Accesses an index value (unchecked).

Parameters
indexPosition of the index to retrieve.
Returns
Index value as uint32_t (automatically converted from storage format).
Warning
No bounds checking. Use at() for checked access.

◆ pushIndex() [1/2]

void Infinity::Types::Rendering::IndexBuffer::pushIndex ( uint16_t  index)

Appends a single 16-bit index.

Parameters
indexIndex value to append.
Note
Only valid when type is UInt16.

◆ pushIndex() [2/2]

void Infinity::Types::Rendering::IndexBuffer::pushIndex ( uint32_t  index)

Appends a single 32-bit index.

Parameters
indexIndex value to append.
Note
Only valid when type is UInt32.

◆ pushIndices() [1/2]

void Infinity::Types::Rendering::IndexBuffer::pushIndices ( const Containers::Array< uint16_t > &  indices)

Appends multiple 16-bit indices from an array.

Parameters
indicesArray of indices to append.

◆ pushIndices() [2/2]

void Infinity::Types::Rendering::IndexBuffer::pushIndices ( const Containers::Array< uint32_t > &  indices)

Appends multiple 32-bit indices from an array.

Parameters
indicesArray of indices to append.

◆ pushLine() [1/2]

void Infinity::Types::Rendering::IndexBuffer::pushLine ( uint16_t  v0,
uint16_t  v1 
)

Appends two 16-bit indices forming a line segment.

Parameters
v0Start vertex index.
v1End vertex index.
Note
Typically used with Lines topology.

◆ pushLine() [2/2]

void Infinity::Types::Rendering::IndexBuffer::pushLine ( uint32_t  v0,
uint32_t  v1 
)

Appends two 32-bit indices forming a line segment.

Parameters
v0Start vertex index.
v1End vertex index.

◆ pushQuad() [1/2]

void Infinity::Types::Rendering::IndexBuffer::pushQuad ( uint16_t  v0,
uint16_t  v1,
uint16_t  v2,
uint16_t  v3 
)

Appends indices for a quad (as two triangles).

Automatically generates two triangles with proper winding:

  • Triangle 1: v0, v1, v2
  • Triangle 2: v0, v2, v3
Parameters
v0First corner (e.g., bottom-left).
v1Second corner (e.g., bottom-right).
v2Third corner (e.g., top-right).
v3Fourth corner (e.g., top-left).

◆ pushQuad() [2/2]

void Infinity::Types::Rendering::IndexBuffer::pushQuad ( uint32_t  v0,
uint32_t  v1,
uint32_t  v2,
uint32_t  v3 
)

Appends indices for a quad (as two triangles).

Parameters
v0First corner.
v1Second corner.
v2Third corner.
v3Fourth corner.

◆ pushTriangle() [1/2]

void Infinity::Types::Rendering::IndexBuffer::pushTriangle ( uint16_t  v0,
uint16_t  v1,
uint16_t  v2 
)

Appends three 16-bit indices forming a triangle.

Parameters
v0First vertex index.
v1Second vertex index.
v2Third vertex index.
Note
Order determines face winding (CCW = front-facing).

◆ pushTriangle() [2/2]

void Infinity::Types::Rendering::IndexBuffer::pushTriangle ( uint32_t  v0,
uint32_t  v1,
uint32_t  v2 
)

Appends three 32-bit indices forming a triangle.

Parameters
v0First vertex index.
v1Second vertex index.
v2Third vertex index.
Note
Order determines face winding (CCW = front-facing).

◆ reserve()

void Infinity::Types::Rendering::IndexBuffer::reserve ( size_t  capacity)

Pre-allocates storage for a specified number of indices.

Reserves memory without changing the index count, avoiding reallocations during subsequent pushes.

Parameters
capacityNumber of indices to reserve space for.

◆ setIndex()

void Infinity::Types::Rendering::IndexBuffer::setIndex ( size_t  index,
uint32_t  value 
)

Sets the value of an existing index.

Parameters
indexPosition of the index to modify.
valueNew index value to set.

◆ typeId()

const Infinity::Types::TypeID & Infinity::Types::Rendering::IndexBuffer::typeId ( ) const
overridevirtual

Gets the TypeID for IndexBuffer.

Returns
TypeID identifying this IndexBuffer type.

Reimplemented from Infinity::Types::Core::Data.

Member Data Documentation

◆ data

Containers::Array<uint8_t> Infinity::Types::Rendering::IndexBuffer::data

Raw index data storage.

Internal byte array storing index values in the format specified by type. Use the convenience methods (pushTriangle, pushIndex, etc.) rather than manipulating this directly.

◆ topology

Topology Infinity::Types::Rendering::IndexBuffer::topology = Topology::Triangles

Primitive topology.

Defines how indices are interpreted to form geometric primitives. Default: Triangles

◆ type

IndexType Infinity::Types::Rendering::IndexBuffer::type = IndexType::UInt16

Index data format.

Determines whether indices are stored as 16-bit or 32-bit values. Default: UInt16