Sunday, July 10, 2022

Implementation:Vertex Buffer

Overview
In this post we shall deep dive and understand mechanics behind sending vertex data to the GPU. Vertex data is obtained from mesh objects such as cubemesh which will be discussed later. The vertex data consists of Position, Color, Normal, and Texture Coordinate of each vertex. They are basically stored into VBO, EBO buffers. Later they are packaged into VAO objects.

Below are some of the OpenGL related terminologies used commonly when vertex data of 3D objects is passed to the pipeline especially vertex shader to render them on screen. Don't get bogged down with its details. It will become clear as we go.

Vertex Buffer Object(VBO)
A vertex buffer object (VBO) is an OpenGL feature that provides methods for uploading vertex data (Position, Color, Normal, Texture Coordinates). A single VBO can store one or more of these data. However in this implementation one buffer is used for one type of data.
 
Element Buffer Object(EBO)
To optimize data transfer, EBO stores indices of the vertices specifically, and OpenGL calls the indices of these vertices to determine which vertices should be drawn.

Vertex Array Object(VAO)
VAO can be considered as a collection of EBO and VBOs. VAO store buffer names for each VBO and EBO, as well as how the vertices are laid out in the vertex buffers. The vertex id associated with VAO maps to location defined in the vertex shader.

The following diagram depicts the relationships between VAO and VBO.


Details

VertexData
The VertexData class is template based and it stores VBO and EBO data and manages their lifetime in the Applications memory space. 

The details are described below.

Constructor
Initializes the gl enumeration type and its size.

Members
NameDescription
id
Identifies the unique id assigned to the buffer.
data
Contains raw data as vector.
valtype
valsize
valtype gl enumeration of the datype.
valsize size of the individual element.

Methods
NameDescription
add
Adds a new element to the vector data
itemsizeReturns the size of template type T or the element type stored in the vector data
datasize
Returns the total size of all the elements in the vector data
cleanup
Releases opengl buffers.
bindEBO
Binds EBO buffers
bindEnableVBO
Binds VBO buffers and enables them.

VAOUtil 
VAOUtil class defines a  VAO and its associated VBOs and EBO discussed in the previous post. 
The VertexData class is template based and it stores VBO and EBO data and manages their lifetime. VAOUtil class defines VertexData class for PositionColorNormalTexture Coordinates.

Members
NameDescription
id
Identifies the unique id assigned to the VAO object.
positions
colors
normals
texcrds
Contains vertex data for VBO buffers
indices
Contains positional data for EBO buffers

Methods
NameDescription
AddColor
Adds a new element to the vector colors
AddPosition
Adds a new element to the vector positions
AddNormal
Adds a new element to the vector normals
AddTexCord
Adds a new element to the vector texcrds
AddIndex
Adds a new element to the vector indices
bindVAO
Binds VAO for drawing
unbindVAOUnbinds VAO after drawing
cleanup
Releases all Vertex Arrays and clears VBO and EBO data.
SetupEBO
Binds EBO buffers
SetupVBO
Binds VBO buffers and enables them.



No comments:

Post a Comment