CS 354 Object Viewing and Representation

39
CS 354 Object Viewing and Representation Mark Kilgard University of Texas February 9, 2012

description

CS 354 Computer GraphicsUniversity of Texas, AustinFebruary 9, 2012

Transcript of CS 354 Object Viewing and Representation

Page 1: CS 354 Object Viewing and Representation

CS 354Object Viewing and RepresentationMark KilgardUniversity of TexasFebruary 9, 2012

Page 2: CS 354 Object Viewing and Representation

CS 354 2

Today’s material

In-class quiz Lecture topic: viewing + object representation

Looking at an object Representing an object

Assignment Reading

Chapter 4, page 238-241; Chapter 6, 331-341 (Tuesday) Chapter 4, pages 241-249

You should be working on Project #1

Page 3: CS 354 Object Viewing and Representation

CS 354 3

Administrative I was sick Tuesday

Thanks to Dr. Fussell for lecturing in my absence

Final exam is during the exam period (Instead of last day of class) Making last day of class a review session instead Date & time: Thursday, May 10, 2:00-5:00 pm Location: TBD

Quiz feedback started Look on piazza

Page 4: CS 354 Object Viewing and Representation

CS 354 4

My Office Hours

Tuesday, before class Painter (PAI) 5.35 8:45 a.m. to 9:15

Not last Tuesday Thursday, after class

ACE 6.302 11:00 a.m. to 12:00

Page 5: CS 354 Object Viewing and Representation

CS 354 5

Last time, this time

Last two lectures, we discussed Consequences of homogeneous coordinates Thursday:

Projection transforms: ortho and frustum Tuesday:

Dr. Fussell discussed rotations with you This lecture

How do we look at objects How do we represent objects for interactive

rendering

Page 6: CS 354 Object Viewing and Representation

CS 354 6

Daily Quiz

1. Multiple choice: Renaissance artists incorporated perspective into their artwork. Which OpenGL command best corresponds to this type of transform:

a) glFrustumb) glOrthoc) glTranslatefd) glScalefe) glRotatef

2. Answer “Clipped” or “Unclipped”: Would the following position in homogenous clip space be clipped or not?

(40, -10, 20, 100)

On a sheet of paper• Write your EID, name, and date• Write #1, #2, #3, followed by its answer

Page 7: CS 354 Object Viewing and Representation

CS 354 7

Conceptual Vertex Transformation

glVertex*API

commands

Modelviewmatrix

User-definedclip planes

View-frustumclip planes

to primitiverasterization

object-space coordinates (xo,yo,zo,wo)

eye-space coordinates (xe,ye,ze,we)

clipped eye-space coordinates

clipped clip-space coordinates Perspective

divisionProjection

matrix

Viewport + Depth Rangetransformation

(xc,yc,zc,wc)

window-spacecoordinates

(xw,yw,zw,1/wc)

normalized device coordinates (NDC)

(xn,yn,zn,1/wc)

clip-spacecoordinates

(xc,yc,zc,wc)

(xe,ye,ze,we)

(xe,ye,ze,we)

Page 8: CS 354 Object Viewing and Representation

CS 354 8

Clip Space Clip Cube

(xmin/w,ymin/w,zmin/w)

Pre-perspective divide puts the region surviving clipping within-w ≤ x ≤ w, -w ≤ y ≤ w, -w ≤ z ≤ w

(xmax/w,ymin/w,zmin/w)

(xmax/w,ymin/w,zmax/w) (xmin/w,ymin/w,zmax/w)

(xmax/w,ymax/w,zmax/w)

(xmax/w,ymax/w,zmin/w) (xmin/w,ymax/w,zmin/w)

(xmin/w,ymax/w,zmax/w) Constraints xmin = -w xmax = w ymin = -w ymax = w zmin = -w zmax = w w>0

Page 9: CS 354 Object Viewing and Representation

CS 354 9

Vertex Transformation

Object-space vertex position transformed by a general linear projective transformation Expressed as a 4x4 matrix

o

o

o

o

c

c

c

c

wzyx

mmmmmmmmmmmmmmmm

wzyx

151173

141062

13951

12840

Page 10: CS 354 Object Viewing and Representation

CS 354 10

Two Transforms in Sequence

OpenGL thinks of the projective transform as really two 4x4 matrix transforms

o

o

o

o

e

e

e

e

wzyx

MVMVMVMVMVMVMVMVMVMVMVMVMVMVMVMV

wzyx

151173

141062

13951

12840

e

e

e

e

c

c

c

c

wzyx

PPPPPPPPPPPPPPPP

wzyx

151173

141062

13951

12840

FIRSTobject-space

toeye-space

SECONDeye-space

toclip-space

16 Multiply-Addoperations

Another16 Multiply-Addoperations

Page 11: CS 354 Object Viewing and Representation

CS 354 11

Modelview-Projection Transform

Matrixes can associate (combine) Combination of the modelview and projection

matrix = modelview-projection matrix or often simply the “MVP” matrix

151173

141062

13951

12840

151173

141062

13951

12840

151173

141062

13951

12840

MVMVMVMVMVMVMVMVMVMVMVMVMVMVMVMV

PPPPPPPPPPPPPPPP

MVPMVPMVPMVPMVPMVPMVPMVPMVPMVPMVPMVPMVPMVPMVPMVP

concatenation is64 Multiply-Addoperations, done by OpenGL driver

Matrix multiplicationis associative (but not commutative)

A(BC) = (AB)C, but ABC≠CBA

Page 12: CS 354 Object Viewing and Representation

CS 354 12

Specifying the Projection and Modelview Transforms

Specified in two parts First the projection

glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-4, +4, // left & right

-3, +3, // top & bottom 5, 80); // near & far

Second the model-view glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0, 0, -14);

So objects centered at (0,0,0) wouldbe at (0,0,-14) in eye-space

0100667.101333.100

00667.1000025.1

10001410000100001

Resulting projection matrix

Resulting modelview matrix

Page 13: CS 354 Object Viewing and Representation

CS 354 13

Frustum Transform

Prototype glFrustum(GLdouble left, GLdouble right,

GLdouble bottom, GLdouble top, GLdouble near, GLdouble far)

Post-concatenates a frustum matrix

0100

2)(00

020

002

nffn

nfnfbtbt

btn

lrlr

lrn

Page 14: CS 354 Object Viewing and Representation

CS 354 14

glFrustum Matrix

Projection specification glLoadIdentity();

glFrustum(-4, +4, -3, +3, 5, 80) left=-4, right=4, bottom=-3, top=3, near=5, far=80

Matrix

010075

800758500

00350

00045

=

-Z axis

0100

2)(00

020

002

nffn

nfnfbtbt

btn

lrlr

lrn

symmetric left/right & top/bottom so zero

805

Page 15: CS 354 Object Viewing and Representation

CS 354 15

Translate Transform

Prototype glTranslatef(GLfloat x, GLfloat y, GLfloat z)

Post-concatenates this matrix

1000100010001

zyx

Page 16: CS 354 Object Viewing and Representation

CS 354 16

glTranslatef Matrix Modelview specification

glLoadIdentity();glTranslatef(0,0,-14)

x translate=0, y translate=0, z translate=-14 Point at (0,0,0) would move to (0,0,-14)

Down the negative Z axis Matrix

10001410000100001

=

0000100010001

zyx

the translation vector

Page 17: CS 354 Object Viewing and Representation

CS 354 17

Resulting Modelview-Projection Transform Matrix

Transform composition via matrix multiplication

10001410000100001

0100667.101333.100

00667.1000025.1

141002.51333.100

00667.1000025.1

Resulting modelview-projection matrix

Page 18: CS 354 Object Viewing and Representation

CS 354 18

Now Draw Some Objects Draw a wireframe cube

glColor3f(1,0,0); // red glutWireCube(6);

6x6x6 unit cube centered at origin (0,0,0)

Draw a teapot in the cube glColor3f(0,0,1); // blue glutSolidTeapot(2.0);

centered at the origin (0,0,0) handle and spout point down the

X axis top and bottom in the Y axis

As we’d expect given a frustum transform, the cube is in perspective The teapot is too but more

obvious to observe with a wireframe cube

Page 19: CS 354 Object Viewing and Representation

CS 354 19

What We’ve Accomplished Simple perspective

With glFrustum Establishes how

eye-space maps to clip-space Simple viewing

With glTranslatef Establishes how

world-space maps to eye-space All we really did was “wheel” the camera 14 units up the Z axis No actual “modeling transforms”, just viewing

Modeling would be rotating, scaling, or otherwise transform the objects with the view

Arguably the modelview matrix is really just a “view” matrix in this example

(0,0,14) (0,0,0)

Page 20: CS 354 Object Viewing and Representation

CS 354 20

Let’s Add Some Simple Modeling Try some modeling transforms to move teapot

But leave the cube alone for reference

glPushMatrix(); { glTranslatef(1.5, -0.5, 0); glutSolidTeapot(2.0);} glPopMatrix();

glPushMatrix(); { glScalef(1.5, 1.0, 1.5); glutSolidTeapot(2.0);} glPopMatrix();

glPushMatrix(); { glRotatef(30, 1,1,1); glutSolidTeapot(2.0);} glPopMatrix();

Notice: We “bracket” the modeling transform with glPushMatrix/glPopMatrix commandsso the modeling transforms are “localized” to the particular object

Page 21: CS 354 Object Viewing and Representation

CS 354 21

Add Some Lighting Some lighting makes the modeling more intuitive

glPushMatrix(); { glTranslatef(1.5, -0.5, 0); glutSolidTeapot(2.0);} glPopMatrix();

glPushMatrix(); { glScalef(1.5, 1.0, 1.5); glutSolidTeapot(2.0);} glPopMatrix();

glPushMatrix(); { glRotatef(30, 1,1,1); glutSolidTeapot(2.0);} glPopMatrix();

We’ve not discussed lighting yet but per-vertex lighting allowsa virtual light source to “interact” with the object’s surface orientation and material properties

Page 22: CS 354 Object Viewing and Representation

CS 354 22

ResultingModelview-Projection Matrix

Let’s consider the “combined” modelview matrix with the rotation glRotate(30, 1,1,1) defines a

rotation matrix Rotating 30 degrees… …around an axis in the (1,1,1) direction

100009107.03333.02440.002440.09107.03333.003333.02440.09107.0

10001410000100001

0100667.101333.100

00667.1000025.1

100009107.03333.02440.002440.09107.03333.003333.02440.09107.0

projection view model

Page 23: CS 354 Object Viewing and Representation

CS 354 23

Combining All Three

100009107.03333.02440.002440.09107.03333.003333.02440.09107.0

10001410000100001

0100667.101333.100

00667.1000025.1

projection view model

1000149107.03333.02440.002440.09107.03333.003333.02440.09107.0

modelview

149107.03333.02440.02.50321.13778.02766.0

04067.05178.15556.004167.03050.01384.1

modelview-projection

Matrix-by-matrix multiplication is associative soPVM = P (V M) = (P V) M

OpenGL keeps V and M “together” becauseeye-space is a convenient space for lighting

Page 24: CS 354 Object Viewing and Representation

CS 354 24

Equivalent Math Paths fromObject- to Clip-space

object

object

object

object

world

world

world

world

wzyx

wzyx

100009107.03333.02440.002440.09107.03333.003333.02440.09107.0

world

world

world

world

eye

eye

eye

eye

wzyx

wzyx

10001410000100001

eye

eye

eye

eye

clip

clip

clip

clip

wzyx

wzyx

0100667.101333.100

00667.1000025.1

object

object

object

object

clip

clip

clip

clip

wzyx

wzyx

149107.03333.02440.02.50321.13778.02766.0

04067.05178.15556.004167.03050.01384.1

object

object

object

object

eye

eye

eye

eye

wzyx

wzyx

1000149107.03333.02440.002440.09107.03333.003333.02440.09107.0

eye

eye

eye

eye

clip

clip

clip

clip

wzyx

wzyx

0100667.101333.100

00667.1000025.1

object-to-world-to-eye-to-clip

object-to-eye-to-clip

object-to-clip

modelview

projection

projection

modelview-projection

model

view

Page 25: CS 354 Object Viewing and Representation

CS 354 25

A Better Viewing Matrix “Look at” Transform

Concept Given the following

a 3D world-space “eye” position a 3D world-space center of view position (looking “at”), and an 3D world-space “up” vector

Then an affine (non-projective) 4x4 matrix can be constructed

For a view transform mapping world-space to eye-space

A ready implementation The OpenGL Utility library (GLU) provides it

gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble atx, GLdouble atz, GLdouble atz, GLdouble upx, GLdouble upy, GLdouble upz);

Primary OpenGL librariesLink with –lglut for GLUTLink with –lGLU for GLULink with –lGL for OpenGL

Page 26: CS 354 Object Viewing and Representation

CS 354 26

“Look At” Concept High-level goal

Puts (eyex,eyey,eyez) at the origin of clip space Essentially a translate

Then rotates so “eye – at” direction vector looks down the negative Z (-Z) axis, AND Orthogonalize “up” vector to correspond to positive Y (+Y) axis

Application Useful when you want the camera to maintain observation of a

point of interest in the scene Whether the camera or the point of interest is moving, or both! A lot like your object viewer Project #1

Study this topic Section 4.3 of your text (pages 204-214)

Page 27: CS 354 Object Viewing and Representation

CS 354 27

“Look At” Diagram

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Page 28: CS 354 Object Viewing and Representation

CS 354 28

“Look At” in Practice

Consider our prior view situation Instead of an arbitrary view… …we just translated by 14 in negative Z direction

glTranslatef(0,0,14) What this means in “Look At” parameters

(eyex,eyey,eyez) = (0,0,14) (atx,aty,atz) = (0,0,0) (upx,upy,upz) = (0,1,0)

10001410000100001

10001410000100001

glTranslatef(0,0,-14)

gluLookAt(0,0,14, 0,0,0, 0,1,0)

Same matrix;same transform

Not surprising both are “just translates in Z”since the “Look At” parametersalready have use looking down the negative Z axis

Page 29: CS 354 Object Viewing and Representation

CS 354 29

The “Look At” Algorithm Vector math

Z = eye – at Z = normalize(Z) /* normalize means Z / length(Z) */ Y = up X = Y × Z /* × means vector cross product! */ Y = Z × X /* orthgonalize */ X = normalize(X) Y = normalize(Y)

Then build the following affine 4x4 matrix

1000eyeZZZZeyeYYYYeyeXXXX

zyx

zyx

zyx Warning: Algorithm is proneto failure if normalize dividesby zero (or very nearly does)

So1. Don’t let Z or up be zero length vectors2. Don’t let Z and up be coincident vectors

Page 30: CS 354 Object Viewing and Representation

CS 354 30

“Look At” Examples

gluLookAt(0,0,14, 0,0,0, 0,1,0);

// eye (x,y,z)// at (x,y,z)// up (x,y,z)

gluLookAt(1,2.5,11, 0,0,0, 0,1,0);

// eye (x,y,z)// at (x,y,z)// up (x,y,z)

Same as the glTranslatef(0,0,-14) as expected

Similar to original, but just a little off angledue to slightly perturbed eye vector

Page 31: CS 354 Object Viewing and Representation

CS 354 31

“Look At” Major Eye Changes

gluLookAt(-2.5,11,1, 0,0,0, 0,1,0);

// eye (x,y,z)// at (x,y,z)// up (x,y,z)

gluLookAt(-2.5,-11,1, 0,0,0, 0,1,0);

// eye (x,y,z)// at (x,y,z)// up (x,y,z)

Eye is “above” the scene

Eye is “below” the scene

Page 32: CS 354 Object Viewing and Representation

CS 354 32

“Look At” Changes to AT and UP

gluLookAt(0,0,14, 2,-3,0, 0,1,0);

// eye (x,y,z)// at (x,y,z)// up (x,y,z)

gluLookAt(0,0,14, 0,0,0, 1,1,0);

// eye (x,y,z)// at (x,y,z)// up (x,y,z)

Original eye position, but “at” position shifted

Eye is “below” the scene

Page 33: CS 354 Object Viewing and Representation

CS 354 33

Complex Scene Example

Each character, wall, ceiling, floor, and light have their own modeling transformation

Page 34: CS 354 Object Viewing and Representation

CS 354 34

Representing Objects Interested in object’s boundary (or manifold) Various approaches

Procedural representations Often fractal

Explicit polygon (triangle) meshes By far, the most popular method

Curved surface patches Often displacement mapped

Implicit representation Blobby, volumetric

Sierpinski gasket

Utah Teapot

Blobby modeling in RenderMan

Quake 2 key frame triangle meshes

Fractaltree

[Philip Winston]

Page 35: CS 354 Object Viewing and Representation

CS 354 35

Focus on Triangle Meshes Easiest approach to representing object boundaries So what is a mesh and how should it be stored?

Simplest view A set of triangles, each with its “own” 3 vertices

Essentially “triangle soup” Yet triangles in meshes share edges by design

Sharing edges implies sharing vertices More sophisticated view

Store single set of unique vertexes in array Then each primitive (triangle) specifies 3 indices into array of vertexes More compact

Vertex data size >> index size Avoids redundant vertex data

Separates “topology” (how the mesh is connected) from its “geometry” (vertex positions and attributes)

Connectivity can be deduced more easily Makes mesh processing algorithms easier Geometry data can change without altering the topology

Page 36: CS 354 Object Viewing and Representation

CS 354 36

Consider a Tetrahedron Simplest closed volume

Consists of 4 triangles and 4 vertices (and 4 edges)

v0

v1

v3

v2 triangle list0: v0,v1,v21: v1,v3,v2 2: v3,v0,v23: v1,v0,v3(x0,y0,z1)

(x1,y1,z1)

(x2,y2,z2)

(x3,y3,z3)

vertex list0: (x0,y0,z0)1: (x1,y1,z1)2: (x2,y2,z2)3: (x3,y3,z3)

topology geometry potentially on-GPU!

Page 37: CS 354 Object Viewing and Representation

CS 354 37

Benefits of Vertex Array Approach

Unique vertices are stored once Saves memory

On CPU, on disk, and on GPU Matches OpenGL vertex array model of

operation And this matches the efficient GPU mode of

operation The GPU can “cache” post-transformed vertex results by

vertex index Saves retransformation and redundant vertex fetching

Direct3D has the same model Allows vertex data to be stored on-GPU for even

faster vertex processing OpenGL supported vertex buffer objects for this

Page 38: CS 354 Object Viewing and Representation

CS 354 38

More Information

See “Modern OpenGL Usage: Using Vertex Buffer Objects Well” http://www.slideshare.net/Mark_Kilgard/using-vertex-bufferobjectswell

Page 39: CS 354 Object Viewing and Representation

CS 354 39

Next Lecture More about triangle mesh representation Blending, Compositing, Anti-aliasing

More than simply writing pixels into the framebuffer Also information on GLUT input callbacks As usual, expect a short quiz on today’s lecture

Assignments Reading

Chapter 4, page 238-241; Chapter 6, 331-341 (Tuesday) Chapter 4, pages 241-249

Work on Project #1 Building a 3D object model loader Due Tuesday, February 21