Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Graphics 3 Lecture 1: Introduction to C/C++ Programming Benjamin Mora 1 University of Wales Swansea Pr. Min Chen Dr. Benjamin Mora.

Similar presentations


Presentation on theme: "Computer Graphics 3 Lecture 1: Introduction to C/C++ Programming Benjamin Mora 1 University of Wales Swansea Pr. Min Chen Dr. Benjamin Mora."— Presentation transcript:

1 Computer Graphics 3 Lecture 1: Introduction to C/C++ Programming Benjamin Mora 1 University of Wales Swansea Pr. Min Chen Dr. Benjamin Mora

2 Content 2 Benjamin Mora University of Wales Swansea Why C/C++ for Graphics? Differences With Java. Memory Allocation. Operator Overloading. Not seen here: –Multiple Inheritance. –Standard Template Libraries (STLs). –Templates (Genericity)

3 Why C/C++ for Graphics? 3 Benjamin Mora University of Wales Swansea

4 Why C/C++? 4 Benjamin Mora University of Wales Swansea Why not! –Java? –Pascal? –Lisp? –Prolog? C/C++ generates efficient code. –Close to assembly code… –With some experience, programmers can guess what the compiled code will look like. Easier to optimize. –C/C++ Compilers heavily optimized!

5 Why C/C++? 5 Benjamin Mora University of Wales Swansea Rendering times are crucial in Computer Graphics! –C/C++ much faster than Java. –Real-Time renderings. –Non real-time renderings. –C/C++ much used by the Graphics community. Programming skills and chosen algorithms will also make a big difference. –However, code readability and simplicity should not be neglected. The Object-Oriented side of C++ can favour simplicity. –Using classes may be slower.

6 Differences with Java 6 Benjamin Mora University of Wales Swansea Compiled vs Interpreted. Portability. –Not as Portable as Java, although the code remains the same! Preprocessor Stage (C/C++). –#directive –Replace code by directive. Memory management –Programmer’s role in C/C++ Very costly in Java. –Memory Leaks. –Pointers. –Usually no bound checking. Unsecure if not well-programmed.

7 Differences with Java 7 Benjamin Mora University of Wales Swansea Struct, unions and class types versus class only types. Multiple inheritance with C/C++. –Can lead to problems. –Java “Interface”. Strings. Operator Overloading. –Though a few issues sometime. Implicit casting.

8 Memory Management 8 Benjamin Mora University of Wales Swansea

9 Memory Management 9 Benjamin Mora University of Wales Swansea Programmer’s role. –Program must keep track of every piece of memory dynamically allocated by the program. –Allocation functions: malloc and free (C) new, delete, new[] and delete[] (C++) Elements stored on the heap. Handled by the OS. Process Control Block Program Heap -Global Variables -Allocated Data Stack Local Variables (function calls) Top of Stack Address

10 Memory Management 10 Benjamin Mora University of Wales Swansea Why dynamic allocation? –Because memory allocation cannot always be determined at compilation time. Must be done very carefully –Memory leaks. –Releasing allocated memory too many times. –See VectorND Example. In conclusion, rigorous programming is required.

11 Pointers 11 Benjamin Mora University of Wales Swansea A pointer is an integer variable pointing at a specific address. char *myPointer=“Hello”; char *myPointer2=myPointer; int i=0; int *intPointer=&i; (* intPointer)++;//*intPointer=Pointed object The pointer size depends on the OS: –32 bits OS => 4 bytes –64 bits OS => 8 bytes 0xab123456 myPointer ‘H’‘e’‘l’ ‘o’0 0xab123456 myPointer2 = 0xab123456

12 Pointers 12 Benjamin Mora University of Wales Swansea char *myPointer =new char; char *myPointer2 =new char[5]; myPointer=NULL; //very bad:loosing the reference Delete[] myPointer2; Delete[] myPointer2; //Now wrong myPointer myPointer2 0xef01234a ? 0xab123456 ????? 0xef01234a 0xab123456 0

13 Using Multi-Dimensional Arrays in C/C++ 13 Benjamin Mora University of Wales Swansea

14 Linearized arrays in C/C++ 14 Benjamin Mora University of Wales Swansea Problem: how to store a multidimensional array? –Example: A grey-level image (256 levels) made of 640*480 pixels –A possible solution: unsigned char image[640][480]; –Accessing the pixel value at the (i,j) location: image[i][j] A huge drawback: – The code can only process images of that size !!! Smaller images can actually be processed, but coding this way is not really recommended (especially for maintenance)

15 Linearized arrays in C/C++ 15 Benjamin Mora University of Wales Swansea In C/C++, arrays are linearly (consecutively) stored in memory (every array allocates one memory block of the size of the array). –The “unsigned char image[640][480]” declaration will allocate a 640*480*sizeof(unsigned char) (usually 1 byte) bytes in memory. –The image is stored as a sequence of consecutive rows (every row is made of 480 pixels) –The “image[i][j]” value is in fact located at the memory address: Address(image)+480*i+j

16 Linearized arrays in C/C++ 16 Benjamin Mora University of Wales Swansea The extension to a multidimensional array is the same: –example: float volume[size_z] [size_y] [size_x] –Allocation size: size_z*size_y*size_x *sizeOf(float) bytes () –the “volume[k][j][i]” value is located at the memory address: Address(volume)+(i+j*size_x+k*size_x*size_y)*sizeOf(float) –Or in an Horner’s scheme like formulation Address(volume)+(i+(j+size_y*k)*size_x) *sizeOf(float) In conclusion, in order to handle multidimensional arrays, it is best to simulate the indexing by using a 1D array, and also to use dynamic allocations!

17 Linearized arrays in C/C++ 17 Benjamin Mora University of Wales Swansea Examples: –Declaration: float *volume; –Allocation: volume=(float *) malloc (sizeOf(float)*size_x*size_y*size_z);/*c*/ volume=new float[size_x*size_y*size_z] //c++ –Use: volume[(k*size_y+j)*size_x+i] –Do not forget to de-allocate!!! –In case of polymorphic data (either char, float,…), void * can be used instead of float here

18 Overloading 18 Benjamin Mora University of Wales Swansea

19 Operator Overloading 19 Benjamin Mora University of Wales Swansea C++ allows overloading common operators: – New, delete[], … – +, -, /, *, *=, … – >>, <<, &, &&, |, ^, … – >, <, == – =, [], … Can be tricky sometimes. The programmer must be careful. –E.g., redefining operator + on integers. a=b stands for a.operator=(b)

20 Operator Overloading 20 Benjamin Mora University of Wales Swansea Very useful when logically done: Complex C1(1,0), C2(2,2); C1=C1*C2; But also requires more CPU resources at run-time. –Avoid classes and overloading if code must be as efficient as possible. –The right balance between speed and code readability is an issue of CG.

21 Example 21 Benjamin Mora University of Wales Swansea

22 class VectorND 22 Benjamin Mora University of Wales Swansea class VectorND //.h file { public: VectorND(void); VectorND(int n); ~VectorND(void); void operator=(const VectorND &v); float &operator[](const int &i); VectorND operator+(const VectorND &v); int Length() {return size;}; protected: private: int size; float *pointer; void resize (int n); };

23 class VectorND 23 Benjamin Mora University of Wales Swansea VectorND::VectorND(void) { size=0; pointer=NULL; } VectorND::VectorND(int n) { pointer=NULL; size=0; resize(n); }

24 class VectorND 24 Benjamin Mora University of Wales Swansea VectorND::~VectorND(void) { if (pointer!=NULL) delete[] pointer; } void VectorND::resize(int n) { if (pointer!=NULL) delete[] pointer; pointer=new float[n]; if (pointer!=NULL) size=n; else size=0; }

25 class VectorND 25 Benjamin Mora University of Wales Swansea void VectorND::operator=(const VectorND &v) { int i; if (this==&v) //case v=v; return; resize(v.size); for (i=0;i<v.size;i++) { pointer[i]=v.pointer[i];//Duplicate the Data } }

26 class VectorND 26 Benjamin Mora University of Wales Swansea float &VectorND::operator[] (const int &i) { return pointer[i]; }; VectorND VectorND::operator+(const VectorND &v) { int i; static VectorND tmp; tmp.resize(v.size); if (v.size!=size) return v; //Not necessarily needed for (i=0;i<v.size;i++) tmp[i]=pointer[i]+v.pointer[i]; return tmp; }


Download ppt "Computer Graphics 3 Lecture 1: Introduction to C/C++ Programming Benjamin Mora 1 University of Wales Swansea Pr. Min Chen Dr. Benjamin Mora."

Similar presentations


Ads by Google