Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf ·...

36
Introduction to Computer Graphics with OpenGL Ioannis Tsiombikas [email protected] Introduction to Computer Graphics with OpenGL – p. 1

Transcript of Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf ·...

Page 1: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

Introduction to Computer Graphicswith OpenGL

Ioannis [email protected]

Introduction to Computer Graphics with OpenGL – p. 1

Page 2: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

Computer graphics

Algorithms to transform mathematical representations of3D environments to images.

Possible representations:

Polyhedral approximation of surfaces.

Mathematical equations describing surfaces (i.e.x2 + y2 + z2 = r2).

Volume defined by density values (binary or not) atdiscrete points in a 3D scalar field (voxels).

Introduction to Computer Graphics with OpenGL – p. 2

Page 3: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

Computer graphics

Algorithms to transform mathematical representations of3D environments to images.

Possible representations:

Polyhedral approximation of surfaces.

Mathematical equations describing surfaces (i.e.x2 + y2 + z2 = r2).

Volume defined by density values (binary or not) atdiscrete points in a 3D scalar field (voxels).

Introduction to Computer Graphics with OpenGL – p. 2

Page 4: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

Computer graphics

Algorithms to transform mathematical representations of3D environments to images.

Possible representations:

Polyhedral approximation of surfaces.

Mathematical equations describing surfaces (i.e.x2 + y2 + z2 = r2).

Volume defined by density values (binary or not) atdiscrete points in a 3D scalar field (voxels).

Introduction to Computer Graphics with OpenGL – p. 2

Page 5: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

Computer graphics

Algorithms to transform mathematical representations of3D environments to images.

Possible representations:

Polyhedral approximation of surfaces.

Mathematical equations describing surfaces (i.e.x2 + y2 + z2 = r2).

Volume defined by density values (binary or not) atdiscrete points in a 3D scalar field (voxels).

Introduction to Computer Graphics with OpenGL – p. 2

Page 6: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

Real–time graphics

The major distinction in graphics: real–time vs off–linerendering.

Real–time graphics algorithms sacrifice image quality toachieve rapid, sub–second, drawing rates. This enablesus to interactively rearrange objects or the view–pointthus allowing us to “navigate” in a 3D environment ormanipulate it.

Used in games, interactive visualizations, 3Dmodelling/animation tools, etc.

Introduction to Computer Graphics with OpenGL – p. 3

Page 7: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

OpenGL

OpenGL is an open standard for dealing with 3Dgraphics, with source–compatible implementations onevery major platform capable of graphical output.

Controlled by a special committee, the ArchitectureReview Board (ARB).

Targeted towards interactive programs and real–timegraphics.

Stable programming interface.

Flexible due to an extension mechanism foradditional “cutting–edge” functionality.

Simple state–machine design.

Introduction to Computer Graphics with OpenGL – p. 4

Page 8: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

The rendering pipeline

Introduction to Computer Graphics with OpenGL – p. 5

Page 9: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

Transformations

A 3x3 matrix defines a linear transformation in 3D space(rotation, scaling, etc.). However it is more convinient towork on 4D space and at the end keep a 3D projection ofthat.

Our vectors become (x, y, z, w) with w = 1 for theequivalent of 3D vectors, and we use 4x4 matrices totransform them.

By using this technique (called homogeneouscoordinates) we can place points at infinity (w = 0), butmost importantly, include 3D translation in ourtransformation matrices.

Introduction to Computer Graphics with OpenGL – p. 6

Page 10: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

Transformations

Transform vectors by multiplying them with theappropriate matrix.

x′

y′

z′

w′

=

m11 m12 m13 m14

m21 m22 m23 m24

m31 m32 m33 m34

m41 m42 m43 m44

·

x

y

z

w

To concatenate a series of transformations in one matrix,multiply all the matrices together.Note: order matters! Matrix multiplication is notcommutative.

Introduction to Computer Graphics with OpenGL – p. 7

Page 11: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

Transformations: rotation

Rotx(θ) =

1 0 0 0

0 cos θ − sin θ 0

0 sin θ cos θ 0

0 0 0 1

Roty(θ) =

cos θ 0 sin θ 0

0 1 0 0

− sin θ 0 cos θ 0

0 0 0 1

Rotz(θ) =

cos θ − sin θ 0 0

sin θ cos θ 0 0

0 0 1 0

0 0 0 1

Introduction to Computer Graphics with OpenGL – p. 8

Page 12: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

Transformations: translation/scaling

T (tx, ty, tz) =

1 0 0 tx0 1 0 ty0 0 1 tz0 0 0 1

S(sx, sy, sz) =

sx 0 0 0

0 sy 0 0

0 0 sz 0

0 0 0 1

Introduction to Computer Graphics with OpenGL – p. 9

Page 13: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

OpenGL transformations

OpenGL maintains matrix stacks for all stages of thepipeline. However world and view transformations arecombined in one (modelview).To manipulate the matrix state, first specify which stackwe wish to affect with glMatrixMode(), and call:

glLoadMatrixf() / glMultMatrixf() to load orconcatenate an arbitrary matrix to the top matrix.

glLoadIdentity() to load the identity matrix.

glTranslatef() / glRotatef() / glScalef() toconcatenate the desired transformation matrix to thetop matrix.

glPushMatrix() / glPopMatrix() for the usual stackoperations.

Introduction to Computer Graphics with OpenGL – p. 10

Page 14: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

OpenGL transformations

OpenGL maintains matrix stacks for all stages of thepipeline. However world and view transformations arecombined in one (modelview).To manipulate the matrix state, first specify which stackwe wish to affect with glMatrixMode(), and call:

glLoadMatrixf() / glMultMatrixf() to load orconcatenate an arbitrary matrix to the top matrix.

glLoadIdentity() to load the identity matrix.

glTranslatef() / glRotatef() / glScalef() toconcatenate the desired transformation matrix to thetop matrix.

glPushMatrix() / glPopMatrix() for the usual stackoperations.

Introduction to Computer Graphics with OpenGL – p. 10

Page 15: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

OpenGL transformations

OpenGL maintains matrix stacks for all stages of thepipeline. However world and view transformations arecombined in one (modelview).To manipulate the matrix state, first specify which stackwe wish to affect with glMatrixMode(), and call:

glLoadMatrixf() / glMultMatrixf() to load orconcatenate an arbitrary matrix to the top matrix.

glLoadIdentity() to load the identity matrix.

glTranslatef() / glRotatef() / glScalef() toconcatenate the desired transformation matrix to thetop matrix.

glPushMatrix() / glPopMatrix() for the usual stackoperations.

Introduction to Computer Graphics with OpenGL – p. 10

Page 16: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

OpenGL transformations

OpenGL maintains matrix stacks for all stages of thepipeline. However world and view transformations arecombined in one (modelview).To manipulate the matrix state, first specify which stackwe wish to affect with glMatrixMode(), and call:

glLoadMatrixf() / glMultMatrixf() to load orconcatenate an arbitrary matrix to the top matrix.

glLoadIdentity() to load the identity matrix.

glTranslatef() / glRotatef() / glScalef() toconcatenate the desired transformation matrix to thetop matrix.

glPushMatrix() / glPopMatrix() for the usual stackoperations.

Introduction to Computer Graphics with OpenGL – p. 10

Page 17: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

OpenGL transformations

OpenGL maintains matrix stacks for all stages of thepipeline. However world and view transformations arecombined in one (modelview).To manipulate the matrix state, first specify which stackwe wish to affect with glMatrixMode(), and call:

glLoadMatrixf() / glMultMatrixf() to load orconcatenate an arbitrary matrix to the top matrix.

glLoadIdentity() to load the identity matrix.

glTranslatef() / glRotatef() / glScalef() toconcatenate the desired transformation matrix to thetop matrix.

glPushMatrix() / glPopMatrix() for the usual stackoperations.

Introduction to Computer Graphics with OpenGL – p. 10

Page 18: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

OpenGL 3D object data

Vertices, grouped in triangles, quadrilaterals, or polygonsdefine the surfaces of objects in 3D space. Apart fromtheir positions that define the surface, there is a numberof additional per–vertex data commonly given toOpenGL:

Vertex colors (if lighting is disabled, useful forprecalculated lighting).

Normal vectors (used for lighting calculations).

Texture mapping coordinates.

Introduction to Computer Graphics with OpenGL – p. 11

Page 19: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

OpenGL 3D object data

Vertex data can be given to OpenGL in many ways.

Immediate mode, glBegin() / glEnd().

Vertex arrays (in GL client memory).

Vertex buffer objects (vertex arrays in GL servermemory).

Display lists.

Introduction to Computer Graphics with OpenGL – p. 12

Page 20: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

OpenGL 3D object data

Vertex data can be given to OpenGL in many ways.

Immediate mode, glBegin() / glEnd().

Vertex arrays (in GL client memory).

Vertex buffer objects (vertex arrays in GL servermemory).

Display lists.

Introduction to Computer Graphics with OpenGL – p. 12

Page 21: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

OpenGL 3D object data

Vertex data can be given to OpenGL in many ways.

Immediate mode, glBegin() / glEnd().

Vertex arrays (in GL client memory).

Vertex buffer objects (vertex arrays in GL servermemory).

Display lists.

Introduction to Computer Graphics with OpenGL – p. 12

Page 22: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

OpenGL 3D object data

Vertex data can be given to OpenGL in many ways.

Immediate mode, glBegin() / glEnd().

Vertex arrays (in GL client memory).

Vertex buffer objects (vertex arrays in GL servermemory).

Display lists.

Introduction to Computer Graphics with OpenGL – p. 12

Page 23: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

OpenGL 3D object data

Vertex data can be given to OpenGL in many ways.

Immediate mode, glBegin() / glEnd().

Vertex arrays (in GL client memory).

Vertex buffer objects (vertex arrays in GL servermemory).

Display lists.

Introduction to Computer Graphics with OpenGL – p. 12

Page 24: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

Lighting

For each vertex, a color is calculated as a function of theintensity of the illumination reflected off the surfacetowards the viewpoint, and the material of the surface.Then color from each polygon’s vertices are linearlyinterpolated across its surface to calculate the color ofeach pixel. Lights are represented as points, typically inworld coordinates.

A Ma +lnum∑

i=1

D(li, n) Md + S(li, v) Ms

D(l, n) = l · n

S(l, n, v, p) = (l · reflect(v, n))p

Introduction to Computer Graphics with OpenGL – p. 13

Page 25: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

Lighting

For each vertex, a color is calculated as a function of theintensity of the illumination reflected off the surfacetowards the viewpoint, and the material of the surface.Then color from each polygon’s vertices are linearlyinterpolated across its surface to calculate the color ofeach pixel. Lights are represented as points, typically inworld coordinates.

A Ma +lnum∑

i=1

D(li, n) Md + S(li, v) Ms

D(l, n) = l · n

S(l, n, v, p) = (l · reflect(v, n))p

Introduction to Computer Graphics with OpenGL – p. 13

Page 26: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

Lighting

For each vertex, a color is calculated as a function of theintensity of the illumination reflected off the surfacetowards the viewpoint, and the material of the surface.Then color from each polygon’s vertices are linearlyinterpolated across its surface to calculate the color ofeach pixel. Lights are represented as points, typically inworld coordinates.

A Ma +lnum∑

i=1

D(li, n) Md + S(li, v) Ms

D(l, n) = l · n

S(l, n, v, p) = (l · reflect(v, n))p

Introduction to Computer Graphics with OpenGL – p. 13

Page 27: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

Lighting

For each vertex, a color is calculated as a function of theintensity of the illumination reflected off the surfacetowards the viewpoint, and the material of the surface.Then color from each polygon’s vertices are linearlyinterpolated across its surface to calculate the color ofeach pixel. Lights are represented as points, typically inworld coordinates.

A Ma +lnum∑

i=1

D(li, n) Md + S(li, v) Ms

D(l, n) = l · n

S(l, n, v, p) = (l · reflect(v, n))p

Introduction to Computer Graphics with OpenGL – p. 13

Page 28: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

OpenGL Lighting: Materials

OpenGL handles light calculations if we provide vertexnormals, light and material parameters, and enable setthe appropriate state.

The following material parameters can be set using theglMaterialf() and glMaterialfv() functions:

Color (seperate for ambient, specular and diffuse).

Shininess (the specular power).

Self–illumination.

Introduction to Computer Graphics with OpenGL – p. 14

Page 29: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

OpenGL Lighting: Materials

OpenGL handles light calculations if we provide vertexnormals, light and material parameters, and enable setthe appropriate state.

The following material parameters can be set using theglMaterialf() and glMaterialfv() functions:

Color (seperate for ambient, specular and diffuse).

Shininess (the specular power).

Self–illumination.

Introduction to Computer Graphics with OpenGL – p. 14

Page 30: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

OpenGL Lighting: Materials

OpenGL handles light calculations if we provide vertexnormals, light and material parameters, and enable setthe appropriate state.

The following material parameters can be set using theglMaterialf() and glMaterialfv() functions:

Color (seperate for ambient, specular and diffuse).

Shininess (the specular power).

Self–illumination.

Introduction to Computer Graphics with OpenGL – p. 14

Page 31: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

OpenGL Lighting: Materials

OpenGL handles light calculations if we provide vertexnormals, light and material parameters, and enable setthe appropriate state.

The following material parameters can be set using theglMaterialf() and glMaterialfv() functions:

Color (seperate for ambient, specular and diffuse).

Shininess (the specular power).

Self–illumination.

Introduction to Computer Graphics with OpenGL – p. 14

Page 32: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

OpenGL Lighting: Lights

Light parameters are set using the glLightf() andglLightfv().

Light parameters:

Position or direction.

Color (seperated into ambient, diffuse and specularcomponents).

Optional spotlight illumination cone.

Optional distance attenuation coefficients.

Introduction to Computer Graphics with OpenGL – p. 15

Page 33: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

OpenGL Lighting: Lights

Light parameters are set using the glLightf() andglLightfv().

Light parameters:

Position or direction.

Color (seperated into ambient, diffuse and specularcomponents).

Optional spotlight illumination cone.

Optional distance attenuation coefficients.

Introduction to Computer Graphics with OpenGL – p. 15

Page 34: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

OpenGL Lighting: Lights

Light parameters are set using the glLightf() andglLightfv().

Light parameters:

Position or direction.

Color (seperated into ambient, diffuse and specularcomponents).

Optional spotlight illumination cone.

Optional distance attenuation coefficients.

Introduction to Computer Graphics with OpenGL – p. 15

Page 35: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

OpenGL Lighting: Lights

Light parameters are set using the glLightf() andglLightfv().

Light parameters:

Position or direction.

Color (seperated into ambient, diffuse and specularcomponents).

Optional spotlight illumination cone.

Optional distance attenuation coefficients.

Introduction to Computer Graphics with OpenGL – p. 15

Page 36: Introduction to Computer Graphics with OpenGLnuclear.mutantstargoat.com/articles/gltut.pdf · Introduction to Computer Graphics with OpenGL – p. 13. Lighting For each vertex, a

OpenGL Lighting: Lights

Light parameters are set using the glLightf() andglLightfv().

Light parameters:

Position or direction.

Color (seperated into ambient, diffuse and specularcomponents).

Optional spotlight illumination cone.

Optional distance attenuation coefficients.

Introduction to Computer Graphics with OpenGL – p. 15