17
CPSC 453 Tutorial Xin Liu Sep 16, 2013

Xin Liu Sep 16, 2013. Introduction Xin (Shane) Liu PhD Candidate in Computer Science Research Area: Computer Graphics Tutorial Page: pages.cpsc.ucalgary.ca/~liuxin/CPSC453

Embed Size (px)

Citation preview

Page 1: Xin Liu Sep 16, 2013. Introduction Xin (Shane) Liu PhD Candidate in Computer Science Research Area: Computer Graphics Tutorial Page: pages.cpsc.ucalgary.ca/~liuxin/CPSC453

CPSC 453 TutorialXin Liu

Sep 16, 2013

Page 2: Xin Liu Sep 16, 2013. Introduction Xin (Shane) Liu PhD Candidate in Computer Science Research Area: Computer Graphics Tutorial Page: pages.cpsc.ucalgary.ca/~liuxin/CPSC453

IntroductionXin (Shane) Liu

PhD Candidate in Computer ScienceResearch Area: Computer GraphicsTutorial Page:

pages.cpsc.ucalgary.ca/~liuxin/CPSC453Email: [email protected]

Page 3: Xin Liu Sep 16, 2013. Introduction Xin (Shane) Liu PhD Candidate in Computer Science Research Area: Computer Graphics Tutorial Page: pages.cpsc.ucalgary.ca/~liuxin/CPSC453

What do we need?Computer Graphics

C/C++

OpenGL/GLSL

GLUT

Page 4: Xin Liu Sep 16, 2013. Introduction Xin (Shane) Liu PhD Candidate in Computer Science Research Area: Computer Graphics Tutorial Page: pages.cpsc.ucalgary.ca/~liuxin/CPSC453

RulesLinux/Unix: √Windows: √Mac OS: ×

c/c++: √Java: × Java = ZEROGNU c/c++: √Visual c++: √Objective c: ×

Page 5: Xin Liu Sep 16, 2013. Introduction Xin (Shane) Liu PhD Candidate in Computer Science Research Area: Computer Graphics Tutorial Page: pages.cpsc.ucalgary.ca/~liuxin/CPSC453

Getting started with C ++C/ C ++

The most widely used programming language Operating systems Applications

Fast Good for real-time applications

Full control of your computer A better understanding of the hardware Can be “dangerous”

Professional Good for your future career

Page 6: Xin Liu Sep 16, 2013. Introduction Xin (Shane) Liu PhD Candidate in Computer Science Research Area: Computer Graphics Tutorial Page: pages.cpsc.ucalgary.ca/~liuxin/CPSC453

C vs. C++ vs. JavaJava is the son of C++

Java is a totally different language thoughC++ is the son of CC++ is a superset of C

You can compile pure C with a C++ compiler

Java is a good start for c/c++ programmingSimilar grammar

Page 7: Xin Liu Sep 16, 2013. Introduction Xin (Shane) Liu PhD Candidate in Computer Science Research Area: Computer Graphics Tutorial Page: pages.cpsc.ucalgary.ca/~liuxin/CPSC453

C++ vs Java – some differencesFunction C++ Java

Reference Yes Yes

Pointer Yes No

Garbage collection No (new, delete) Yes

Operator overloading Yes No

Class Yes Yes

Struct Yes No

Destructor Yes No

Page 8: Xin Liu Sep 16, 2013. Introduction Xin (Shane) Liu PhD Candidate in Computer Science Research Area: Computer Graphics Tutorial Page: pages.cpsc.ucalgary.ca/~liuxin/CPSC453

Hello worldEdit program

Compile

Run

#include “iostream.h”int main (){ cout << “Hello\n”;}

g++ hello.c –o hello----------- OR ------------g++ -c hellog++ hello.o hello

hello

Page 9: Xin Liu Sep 16, 2013. Introduction Xin (Shane) Liu PhD Candidate in Computer Science Research Area: Computer Graphics Tutorial Page: pages.cpsc.ucalgary.ca/~liuxin/CPSC453

Compile options-Wall

Generate many warnings about questionable looking code

g++ -Wall myprog.c –o myprog-O

Generate optimized codeg++ -O myprog.c –o myprog

-gGenerate code with symbolic info for debuggingg++ -g myprog.c myprog

Page 10: Xin Liu Sep 16, 2013. Introduction Xin (Shane) Liu PhD Candidate in Computer Science Research Area: Computer Graphics Tutorial Page: pages.cpsc.ucalgary.ca/~liuxin/CPSC453

Compiling multi source filesA program composed of several souce files,

“file1.c”, “file2.c”

g++ -file1.c file2.c –o myprog------------ OR ------------g++ -c file1.cg++ -c file2.cg++ file1.o file2.o –o myprog

Page 11: Xin Liu Sep 16, 2013. Introduction Xin (Shane) Liu PhD Candidate in Computer Science Research Area: Computer Graphics Tutorial Page: pages.cpsc.ucalgary.ca/~liuxin/CPSC453

Make fileThe “make program”

Compile all source files by a “make” commandTrack changes of source filesRecompile only affected files

according to the dependency of source filesRequires a makefile

to define the dependency

Page 12: Xin Liu Sep 16, 2013. Introduction Xin (Shane) Liu PhD Candidate in Computer Science Research Area: Computer Graphics Tutorial Page: pages.cpsc.ucalgary.ca/~liuxin/CPSC453

Writing a makefileFour basic types of statements

Comments any line beginning with a #

Macros defined by: name = data used by: $(name), “$(name)” is then replaced by “data”

Explicit rules defines the dependency take the form of

Example:

Implicit rules similar to explicit rules, except listed without commands. uses the suffixes on the files to determine what command to perform

targetfile: sourcefilescommands # there is a TAB before each

command

main: main.c List.h # to create main, these files must exist

gcc –o main main.m List.h # the command to create main

Page 13: Xin Liu Sep 16, 2013. Introduction Xin (Shane) Liu PhD Candidate in Computer Science Research Area: Computer Graphics Tutorial Page: pages.cpsc.ucalgary.ca/~liuxin/CPSC453

Using IDEFunctions of an IDE (an Integrated Development

Environment)Edit programGenerating a makefile automaticallyCompile programDebug programRun program

Popular IDEsEclipse on Linux - suggestedVisual Studio on Windows - suggestedXcode on Mac OS - no OpenGL 3.3

Page 14: Xin Liu Sep 16, 2013. Introduction Xin (Shane) Liu PhD Candidate in Computer Science Research Area: Computer Graphics Tutorial Page: pages.cpsc.ucalgary.ca/~liuxin/CPSC453

Example – MyColor.h#include <stdio.h>

class CMyColor {public:

double r;double g;double b;

CMyColor (double argR = 0.0, double argG = 0.0, double argB = 0.0);void print();

};

CMyColor operator + (const CMyColor& a, const CMyColor& b);CMyColor operator - (const CMyColor& a, const CMyColor& b);CMyColor operator * (const CMyColor& a, const CMyColor& b);CMyColor operator * (const CMyColor& a, const double& k);

Page 15: Xin Liu Sep 16, 2013. Introduction Xin (Shane) Liu PhD Candidate in Computer Science Research Area: Computer Graphics Tutorial Page: pages.cpsc.ucalgary.ca/~liuxin/CPSC453

Example – MyColor.cpp#include "stdafx.h"#include "MyColor.h"

CMyColor:: CMyColor (double argR, double argG, double argB){

r = argR;g = argG;b = argB;

}

void CMyColor:: print(){

printf("[%f, %f, %f]\n", r, g, b);}

CMyColor operator + (const CMyColor& a, const CMyColor& b){

return CMyColor(a.r + b.r, a.g + b.g, a.b + b.b);}

CMyColor operator - (const CMyColor& a, const CMyColor& b){

return CMyColor(a.r - b.r, a.g - b.g, a.b - b.b);}

CMyColor operator * (const CMyColor& a, const CMyColor& b){

return CMyColor(a.r * b.r, a.g * b.g, a.b * b.b);}

CMyColor operator * (const CMyColor& a, const double& k){

return CMyColor(k * a.r, k * a.g, k * a.b);}

Page 16: Xin Liu Sep 16, 2013. Introduction Xin (Shane) Liu PhD Candidate in Computer Science Research Area: Computer Graphics Tutorial Page: pages.cpsc.ucalgary.ca/~liuxin/CPSC453

Example – Colors.cpp#include "MyColor.h"

int main(int argc, char* argv[]){

CMyColor clr;clr.print();

CMyColor *pClr = 0;pClr = new CMyColor(1.0, 1.0, 1.0);pClr->print();

CMyColor clr2 = (clr + (*pClr)) * 2.0;clr2.print();

delete pClr;return 0;

}

Page 17: Xin Liu Sep 16, 2013. Introduction Xin (Shane) Liu PhD Candidate in Computer Science Research Area: Computer Graphics Tutorial Page: pages.cpsc.ucalgary.ca/~liuxin/CPSC453

The End