Monday, July 11, 2022

Implementation: Pipeline

Overview
In this post we shall deep dive and understand mechanics behind rendering 3D objects on the screen.
So far we discussed  FrameBuffers,Vertex Array Object containing VBO and EBO buffers and  Program Object containing Vertex Shader and Fragment Shaders.

The following diagram combines all the above and represents it,
Details
 Every 3D object such as cube derives from BaseGeometry class. The following describes this in details.


BaseGeometry
BaseGeometry class implements base class for all the 3D objects. All the 3D objects such as cube must derive from this class.  

Members
NameDescription
bindexed
Indexed if the data is VBO or EBO. True if EBO.
kount
Number of elements in VBO or EBO. It's populated by derived classes.
mesh
Geometry provider. Supplies the vertices and other data.
ProjectionMatrix
Contains project matrix computed based on mouse and keyboard inputs.
MM
Contains 3D angles and other info based on keyboard/Mouse inputs.
ViewMatrix
Contains view matrix computed based on mouse and keyboard inputs.
shader
Contains instances of vertex shader and fragment shaders.
vaoutil
Contains vertex data containing VBO and EBO

Methods
NameDescription
Cleanup Releases resources associated with VAO, EBO, shader programs and others. This is overridden by derived classes.
DrawCalls  OpenGL APIs such as glDrawArrays or glDrawElements are used to draw the 3D objects from VAO buffers using shaders.
InitLoads and attach shaders to program object.  This is overridden by derived classes. This is shown in the diagram below.
UpdateUniformsUpdates uniform transform containing Transformation Matrix  This is overridden by derived classes,
GetTransformationMatrixReturns transformation matrix after combining with view and model matrix.
vertexShaderSourceReturns vertex shader string. This is overridden by derived classes.
fragmentShaderSourceReturns fragment shader string. This is overridden  by derived classes.


Operations
The following describes the key operations that happens during the lifetime of a scene object.

Init
In a typical usage scenario, a 3D object first loads all the vertex data such as position by calling  GenerateVerticesData method of the mesh. Then it calls SetupVBO method for all the vertex data with location number of the the vertex data. Note that the location number has to match with the location number defined in the vertex shader. First time, this will generate a VAO and bind it. VBO is created, data is loaded for GL_STATIC_DRAW, bound and associated with the location and the location is enabled.

In cases of indices, the indices are loaded by calling  GenerateIndicesData method. Later BindEBO method is called. This will  generate a EBO, data is loaded for GL_STATIC_DRAW, bound.

Next  the shader code is attached and linked. 

DrawScene

At the time of drawing, uniforms are updated and program is set for use. Draw method is called to draw the 3D object. finally program is reset for use. 
In DrawTriangles function, to draw a VBO cube glDrawArrays API is used where as for EBO or Indexed, glDrawElements is used. Here the EBO contains indices data.



Cleanup
Releases the resources.


In the next post we will draw a single colored cube.


No comments:

Post a Comment