Mathematics for Computer Graphics - Lecture 8 Dr. Philippe B. Laval Kennesaw State University September 22, 2003 Abstract This document is about our rst project: the creation of a 3D vector class. It gives the speci cations for this class. It also gives some programming techniques students should follow. 1 Developing a 3D Vector Class Recall that one of our goals in this class is to develop a small software package which will illustrate some of the techniques involved with 3D graphics. We will build this package in stages. The smallest building block of this package is 3D vectors. We begin by implementing them. 1.1 Goal The goal of this rst project is to develop a 3D vector class. We will also develop a test program to test our class. Though the nal software package will be implemented as an applet, this rst project can be implemented as a java application. See below for the di erence between the two. 1.2 Programming Techniques to Follow 1.2.1 Java Applets versus Java Applications Usually, when a program written in most programming languages such as C, C++, pascal, Fortran,... is compiled, an executable is produced. The executable code is written in machine dependent code a particular machine can understand. To run the program, one then either types its name, or double clicks on it. Java is an interpreted language. When a java program is compiled, a class le is produced. A class le is written in machine independent code. A class le cannot be run by itself. It can only be run inside the java runtime environment. Each machine type has its own version of the java runtime environment. They all can run the same class le. A java class le can be run in one of two ways. The method used will be determined by whether we have a java applet or a java application. 1
A java applet runs inside a web browser. When a java applet is developed, one must also write an html le which will call the applet. Many java IDE s do this for you. For this to work, the web browser must implement the Java Virtual machine. This is the case for many browsers including Netscape. For Internet Explorer, the situation is more delicate. Microsoft is no longer shipping the Java Virtual Machine (Java VM). It can be downloaded from http://java.sun.com. In contrast, a java application runs alone, like any other program. Well, almost like any other program. Since it has to be run inside the java runtime environment, the java runtime environment is invoked, with the class le to run as a parameter. For example, to run a class le named myprogram.class, one would type java myprogram. There are also other di erences between java applets and applications. Here are a few outlined here. An important one deals with security. Since a java applet will be more than likely run across the internet, its access to the local le system is highly restricted. A java application must have a method called main, a java applet does not. 1.2.2 Good Programming Techniques Here is a list of programming tips each student should follow: A Java application or applet is usually made of several classes and a main class (the main class must have the same name as the program you are creating. If you create a program called test1, the main class will be called test1). The various classes implement the functionality of the program. The main class controls its ow. Use a di erent le for each class. At the beginning of every le, put comments explaining what the class does, how it does it. When declaring a variable, put comments indicating what this variable will be used for. When de ning a method, put comments indicating what the methods will do, what its parameters are, what the method will return. Insert plenty of comments in your code explaining what you are doing, how you are doing it. 1.3 Tools to Use If you are going to work from the computers on campus, you do not have any choice. You must use what is installed on them. JGrasp is the IDE installed on them. If you are going to work from your own computer, you rst must have the following: 2
1. Java Software Development Kit (JSDK). This can be downloaded from Sun Microsystems at java.sun.com. The current latest version (J2SE 1.4.2 SDK). This contains all the runtime libraries needed as well as an IDE called NetBeans. This IDE has a lot of functionality, including a GUI builder. 2. A java IDE. You can either get a commercial one. There are also free ones. Here are some IDE s listed below. JBuilder. It is a commercial package, not very expensive for students. It has a lot of functionality, including a GUI builder. The web site for this software is: http://borland.com/jbuilder/index.html. JCreator. There is a free version available. It is simple to use. The web site for this software is: http://www.jcreator.com/ Eclipse. This is also a free IDE. It is a little bit more complex to use, but it has more functionality than JCreator. The web site for this software is: http://eclipse.org/. 3. The Java VM to run the applets. To see if you have it, try to run some of my applets at http://science.kennesaw.edu/~plaval/tools/index.html. If you see a red X where the applet should be, it means you need to install the java VM. You can download it from Sun Microsystems at java.sun.com. I have made a CD containing the J2SE 1.4.2 SDK as well as the free IDE s mentioned above. If you need one, ask me. Even if you use my CD (to save download time), you should visit the web site of the product you select, for installation instructions and other information. If you do not have a favorite java IDE, I suggest you use NetBeans. It is free, has a GUI builder and all the functionality we will need in this class. 1.4 Class Speci cations As we build this class, we have to answer the following questions: 1. What is the data this class will operate on? This includes what data type. Do we need a separate class for points and vectors? 2. Should the data be public, private? 3. What constructors do we want for the class? 4. What are the operations we want to perform on the data? 1.4.1 Data This class is designed to handle 3D vectors. A 3D vector has 3 coordinates, x, y, and z. There are two questions to answer here. 3
1. Since we know that we will be using homogeneous coordinates, should we assign 4 coordinates to the vector? Since the fourth coordinate is 1, we do not need to store it. 2. What data type should each coordinate have? We do not need the precision doubles would give us. We will use oat for the data type of x, y and z. 3. Do we need a separate class for points. Since points are also represented with three coordinates, we do not. It is not necessary to add a separate class for points. As far as our software is concerned, we will treat points as vectors. We can always add such a class if it becomes necessary. 4. The name of the class will be vect3d. 1.4.2 Public versus private data Usually, all data within a class is private. The internals of a class should be hidden from the user. In this case, the class is simple. It represents a vector, so we know what the data is. The x, y and z coordinates of the vector. 1.4.3 Class Constructors There should be a default constructor, called with no arguments. It creates an object of this class, and sets its coordinates to 0. Since a vector can be constructed from either a point (it is the vector from the origin to that point) or from two points, we need a constructor for each situation. 1.4.4 Operations We need the basic operations on vectors. For each operation, we need to decide how to call it, what its arguments are, how it will be used, what it will return. Addition Name: add Parameters: 1 parameter of type vect3d, the second vector to add. What it does: adds 2 vectors. Subtraction Name:minus Parameters: 1 parameter of type vect3d, the second vector to subtract. 4
What it does: subtracts 2 vectors. Multiplication by a scalar Name: times Parameters: 1 parameter of type oat, the scalar by which we multiply. What it does: multiplies a vector by a scalar. Dot product Name:dot Parameters: 1 parameter of type vect3d, the second vector to take the dot product with. Return type: an object of type oat. What it does: takes the dot product of two vectors. Cross product Name: cross Parameters: 1 parameter of type vect3d, the second vector to take the cross product with. What it does: takes the cross product of two vectors. We could also use functions to check the following: If a vector is the zero vector Name: iszero Parameters: none Return type: boolean (true or false) What it does: checks to see if the vector is the zero vector. If two vectors are equal Name: isequal Return type: boolean What it does: checks to see if the two vectors are equal. 5
If two vectors are parallel Name: isparallel Return type: boolean What it does: checks to see if the two vectors are parallel. If two vectors are perpendicular Name: isperp Return type: boolean What it does: checks to see if the two vectors are perpendicular. We could have additional functions for the following: Find the norm of a vector Name: norm Parameters: none Return type: an object of type oat What it does: returns the norm of a vector Normalize a vector Name: normalize Parameters: none What it does: returns a vector having the same direction, of length 1. Finds the distance between two points Name: dist Return type: an object of type oat What it does: compute the distance between the two points (which are treated as vectors) 1.4.5 Conclusion We will develop a class named vect3d, whose data is the x, y and z coordinates of a vector, of type oat. This class will have the methods described above. 6
2 Assignments 1. Implement the 3D vector class as described in this document. You will also write a test program which can be used to test your code. The test program should test all the methods in your class. The test program will be called test1. The program is due on Monday September 29. 2. The next project is to develop a matrix class so we can implement the various transformations. Think about the speci cations such a class should have. We will discuss these at our next meeting, and make the speci cations of the class nal. 3 Resources This is a list of books and other resources I used to compile these notes. References [BG1] Burger, Peter, and Gillies, Duncan, Interactive Computer Graphics, Addison-Wesley, 1990. [DP1] Dunn, Fletcher and Parberry, Ian, 3D Math Primer for Graphics and Game Development, Wordware Publishing, Inc., 2002. [FD1] Foley, J.D., Van Dam, A., Feiner, S.K., and Hughes, J.F., Computer Graphics, Principles and Practices, Addison-Wesley, 1995. [FD2] Foley, J.D., Van Dam, A., Feiner, S.K., Hughes, J.F., and Philipps, R.L., Introduction to Computer Graphics, Addison-Wesley, 1997. [H1] Hill, F.S. JR., Computer Graphics Using Open GL, Prentice Hall, 2001. [LE1] Lengyel, Eric, Mathematics for 3D Game Programming & Computer Graphics, Charles River Media, Inc., 2002. [SE1] Schneider, Philip J., and Eberly, David H., Geometric Tools for Computer Graphics, Morgan Kaufman, 2003. [SP1] Shirley, Peter, Fundamentals of Computer Graphics, A K Peters, 2002. [SJ1] Stewart, James, Calculus, Concepts and Contexts, second edition, Brooks/Cole, 2001. [AW1] Watt, Alan, 3D Computer Graphics, Addison-Wesley, 2000. [AW2] Watt, Alan, The Computer Image, Addison-Wesley, 1999. 7