Saturday, July 23, 2022

Implementation: Importing WaveFront OBJ Models

Overview 
As discussed before, WaveFront OBJ and MTL files needs to be parsed to get geometry, Material information. A new mesh class and new geometry object are required for this.

Details
A new mesh class  WFObjMesh is defined for loading OBJ and MTL files. It's used by another new class  WFObj object for loading vertex data and texture data to the vertex and fragment shaders. Note that  a texture is not mandatory.

System  class diagram
WFObj derives from BaseGeometry class. It overrides mesh with an instance of WFObjMesh to generate geometry and material color information.
MaterialInfo
This class contains information about a single Material obtained after parsing a mtl file. This information includes  range of vertices, Ambient, Diffuse, Specular Colors and Texture information etc.  Note that a mtl file can have multiple Materials applicable to a range of vertices.
These are stored in WFObjMesh object discussed below.

Members
NameDescription
ambientclrContains ambient color
diffuseclrContains material information. It contains name of the material, mapped to the MatrialInfo structure.
diffusetxtfilenameContains material file name associated with the obj file
emissiveclrContains normals
rangeContains texture info
shininessContains vertex info
specularclrContains Specular color

WFObjMesh 
WFObjMesh derives from IGeometryMesh class. It parses OBJ file and MTL files to provide geometry  and material color information for rendering. It supplies vertices, texture coordinates, normals, Ambient, Diffuse, Specular Colors and Texture information.
Members
NameDescription
facesContains vertex/texture coordinate/Normal information for each of the vertices of the geometry triangle.
matinfomapContains material information. It contains name of the material, mapped to the MatrialInfo structure.
mtlfilenameContains material file name associated with the obj file
normalsContains normals
texturemapContains texture info
verticesContains vertex info

Constructor
Parses obj and mtl files to generate geometry and color information.

Methods
NameDescription
GenerateVerticesDataThis method generates vertex data for the GPU to render the geometrical shape. This method is called if the data is sent in  non indexed mode.
It generates position and vertex data.
normals_countThis method returns number of normals.
texture_countThis method returns number of texture coordinates.
vertex_countThis method returns number of vertices.
readfileThis method reads obj file and mtl file and stores contents in text member
parsegeometrydataThis method generates geometry data from the obj file
parsematerialdataThis method generates material data from the mtl file

Light
This class contains light properties such as ambient Coefficient, light position, and light color.
These are stored in WFObjInfo object discussed below.

Members
NameDescription
ambientCoefficientContains ambient Coefficient. This is multiplied with ambient color of the material.
PositionContains light position. Used in lighting calculations.
ColorContains light color. Used in lighting calculations.

WFOBJInfo
This class contains information about Geometry information and Material color information obtained after parsing .obj and .mtl file.
These are stored in WFObj object discussed below.

Constructors
Creates a fully populated WFOBJInfo from the input parameters.

Members
NameDescription
objfilenameContains name of the obj file
mtlfilenameContains name of the mtl file
viewerposContains texture info if a diffuse texture is available.
lightContains an instance of Light object describing properties of the lighting.
texturemapContains texture info
verticesContains vertex info

WFObj
WFObj derives from BaseGeometry class. It renders objects after parsing obj and mtl files.

Members
NameDescription
hastextureContains true if the object has a diffuse texture file
shapeinfContains an instance of WFOBJInfo
texutlContains an instance of TextureUtil containing texture information if available.

Methods
NameDescription
InitThis method overrides the base class Init method to loads the WFOBJInfo object. It parses obj and mtl files to generate geometric and material information. Generates the vertices data and material data consisting of surface normals in non indexed mode and later sets them up in VBO buffer.  It also sets up initializes texutl to load texture if available.
UpdateUniformsThis method overrides the base class UpdateUniforms method. First it updates  "cameraposition" uniform. It updates "tex" uniform to pass texture object. Also Light and Material properties are passed to the shaders.
vertexShaderSourceThis method overrides the base class vertexShaderSource method. It returns the vertex shader code to render the geometric object.
fragmentShaderSourceThis method overrides the base class fragmentShaderSource method. It returns the fragment shader code to render the  geometric object.
UpdateUniformsLightMatThis method is called by UpdateUniforms() to update light and material properties to the shader.

Output











 

No comments:

Post a Comment