Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt...

44
Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage ([email protected]) Authors: Magnus Andersson, PhD student ([email protected]) Carl Johan Gribel, PhD student ([email protected])

Transcript of Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt...

Page 1: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Introductory Seminar

C++ & RenderChimp

EDA221 Introduction to Computer Graphics

Universitetsadjunkt Mathias Haage ([email protected])

Authors: Magnus Andersson, PhD student ([email protected])

Carl Johan Gribel, PhD student ([email protected])

Page 2: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

point.h:

class Point{public:

Point(float x, float y); // constructorfloat getX(void) const; /* accessor: const means

does not change state of object */float getY(void) const;

protected:float mX; // member attributesfloat mY;

};

The actual implementation is in point.cpp

Class Definition

Page 3: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

point.cpp:

Point::Point(float x,float y) // Point:: indicates which class{

mX = x;mY = y;

}

float Point::getX(void) const // getY() in the same way{

return mX;}

Implementation of Point

Page 4: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Function declaration:

In header file (for example):

bool finished(int t); // note semi-colon instead of function body

In cpp-file, function definition:

bool finished(int t){

return t > 1;}

Declarations and Definitions

Page 5: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

void func(void) // function not belonging to class (no ::){

int a;a = sin(0.314);Point pl;Point *p = new Point(10, 20); // a pointerPoint *pa = new Point[20]; // array of 20 point objects

}

When func() is entered, a & pl are allocated on the stack, and when exited, a & pl are automatically deleted.

p and pa is allocated using new, which means that you need to delete it at some point: delete p; delete [] pa;

There is NO garbage collection in C++.

Allocation

Page 6: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Used when a class allocates memory using new.

The destructor deletes what it has allocated

point.h

class Point{public:

Point();~Point(); // destructor

};

point.cpp:

Point::~Point(){

delete mNameOfPointString; //if there was such a variable}

Destructor

Page 7: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

class Point{public:

virtual void update(void);};

class TimePoint : public Point // inherit from Point{public:

void update(void); // overloads Point::update};

Inheritance

Page 8: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Default as in Java: parameter is copied

Then we have pointers and references as well

int func(Point &pr, Point *pp) // & is ref, * is pointer{

pr.setX( pr.getX()+10 ); // note .pp->setX( pp->getX()+10 ); // note ->

}

void test(void){

Point pr(1,1); // stack allocationPoint *pp= new Point(1,1); // heap allocation

func(pr, pp);}

Reference and Pointer Parameters

Page 9: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

An array:

int a[10]; // no a.length() as in Java

Array is passed as a pointer to first element:

void func(int b[], int sizeOfArray) {...}

or

void func(int *b, int sizeOfArray) {...}

Functions and Arrays

Page 10: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Regular operators can be overloaded.

For example:

Matrix4x4 m = Matrix4x4(...);Matrix4x4 n = Matrix4x4(...);Matrix4x4 o = m * n; // Matrix multiplication!

Matrix4x4 Matrix4x4::operator*(const Matrix4x4 &v){

// Matrix multiplication implementation}

(take a peek inside VectorMath.h and VectorMath.cpp)

Operator Overloading

Page 11: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

For example, might sometimes be good for debugging

std::cout << "drawing..." << std::endl;

Prints “drawing...” to standard output

You can also use

printf(“drawing...”);

which is more C-like

Output

Page 12: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Lines beginning with #

The pre-processor is executed prior to the compiler

#define X

#ifdef Xprintf(“X is defined”);

#elseprintf(“X is not defined”);

#endif

Preprocessor

Page 13: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

cplusplus.comhttp://www.cplusplus.com/doc/tutorial/

More info

Page 14: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Moving on...

RenderChimpScene Graph

Page 15: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Overview

Page 16: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

C++ Class Hierarchy

abstract classesclass inheritance

Page 17: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Scene

X

Y

Z

Page 18: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Scene graph building example

X

Y

Z

World *world = SceneGraph::createWorld(“world1”);

Page 19: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Scene graph building example

X

Y

Z

Camera *camera = SceneGraph::createCamera(“camera1”);camera->setPerspectiveProjection(1.2f, 4.0f/3.0f, 1.0f, 1000.0f);world->setActiveCamera(camera);world->attachChild(camera);

Page 20: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Scene graph building example

X

Y

Z

Light *light = SceneGraph::createLight(“light1");light->setColor(1.0f, 1.0f, 1.0f);light->setTranslate(10.0f, 10.0f, 10.0f);world->attachChild(light);

Page 21: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Scene graph building example

Group *solarsys = SceneGraph::createGroup(“solarsys");

Geometry *sun =SceneGraph::createGeometry(“sun”, "sphere.obj",

true);solarsys->attachChild(sun);

Page 22: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Scene graph building example

world->attachChild(solarsys);

Page 23: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Scene graph building example

X

Y

Z

Page 24: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Hierarchical transformation

leftGroup rightGroup

Page 25: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

leftGroup rightGroup

rightGroup->translate(2.0f, 1.0f, 0.0f);

Hierarchical transformation

Page 26: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

leftGroup rightGroup

Hierarchical transformation

rightGroup->translate(2.0f, 1.0f, 0.0f);

Page 27: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Each node can have any number of children and may or may not have a parent.

void attachChild(...);void detachChild(...);void detachFromParent(...);Node *getChild(...);Node *getNextSibling();Node *getParent();

Duplicate the node, either by itself its entire subtree.

Node *duplicate(...);

Page 28: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

The world node is a convenient root node for your graph (it's not permitted anywhere else in the scene graph).

Draw the entire scene graph using:

void renderAll(...);

Must set an active camera. Render scene using this.

void setActiveCamera(...);

Page 29: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

All nodes besides the World node inherit from this.

Rotate (R), Scale (S) and Translate (T)... Hierarchically!Computed as:

Node transform = T * R * S

( think: scale, then rotate, then translate )

Page 30: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

An “empty” transformable node.

Use to stack hierarchical transformations.

Page 31: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Looks down negative Z-axis

Projection is set to either orthogonal;

void setOrthogonalProjection(...);

perspective;

void setPerspectiveProjection(...);

...or a custom matrix;

void setProjection(...);

Page 32: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Perspective projection:

Page 33: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Very simple point light source. Color (RGB) Intensity multiplier Falloff

From the SceneGraph's perspective; basically just a point in your scene with a few added attributes.

Page 34: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

A node with some geometry attached VertexArray defines primitives (usually triangles). Attached ShaderProgram can define appearance (or just about

anything else).

Page 35: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Nodes vs. Resources

Nodes: Describe spatial hierarchical relationships in a scene. Organized in a tree-like manner. One parent per node. One node describes one object. Relatively cheap (~10s - 100s of bytes)

Resources: Describe data. Not organized in any particular way. One resource can be instantiated many times. Relatively expensive (~10s – 100...0s of bytes)

Page 36: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Nodes vs. Resources

Imagine a game with two enemies...

Their triangle data is identical. Storing that data twice = twice as expensive!

...what about 50 enemies?

Solution: Shared data!

However: located atdifferent positions inthe scene.

Page 37: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Scene Graph Resources

Page 38: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Resources are identified by a unique identifier.

Can be referenced by multiple Nodes and/or other Resources

Page 39: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

An array of attributes associated with each vertex

Most obvious: Also common:

texture coordinates: normal: color:

May be of different type sizes (float, byte, etc)

More on thisin seminar 2

Page 40: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Supported formats: RGBA, RGB, Grayscale

Filtering (to prevent aliasing): Nearest neighbour, Bilinear, Trilinear

Wrapping: Clamp, repeat, mirrored repeat

Can be loaded from a file or produced at runtime

More on thisin seminar 3

Page 41: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

(a.k.a. Skybox)

A set of 6 image files, one for each face on a cube seen from the inside.

Can be used for reflecting materials, for example.

Page 42: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Render to texture(s) instead of frame buffer.

Useful for multi pass algorithms: Post-processing shaders (blur, bloom, ...) Shadow maps Deferred shading

Page 43: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

Renderer specific code.

In OpenGL 2.0: One vertex shader + one fragment shader

Vertex Shader Loaded from gl20.vs or gles20.vs source files. Per vertex calculations

Fragment Shader Loaded from gl20.fs or gles20.fs source files. Per pixel (fragment) calculations

Shaders are compiled at run-time

More on thisin seminar 3 & 4

Page 44: Introductory Seminar C++ & RenderChimp EDA221 Introduction to Computer Graphics Universitetsadjunkt Mathias Haage (mathias.haage@cs.lth.se) Authors: Magnus.

To get started...

...you’ll need:

Visual Studio C++Free Express-editions at Microsoft: http://www.microsoft.com/express/Downloads/

Recommended: Visual Studio C++ Express 2010

RenderChimp In-house scene graph engine. Available at course webpage

Download, unzip and then click solution file (.sln) to start up

Build & Run by pressing F5