opengl lab3

advertisement
OpenGL LAB III
Outline


Viewing Transformation
Projection and Orthographic Projection
2
Transformation pipeline

Stages of vertex transformation
Modelview
Matrix
Object
coords
Projection
Matrix
Camera
coords
Viewport
Mapping
Normalized
coords
Window
coords
3
Transformation pipeline
Matrices are set up on stacks
 Matrix commands are post-multiplied onto
the current matrix

 The
last command issued is
the first transformation applied to the object

Can save/restore the current matrix
4
Transformation pipeline

Save / Restore the current matirx:
 glPushMatrix()
 glPopMatrix()

Change the current matrix stack:
 glMatrixMode(Glenum
mode)
 GL_MODELVIEW, GL_PROJECTION,
GL_TEXTURE
5
Modelview transformation

Modeling transformation


Model local coordinates → world coordinates
Viewing transformation

world coordinates → eye coordinates
6
Viewing Transformation






Another change of
coordinate systems
Maps points from world
space into eye space
Viewing position is
transformed to the origin
Viewing direction is
oriented along some axis
A viewing volume is defined
Combined with modeling
transformation to form the
modelview matrix in OpenGL
7
Camera View

Camera coordinate system

The camera is located at the origin
 The camera’s optical axis is along
one of the coordinate axes (-z in
OpenGL convention)
 The up axis (y axis) is aligned with
the camera’s up direction


We can greatly simplify the clipping
and projection steps in this frame
The viewing transformation can be
expressed using the rigid body
transformations discussed before
8
Viewing Transformation Steps


Viewing transformation should align the world and camera
coordinate frames
We can transform the world frame to the camera frame with a
rotation followed a translation
Rotate
Translate
9
Intuitive Camera Specification

How to specify a camera
gluLookAt (eyex, eyey, eyez, centerx, centery, centerz, upx, upy,
upz)

(eyex, eyey, eyez): Coordinates of the camera (eye) location in the
world coordinate system
 (centerx, centery, centerz): the look-at point, which should appear in
the center of the camera image, specifies the viewing direction
 (upx, upy, upz): an up-vector specifies the camera orientation by
defining a world space vector that should be oriented upwards in the
final image


This intuitive specification allows us to specify an arbitrary camera
path by changing only the eye point and leaving the look-at and up
vectors untouched
Or we could pan the camera from object to object by leaving the
eye-point and up-vector fixed and changing only look-at point
10
Example
void display() {
glClear(GL_COLOR_BUFFER | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0, 1.0, 0.0);
glLoadIdentity();
gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 1.0, 1.0, 0.0);
glutWireTeapot(0.5);
glFlush();
}
11
Tutorials

Change your camera view based on key inputs









‘q’ : eyex +0.1, ‘i’ : eyex -0.1
‘w’ : eyey + 0.1, ‘o’ : eyey -0.1
‘e’ : eyez +0.1, ‘p’ : eyez -0.1
‘a’ : centerx +0.1, ‘j’ : centerx -0.1
‘s’ : centery +0.1, ‘k’ : centery -0.1
‘d’ : centerz +0.1, ‘l’ : centerz -0.1
‘z’ : upx +0.1, ‘b’ : upx -0.1
‘x’ : upy +0.1, ‘n’ : upy -0.1
‘c’ : upz +0.1, ‘m’ : upz -0.1
12
Tutorials

Set camera view to following figures
13
The Matrix for glLookAt

(u, v, w, eye) forms the
viewing coordinate system




w = eye – look
u = up × w
v=w×u
The matrix that transforms
coordinates from world
frame to viewing frame.



dx = - eye · u
dy = - eye · v
dz = - eye · w
 ux
v
 x
 wx

0
uy
vy
wy
uz
vz
wz
0
0
dx 
d y 
dz 

1
14
Setup Camera

Since viewing transformation is a rotation and translation
transformation. We can use glRotatef() and glTranslatef() instead
of gluLookAt()

In the previous example (view a scene at origin from (10, 0, 0) ), we
can equivalently use
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(45, 0, 0, 1);

Since the viewing transformation is applied after modeling
transformations, it should be set before modeling transformations.
15
Set Viewing Transformation

Furthermore, glTranslatef() and glRotatef() can be used to define
custom viewing control.

Example
void display() {
…
glRotatef(angle, axisx, axisy, axisz);
glTranslatef(xpos, ypos, zpos);
…
}
16
Projection Transformations

The projection transformation
maps all of our 3-D
coordinates onto our desired
viewing plane.

Greatly simplified by using the
camera (viewing) frame.
 projection matrices do not
transform points from our
affine space back into the
same space.
 Projection transformations are
not affine and we should
expect projection matrices to
be less than full rank
17
Orthographic Projection

The simplest form of projection

simply project all points
along lines parallel to the
z-axis (x, y, z)->(x,
y, 0)

Here is an example of an
parallel projection of our
scene. Notice that the
parallel lines of the tiled
floor remain parallel after
orthographic projection
18
Orthographic Projection



The projection matrix for orthographic projection is simple:
 x  1 0 0 0  x 
 y   0 1 0 0   y 
 
 
 z   0 0 0 0   z 
  
 
1
0
0
0
1
  
 1 
Notice the units of the transformed points are still the same as the
model units. We need to further transform them to the screen space.
OpenGL functions for orthographic projection gluOrtho2D(left,
right, bottom, top), glOrtho(left, right, bottom,
top, near, far)
19
Perspective Projection

Perspective projection is important for making images appear
realistic.

causes objects nearer to the viewer to appear larger than the same
object would appear farther away
 Note how parallel lines in 3D space may appear to converge to a single
point when viewed in perspective.
20
Viewing Frustum and Clipping




The right picture shows the
view volume that is visible
for a perspective projection
window, called viewing
frustum
It is determined by a near
and far cutting planes and
four other planes
Anything outside of the
frustum is not shown on the
projected image, and
doesn’t need to be rendered
The process of remove
invisible objects from
rendering is called clipping
21
OpenGL Perspective Projection






Set viewing frustum and perspective projection matrix
glFrustum(left,right,bottom,top,near,far)
left and right are coordinates of left and right window
boundaries in the near plane
bottom and top are coordinates of bottom and top window
boundaries in the near plane
near and far are positive distances from the eye along the
viewing ray to the near and far planes
Projection actually maps the viewing frustum to a canonical cube the
preserves depth information for visibility purpose.
22
The OpenGL Perspective Matrix


Matrix M maps the viewing frustum to a NDC (canonical cube)
r l
 2n

0
0
r  l

r l


2n
t b
 0
0 
M 
t b
t b

 ( f  n)  2 fn 
 0
0

f n
f n
 0

0

1
0


We are looking down the -z direction
23
Near/Far and Depth Resolution

It may seem sensible to specify a very near clipping plane and a
very far clipping plane


Sure to contain entire scene
But, a bad idea:

OpenGL only has a finite number of bits to store screen depth
 Too large a range reduces resolution in depth - wrong thing may be
considered “in front”

Always place the near plane as far from the viewer as possible, and
the far plane as close as possible
24
Perspective Projection

If the viewing frustum is
symmetrical along the x and y
axes. It can be set using
gluPerspective()

gluPerspective(θ,aspect,n,f)

θ: the field of view angle
aspect: the aspect ratio of the
display window (width/height)

25
Set a view
int main( int argc, char* argv[] )
{
…
glutReshapeFunc( reshape );
…
}
void reshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double aspect = width/double(height);
gluPerspective(45, aspect, 1, 1024);
}
26
Next time

Lighting and Texture mapping
27
Related documents
Download