teaching machines

CS 455 Lecture 1 – Vector

January 27, 2015 by . Filed under cs455, lectures, spring 2015.


Who are you?

On a 1/4 sheet of paper, please answer the following questions:

Program This

Three-dimensional coordinates are of paramount importance in computer graphics. Let’s write a class that we’ll use all semester long to represent 3-D locations and directions. We’ll also (re)steep ourselves in some C++, see how Bitbucket works, and use CMake to build a project that works on many different platforms.


Create if necessary and log in to an account on Bitbucket. Visit https://bitbucket.org/twodee/cs455_2015a and hit Fork. Check This is a private repository and Inherit repository user/group permissions.

On a lab machine, run the Git GUI client. Clone your repository, using its URL as the source location. Check out to a directory on your H: drive.


Download the 64-bit binary for the OpenGL Extension Wrangler (GLEW). My understanding is that the Windows OpenGL driver by default only exposes functions from the 1997 standard of OpenGL 1.1 (we say it’s application binary interface or ABI is stuck at 1.1). If you want to access newer functions, you must query for them dynamically. GLEW does this and stores pointers to all the newer functions.

Unzip the archive into your H: drive or some other persistent location. You’ll need the path to this directory when we get to CMake.


Download Martin Payne’s Windows build of Freeglut, a simple cross-platform windowing toolkit. This library doesn’t support a comprehensive set of widgets, but it gets the job done.

Unzip the archive into your H: drive or some other persistent location. You’ll need the path to this directory when we get to CMake.


Run CMake. Set the source location to your checkout directory. Set the build directory to some directory outside your source directory.

If Freeglut and GLEW are just inside your H: drive and use the default directory names, you can hit Configure and Generate. If not, click Add Entry and define GLUT_ROOT and GLEW_ROOT to be paths to the actual directories.

Pick Visual Studio 2011 Win64 as your target IDE. Our installation of CMake is a bit old, which will give us some grief. We’re really using Visual Studio 2013. Hopefully LTS can upgrade soon.

Open the solution file that CMake generates in the build directory. Say yes on all the dialogs. Visual Studio will ask you to upgrade the project. Do so. It may do so a couple of times.

Click on the project named first. In the project menu, set this as the startup project. Now hit run.

Visual Studio will want to upgrade your project again. I don’t know why. Do so through Project / Retarget Solution. I agree; it’s silly.

Eventually, you should get a window with a white triangle. Clicking and dragging should generate some debug messages in the console window.



Pair up with a neighbor to write a Vector3 class with the following specification:

  1. Uses floats to represent the vector’s three components.
  2. A default constructor that zeroes the vector’s three components.
  3. A constructor that accepts the initial values of the three components.
  4. A subscript method ( operator[]) that accepts an index of 0, 1, or 2. When the index is 0, it returns the x-component. 1, the y-component. 2, the z-component. So that this message may serve as both a getter ( cout << v[0]) and a setter ( v[0] = 1.2f) return the float as a reference to a member variable.
  5. A const subscript method.
  6. A method that gets the vector’s length or magnitude.
  7. A const method that determines the dot product of this vector and a parameter vector (which comes in as a const reference).
  8. A method that normalizes the vector.
  9. A + method that adds component-wise this vector to a parameter vector and returns the sum as a new vector.
  10. A += method similar to the previous method but which alters this vector instead of yielding a new one.
  11. A * method that scales component-wise this vector by a float scale factor parameter and returns the scaled vector as a new vector.
  12. A *= method similar to the previous method but which alters this vector instead of yielding a new one.

Declare the class in Vector.h. Define it in Vector.cpp. Create these files through Windows Explorer in the top-level source directory.

Many students prefer editing in Notepad++. Make you edits there, and when you go to run your projects, jump to Visual Studio. Messy, huh? I prefer the workflow of Linux or Mac.

Also write a << method for printing vectors. Note that this can’t be a member of Vector3, since the invoking object will be an ostream, not a Vector3.


Now let’s create a second project in our Git repository for testing your Vector3 class. Create a directory named vectest and create a file with a main function that creates some vectors, scales them, adds them, and prints them. Invoke every method you wrote.

Copy over the CMakeLists.txt file from first. Change all the first references to vectest. Append  ../Vector3.cpp to the add_executable command.

Add your new files to your Git project using the GUI client and push them to Bitbucket.


Before next class:


on prerequisites:
In the beginning
God made the heavens and the earth
After he made math