Title: Advanced Viewing
1Advanced Viewing
- Glenn G. ChappellCHAPPELLG_at_member.ams.org
- U. of Alaska Fairbanks
- CS 381 Lecture Notes
- Friday, October 31, 2003
2ReviewProjection 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.
3ReviewProjection 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).
Screen
Center of Projection (eye)(0, 0, 0)
(x, y, z)
z
(x/z/near, y/z/near, near)
z near
View Frustum
z far
4ReviewProjection 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.
5ReviewMore on OpenGL Matrices 1/2
- We store an OpenGL matrix in an array of 16
GLdoublesGLdouble matrixd16 - To put the model/view matrix into this
arrayglGetDoublev(GL_MODELVIEW_MATRIX,
matrixd) - This is column-major. 0..3 are the first column,
not the first row. - To restore the saved model/view
matrixglLoadMatrixd(matrixd) // Mode must be
GL_MODELVIEW! - More usefully, to multiply saved matrix by
current matrixglMultMatrixd(matrixd) // Mode
must be GL_MODELVIEW!
6ReviewMore on OpenGL Matrices 2/2
- How would you write glTranslate yourself?
- void myglTranslate(double x, double y, double z)
-
- GLdouble m16 // The translation matrix
- m 0 1. m 4 0. m 8 0. m12 x
- m 1 0. m 5 1. m 9 0. m13 y
- m 2 0. m 6 0. m10 1. m14 z
- m 3 0. m 7 0. m11 0. m15
1. - glMultMatrixd(m) // Multiply it
-
- Why dont I set GL_MODELVIEW mode in this
function?
7Advanced ViewingProblem 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.
8Advanced ViewingExample 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?
9Advanced ViewingExample 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).
10Advanced ViewingHandling the Saved Matrix
- Dont 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()
11Advanced ViewingExample 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.
12Advanced ViewingExample 3 Flying 1/2
- We implemented the first two examples using a
saved matrix to hold viewing transformations. - Viewing transforms camera motions.
- Okay, the zoom part didnt 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.
13Advanced ViewingExample 3 Flying 2/2
- Wouldnt it be nicer to use the mouse?
- Yes. Well talk about that next time.