Docsity

Virtual Function - Computer Graphics - Lecture Slides, Slides of Computer Graphics

Partial preview of the text

Download Virtual Function - Computer Graphics - Lecture Slides and more Slides Computer Graphics in PDF only on Docsity!

C

Tutorial

Overview

•^

Pointers

•^

Arrays and strings

•^

Parameter passing

•^

Class basics

•^

Constructors & destructors

•^

Class Hierarchy

•^

Virtual Functions

•^

Coding tips

•^

Advanced topics

Arrays

Stack allocation int

intArray[10];

intArray[0]

int

*intArray;

intArray

new

int[10];

intArray[0]

... delete[]

intArray;

Heap allocation

Strings

A string in C++ is an array of characters char

myString[20];

strcpy(myString,

"Hello

World");

Strings are terminated with the

NULL

or

'\0'

character

myString[0]

'H';

myString[1]

'i';

myString[2]

'\0';

printf("%s",

myString);

output:

Hi

Parameter Passing

pass by reference – alternate notation int

add(int

&a,

int

&b)

return

a+b;

} int

a,

b,

sum;

sum

add(a,

b);

Class Basics

#ifndef

IMAGE_H

#define

IMAGE_H

#include

<assert.h>

#include

"vectors.h“

class

Image

public:

private:

}; #endif

Prevents multiple references Include a library fileInclude a local file

Variables and functionsaccessible from anywhereVariables and functions accessibleonly from within this class

Note that “private:” is the default

Organizational Strategy

image.h

Header file: Class definition & function prototypes

void

SetAllPixels(const

Vec3f

&color);

image.C

.C file: Full function definitions

void

Image::SetAllPixels(const

Vec3f

&color)

for

(int

i

i

width*height;

i++)

data[i]

color;

main.C

Main code: Function references

myImage.SetAllPixels(clearColor);

Constructors & Destructors

class

Image

public:

Image(void)

width

height

data

NULL;

~Image(void)

if

(data

NULL)

delete[]

data;

} int

width;

int

height;

Vec3f

*data;

Constructor: Called whenever a new instance is created Destructor: Called whenever an instance is deleted

The Copy Constructor

Image(Image

*img)

width

img->width;

height

img->height;

data

new

Vec3f[width*height];

for

(int

i=0;

i<width*height;

i++)

data[i]

new

data[i];

} Image(Image

*img)

width

img->width;

height

img->height;

data

img->data;

A default copy constructor is created automatically,but it is usually not what you want:^ }^ Warning: if you do not create a default (void parameter) or copy constructor explicitly, they are created for you.

Passing Classes as Parameters

If a class instance is passed by reference, the copy constructorwill be used to make a copy.

bool

IsImageGreen(Image

img);

Computationally expensive

It’s much faster to pass by reference:

bool

IsImageGreen(Image

*img);

or

bool

IsImageGreen(Image

&img);

Class Hierarchy

Child classes can

call

parent functions

Sphere::Sphere()

Object3D()

radius

Call the parent constructor

Child classes can

override

parent functions

class

Object3D

virtual

void

setDefaults(void)

color

RED;

}; class

Sphere

public

Object3D

void

setDefaults(void)

color

BLUE;

radius

Virtual Functions

A superclass pointer can reference a subclass object Sphere

*mySphere

new

Sphere();

Object3D

*myObject

mySphere;

If a superclass has virtual functions, the correct subclassversion will automatically be selected^ class

Object3D

virtual

void

intersect(Vec3f

*ray,

Vec3f

*hit);

}; class

Sphere

public

Object3D

virtual

void

intersect(Vec3f

*ray,

Vec3f

*hit);

}; myObject->intersect(ray,

hit);

Superclass Subclass

Actually calls Sphere::intersect

Coding tips

Use the

#define

compiler directive for constants

#define

PI

#define

sinf

sin

Use the

printf

or

cout

functions for output and debugging

printf("value:

%d,

%f\n",

myInt,

myFloat);

cout

"value:"

myInt

myFloat

endl;

Use the

assert

function to test “always true” conditions

assert(denominator

quotient

numerator/denominator;

“Segmentation fault (core dumped)”

Typical causes:

Access outside of array bounds

int

intArray[10];

intArray[10]

Image

*img;

img->SetAllPixels(ClearColor);

Attempt to access a NULL or previously deleted pointer

These errors are often very difficult to catch and

can cause erratic, unpredictable behavior.