computer graphics 3 lecture 1: introduction to c/c++ programming benjamin mora 1 university of wales...
TRANSCRIPT
Computer Graphics 3Lecture 1:
Introduction to C/C++ Programming
Benjamin Mora 1University of Wales
Swansea
Pr. Min ChenDr. Benjamin Mora
Content
2Benjamin MoraUniversity 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)
Why C/C++ for Graphics?
3Benjamin MoraUniversity of Wales
Swansea
Why C/C++?
4Benjamin MoraUniversity 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!
Why C/C++?
5Benjamin MoraUniversity 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.
Differences with Java
6Benjamin MoraUniversity 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.
Differences with Java
7Benjamin MoraUniversity 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.
Memory Management
8Benjamin MoraUniversity of Wales
Swansea
Memory Management
9Benjamin MoraUniversity 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
StackLocal Variables
(function calls)
Top of Stack
Address
Memory Management
10Benjamin MoraUniversity 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.
Pointers
11Benjamin MoraUniversity 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’ ‘l’ ‘o’ 0
0xab123456
myPointer2
=
0xab123456
Pointers
12Benjamin MoraUniversity 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
0xab1234560
Using Multi-Dimensional Arrays in C/C++
13Benjamin MoraUniversity of Wales
Swansea
Linearized arrays in C/C++
14Benjamin MoraUniversity 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)
Linearized arrays in C/C++
15Benjamin MoraUniversity 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
Linearized arrays in C/C++
16Benjamin MoraUniversity 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!
Linearized arrays in C/C++
17Benjamin MoraUniversity 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
Overloading
18Benjamin MoraUniversity of Wales
Swansea
Operator Overloading
19Benjamin MoraUniversity 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)
Operator Overloading
20Benjamin MoraUniversity 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.
Example
21Benjamin MoraUniversity of Wales
Swansea
class VectorND
22Benjamin MoraUniversity 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);
};
class VectorND
23Benjamin MoraUniversity of Wales
Swansea
VectorND::VectorND(void){
size=0;pointer=NULL;
}
VectorND::VectorND(int n){
pointer=NULL;size=0;resize(n);
}
class VectorND
24Benjamin MoraUniversity 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;
}
class VectorND
25Benjamin MoraUniversity 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}
}
class VectorND
26Benjamin MoraUniversity 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 neededfor (i=0;i<v.size;i++)
tmp[i]=pointer[i]+v.pointer[i];return tmp;
}