Saturday, July 23, 2022

Lesson 08: Importing WaveFront OBJ Models

Overview 
As discussed in the  previous article, we saw how geometric objects can be imported from obj and mtl files. Also, the implementation of the WFObjMesh, WFOBJInfo and WFObj were covered. 
In this post we will try to render a model from "dolphin.obj" and "dolphin.mtl" files.

Details
The purpose of  Lesson08 is to demonstrate importing an obj and mtl files to extract geometry and material information.

System  class diagram
The scene class overrides camera with an instance of ThreeDCamera. It has a pointer to an instance of WFObj  called pshape derived from BaseGeometry class.

Implementation
Scene
The functionality is implemented in the Scene class derived from BaseScene class.
The three methods Init, DrawScene and Cleanup are overridden as below:
The Init method calls 
  1. BaseScene::Init to create hosting window and OpenGL Context. 
  2. It creates an input dialog to import obj, and mtl files and input texture files.
  3. It calls init function with an instance of  WFOBJInfo object. Internally it creates a WFObjMesh object to load geometry and material information from the object files and to populate VBO/EBO buffers and bind them. Also create texture if necessary 
  4. It also compiles and links shader programs.
The DrawScene method draws a string and image. 

The message handler for IDOK is called when Apply button is pressed after updates are made in the input dialog. It refreshes the display.

The message handler for IDCANCEL is called when in Cancel button is pressed the input dialog. It calls 
OnCloseWindow to destroy window.

Finally the WM_CLOSE  event is  handled in OnCloseWindow function and the window is destroyed and  application is shutdown when the window is closed or Escape key is pressed. 

Application
Scene class is hosted main.cpp. which creates the scene object and displays it. Message pump is added to process windows messages.

Input dialog
The input dialog as shown below can be used to change settings and understand drawing text and images.
Output
The output looks as shown in the top. 



No comments:

Post a Comment