Computer Graphics

Notice

PLEASE note that you are supposed to do the lab work on your own. We know that it is nice, and beneficial to work in a group/pair/etc to understand what needs to be done: this is fine! BUT you must also sit at your own terminal and write the code by yourself.

Remember, the coursework is INDIVIDUALLY marked. If a group of people submit the same coursework (we can tell), then we will give all members of the group ( the mark / number of members ).

Lab Exercise 2: Faces and Objects

You should already have a working class that defines 3D points, ie, the Point3D class of Exercise 1. This week, construct another two classs: (1) Face, for storing indices to vertices in a face and the colour of th face (using java.awt.Color); (2) GObjects, for linking vertices and faces together, and read data from a text file.

Face.java

import java.awt.Color;

public class Face
{
  public int[] index; /* list of indices of vertices of the face */
 
  public Color color;

  public Face(int[] i, Color c){}

  public String toString(){/* Make it look nice to save your debugging time! */}
}

GObject.java

import java.util.Scanner;
import java.io.*;
import java.awt.Color;

public class GObject
{
  public Point3D[] vertex; /* list of vertices of the model */
 
  public Face[] face; /* list of faces of the model */
  
  public GObject(Point3D[] v, Face[] f){}

  public GObject(String fileName){}

  public String toString(){/* Make it look nice to save your debugging time! */}
}


Loading objects from file

You now need to write a new constructor for GObject which extends the present constructor to read in data from a file according to the specified format below.

public GObject(String fileName){};
/*Another constructor which reads in the object data from file "fileName"; */

The format is decribed here for a simple object. We are interested in loading a cube and a pyramid.

cube.dat

8                               number of vertices
1.0 0.0 1.0                     vertex 0
2.0 0.0 1.0                     vertex 1
2.0 0.0 0.0                     vertex 2
1.0 0.0 0.0                     vertex 3
1.0 1.0 1.0                     vertex 4
2.0 1.0 1.0                     vertex 5
2.0 1.0 0.0                     vertex 6
1.0 1.0 0.0                     vertex 7

6                               number of faces
4                               number of vertices in face 0
0 1 5 4                 vertices in face 0
0.5 0.5 0.0                     colour of face
4
1 2 6 5
0.5 0.0 0.0                     colour of face
4
3 7 6 2
0.5 0.0 0.5                     colour of face
4                               
0 4 7 3
0.0 0.5 0.5                     colour of face                  
4
0 3 2 1
0.5 0.5 0.0                     colour of face
4
4 5 6 7
1.0 0.0 0.0                     colour of face

pyramid.dat

5                               number of vertices
-2.0 0.0 1.0                    vertex 0
-1.0 0.0 1.0                    vertex 1
-1.0 0.0 0.0                    vertex 2
-2.0 0.0 0.0                    vertex 3
-1.5 2.0 0.5                    vertex 4

5                               number of faces
3                               number of vertices in face 0
0 1 4                           vertices in face 0
0.5 0.5 0.0                     colour of face
3
1 2 4
0.5 0.0 0.0                     colour of face
3
3 4 2
0.5 0.0 0.5                     colour of face
3                               
0 4 3
0.0 0.5 0.5                     colour of face                  
4
0 3 2 1
0.5 0.5 0.0                     colour of face