170 likes | 276 Views
CPSC 453 Tutorial. 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 Email: liuxin@ucalgary.ca. What do we need?. Computer Graphics C/C++ OpenGL/GLSL GLUT.
E N D
CPSC 453 Tutorial 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 • Email: liuxin@ucalgary.ca
What do we need? • Computer Graphics • C/C++ • OpenGL/GLSL • GLUT
Rules • Linux/Unix: √ • Windows: √ • Mac OS: × • c/c++: √ • Java: × Java = ZERO • GNU c/c++: √ • Visual c++: √ • Objective c: ×
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
C vs. C++ vs. Java • Java is the son of C++ • Java is a totally different language though • C++ is the son of C • C++ is a superset of C • You can compile pure C with a C++ compiler • Java is a good start for c/c++ programming • Similar grammar
Hello world • Edit program • Compile • Run #include “iostream.h” int main () { cout << “Hello\n”; } g++ hello.c –o hello ----------- OR ------------ g++ -c hello g++ hello.o hello hello
Compile options • -Wall • Generate many warnings about questionable looking code • g++ -Wall myprog.c –o myprog • -O • Generate optimized code • g++ -O myprog.c –o myprog • -g • Generate code with symbolic info for debugging • g++ -g myprog.cmyprog
Compiling multi source files • A program composed of several souce files, “file1.c”, “file2.c” g++ -file1.c file2.c –o myprog ------------ OR ------------ g++ -c file1.c g++ -c file2.c g++ file1.o file2.o –o myprog
Make file • The “make program” • Compile all source files by a “make” command • Track changes of source files • Recompile only affected files • according to the dependency of source files • Requires a makefile • to define the dependency
Writing a makefile • Four 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: sourcefiles commands # there is a TAB before each command main: main.cList.h # to create main, these files must exist gcc –o main main.mList.h # the command to create main
Using IDE • Functions of an IDE (an Integrated Development Environment) • Edit program • Generating a makefile automatically • Compile program • Debug program • Run program • Popular IDEs • Eclipse on Linux - suggested • Visual Studio on Windows - suggested • Xcode on Mac OS - no OpenGL 3.3
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);
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); }
Example – Colors.cpp #include "MyColor.h" int main(intargc, char* argv[]) { CMyColorclr; 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; }