Write an OpenGL program to read and display a 3D mesh (in .obj format).
1. 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.
2. 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.
3. Implement the “Render_Mesh()” function to finish the OpenGL rendering. (See next page).
4. 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();