TUTO2
TUTO2
Tutorial 2
Yang Rong
2003/01/20 Positioning the viewing volume
in the world
1. Point the camera at the scene modeling 1. Point the camera at the scene projection
- Viewing transformation - Viewing transformation
2. Arrange the scene into the 2. Arrange the scene into the
desired composition desired composition
- Modeling transformation - Modeling transformation
3. Choose camera lens or zoom
- Projection transformation
Positioning the models Determining shape of view volume
in the world
OpenGL Transformation Transformation Pipeline
Viewing & Modeling are combined in Modelview matrix
The transformation process is analogous to
taking a photo with camera
VERTEX Modelview Projection Perpective Viewport
Matrix Matrix Division Transformation
1. Point the camera at the scene viewport x
- Viewing transformation y
z
2. Arrange the scene into the w
desired composition Normalized
- Modeling transformation Object(Local) Eye(Fixed) clip window
device
coordinates coordinates coordinates coordinates coordinates
3. Choose camera lens or zoom
- Projection transformation
4. Determine how large your photo
to be
- Viewport Transformation
As a frame of reference -x +x
Translate the object by (x,y,z).
glTranslatef(1.0, 0, 0); +y +y +z
Matrix T
void display() +y
glutSolidCube(1.0);
Coordinate
+x
Vertex v +x {
+x +X
+z +z glMatrixMode(GL_MODELVIEW); +z+x
v’ = RTv
Respect to the +Z glLoadIdentity(); +z
Fixed Coordinate
+y glTranslated(0.0, 0.0, -2.0);
+x +y
glTranslated(0.0, 2.0, 0.0);
+z glRotated(90, 0, 1, 0);
glRotated(90, 0, 1, 0); Order 1
+Y +Y
Local
glTranslatef(1.0, 0, 0); +x
glTranslatef(1.0, 0, 0); +y +y
Order 2
Coordinate +x glutSolidCube(1.0); +z
glutSolidCube(1.0); Order 3 +z }
+x +X
+z +x +X
Respect to the +Z +z
1->2->3
Fixed Coordinate +Z
Projection Transformation Perspective Projection
In OpenGL, you can specify the projection type (perspective gluPerspective(GLdouble fovy, GLdouble aspect,
or orthographic) and the parameters of the projection GLdouble zNear, GLdouble zFar);
+y +y
+z +z +y aspect = w/h
h
+z
fovy
+x +x w
+x
near
far
In reshape(),
glMatrixMode(GL_PROJECTION);
Viewing Volume Clipping – Any primitives that lie outside the gluPerspective(45.0, 1.0, 1.0, 10.0);
viewing volume are clipped and will not be displayed in final
scene
+y
top
glViewport(GLint x, GLint y, GLsizei width,
+z GLsizei height); +x
left
(x, y) specify lower left corner of
+x right the viewport, width & height are the
near bottom size of the viewport rectangle
far
E.g. assume Window Size is 500 x
In reshape(), 400
glMatrixMode(GL_PROJECTION);
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); glViewport(0, 0, 200, 200);
Mapping Viewing Volume to the
Overall Transformation (example)
Viewport modify Projection
void reshape(GLsize w, GLsize h) matrix
The aspect ratio of a viewport (width/height) {
glViewport(0, 0, w, h);
should equal to the aspect ratio of the viewing glMatrixMode(GL_PROJECTION);
volume, otherwise, image distorted!! glLoadIdentity();
gluPerspective(45,(Glfloat)w/(Glfloat)h, 1.0, 10.0);
}
void display(void) {
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslated(0.0, 0.0, -5.0);
gluPerspective(fovy, gluPerspective(fovy,
glTranslated(0.0, 1.0, 0.0);
1.0, near, far); 1.0, near, far);
glRotated(45, 1, 1, 0);
if(G_mousedown) modify Modelview
glViewPort(0, 0, glViewPort(0, 0,
glRotatef(angle, 0, 1, 0);
400, 400); 400, 200);
glutSolidCube(1.0); matrix
undistorted distorted }
glPushMatrix();
+y
M1
Modelview
Matrix stack
Projection
Matrix stack
glRotated(90.0, 0.0, 1.0, 0.0);
T1R T
1
+y +y
+y
+y
glutSolidCube(1.0);
(32 4x4 matrices) (2 4x4 matrices) +x
glPopMatrix(); T +z +x +z
+z +x
+x
+z
1
+z
A stack of matrices is useful for constructing
glTranslated(0.0, 3.0, 0.0);
T1 T 2
glutWireCube(2.0);
hierarchical models Modelview
Matrix
Hierarchical Models Hierarchical Models
(example 1) (robot arm)
Y
glPushMatrix ();
Draw_body_and_wheel_and_bolts() {
glTranslatef (-1.0, 0.0, 0.0);
draw_bus_body(); Y
glPushMatrix();
glRotatef ((Glfloat) shoulder, 0.0, 0.0, 1.0);
X glTranslatef(5.0, -4.0, 0.0); glTranslatef(1.0, 0.0, 0.0);
draw_wheel_and_bolts(); Matrix
glPopMatrix();
X glPushMatrix ();
Multiplicat
glPushMatrix();
glScalef (2.0, 0.4, 1.0);
ion order
glTranslatef(-5.0, -4.0, 0.0);
glutWireCube (1.0);
draw_wheel_and_bolts();
glPopMatrix(); Z glPopMatrix();