class EE::Graphics::VertexBuffer

Overview

The vertex buffer class holds vertex data. The vertex position, colors, texture coordinates and indexes. This is useful to accelerate and encapsulate data. More…

#include <vertexbuffer.hpp>

class VertexBuffer {
public:
    // construction

    virtual ~VertexBuffer();

    // methods

    static VertexBuffer* New(const Uint32& vertexFlags = VERTEX_FLAGS_DEFAULT, PrimitiveType drawType = PRIMITIVE_QUADS, const Int32& reserveVertexSize = 0, const Int32& reserveIndexSize = 0, VertexBufferUsageType usageType = VertexBufferUsageType::Static);
    static VertexBuffer* NewVertexArray(const Uint32& vertexFlags = VERTEX_FLAGS_DEFAULT, PrimitiveType drawType = PRIMITIVE_QUADS, const Int32& reserveVertexSize = 0, const Int32& reserveIndexSize = 0, VertexBufferUsageType usageType = VertexBufferUsageType::Static);
    void addVertex(const Uint32& type, const Vector2f& vertex);
    void addVertex(const Vector2f& vertex);
    void addTextureCoord(const Vector2f& vertexCoord, const Uint32& textureLevel = 0);
    void addColor(const Color& color);
    void addIndex(const Uint32& indexValue);
    void setVertex(const Uint32& index, const Uint32& type, const Vector2f& vertex);
    void setVertex(const Uint32& index, const Vector2f& vertex);
    void setTextureCoord(const Uint32& index, const Vector2f& vertexCoord, const Uint32& textureLevel = 0);
    void setColor(const Uint32& index, const Color& color);
    void setIndex(const Uint32& index, const Uint32& indexValue);
    void resizeArray(const Uint32& type, const Uint32& size);
    void resizeIndices(const Uint32& size);
    void addQuad(const Vector2f& pos, const Sizef& size, const Color& color);
    void setQuad(const Vector2u& gridPos, const Vector2f& pos, const Sizef& size, const Color& color);
    void setQuadColor(const Vector2u& gridPos, const Color& color);
    void setQuadFree(const Vector2u& gridPos, const Vector2f& pos0, const Vector2f& pos1, const Vector2f& pos2, const Vector2f& pos3, const Color& color);
    void setQuadTexCoords(const Vector2u& gridPos, const Rectf& coords, const Uint32& textureLevel);
    void setGridSize(const Sizei& size);
    std::vector<Vector2f>& getPositionArray();
    std::vector<Color>& getColorArray();
    std::vector<Uint32>& getIndices();
    std::vector<Vector2f>& getTextureCoordArray(const Uint32& textureLevel);
    Uint32 getVertexCount();
    Uint32 getIndexCount();
    Color getColor(const Uint32& index);
    Uint32 getIndex(const Uint32& index);
    void setElementNum(Int32 num);
    const Int32& getElementNum() const;
    virtual void bind() = 0;
    virtual void unbind() = 0;
    virtual void draw() = 0;
    virtual bool compile() = 0;
    virtual void update(const Uint32& types, bool indices) = 0;
    virtual void reload() = 0;
    virtual void clear();
};

Detailed Documentation

The vertex buffer class holds vertex data. The vertex position, colors, texture coordinates and indexes. This is useful to accelerate and encapsulate data.

Some example usage of this class:

// Creates a rounded rectangle.
Polygon2f Poly = Polygon2f::createRoundedRectangle( 0, 0, 256, 50 );

VertexBuffer * VBO = VertexBuffer::New( VERTEX_FLAGS_PRIMITIVE, PRIMITIVE_TRIANGLE_FAN );

if ( NULL != VBO ) {
    // Upload the rounded rectangle data to the vertex buffer.
    for ( Uint32 i = 0; i < Poly.Size(); i++ ) {
        VBO->addVertex( Poly[i] );  // Adds the vertex position
        VBO->addColor( ColorA( 255, 255, 255, 255 ) ); // Adds the vertex color
    }

    // Compiles the buffered data
    VBO->compile();
}

// ... in the main loop draw the VBO
while ( win->isRunning() )
{
    ...

    VBO->bind();    // First must be binded.
    VBO->draw();    // Then rendered.
    VBO->unbind();  // Then unbinded to allow render other things.

    ...

    win->display()
}

Methods

static VertexBuffer* New(const Uint32& vertexFlags = VERTEX_FLAGS_DEFAULT, PrimitiveType drawType = PRIMITIVE_QUADS, const Int32& reserveVertexSize = 0, const Int32& reserveIndexSize = 0, VertexBufferUsageType usageType = VertexBufferUsageType::Static)

Creates a new Vertex Buffer.

Parameters:

vertexFlags

The vertex flags indicates which vertex data will be used.

drawType

The type of the primitive to draw.

reserveVertexSize

If the vertex size is known is possible to reserve the space in memory to avoid resizeing the array.

reserveIndexSize

If the vertex size is known is possible to reserve the space in memory for the indices to avoid resizeing the array.

usageType

This indicates the kind of usage VBO will have. It’s only useful if VBO extensions are supported ( almost for sure that it’s supported ). More information here: http://www.opengl.org/sdk/docs/man/xhtml/glBufferData.xml

See also:

VertexFlags

static VertexBuffer* NewVertexArray(const Uint32& vertexFlags = VERTEX_FLAGS_DEFAULT, PrimitiveType drawType = PRIMITIVE_QUADS, const Int32& reserveVertexSize = 0, const Int32& reserveIndexSize = 0, VertexBufferUsageType usageType = VertexBufferUsageType::Static)

Creates the simple vertex array implementation ( without VBOs or VAO ), which it’s faster for many cases.

void addVertex(const Uint32& type, const Vector2f& vertex)

Adds a vertex of the type indicated to the buffer.

Parameters:

type

Can be the position or texture coordinates.

vertex

The vexter data

void addVertex(const Vector2f& vertex)

Adds a vertex position to the buffer.

Parameters:

vertex

The vexter data

void addTextureCoord(const Vector2f& vertexCoord, const Uint32& textureLevel = 0)

Adds a vertex texture coordinate.

Parameters:

vertexCoord

The vertex texture coordinate.

textureLevel

Indicates the texture level if it’s using multitextures.

void addColor(const Color& color)

Adds a color to the buffer.

Parameters:

color

The color value.

void addIndex(const Uint32& indexValue)

Adds an index to the buffer.

Parameters:

indexValue

The index value.

void setVertex(const Uint32& index, const Uint32& type, const Vector2f& vertex)

Set a vertex index of the type indicated to the buffer.

Parameters:

index

The array index of the vertext to set

type

Can be the position or texture coordinates.

vertex

The vexter data

void setVertex(const Uint32& index, const Vector2f& vertex)

Adds a vertex position to the buffer.

Parameters:

index

The array index of the vertext to set

vertex

The vexter data

void setTextureCoord(const Uint32& index, const Vector2f& vertexCoord, const Uint32& textureLevel = 0)

Adds a vertex texture coordinate.

Parameters:

index

The array index of the vertext to set

vertexCoord

The vertex texture coordinate.

textureLevel

Indicates the texture level if it’s using multitextures.

void setColor(const Uint32& index, const Color& color)

Adds a color to the buffer.

Parameters:

index

The array index of the vertext to set

color

The color value.

void setIndex(const Uint32& index, const Uint32& indexValue)

Adds an index to the buffer.

Parameters:

index

The array index of the vertext to set

indexValue

The index value.

void resizeArray(const Uint32& type, const Uint32& size)

Resizes the array of the type indicated.

Parameters:

type

The type must be one of the vertex flags (

size

The size to be resized

See also:

VertexFlags).

void resizeIndices(const Uint32& size)

Resizes the indices array.

Parameters:

size

The new size

std::vector<Vector2f>& getPositionArray()

Returns:

The position array

std::vector<Color>& getColorArray()

Returns:

The color array pointer.

std::vector<Uint32>& getIndices()

Returns:

The indices array pointer.

std::vector<Vector2f>& getTextureCoordArray(const Uint32& textureLevel)

Returns:

The texture coord array from the texture level

Uint32 getVertexCount()

Returns:

The number of vertex added.

Uint32 getIndexCount()

Returns:

The number of indexes added.

Color getColor(const Uint32& index)

Parameters:

index

The position in the buffer.

Returns:

The color at the buffer position.

Uint32 getIndex(const Uint32& index)

Parameters:

index

The position in the buffer.

Returns:

The index at the buffer position.

void setElementNum(Int32 num)

Sets the number of elements to draw. If not set, it will draw all the elements.

const Int32& getElementNum() const

Returns:

The number of elements added.

virtual void bind() = 0

Activates the vertex buffer.

virtual void unbind() = 0

Deactivates the vertex buffer.

virtual void draw() = 0

Draw the vertex buffer.

virtual bool compile() = 0

Compile the vertex buffer. After adding all the vertex buffer data Compile() must be called to upload the data to the GPU.

virtual void update(const Uint32& types, bool indices) = 0

Update is used in the case of some data is modified and need to be reuploaded to the GPU.

virtual void reload() = 0

Reupload all the data to the GPU.

virtual void clear()

Clear the cached data and destroy the buffers