Β
Write an OpenGL program to read and display a 3D mesh (in .obj format).
- Write a function, βbool ReadOBJFile(const char filename[])β, to read in an obj file and store the elements into a vertex list and face list.
- Implement a βComputeBoundingBox()β function, to compute the bounding box of the object, its diagonal axis length, and its center, then put the camera at:
x_cam = x_BCenter;Β y_cam = y_BCenter;
z_cam = z_BCenter + 1.5 *BDiagonalAxisLength;
and look towards the bounding box center (x_BCenter, y_BCenter, z_BCenter).
Some more explanation about this computation is given in the next page.
- Implement the βRender_Mesh()β function to finish the OpenGL rendering. (See next page).
- Follow βEx8.cppβ to perform keyboard + mouse-controlled rotation (R, mouse left button), panning (T, mouse left button), and zooming (Z, mouse left button) functions.
Note 1: Please write all you codes in one βhw1.cppβ file. Test and make sure it compiles and renders correctly, then upload your βhw1.cppβ file only. I will compile and run it on my computer to see your result.
Note 2: You only need to consider the simplest OBJ format in this homework.
It contains a list of vertices (each row starts with a keyword βvβ, e.g., v x y z), and a list of faces (each row starts with a keyword βfβ, e.g., f vInd1 vInd2 vInd3). Note that the vertex index vInd starts at 1. So if you have store vertex positions in an array, the first vertex is at position 0, and each vertexβs location is vInd-1.
The homework is due: 11:59pm, Feb. 10th. If you are late for x days, each one more day you get 5% off. Namely, your homework score will be calculated as: (the score based on your codes) * (0.95^x) Computing Bounding Box (BB) of a 3D object
A bounding box (BB) of an object is defined as a minimal coordinate-axis-aligned box that completely contains this object.
BB can be simply defined by the minimal and maximal x, y, and z coordinate
values of the objectsβ vertices:Β π΅π΅ = [π₯πππ, π₯πππ₯] Γ [π¦πππ, π¦πππ₯] Γ [π§πππ, π§πππ₯]
The center of a BB can be simply defined as the
The diagonal axis length of a BB roughly measures the size of the object:
π = β(π₯πππ₯ β π₯πππ)2 + (π¦πππ₯ β π¦πππ)2 + (π§πππ₯ β π§πππ)2
Rendering a Triangle Mesh
Each triangle mesh contains a list of faces. Each face π has three points (ππ, ππ, ππ) where each point ππ has three coordinates (π₯π, π¦π, π§π) .
Therefore, you can implement Render_Mesh() based on the following pseudo-code:
glBegin(GL_TRIANGLES);
//Let F denote the total number of faces
for (int ind=0; ind< F; ++ind) {Β Β Β Β Β Β let π be the πππ-th face;
get πβs three points ππ, ππ, ππ using their vertex indices;Β get ππβs coordinates (π₯π, π¦π, π§π); same for ππ and ππ
glVertex3f(π₯π, π¦π, π§π);Β Β glVertex3f(π₯π, π¦π, π§π); glVertex3f(π₯π, π¦π, π§π);
}
glEnd();



