39
Introduction to OpenGL Transformations, Viewing and Lighting Athanasios Karamalis Email: [email protected] Room: FMI 03.13.060

Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

  • Upload
    buibao

  • View
    303

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Introduction to OpenGLTransformations, Viewing and Lighting

Athanasios Karamalis

Email: [email protected]

Room: FMI 03.13.060

Page 2: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon
Page 3: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

What’s OpenGL

• OpenGL = Open Graphics Library

• Developed by Silicon Graphics (first version 1992)

• Independent of

� Platform

� Programming Language

� Hardware/ Graphics Card� Hardware/ Graphics Card

• Current Version 4.1

• API Specification

• Does only graphics. Does not handle windowing and input events

• Extensions to specification provide additional functionality to OpenGL

• Websites http://www.opengl.org, http://glew.sourceforge.net/

Page 4: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

OpenGL API– Function Naming Convection

<Library prefix><Main command><Optional argument count> <Optional

argument type>

Example: glColor3f

gl : Prefix OpenGL function

Color : Main Command

3 : number of arguments

f : type of arguments (here float)

glColor4f, glColor3i, glVertex3f,...

Page 5: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Clear your scene before drawing

glClearColor(0.0,0.0,0.0,0.0)

glClear(GL_COLOR_BUFFER_BIT)

• Color Format is specified as RGBA (Red, Green, Blue, Alpha)

• Each value is in the range [0.0..1.0]• Each value is in the range [0.0..1.0]

Page 6: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Rendering Basic Primitives

Draw Primitive (version 2.1):

glColor3f(1.0f,0.0f,0.0f);

glBegin(GL_POLYGON);

glVertex3f(0.0, 0.0, -1.0);

glVertex3f(0.0, 3.0, -1.0);

glColor3f(0.0f,1.0f,0.0f); glColor3f(0.0f,1.0f,0.0f);

glVertex3f(4.0, 3.0, -1.0);

glVertex3f(6.0, 1.5, -1.0);

glVertex3f(4.0, 0.0, -1.0);

glEnd();

Coordinates in (x,y,z)

x

z

y

Page 7: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Display lists

• Upload drawing calls between glBegin() and glEnd() to the GPU

• Increases performance, especially for complex objects

• Generate listGLuint model;model = glGenLists(2); // generate two lists and store names in model

• Fill lists with drawing calls• Fill lists with drawing callsglNewList(model, GL_COMPILE);

// glBegin und glEnd() etc.

glEndList();

• Draw from listglCallList( model );

• Use second listglNewList(model+1, GL_COMPILE); ...

glCallList( model+1 );

• Check if lists existsGLboolean glIsList( model + n );

Page 8: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Better Performance with Vertex Buffer Objects (VBO)

glBegin()…glEnd() Deprecated after OpenGL 2.1

Example for drawing triangular mesh

// Buffer ID’s

GLuint vbo,ibo; GLuint vbo,ibo;

//Client-side vertices

GLfloat* vertices = new GLfloat[NumVertices*3];

//Client-side indices

GLuint* indices = new GLfloat[NumIndices*3];

Page 9: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Generate Buffers and Upload Data

// Generate buffer

glGenBuffers(1, &vbo);

// Bind VBO for subsequent calls

glBindBuffer(GL_ARRAY_BUFFER, vbo);

// Upload vertex data to GPU

glBufferData(GL_ARRAY_BUFFER, NumVertices *sizeof(GLfloat), glBufferData(GL_ARRAY_BUFFER, NumVertices *sizeof(GLfloat),

vertices , GL_DYNAMIC_DRAW);

glGenBuffers(1, &ibo);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);

// Upload index data to GPU

glBufferData(GL_ELEMENT_ARRAY_BUFFER, NumIndices*sizeof(GLuint),

indices , GL_DYNAMIC_DRAW);

delete[] vertices; delete[] indices;//Client-side now not required

Page 10: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Draw VBO

// Draw only vertices, more required for normals and UV

glEnableClientState(GL_VERTEX_ARRAY);

//vector consists of 3 floats

glVertexPointer(3, GL_FLOAT, 0, 0);

//Draw the geometry

glDrawElements(GL_TRIANGLES, NumIndices, GL_UNSIGNED_INT, 0);glDrawElements(GL_TRIANGLES, NumIndices, GL_UNSIGNED_INT, 0);

// Disable VBO Drawing

glDisableClientState(GL_VERTEX_ARRAY);

//Unbind buffers

glBindBuffer(GL_ARRAY_BUFFER, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

Page 11: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

External helpers for models

• GLUT Objects

• glutSolidSphere(GLdouble radius, GLint slices, GLint

stacks), glutSolidCube(GLdouble size),

glutSolidCone(GLdouble base, GLdouble height, GLint

slices, GLint stacks), glutSolidTeapot(GLdouble size) and

moremore

• Also available as wireframe (wireframe visualization can also be enabled

through OpenGL calls)glutWireTeapot(GLdouble size)

• http://www.opengl.org/resources/libraries/glut/

Page 12: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

OpenGL is a State-Machine

• Every change on the state affects subsequent calls and is only explicitly

change with a new OpenGL call

� E.g. . glColor3f(1.0,0.0,0.0), the vertex color is set to red until it is

changed

� The current state can be pushed onto a stack and recalled later

� glPushAttrib(GLbitfield mask)pushes the state specified by mark on the � glPushAttrib(GLbitfield mask)pushes the state specified by mark on the

stacke.g. glPushAttrib(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

stores the current color and depth information on the stack

� glPopAttrib() restores the state from the top of the stack

Page 13: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

View your scene

Page 14: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

OpenGL Rendering Pipeline

Page 15: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Vertices in Homogenous Coordinates

• Increase column space by one dimension,

usually

• All transformations represented in matrix form

w

z

y

x

z

y

x

1=w

• All transformations represented in matrix form

• Column-major format

w

44 xM

=

w

z

y

x

mmmm

mmmm

mmmm

mmmm

w

z

y

x

161284

151173

141062

13951

'

'

'

'

Page 16: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

The Modelview Matrix

glMatrixMode(GL_MODELVIEW);

Setting the ModelView matrix corresponds to

• Positioning/Orienting the camera

• Positioning/Orienting the model• Positioning/Orienting the model

Page 17: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

The Projection Matrix

glMatrixMode(GL_PROJECTION);

Setting the Projection matrix corresponds to

• Choosing a lens for the camera

• Defining the viewing/culling frustum• Defining the viewing/culling frustum

Page 18: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Operations on OpenGL matrices

• glMatrixMode(matrix)

matrix: GL_MODELVIEW, GL_PROJECTION, GL_TEXTURE, GL_COLOR

specify which matrix should be affected by subsequent operations

• glLoadIdentity();

set current matrix to identity, i.e. [1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1]

• glLoadMatrix{f,d}(pointerToMatrix);

set current matrix to the one stored in pointerToMatrix

• glMultMatrix{f,d}(pointerToMatrix);

multiply current matrix with the one stored in pointerToMatrix

• GLdouble model[16];

glGetDoublev(GL_MODELVIEW_MATRIX,model);

read and store matrix (here modelview matrix) into model

Page 19: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Perspective Projection

No need to define projection matrix manually.

glMatrixMode(GL_PROJECTION); glLoadIdentity();

gluPerspective(fovy, aspect, near, far);

glFrustum(left, right, bottom, top, near, far);

Attention: near >= 1.0, otherwise depth accuracy unacceptable

Page 20: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

gluLookAt for Perspective Projection

gluLookAt(eyex, eyey, eyez, centerx, centery, centerz, upx,

upy, upz)

eyex,eyey,eyez : camera position

centerx, centery, centerz : view-directioncenterx, centery, centerz : view-direction

upx, upy, upz : camera orientation/pitch

Page 21: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Orthogonal Projection

Orthogonal projection (used in CAD applications)

glMatrixMode(GL_PROJECTION); glLoadIdentity();

glOrtho(left, right, bottom, top, near, far),

Attention: near >= 1.0, otherwise depth accuracy unacceptable

Page 22: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Transformation - Translation

glMatrixMode(GL_MODELVIEW);

glTranslate3f(x,y,z);

y

010

001

TIt

tx

TI vv y

x

Translation

=

10

1000

100

010 TI

t

t

z

y pTI

pvv

=

10'

Translations matrix Translation

Page 23: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Transformation - Rotation

glRotatef(angle,x,y,z);

y

Euler-Angle for Rotation R=RzRyRx

y

x

Rotation

Page 24: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Transformation Scaling

glScale(x,y,z);

yy

x

Scaling

1

*

1000

000

000

000

1

z

y

x

s

s

s

z

y

x

z

y

x

Page 25: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Order of transformations

glTranslatef( 2.f, 0.f, 0.f );

glRotatef( 45.f, 0.f, 0.f, 1.f );

glRotatef( 45.f, 0.f, 0.f, 1.f );

glTranslatef( 2.f, 0.f, 0.f );

1221 TTTT ∗≠∗

Teapot initially at center. Image left result of first rotate than translate. Image on right first

translate than rotate

Page 26: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Matrix Stack

glMatrixMode(Glenum mode) : Define active stack

• Each of the previous OpenGL transformation commands

� Generates a 4x4 matrix

� Multiplies it with the matrix at the top of the active stack

glPushMatrix() : pushes the current matrix stack down by one, duplicating

the current matrix.

glPopMatrix() : replace matrix on top

of the stack with the matrix below it

Page 27: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Example with Multiple Objects and Transformations

glPushMatrix();

glColor3f(1.f,0.f,0.f);

glutSolidTeapot(5.0); // Red teapot

glTranslatef(0.0f,25.0f,0.0f);

glColor3f(0.f,1.f,0.f);

glutSolidTeapot(5.0); // Green teapot

glTranslatef(20.0f,0.0f,0.0f);

glColor3f(0.f,0.f,1.f);glColor3f(0.f,0.f,1.f);

glutSolidTeapot(5.0); // Blue teapot

glTranslatef(0.0f,-15.0f,0.0f);

glRotatef(45.0f,0.0f,0.0f,1.0f);

glColor3f(0.f,1.f,1.f);

glutSolidTeapot(5.0); // Cyan teapot

glPopMatrix();

glPushMatrix();

glColor3f(1.f,0.f,1.f);

glTranslatef(0.0f,-25.0f,0.0f);

glutSolidTeapot(5.0); // Magenta teapot

glPopMatrix();

Page 28: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Hide-Surface-Removal with Depth-Buffer

• For each pixel a depth value is stored

• Depthfunc specifies condition for pixels passing the depth test

• GL_LEQUAL: The incoming pixel (fragment) passes the depth test if its depth is less or equal to the one stored in the buffer

glEnable(GL_DEPTH_TEST);

glDepthFunc(GL_LEQUAL); //default

depth is less or equal to the one stored in the buffer

• Example: Teapots have the same size. Perspective projection. Red teapot is further away from the camera.

Depth Test Disabled Depth Test Enabled

Page 29: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

OpenGL Lighting

Page 30: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Different Types of Light

• Ambient Light

� “Global” light emitted from a scene

• „ Diffuse Light”

� Depends on surface normal + light direction

Ambient

� Depends on surface normal + light direction

• „Specular Light“

� Depends on surface normal + light direction + camera (eye)

position

Diffuse

Specular

Page 31: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Phong Lighting Model

Ambient Diffuse Specular

+ +

Phong lighting

=

Page 32: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Light in OpenGL

GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 }; GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };

glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glEnable(GL_LIGHTING); glEnable(GL_LIGHT0);

Maximal number of lights:int maxnumlights = 0;glGetIntegerv(GL_MAX_LIGHTS, &maxnumlights);

The performance decreases with increasing number of lights

Page 33: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Light in OpenGL

GLfloat light_position[] = { 1.0, 1.0, 1.0, w };

glLightfv(GL_LIGHT0, GL_POSITION, light_position);

Positional light source w>0

Directional light source w = 0

glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 15.0f);

glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0f);

glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.0f);

glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.0f);

glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, direction);

glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, spotCutOff);

Spot Light

Page 34: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Material Properties

glFloat material[] = {0.3, 0.6, 0.1, 1.0};

glMaterialfv(GL_FRONT_AND_BACK , GL_AMBIENT_AND_DIFFUSE,

material);

GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };

GLfloat shininess[] = { 2.5 };

glMaterialfv(GL_FRONT, GL_SPECULAR, specular);

glMaterialfv(GL_FRONT, GL_SHININESS, shininess);

See OpenGL Specification for complete list of available parameters for glLight and glMaterial

Page 35: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

OpenGL is not a GUI Toolkit

• OpenGL does not offer functionality to generate and manage GUI windows

• OpenGL assumes that the programmer provides a valid GUI window together

with an OpenGL render context

• Window and render context can be provided through different toolkits, such

as: GLUT, Qt, FLTK, MFC,...

• FreeGLUT : A simple platform independent library for generating a window

with OpenGL context.

� Can also handle keyboard and mouse events.

• Websites http://freeglut.sourceforge.net/

Page 36: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Kinect skeleton tracking

• Add <Node type="User" name="User1"/> to the xml file

• OpenNI has a UserGenerator similar to ImageGenerator or DepthGenerator

• Requires a calibration pose: „Y“

• Gives position + orientation for each joint

Page 37: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

This was an introduction, OpenGL has a LOT more to offer

• For more info see:

• The Red Book, Version 1.1 is freely available online

http://www.opengl.org/documentation/red_book/

• NeHe Online Tutorials including code http://nehe.gamedev.net/

• The OpenGL specification (very technical, but includes everything)• The OpenGL specification (very technical, but includes everything)

http://www.opengl.org/

• GLUT Specification

http://www.opengl.org/resources/libraries/glut/spec3/spec3.html

• OpenGL Interactive Tutorials

http://www.xmission.com/~nate/tutors.html

• Common OpenGL Pitfalls

http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/

• www.GameDev.net, Lots of useful articles and active developer

community

Page 38: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

High-End Visualization

• OpenGL Fixed-Pipeline is relatively very limited

• Shader programming allows high-end graphics programming

• Got interested?

� Check out GPU Gems 1, 2 ,3 freely available at http://www.developer.nvidia.com/

to see what is possible

• Further literature

� GLSL – OpenGL Shading Language

� Specification http://www.opengl.org/documentation/glsl/

� Tutorials http://www.lighthouse3d.com/opengl/glsl/

� OpenGL(R) Shading Language by Randi J. Rost (aka Orange Book)

� GPU Pro and Shader X series by Wolfgang J. Engel

Page 39: Introduction to OpenGL - WebHomecampar.in.tum.de/files/teaching/2011ss/KinectCourse/IntroOpenGL.pdf · What’s OpenGL • OpenGL = Open Graphics Library • Developed by Silicon

Thank you for your attention.Questions?