CPSC 453: Intro to Computer Graphics
Tutorial 1: Intro/C++ - Sept 15th
Introduction
TA: Joshua Horacsek
joshua.horacsek@ucalgary.ca
- I'm here to provide additional content and support that supplements lectures and to make sure you understand course content
- I will cover course content that you need to know, but is not covered in lecture (i.e. come to tutorial)
- I am not here to do your assignments for you
- Please ask questions, I'll do my best to help you (I may defer to an email discussion tough)
- Late assignments are assigned a grade of zero (some exceptions for medical issues)
My Job:
Prof: Erika Harrison
eharris@ucalgary.ca
Policy
- No food or drink in the lab (myself included)
- Be punctual (please don't make a habit of being late)
- Cellphones off or in silent mode
- Respect the privacy of your peers
- Keep on-topic with discussion
Assignments
Assignment 0 (0%) - Environment Setup
Although this is not worth any marks, I will grade this assignment and it is in your best interest to do it. You'll use what you learn here in assignment 1.
Assignment 1 (10%) - Intro to OpenGL
Assignment 2 (10%) - ¯\_(ツ)_/¯
Assignment 3 (10%) - Object Modeller
Assignment 4 (10%) - Ray Tracer
This last assignment is no joke, get started on it early!
You must also demonstrate your assignment to me. Demos will take place during the tutorial immediately following the assignment deadline.
Exams
Midterm Exam (30%) - Nov 4th, in class
Final Exam (30%) - ???
I will likely not be marking these : )
Academic Dishonesty
Assignments are to be done independently. Any instances of code reuse by you for the assignment must be explicitly mentioned within the assignment's README file. As a TA, I will not (and cannot) confront offenders about plagiarism. Just because I won't speak to an offender about plagiarism, does not mean I will not notice it. Instances of suspected plagiarism will be reported and escalated up the chain of command -- at best it will result in a zero on your assignment.
Please see http://www.ucalgary.ca/honesty/plagiarism
Tutorial structure
In general, half problem solving, half content. With the exception of today, work periods and demo periods
Question: What are the principles of agile development?
Principles of Agile Development
12 principles of agile development (wikipedia):
-
Customer satisfaction by rapid delivery of useful software
-
Welcome changing requirements, even late in development
-
Working software is delivered frequently (weeks rather than months)
-
Close, daily cooperation between business people and developers
-
Projects are built around motivated individuals, who should be trusted
-
Face-to-face conversation is the best form of communication (co-location)
-
Working software is the principal measure of progress
-
Sustainable development, able to maintain a constant pace
-
Continuous attention to technical excellence and good design
-
Simplicity - the art of maximizing the amount of work done - is essential
-
Self-organizing teams
-
Regular adaptation to changing circumstance
Scrums
Very short, daily updates, covering:
- What did I do yesterday to meet the team goals?
- What do I plan to do today to meet the team goals?
- What roadblocks (if any) do I have for reaching the team goals?
Problem solving sections?
Today
- C++ crash course (some Qt too)
- Go through examples from assignment 0
mkdir ~/cpsc453/
cd ~/Downloads/ # The file might actually be in /tmp/
unzip src.zip -d ~/cpsc453/
qmake -project QT+=widgets # Makes a .pro file for your project
qmake # Generates a makefile
make # Builds the project
# You may also need to add
export PATH=/usr/lib64/qt5/bin:$PATH
export QTDIR=/usr/lib64/qt5
export QTINC=/usr/include/qt5
# to ~/.bashrc, which you can do via
gedit ~/.bashrc &
source ~/.bashrc # reload the file
# If you're still having issues, add
setenv PATH /usr/lib64/qt5/bin:$PATH
setenv QTDIR /usr/lib64/qt5
setenv QTINC /usr/include/qt5
# To ~/.login via
gedit ~/.login & Getting Started
C++ Programming
- I'm expecting familiarity with Java.
- Unlike Java, C++ does not restrict us to only using object oriented programming.
- There is no garbage collection, you're responsible for allocating and freeing your own memory.
C++ Programming
Variables:
- All your friends are here: int, float, double, boolean (bool in C++)
- Global variables need not be associated with a class
- Use const to declare a constant "variable:
- Always initialize your variables (bad things will happen if you don't)
int width = 400;
int height = width + 100;
bool check = (width < height);
float pi = 3.14f;
int start; // What is this initialized to?
const int start; // What is this initialized to?Strings in C++ are a bit different from those in Java, more on that soon
C++ Programming
Functions:
- All C++ programs enter from main
- Use int main (int argc, char * argv[]) to access command line variables (argc = count; argv = array of char * (like string))
- Structure similar to Java
void aCoolFunction(int param1) {
param1 = param1 + 1;
}
void aCoolerFunction(int ¶m1) { // pass param1 by reference
param1 = param1 + 1;
}
// Example of a main function
int main(int argc, char *argv[]) {
int value = 1;
aCoolFunction(value); // What is the number in "value"
aCoolerFunction(value); // What is the number in "value" now?
return 0; // 0 - normal exit
}
C++ Programming
/*
* CPSC 453 - Introduction to Computer Graphics
* Assignment 0
*
* Entry point for execution into the program
*/
#include "window.h"
#include <QApplication>
int main(int argc, char *argv[])
{
// Allocate and construct a QApplication instance on the stack
QApplication a(argc, argv);
// Allocate and construct a Window instance (default constructor)
Window w;
w.show();
return a.exec();
}
C++ Programming
Classes:
- public, protected, private
- interface and implementation are generally separated (templated classes are an exception, but I won't be covering those)
/*
* window.h
* h/hpp files generally contain the specification for a class.
*/
class Window : public QMainWindow // Window inherits from QMainWindow (defined elsewhere)
{
// Don't worry about this, this isn't standard C++
// but rather a macro that Qt uses to know that this
// add some members to this class.
Q_OBJECT
public:
// Constructor, with argument parent (default value = 0, or NULL)
Window(QWidget *parent = 0);
// Deconstructor, these are used in C++ to free resources your object
// may be holding on to
~Window();
C++ Programming
Classes:
window.h
class Window : public QMainWindow
{
Q_OBJECT
public:
Window(QWidget *parent = 0);
~Window();
private slots: // this also isn't standard C++!
/* Here, "private slots:" is a Qt specific way of exporting
"slot" class methods, more on these next tutorial */
// Activates the given drawing mode
void setDrawMode(QAction * action);
private:
// Main widget for drawing
Renderer *renderer; // This is a pointer to an object, more on those later
// ...
}; // <- Don't forget the ";"! C++ Programming
Classes:
window.cpp
#include "window.h"
#include "renderer.h"
// after ":" is a call to a parent constructor
Window::Window(QWidget *parent) : QMainWindow(parent) {
// Do stuff
}
// Method function implementations are declared by
// returnType ClassName::Method(parameters) { ... }
// Activates the given drawing mode
void Window::setDrawMode(QAction * action) {
if (action == mLineAction)
this->renderer->setDrawMode(Renderer::DRAW_LINE);
else if (action == mOvalAction)
this->renderer->setDrawMode(Renderer::DRAW_OVAL);
else // (action == mRecAction)
this->renderer->setDrawMode(Renderer::DRAW_RECTANGLE);
}C++ Programming
C++ File Structure
In general, header files (.h) contain class and function declarations (ala Java's interface)
Implementation files (.cpp) contain the respective implementations. This is why we must use :: to identify the functions to the classes (eg. void SampleClass::SampleFunction() { ... } )
C++ Programming
Pointers:
- Pointers are denoted by a * before the variable name
- These are not objects, internally they're unsigned integers that point to the memory location where the object resides
Window *myWindow = new Window();
// Memory is allocated for myWindow, then the constructor is called
// with the address of that memory as its base
// Question, what happens when comparing these, for example
myWindow == anotherWindow;
*myWindow == *anotherWindow; // Better?
// The & operator will return the address of variable on the stack
int variable = 100;
int *ptrToVariable = &variable;
// The * dereferences the data at an address
printf("ptrToVariable points to the value %d\n", *ptrToVariable);
// What happens if you do this
ptrToVariable = 100;
// Question, what will the line above do if we do the following right before it
variable = 200;
C++ Programming
Memory Management (Stack vs Heap):
- Generally, the stack is for local objects, and the heap is for global
void aCoolFunction(int nLength) {
// Stack allocation
int intList[100]; // How do we allocate nLength of these on the stack?
// Some code...
}
void anotherCoolFunction(int nLength)
{
// Heap allocation
int * intList = new int[nLength]; // Dynamically allocate memory
// Some code...
delete intList; // You need to delete (free) the pointer when you're done
}
C++ Programming
Window *gWindow = NULL;
void aCoolFunction() {
Window w; // Constructor called
gWindow = &w;
return; // What happens here?
}
gWindow->show();
// ---------
Window *gWindow = NULL;
void anotherCoolFunction() {
Window *w = new Window();
gWindow = w;
return;
}
gWindow->show();
delete gWindow;Memory Management (Stack vs Heap):
- A less trivial example
C++ Programming
Preprocessor:
- To use external files, we must identify which ones we're interested in.
- Preprocessor commands send directions to the compiler.
- #include <SampleClass.h> includes a header file
- If the header file is local, use #include "SampleClass.h". The use of > ... < refer to system (or include) directories
- You can also define values. #define SAMPLE_PREPRO_CONSTANT 10 These will replace every instance of SAMPLE_PREPRO_CONSTANT with the value 10 at compile time. May be helpful.
When defining classes, you must only define it once. If it needs to be included in several files, a redeclaration will take place, causing compiler issues. For good practise, be sure to use the following to prevent redeclaration,
#ifndef SAMPLE_CLASS_H
#define SAMPLE_CLASS_H
class SampleClass
{
SampleClass();
~SampleClass();
...
};
#endifC++ Programming
C++ has a large collection of data structures and libraries within its Standard Template Library. This will help us for I/O. See Google for other examples (or cplusplus.com)
#include <iostream.h> // not all systems require the .h (ie. <iostream>)
using std::cout;
using std::cin;
using std::endl;
...
int myVar = 5;
cout << "The value of my variable is: " << myVar << endl;
cin >> myVar;
cout << "The new value of my variable is: " << myVar << endl;cout sends output to the console. cin gets input from the console. This information is sent in the form of streams. << pushes information into a stream. >> retrieves information from a stream.
C++ Programming
Compilation
To compile from the command line, you would do:
g++ -c Point2D.cpp
g++ -c example.cpp
# The above creates separate output files.
# They are then linked together to form a cohesive executable:
g++ -o Output Point2D.o example.oLuckily, Qt offers a way to automatically:
- Find all the needed libraries on your computer (qmake)
- Build a collection of directives for compiling the program (Makefile)
C++ Programming
Compilation
# To reiterate!
# Create the .pro file
qmake -project QT+=widgets
# Generate a makefile from that .pro file
qmake
# Build
make
# Run the code
./a0
C++ Programming
Compilation
Or you can make your own Makefile
MyProgram: Point2D.o example.o
g++ -o MyProgram Point2D.o example.o
example.o: example.cpp
g++ -c example.cpp
Point2D.o: Point2D.cpp Point2D.h
g++ -c Point2D.cpp
clean:
rm MyProgram Point2D.o example.o
# At least one blank line must be added at the end of the Makefile
Now, to compile, all we need to do is type make (Look at man make for more details). To clean things up, we type make clean, based on the target we created.
C++ Programming
Stuff you should checkout:
- Strings in C++
- Namespaces (std is a namespace in the previous examples)
- Standard Template Library
- Templated Classes
- Operator Overloading
- Unit Testing
Remember, Google is your friend : )
Next Time
- Continuing with A0
- Qt and Widgets
Questions? Come speak to me, or e-mail me
CPSC 453: Intro to Computer Graphics: Tutorial 1
By Joshua Horacsek
CPSC 453: Intro to Computer Graphics: Tutorial 1
- 1,519