advanced viewing glenn g. chappell [email protected] u. of alaska fairbanks cs 381 lecture...

13
Advanced Viewing Glenn G. Chappell [email protected] U. of Alaska Fairbanks CS 381 Lecture Notes Friday, October 31, 2003

Upload: ronald-lewis

Post on 17-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Advanced Viewing Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 381 Lecture Notes Friday, October 31, 2003

Advanced Viewing

Glenn G. [email protected]

U. of Alaska Fairbanks

CS 381 Lecture NotesFriday, October 31, 2003

Page 2: Advanced Viewing Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 381 Lecture Notes Friday, October 31, 2003

31 Oct 2003 CS 381 2

Review:Projection in 3-D [1/3] We use the projection to handle camera properties.

Perspective or parallel (orthogonal) projection. Wide or narrow angle. But not camera position & orientation.

How do we determine wide & narrow angle using glFrustum or gluPerspective? With gluPerspective, change the 1st parameter (fovy). With glFrustum, multiply left, right, bottom, top by some

number. Fancy projections can mess up screen text, buttons, etc.

For these, use a separate gluOrtho2D projection. See printmatrix.cpp, on the web page, for sample code.

Page 3: Advanced Viewing Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 381 Lecture Notes Friday, October 31, 2003

31 Oct 2003 CS 381 3

Review:Projection in 3-D [2/3] Computing Perspective Projection

Based on the synthetic-camera model, we can find the coordinates of a projected point.

We use similar triangles (outlined in red).

z = –far

z = –near

Center of Projection

(“eye”)(0, 0, 0)

(x, y, z)

Screen

–z(x/[–z/near], y/[–z/near], –near)

View Frustum

Page 4: Advanced Viewing Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 381 Lecture Notes Friday, October 31, 2003

31 Oct 2003 CS 381 4

Review:Projection in 3-D [3/3] We can perform this projection using the

following matrix.

This matrix is not exactly what glFrustum produces, since it deals with right, left, top, bottom, far, too. But this gives the general idea.

near

nearz

ynearz

x

nearz

z

y

x

z

y

x

near

/

/

/10/100

0100

0010

0001

Page 5: Advanced Viewing Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 381 Lecture Notes Friday, October 31, 2003

31 Oct 2003 CS 381 5

Review:More on OpenGL Matrices [1/2] We store an OpenGL matrix in an array of 16 GLdouble’s:

GLdouble matrixd[16];

To put the model/view matrix into this array:

glGetDoublev(GL_MODELVIEW_MATRIX, matrixd);

This is column-major. 0..3 are the first column, not the first row. To restore the saved model/view matrix:

glLoadMatrixd(matrixd); // Mode must be GL_MODELVIEW!

More usefully, to multiply saved matrix by current matrix:

glMultMatrixd(matrixd); // Mode must be GL_MODELVIEW!

Page 6: Advanced Viewing Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 381 Lecture Notes Friday, October 31, 2003

31 Oct 2003 CS 381 6

Review:More on OpenGL Matrices [2/2] How would you write glTranslate* yourself?

void myglTranslate(double x, double y, double z){ GLdouble m[16]; // The translation matrix

m[ 0] = 1.; m[ 4] = 0.; m[ 8] = 0.; m[12] = x; m[ 1] = 0.; m[ 5] = 1.; m[ 9] = 0.; m[13] = y; m[ 2] = 0.; m[ 6] = 0.; m[10] = 1.; m[14] = z; m[ 3] = 0.; m[ 7] = 0.; m[11] = 0.; m[15] = 1.; glMultMatrixd(m); // Multiply it}

Why don’t I set GL_MODELVIEW mode in this function?

Page 7: Advanced Viewing Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 381 Lecture Notes Friday, October 31, 2003

31 Oct 2003 CS 381 7

Advanced Viewing:Problem & Solution

So far, we have always created the model/view transformation from scratch for each frame.

This can get unwieldy if an increasingly long sequence of transformations must be remembered. Consider “flying”.

Solution Keep the current state of a sequence of

transformations in a matrix. Modify the matrix appropriately when a new

transformation is added. In the display function, just do glMultMatrix*.

Page 8: Advanced Viewing Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 381 Lecture Notes Friday, October 31, 2003

31 Oct 2003 CS 381 8

Advanced Viewing:Example 1: “Zoom & Pan” [1/2]

In traditional zoom & pan (with a real camera): Zoom means changing the wide/narrow-angle

properties of the lens. Pan means rotating the camera.

We will misuse these terms somewhat, in order to make a more helpful example. Our “zoom” will scale the world. Our “pan” will translate it. We will use model/view for both of these.

Think: What is a problem with panning while zoomed in close?

Page 9: Advanced Viewing Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 381 Lecture Notes Friday, October 31, 2003

31 Oct 2003 CS 381 9

Advanced Viewing:Example 1: “Zoom & Pan” [2/2]

Suppose we handle “zoom & pan” with a saved viewing matrix. What do we do to this matrix, in order to pan?

• Translate, then do the saved transformations. Result is the new transformation.

What do we do to this matrix, in order to zoom?• Scale, then do the saved transformations. Result is the

new transformation. How do we use this matrix in the display function?

• Load it (glLoadMatrixd).• Or do whatever else needs to be done first, then multiply

by it (glMultMatrixd).

Page 10: Advanced Viewing Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 381 Lecture Notes Friday, October 31, 2003

31 Oct 2003 CS 381 10

Advanced Viewing:Handling the Saved Matrix Don’t forget to initialize the saved matrix.

glMatrixMode(GL_MODELVIEW);glPushMatrix(); ***** Transformation commands go here? ***** glGetDoublev(GL_MODELVIEW_MATRIX, your_matrix_variable);glPopMatrix();

Whenever you do anything with the model/view transformation, use push & pop. Changing the matrix alters the display.

So post a redisplay event whenever you change the matrix outside the display function. Generally, to alter a saved model/view matrix outside the display function:

glPushMatrix(); glLoadIdentity(); // May not be necessary ***** Transformation commands go here ***** glMultMatrixd(your_matrix_variable); // If this is appropriate glGetDoublev(GL_MODELVIEW_MATRIX, your_matrix_variable);glPopMatrix();glutPostRedisplay();

Page 11: Advanced Viewing Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 381 Lecture Notes Friday, October 31, 2003

31 Oct 2003 CS 381 11

Advanced Viewing:Example 2: A Driving Interface

Imagine a car at the center of the window, driving through the city streets.

How can we turn our zoom-pan interface into a 2-D driving-style interface (viewed from above) with only minimal modifications? Change the pan-left and pan-right code to do

rotations about the z-axis.

Page 12: Advanced Viewing Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 381 Lecture Notes Friday, October 31, 2003

31 Oct 2003 CS 381 12

Advanced Viewing:Example 3: Flying [1/2] We implemented the first two examples using a

saved matrix to hold viewing transformations. Viewing transform’s = camera motions.

• Okay, the “zoom” part didn’t quite fit this model … The ultimate generality in camera motions is

achieved in “flying”. Flying = moving in the viewing direction & rotating

about the camera. How do we fly forward?

• Put a +z translation before (in the code) all previous transformations.

How to we turn?• Put a y-axis (or x-axis, for going up & down) rotation

before (in the code) all previous transformations.

Page 13: Advanced Viewing Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 381 Lecture Notes Friday, October 31, 2003

31 Oct 2003 CS 381 13

Advanced Viewing:Example 3: Flying [2/2]

Wouldn’t it be nicer to use the mouse? Yes. We’ll talk about that next time.