homogeneous form, introduction to 3-d graphics glenn g. chappell [email protected] u. of...

21
Homogeneous Form, Introduction to 3-D Graphics Glenn G. Chappell [email protected] U. of Alaska Fairbanks CS 381 Lecture Notes Monday, October 20, 2003

Upload: jared-johnson

Post on 02-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Homogeneous Form, Introduction to 3-D Graphics

Glenn G. [email protected]

U. of Alaska Fairbanks

CS 381 Lecture NotesMonday, October 20, 2003

20 Oct 2003 CS 381 2

Note

Friday we looked at some virtual-reality research projects. This is part of the class, and will be

covered on the test. However, it is outside the normal flow

of topics. I have no review slides on VR.

20 Oct 2003 CS 381 3

Review:Unit Vectors [1/2]

A unit vector is a vector whose magnitude is 1. We use unit vectors to represent quantities that

have only direction. To find the unit vector pointing in the same

direction as a given vector, divide the given vector by its magnitude. That is, given v, the unit vector pointing in the same

direction is v/|v| = (1/|v|)v. Unit vectors are convenient when dealing with

angles. If u and v are unit vectors, then the cosine of the angle

between u & v is just u · v.

20 Oct 2003 CS 381 4

Review:Unit Vectors [2/2]

Example Find a unit vector with the same

direction as the vector (1, 2, 3). Solution:

• The magnitude of this vector is sqrt[12+22+32] = sqrt[14].

• Multiply: (1/sqrt[14])(1, 2, 3) = (1/sqrt[14], 2/sqrt[14], 3/sqrt[14]).

20 Oct 2003 CS 381 5

Review:Matrices & Transformations [1/3]

We can use 33 matrices to represent some kinds of 3-D transformations: All rotations (about any line through the

origin). All scaling.

We apply the transformation to a point by using matrix multiplication. Matrix point = transformed point.

20 Oct 2003 CS 381 6

Review:Matrices & Transformations [2/3] Example 1

What 33 matrix represents the transformation produced by:glScaled(2., 1., 1.);

Solution:

Note: This is not the actual matrix produced by this command. OpenGL does not use 33 matrices.

Example 2 Apply the above transformation to the point (2, 3, 5) using matrix

multiplication.

100

010

002

5

3

4

513020

503120

503022

5

3

2

100

010

002

20 Oct 2003 CS 381 7

Review:Matrices & Transformations [3/3] We cannot represent translations in this way.

Translations move the origin (0, 0, 0).• Except for the do-nothing (identity) translation, which

leaves all points alone. But matrix multiplication always leaves the origin

alone. So, this method of representing transformations

is not the one we want. How can we represent transformations in a more

general way? In order to solve this, we need to alter the way we

represent points & vectors. Next we look at “homogeneous form”.

20 Oct 2003 CS 381 8

Homogeneous Form:What Is It? OpenGL (along with other CG libraries) represents points

and vectors internally using not 3 coordinates, but 4. Make the 4th coordinate (“w”) a 1 to get the internal

representation. This is called homogeneous form. For example,

glVertex3d(4., 5., 6.);would result in (4, 5, 6, 1) being sent through the pipeline.

We allow other values besides 1 in the 4th coordinate. Divide x, y, and z by w in order to find the point or vector

represented. For example, (4, 5, 6, 2) would be a homogeneous

representation for (4/2, 5/2, 6/2) = (2, 2.5, 3). But (4, 5, 6, 1) represents (4/1, 5/1, 6/1) = (4, 5, 6), as above.

20 Oct 2003 CS 381 9

Homogeneous Form:What Is It Good For?

Using homogenous form allows many kinds of transformations to be represented in a common form: Translation. Rotation. Scaling. Orthogonal projection. Perspective projection.

We represent all of these using 44 matrices.

20 Oct 2003 CS 381 10

Homogeneous Form:Transformations [1/3]

If a 3-D transformation can be represented as a 33 matrix (like rotation & scaling), then we make a 44 matrix for it as follows:

1000

0

0

0

ihg

fed

cba

ihg

fed

cba

20 Oct 2003 CS 381 11

Homogeneous Form:Transformations [2/3]

Other transformations are represented in other ways. Here is how we do translation: The codeglLoadIdentity();glTranslated(a, b, c);generates the matrix:

1000

100

010

001

c

b

a

20 Oct 2003 CS 381 12

Homogeneous Form:Transformations [3/3]

We perform the transformations by using matrix multiplication, as before. Matrix multiplication cannot move (0, 0, 0, 0),

as before. However, we now represent the origin in homogenous form as (0, 0, 0, 1).

Remember, we may have to divide by the 4th coordinate when we are done.

• This will not be an issue until we get talk about how to represent perspective projection.

Try it!

20 Oct 2003 CS 381 13

Homogenous Form:The “Punch Line” If matrix multiplication is used for performing

transformations, then multiplying two transformation matrices together results in the matrix representing one transformation followed by the other. Thus, we can use a 44 matrix to represent, not only a

single transformation, but any series of transformations.

What the transformation commands (glTranslate*, glRotate*, glScale*) really do is to multiply the current matrix, on the left, by the matrix for the given transformation. This is why we need glLoadIdentity.

20 Oct 2003 CS 381 14

Introduction to 3-D Graphics:Three Issues

Remember the three issues in 3-D CG: Viewing and Transformations

• This is what we are concentrating on now. Hidden-Surface Removal

• Easy-to-use depth buffering is built into OpenGL. Other methods are tricky …

Lighting• This will come later.

20 Oct 2003 CS 381 15

Introduction to 3-D Graphics:Example Code

In class, we looked at a preliminary version of start3d.cpp and modified it. No further slides were shown in class.

The following slides summarize the topics covered.

The file start3d.cpp is now on the web page. It is intended to be a starting point for 3-D programs.

20 Oct 2003 CS 381 16

Introduction to 3-D Graphics:Double Buffering

Nearly all of your 3-D programs will be double buffered. Drawing in the back buffer, and then swapping, means

users see only completed frames. Result: smooth animation. This topic is review, of course.

How to Do It Use GLUT_DOUBLE in your glutInitDisplayMode call.

• This allocates a back buffer and enables it for drawing.• Maybe double buffering will work using GLUT_SINGLE.

Maybe not. In any case, don’t do it. Use glutSwapBuffers instead of glFlush at the end of

your display routine.

20 Oct 2003 CS 381 17

Introduction to 3-D Graphics:HSR with the Depth Buffer To draw 3-D scenes, hidden-surface removal is required.

HSR is a complex topic. However … OpenGL includes a simple, easy-to-use, general-purpose HSR

method: depth buffering.• This method is memory intensive. That was once a major issue.

How to Do It Use GLUT_DEPTH in your glutInitDisplayMode call.

• This allocates a depth buffer. Enable the depth test with “glEnable(GL_DEPTH_TEST);”

• You can do this in your initialization.• You may wish to do some drawing without the depth test. In that

case, you can enable & disable in the display function. Clear the depth buffer, using GL_DEPTH_BUFFER_BIT in

glClear.• When this happens depends on what you want to achieve.• Typically, you clear the depth buffer when you clear the color

buffer.

20 Oct 2003 CS 381 18

Introduction to 3-D Graphics:Perspective Projection [1/2] So far, we have used only orthogonal projection. In 3-D, we usually use perspective projection.

Distant objects appear small, just as in the real world. To set up a perspective projection, replace gluOrtho2D

with glFrustum. The viewpoint ( “eye”) is at (0,0,0), looking in the –z direction. Now we have near and far clipping planes. Think of the screen as lying on the near clipping plane.

• So you look through the screen at the scene.• Objects in front of the screen cannot be seen.• Objects behind the far plane also cannot be seen.• The left, right, bottom, top parameters set up the coordinate

system on the near plane.

20 Oct 2003 CS 381 19

Introduction to 3-D Graphics:Perspective Projection [2/2] Function glFrustum has 6 parameters:

left, right: just as with gluOrtho2D. bottom, top: just as with gluOrtho2D. near, far: The distance to the near & far clipping planes.

• So near = 1, means that the near clipping plane lies at z = –1.

Remember: The left, right, bottom, top parameters set up the coordinate system on the near plane.

In 3-D it is easy to put things where you cannot see them. In particular, you cannot see objects at the origin. But you still want to draw things with their center at the origin.

• This makes rotation work well, for example. Solution: translate.

• For example, “glTranslated(0., 0., -4.);”.

GLU includes the function gluPerspective. This is an alternate way to set up a perspective projection. It calls

glFrustum. See the doc’s.

20 Oct 2003 CS 381 20

Introduction to 3-D Graphics:Drawing 3-D Objects Serious 3-D graphics always involves lighting.

But we have not covered lighting yet. One solution: Draw the faces of your objects in different

colors. When we get to lighting, we will distinguish between the

“front” and “back” sides of a polygon. These can be lit differently. The front side of a polygon drawn with GL_TRIANGLES,

GL_QUADS, or GL_POLYGON is the one on which the vertices appear in counterclockwise order.

Front or back makes no difference yet. However, drawing objects so that all polygons are front-side-out is a good habit to get into.

Draw your objects so that they are centered at the origin. To make them to appear somewhere else, use

transformations.

20 Oct 2003 CS 381 21

Introduction to 3-D Graphics:Transformations

For now, we generally do our transformations in the display function. Remember: Do glPushMatrix. Do transformations.

• Order: translate, then rotate, then scale. Draw the objects that are to be affected by the

transformations. Do glPopMatrix.

Using glRotate*, we can rotate about any line through the origin. For example, “glRotated(theta, 1.,2.,3.);” rotates

through theta degrees about the line through the points (0, 0, 0) and (1, 2, 3).