CS91 Computer Animation: Lab 0

In which, we begin our work with C++, programming tools, git, and openGL
This lab is a tutorial for C++, UNIX programming tools, git, and OpenGL. We will cover the basics of what you will encounter working on the assignments for this course.

Step 1: Get the source

The source is managed and stored on github.swarthmore.edu. By managed, changes to the source are kept track of with revision control. Revision control makes it easy to keep track of changes, maintain multiple versions of software, collaborate with others, and to revert to specific code versions.

You will need a Swarthmore git account for submitting assignments. If you don't have one already, instructions are here.

> mkdir cs91
> cd cs91
> git clone git@github.swarthmore.edu:cs91-s17/A0Hello-YOURUSERNAME.git A0Hello

You should now have a directory named A0Hello with code examples for this lab and this week's assignment.

Step 2: Compile and run

We will use cmake to generate makefiles for the source. We will use out of source builds for this class.
> cd A0Hello
> mkdir build
> cd build
> cmake ..
> make 

IMPORTANT: Do not check in temporary files in build!!! This should not occur because build* is listed in .gitignore.

Two executables are built. A simple C++ example and a simple OpenGL example. Try running them.

> ./HelloWorld
This will print the famous phrase, "Hello World" to the console.
> ./HelloGL
This should open a window and show a gray blue square.

Homework

The writeup for the first assignment is here.

UNIX tools

Tia Newhall has collected a rich set of UNIX resources, which are available here.

Furthermore, tools such as man and apropos can be used to understand the features of any UNUX call.

Try looking at the man page for recordmydesktop to see how it can be used to record demos form the assignment. A tested workflow is also in the submission guidelines for the assignment. However, you can experiment with the tools available in the lab to discover the easiest and/or best ways to make high quality demos.

> man recordmydesktop

cmake, make and makefiles

We are using cmake (Andy Danner has a tutorial here) to generate makefiles for our demos and assignments. See the assignment documentation for build instructions. I like cmake because I work on windows (MS Dev and cygwin), OS X, and unix, and cmake allows me to support each platform with a single build system. Because we using a unix environment, cmake is generating makefiles under the hood. If the build process produces an error, drilling into the created makefiles and checking the compile and linking flags is usually enough to diagnose the problem. For more information on make and makefiles, check out Tia Newhalls's documentation here.

Debugging tools: GDB and valgrind

We will not go over using gdb and valgrind here. However, Tia Newhall has a very nice tutorial here as well as some additional tips.

C++ Notes

The basecode for the upcoming assignments rely heavily on several C++ features, which we will delve into more detail as we progress in the course.

Passing by reference

C++ allows you to pass arguments to functions by value, by refernce, or by pointer, e.g.
void foo(const A& a); // by const reference, A cannot change
void foo(A& a);       // by reference, A can change inside foo
void foo(A* a);       // by pointer, A can change
void foo(A a);        // by value, A is copied and passed, 
                      // A can change in the function, but the changes don't presist
Quoting Thinking in C++
References are like constant pointers that are automatically dereferenced by the compiler.
For non-basic types, passing by reference allows us to avoid unnecessary object copying. Const references aid with type safety. Also, passing by const references allows to create 'throw-away' temporary objects on the stack, for example,
foo(vec3(0,0,0)); // pass a temporary class instance who won't live past the execution of the fn call
In this week's assignment, you will finish the implementation of a Shape helper class which implements several functions with const reference arguments.

Constructors and assignment operators

Behind the scenes, C++ always creates default copy constructors and assignment operators for each class you create. However, it's best practice to define these yourself (For more information into why, please read Chpt 2, Item 5 from Effective C++). In short, the default version may have behavior you don't want.

In this assignment, we will ask you to implement the copy constructor and assignment operator.

A a;      // call default ctor
A a2;
A a = a2; // call copy ctor
A a(a2);  // call copy ctor
a = a2;   // call assignment operator

Operator overloading

Again quoting Thinking in C++
Operator overloading is just “syntactic sugar,” which means it is simply another way for you to make a function call. The difference is that the arguments for this function don’t appear inside parentheses, but instead they surround or are next to characters you’ve always thought of as immutable operators.
In this assignment, you are asked to implement the iostream out operator for Shape. This allows use to write code that looks like
#include 
using namespace std;
...
Shape s(theQuadId);
cout << s << endl;

Standard template library

The C++ standard template library collects together many useful data structures and algorithms. For this assignment, we ask that you use the vector class to create a list of Shapes, e.g.
vector theShapes;
Vectors are self-resizing arrays. Documentation for using vectors is here Note that we are not storing pointers in our vector.

Random number generation resources

The new C++ numerics library supports more powerful random number generation based on many useful distributions. For your assignment, try using the new uniform number generatori

Code style

A consistent coding style is important for making readable code. Please adhere to the style of the assignments when writing code. Use camelCase for variables. Prepend global variable with 'the'. Prepend local variable with 'm'. Use aligned brackets {} for code blocks. Use comments heavily for explaining algorithm implementations in technical functions. Use all CAPITALS for global constants.

The basecode will use shared libraries for solutions. Be aware that if you change the header files, linking against precompiled libraries will not work.