23
David Luebke 06/15/22 CS 551 / 645: Introductory Computer Graphics David Luebke [email protected] http://www.cs.virginia.edu/~cs551

David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke [email protected] cs551

Embed Size (px)

Citation preview

Page 1: David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

CS 551 / 645: Introductory Computer Graphics

David Luebke

[email protected]

http://www.cs.virginia.edu/~cs551

Page 2: David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Recap: OpenGL

OpenGL provides an interface and implementation for interactive rendering.

It has become a standard because:– A standard was badly needed– OpenGL is pretty good– SGI promoted it and Microsoft (sorta) bought in

OpenGl is particularly tuned to hardware-accelerated transformation, lighting, texturing, and Z-buffering

Page 3: David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Recap: OpenGL Conventions

Functions in OpenGL start with gl (or glu) Function names indicate argument type/#

– E.g., glColor3f() vs glColor3fv() vs glColor4ub()

Geometry is specified as a list of vertices between glBegin() and glEnd() calls:

glBegin(GL_POLYGON);

glVertex3f(x1, y1, z1);

glVertex3f(x2, y2, z2);

glVertex3f(x3, y3, z3);

glEnd();

Page 4: David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Recap: Miscellaneous OpenGL

The vertices of the front side of a polygon are ordered counterclockwise

You can draw multiple triangles between glBegin(GL_TRIANGLES) and glEnd()

The GL_TRIANGLE_STRIP primitive reduces redundancy by sharing vertices:

v0v2

v1v3

v4

v5

Page 5: David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Miscellaneous OpenGL

The GL_TRIANGLE_FAN primitive is another way to reduce vertex redundancy:

v0

v1

v2

v3v4

v5

v6

Page 6: David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Recap: OpenGL Lighting

OpenGL binds our Phong lighting coefficients (ka, kd, ks, nshiny) into materials

Calling glMaterialfv() sets a single attribute of the current material

Example: float green[] = {0, 1, 0, 1};

float white[] = {1, 1, 1, 1};

glMaterialfv(GL_FRONT, GL_DIFFUSE, green);

glMaterialfv(GL_FRONT, GL_SPECULAR, white);

Page 7: David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Recap: OpenGL Lighting

OpenGL supports at least 8 lights, with parameters set by the glLight() call:

float l_amb [] = {.1, .1, .1, 1.0};

float l_diff[] = {1, 0, 0, 1};

float l_spec[] = {1, 1, 1, 1};

float l_pos[] = {10, 100, 30, 0};

glLightfv(GL_LIGHT0, GL_AMBIENT, l_amb);

glLightfv(GL_LIGHT0, GL_DIFFUSE, l_diff);

glLightfv(GL_LIGHT0, GL_SPECULAR, l_spec);

glLightfv(GL_LIGHT0, GL_POSITION, l_pos);

(the 4th coordinate in l_pos is 0.0 for a directional light, 1.0 for a point light)

Page 8: David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

lights

i

n

sisd

id

iaatotal

shiny

RVkILNkIIkI#

1

ˆˆˆˆ

Errata: OpenGL Lighting

Recall the Phong lighting model:

OpenGL modifies this slightly:

– Each light contributes separately to ambient term

– Lights have a separate intensity for specular reflection

(Why might this be useful?)

lights

i

n

sdi

ambientatotal

shiny

RVkLNkIIkI#

1

ˆˆˆˆ

Page 9: David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

OpenGL: Lighting

Don’t forget to enable lighting and each light:glEnable(GL_LIGHTING);

glEnable(GL_LIGHT0); Can set the lighting model too:

– Intensity of the ambient light source– Whether to treat eye point as infinitely far away– Whether to perform lighting calculations for both

sides of polygons– All these things have reasonable default values– man glLightModel for details

Page 10: David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

OpenGL: Display Lists

OpenGL can “compile” a series of rendering commands into a display list...

glNewList(1, GL_COMPILE);

glBegin(GL_TRIANGLES);

glVertex3fv(v0);

/* draw lots of triangles… */

glVertex3fv(v2);

glEnd();

glEndList();

…which can be rendered with a single call:glCallList(1);

Page 11: David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

OpenGL: Display Lists

Display lists can contain:– Geometry– Color, material, and texture specifications– Matrix transforms (up shortly)– Other display lists!

Display lists are not only handy, they usually increase performance– Why might OpenGL be able to render a series of

commands faster if they have been compiled into a display list?

Page 12: David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

OpenGL: Matrix Manipulation

OpenGL keeps two matrices that vertices are multiplied by upon calling glVertex()– The modelview matrix combines all modeling

transforms and the viewing transform– The projection matrix performs the projection (usually a

perspective projection)– Various commands affect the current matrix– You need to specify which matrix is current:

E.g., glMatrixMode(GL_MODELVIEW) or glMatrixMode(GL_PROJECTION)

– glLoadIdentity() replaces the contents of the current matrix with the identity matrix

Page 13: David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

OpenGL: Modeling Transforms

Some OpenGL commands generate transformation matrices:

glTranslatef(Tx, Ty, Tz);

glRotatef(angleDegrees, Ax, Ay, Az);

glScalef(Sx, Sy, Sz);

The resulting matrix concatenates to the right of the current matrix

Page 14: David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

OpenGL: Modeling Transforms

Example:glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glTranslatef(…);

glRotatef(…); Result: the modelview matrix is set to:

I • T • R == T • R

which then multiplies all following vertices– Thus transformations appearing last in the program

have the first effect on the geometry

Page 15: David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

OpenGL: Viewing Transforms

Viewing transforms are treated the same way Ex: gluLookAt() computes a lookat matrix

and concatenates it with the current matrix: gluLookAt(eyeX, eyeY, eyeZ,

centerX, centerY, centerZ,

upX, upY, upZ);– Again, this matrix postmultiplies the current matrix– Should gluLookAt() be called first or last?

Page 16: David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

OpenGL: Projection Transforms

The projection matrix is generally used for the perspective projection matrix– Why do you suppose OpenGL separates the

modelview and projection matrices?

gluPerspective() creates a projection matrix similarly to the call in assignment 6

gluPerspective(double FOVy, double aspect,

double near, double far);

– FOVy: field of view (°) in the y vertical (y) direction aspect: viewport width (y) divided by height (x)

Page 17: David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

The Scene Graph

Recall the concept of instancing, or using the same geometry for multiple objects– Example: 4 wheels on car– How might we use display lists for instancing?

Compile geometry (say a car tire, centered about the origin) into a display list

Set up matrices: viewing transform + modeling transform(s) to put origin at front left corner of car

Call display list for tire Set up matrices, this time putting origin at front right of car Call display list for tire [Etc…]

– Why is this inefficient?

Page 18: David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

The Scene Graph

Answer: because many objects in a scene typically share multiple transformations

The scene graph captures transformations and object-object relationships in a DAG:

Robot

BodyHead

ArmTrunkLegEyeMouth

Objects

Instancing(i.e, a matrix)

Legend

World

Page 19: David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

The Scene Graph

Traverse the scene graph in depth-first order, concatenating and undoing transforms:– For example, to render the robot:

Apply robot head matrix Apply head mouth matrix

– Render mouth Un-apply head mouth matrix Apply head left eye matrix

– Render eye Un-apply head left eye matrix Apply head right eye matrix

– Render eye Un-apply head right eye matrix

Un-apply robot head matrix Apply robot body matrix

How should we implement this

“un-apply” business?

Page 20: David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

The Scene Graph in OpenGL

OpenGL maintains a matrix stack of modeling and viewing transformations:

ArmTrunkLegEyeMouth

Head Body

Robot

Foot

MatrixMatrixStackStack

VisitedVisited

UnvisitedUnvisited

ActiveActive

Page 21: David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

OpenGL: The Matrix Stack

The user can save the current transformation matrix by pushing it onto the stack with glPushMatrix()

The user can later restore the most recently pushed matrix with glPopMatrix()

These commands really only make sense when in GL_MODELVIEW matrix mode

Page 22: David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

OpenGL: Matrix Stack Example

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glTranslatef(…);

// save translation matrix:

glPushMatrix();

glRotatef(…);

// render something translated & rotated:

glCallList(foo);

// restore pushed matrix, undoing rotation:

glPopMatrix();

// render something else, no rotation:

glCallList(bar);

Page 23: David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Coming Up:

Animation: smooth (flicker-free) motion using double buffering

More OpenGL tricks– Backface culling– Gouraud shading– Computing vertex normals