→This assignment is due by Tuesday, October 26, 2021 11:59 PM.←
→ As with all assignments, this must be an individual effort and cannot be pair programmed. Any debugging assistance must follow the course collaboration policy and be cited in the comment header block for the assignment.←
→ Do not forget to complete the following labs with this set:L5A,L5B,L5C←
→ Do not forget to complete theAPTfor this set.←
·Instructions·Rubric·Submission·
In Computer Graphics, it is very common to store a complex model in an external text file. The graphics program then reads and parses the text file to generate a rendered image of the model. Your task for this assignment is to read in a simplified Wavefront OBJ file and validate if it would correctly form a model.
Wavefront OBJ File Format
The actualWavefront OBJ file formatis quite complex, so we will be working with a simplified subset of its features. In the OBJ file, the first value on each line dictates what type of record the line is storing. The remaining format and interpretation of the line is determined by the first value. An example is below:
# this is a comment # this makes a triangle v 0.0 0.0 0.0 v 1.0 0.0 0.0 v 0.0 1.5 0.0 f 1 2 3
Our file can be made up of three different types of lines, outlined below:
COMMENT
A comment line begins with the character#
. A string then follows on the remainder of the line.
The generic form of the line is:# string
.
VERTEX
A vertex line begins with the characterv
. Three floating point values follow corresponding to the (x, y, z) location of the vertex in 3-Dimensional space.
The generic form of the line is:v x y z
.
FACE
A face line begins with the characterf
. Three integer values follow corresponding to the vertices to connect to form a triangle.
The generic form of the line is:f v1 v2 v3
.
The face lines work in a very specific manner. As the file is read, all of the vertices are stored in a list. The integer values specified for each face then correspond to the index within the vertex list to retrieve an individual vertex.IMPORTANT:the values provided for a face are 1-indexed. Meaning1
corresponds to the first indexed vertex,2
to the second, and so forth.
Part I: Read the File
Begin by creating astruct
to represent a single vertex. Name itVertex
and have it contain three floating point values. Then create astruct
to represent a face. Name itFace
and have it contain three integer values.
Next, ask the user for the name of the file they wish to open. Validate that the file opened successfully. If it opened successfully, it's now time to start reading the file's contents. Create three vectors to store the comments, vertices, and faces respectively (carefully consider the appropriate data type of each vector).
Read the first character of the line and act accordingly:
- If it is
#
, then read the remainder of the line in to a string and store it in your comments vector.
- If it is
v
, then read the next three floating point values, create a Vertex, and store it in your vertices vector.
- If it is
f
, then read the next three integer values, create a Face, and store it in your faces vector.
We can assume that the file will be well-formed. Meaning, the format of each line will match what we expect. There will not be any additional characters seen and there will not be any missing values on a line.
When done reading the file, print out a summary of how many comments, vertices, and faces were read in. An example is shown below:
Enter the name of the file to open: triangle.obj Read in 2 comments 3 vertices 1 faces
Two sample files to use for testing:cube.obj&triangle.obj.
Part II: Validate the Faces
Now we need to check each face to see if it is valid. A face is valid IFF:
- The face is made up of unique indices.
- Each index refers to a valid position within the vertex vector.
If ALL faces are valid, then print out the file is valid. If ANY faces are invalid, then print out that the file is invalid. Additionally, print which faces are invalid and why. An example is shown below:
Enter the name of the file to open: error.obj Read in 5 comments 3 vertices 4 faces Validating faces... Face 2 has duplicate indices Face 3 contains vertices out of range Face 4 has duplicate indices Face 4 contains vertices out of range File is invalid.
Enter the name of the file to open: triangle.obj Read in 2 comments 3 vertices 1 faces Validating faces... File is valid!
An additional sample files to use for testing:error.obj.
Part III: Inspect the File
If the file was invalid, then end the program. Otherwise, continually prompt the user with the following options:
Print comments: Print out all of the comments in the file
Print vertices: Print out the (x, y, z) coordinates of all vertices in the file
Print faces: For each vertex that makes up the face, print out the (x, y, z) coordinate
Quit: End the program
A full example is shown below:
Enter the name of the file to open: triangle.obj Read in 2 comments 3 vertices 1 faces Validating faces... File is valid! What do you wish to do? 1) Print comments 2) Print vertices 3) Print faces 4) Quit Choice: 1 Comment #1: this is a comment Comment #2: this makes a triangle What do you wish to do? 1) Print comments 2) Print vertices 3) Print faces 4) Quit Choice: 2 Vertex #1: (0, 0, 0) Vertex #2: (1, 0, 0) Vertex #3: (0, 1.5, 0) What do you wish to do? 1) Print comments 2) Print vertices 3) Print faces 4) Quit Choice: 3 Face #1:(0, 0, 0)(1, 0, 0)(0, 1.5, 0) What do you wish to do? 1) Print comments 2) Print vertices 3) Print faces 4) Quit Choice: 4 Goodbye!
In addition to the three provided public object files, your program will be tested against two additional private object files.