Wednesday, July 13, 2022

Implementation:Indexed Cube with interpolated colors

Overview 
In SingleColoredCube we saw that VBOs were used to draw the single colored cube. In this post we  will discuss how to draw an IndexedCube using VBOs and EBO with interpolated colors. It looks as shown at the bottom. It looks elongated because aspect ratio is not applied.

Details
Drawing a cube with VBO requires binding VBO of 12 vertices containing position and optionally normal vector, color, Texture coordinate data. Whereas an Indexed draw requires binding VBO of 8 vertices containing position, normal vector, color, Texture coordinate data and bind an EBO containing 12 indices.

To draw a VBO cube glDrawArrays API is used where as for EBO or Indexed, glDrawElements is used. Here the EBO contains indices data.

Color interpolation means a triangle's fragment's color is computed by interpolating colors of the vertices.

System  class diagram
Every 3D object such as IndexedCube  derives from BaseGeometry class. It overrides mesh with an instance of CubeMesh to generate geometry.


IndexedCube
IndexedCube is derived from BaseGeometry class. It assembles vertex data generated by mesh in VBO and EBO buffers. Later  sent to GPU using shader programs.

Methods
NameDescription
InitThis method overrides the base class Init method. It creates the mesh object. Generates the vertices data consisting of Position and color in indexed mode. and later sets them up in VBO buffers. It also sets up Indices data in EBO buffer. Both are sent to the GPU.
vertexShaderSourceThis method overrides the base class vertexShaderSource method. It returns the vertex shader code to render the cube object.
fragmentShaderSourceThis method overrides the base class fragmentShaderSource method. It returns the fragment shader code to render the cube object.

Output

No comments:

Post a Comment