Computer Graphics 3 Lecture 1: Introduction to CC Programming - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Computer Graphics 3 Lecture 1: Introduction to CC Programming

Description:

Scotland 5,064,200. Wales 2,903,085. Northern Ireland 1,685,267. Republic of Ireland 3,458,479 ... Scotland 78,772 km (30,418 m ) Wales 20,761 km (8,019 m ) ... – PowerPoint PPT presentation

Number of Views:120
Avg rating:3.0/5.0
Slides: 27
Provided by: saf88
Category:

less

Transcript and Presenter's Notes

Title: Computer Graphics 3 Lecture 1: Introduction to CC Programming


1
Computer Graphics 3Lecture 1Introduction to
C/C Programming
Pr. Min Chen Dr. Benjamin Mora
University of Wales Swansea
1
Benjamin Mora
2
Content
  • Why C/C for Graphics?
  • Differences With Java.
  • Memory Allocation.
  • Operator Overloading.
  • Not seen here
  • Multiple Inheritance.
  • Standard Template Libraries (STLs).
  • Templates (Genericity)

University of Wales Swansea
2
Benjamin Mora
3
Why C/C for Graphics?
University of Wales Swansea
3
Benjamin Mora
4
Why C/C?
  • 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!

University of Wales Swansea
4
Benjamin Mora
5
Why C/C?
  • 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.

University of Wales Swansea
5
Benjamin Mora
6
Differences with Java
  • 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
  • Programmers role in C/C
  • Very costly in Java.
  • Memory Leaks.
  • Pointers.
  • Usually no bound checking.
  • Unsecure if not well-programmed.

University of Wales Swansea
6
Benjamin Mora
7
Differences with Java
  • 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.

University of Wales Swansea
7
Benjamin Mora
8
Memory Management
University of Wales Swansea
8
Benjamin Mora
9
Memory Management
  • Programmers 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.

Address
Top of Stack
University of Wales Swansea
9
Benjamin Mora
10
Memory Management
  • 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.

University of Wales Swansea
10
Benjamin Mora
11
Pointers
  • A pointer is an integer variable pointing at a
    specific address.
  • char myPointerHello
  • char myPointer2myPointer
  • int i0
  • int intPointeri
  • ( intPointer)//intPointerPointed object
  • The pointer size depends on the OS
  • 32 bits OS 4 bytes
  • 64 bits OS 8 bytes

University of Wales Swansea
11
Benjamin Mora
12
Pointers
  • char myPointer
  • new char
  • char myPointer2
  • new char5
  • myPointerNULL
  • //very badloosing the reference
  • Delete myPointer2
  • Delete myPointer2 //Now wrong

0
University of Wales Swansea
12
Benjamin Mora
13
Using Multi-Dimensional Arrays in C/C
University of Wales Swansea
13
Benjamin Mora
14
Linearized arrays in C/C
  • Problem how to store a multidimensional array?
  • Example A grey-level image (256 levels) made of
    640480 pixels
  • A possible solution unsigned char
    image640480
  • Accessing the pixel value at the (i,j) location
    imageij
  • 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)
University of Wales Swansea
14
Benjamin Mora
15
Linearized arrays in C/C
  • 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 image640480 declaration
    will allocate a 640480sizeof(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 imageij value is in fact located at the
    memory address
  • Address(image)480ij

University of Wales Swansea
15
Benjamin Mora
16
Linearized arrays in C/C
  • The extension to a multidimensional array is the
    same
  • example float volumesize_z size_y size_x
  • Allocation size size_zsize_ysize_x
    sizeOf(float) bytes ()
  • the volumekji value is located at the
    memory address
  • Address(volume)(ijsize_xksize_xsize_y)sizeO
    f(float)
  • Or in an Horners scheme like formulation
  • Address(volume)(i(jsize_yk)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!

University of Wales Swansea
16
Benjamin Mora
17
Linearized arrays in C/C
  • Examples
  • Declaration
  • float volume
  • Allocation
  • volume(float ) malloc (sizeOf(float)size_xsize
    _ysize_z)/c/
  • volumenew floatsize_xsize_ysize_z //c
  • Use
  • volume(ksize_yj)size_xi
  • Do not forget to de-allocate!!!
  • In case of polymorphic data (either char,
    float,), void can be used instead of float
    here

University of Wales Swansea
17
Benjamin Mora
18
Overloading
University of Wales Swansea
18
Benjamin Mora
19
Operator Overloading
  • C allows overloading common operators
  • New, delete,
  • , -, / , , ,
  • ,
  • ,
  • , ,
  • Can be tricky sometimes. The programmer must be
    careful.
  • E.g., redefining operator on integers.
  • ab stands for a.operator(b)

University of Wales Swansea
19
Benjamin Mora
20
Operator Overloading
  • Very useful when logically done
  • Complex C1(1,0), C2(2,2)
  • C1C1C2
  • 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.

University of Wales Swansea
20
Benjamin Mora
21
Example
University of Wales Swansea
21
Benjamin Mora
22
class VectorND
  • 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)

University of Wales Swansea
22
Benjamin Mora
23
class VectorND
  • VectorNDVectorND(void)
  • size0
  • pointerNULL
  • VectorNDVectorND(int n)
  • pointerNULL
  • size0
  • resize(n)

University of Wales Swansea
23
Benjamin Mora
24
class VectorND
  • VectorNDVectorND(void)
  • if (pointer!NULL)
  • delete pointer
  • void VectorNDresize(int n)
  • if (pointer!NULL)
  • delete pointer
  • pointernew floatn
  • if (pointer!NULL)
  • sizen
  • else size0

University of Wales Swansea
24
Benjamin Mora
25
class VectorND
  • void VectorNDoperator(const VectorND v)
  • int i
  • if (thisv) //case vv
  • return
  • resize(v.size)
  • for (i0i
  • pointeriv.pointeri//Duplicate the Data

University of Wales Swansea
25
Benjamin Mora
26
class VectorND
  • float VectorNDoperator (const int i)
  • return pointeri
  • VectorND VectorNDoperator(const VectorND v)
  • int i
  • static VectorND tmp
  • tmp.resize(v.size)
  • if (v.size!size)
  • return v //Not necessarily needed
  • for (i0i
  • tmpipointeriv.pointeri
  • return tmp

University of Wales Swansea
26
Benjamin Mora
Write a Comment
User Comments (0)
About PowerShow.com