Saturday, July 16, 2022

Implementation: LightedTexCube

Overview 
In previous examples, we saw that Position, Color, Texture data were sent to draw cubes. In this post we  will discuss how to implement  Phong lighting models and other lights such as Directional, Point and Spot Lights. For this surface normals needs to be sent as VBO data. They look as shown at the bottom. They look elongated because aspect ratio is not applied.

Details
System  class diagram
LightedTexCube derives from TexturedCube class. It overrides mesh with an instance of CubeMesh to generate geometry. 
LightedTexCube
LightedTexCube is derived from TexturedCube class. It illuminates the textured cube as per settings done in the light parameter. Utility class gLightingUtil is used for lighting related settings.

Members
NameDescription
lightAn instance of LightingUtil. It contains settings for illumination.

Methods
NameDescription
InitThis method overrides the base class Init method to load texture.  Generates the vertices data consisting of surface normals in non indexed mode and later sets them up in VBO buffer.  It also passes an input to use BlinnPhong or Phong illumination and also type of light source as defined by LightSource enum.
UpdateUniformsThis method overrides the base class UpdateUniforms method. Firs t it updates  "cameraposition" uniform. Next it updates lighting related uniforms by calling Updateshader method on light. Finally it c alls base class UpdateUniforms method to send rest.
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 based on light source type.


MaterialInfo
This class contains information about a single Material obtained after parsing a mtl file. This information includes  Ambient, Diffuse, Specular Colors and Texture information etc.  Note that a mtl file can have multiple Materials applicable to a range of meshes.
These are stored in LightingUtil object discussed below.
Members
NameDescription
nameName of the material.
ambientColorContains ambient color
diffuseColorContains Diffuse color
specularColorContains Specular color
ShininessContains shininess value

LightSrcInfo
This class contains information about the light source and viewer position for lighting calculations.
These are stored in GenericObj object discussed below. 
Members
NameDescription
srcContains light source type information. It has to be one of LightSourceType
nameName of the light source.
ambientCoefficient
ambientColor
Contains Ambient Coefficient and color
diffuseCoefficient
diffuseColor
Contains Diffuse Coefficient and color
specularColor
specularCoefficient
Contains Specular Coefficient and color
blinnContains boolean value to indicate to render blinn-phong(true) or phong(false)  lighting.
positionContains light position  information required for basic, point and spot lighting.
directionContains direction information required for directional lighting.
attconstant
attlinear
attquadratic
Contains light attenuation information required for point and spot lighting.
spotlightinner
spotlightouter
Contains light angle information required for spot lighting.

LightingUtil
It defines illumination properties of the light source and reflection properties of the material in the light and material members.
Members
NameDescription
materialAn instance of Material class. It contains settings of the material property for reflection.
lightAn instance of LightSrcInfo class. It contains settings for illumination.

Methods
NameDescription
UpdateShaderUpdates the uniforms that contains copies of the information stored in light and material members.

Output
The output looks as shown below.

No comments:

Post a Comment