OpenGL Transformation
The transformation process is analogous to
taking a photo with camera
CSC3260 Principles of
Computer Graphics 1. Point the camera at the scene
- Viewing Transformation
viewing
Tutorial 2
Yang Rong
2003/01/20 Positioning the viewing volume
in the world
OpenGL Transformation OpenGL Transformation
The transformation process is analogous to The transformation process is analogous to
taking a photo with camera taking a photo with camera
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
General-Purpose Viewing and Modeling
Transformation Commands Transformations
void glMatrixMode(GLenum mode ) mode matrix Viewing and modeling transformation are
GL_MODELVIEW modeview matrix combined into a single modelview matrix
Specify which matrix will be modified, GL_PROJECTION projection Matrix
then subsequent transformation Move the camera in one direction
commands affect the specified matrix GL_TEXTURE texture matrix
= Move the object in opposite direction
Call glMatrixMode(GL_MODELVIEW) before
void glLoadIdentity(void); performing modeling or viewing transformation
1 0 0 0
Set the currently modifiable matrix to M= 0 1 0 0
the 4X4 identity matrix, to clear the 0 0 1 0
matrix 0 0 0 1
Fixed and Local Coordinates Three modeling transformations
+y
Fixed coordinate system: -z glTranslate{fd}(x, y, z)
As a frame of reference -x +x
Translate the object by (x,y,z).
Default virtual camera positiones at camera
glTranslatef(5, 0, 0)
(0, 0, 0) & viewing to -z-axis +z glutSolidCube(1.0);
-y
Local coordinate system:
+y glScale{fd}(x, y, z)
-z Scale the object by factor (x,y,z).
Tied to the object you are drawing
A sphere at (0, 0, 0) in local Coordinate, -x +x glScalef(2, 2, 2)
but at (10,0,0) in Fixed cooredinate +z
glutSolidCube(1.0);
Local coordinate is coherent with fixed
-y
glRotate{fd}(angle, x, y, z)
Rotate the object by about
coordinate system initially
vector (x,y,z) by angle (degree)
Transformed by various Viewing & glRotatef(45, 0, 1, 0)
modeling Transformation glutSolidCube(1.0);
View From Matrix Multiplication
Order and Commands Order
3D Transformation (example)
+y
+x +y
+z Local coordinate +x
glRotated(90, 0, 1, 0); Matrix R +Y
Fixed
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
Orthographic Projection Viewport Transformation
GLdouble left, GLdouble right, GLdouble bottom,
glOrtho( The rectangular region of the
GLdouble top, GLdouble near, GLdouble far); window where the image is drawn +y
+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 }
Manipulating the Matrix Stacks glPushMatrix() and glPopMatrix()
glPushMatrix() glPopMatrix()
ModelView and Projection matrics are actually Copy the current matrix Discard the top matrix
the topmost member of their stack of matrices and adds the copy to on the stack
the top of the stack “go back to where you
M4 “remember where you are” +y
M3
are”
P2
M2 T 1
+z +x
glTranslated(3.0, 0.0, 0.0);
P1
…
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();
Bus body glTranslatef (1.0, 0.0, 0.0);
Draw_wheel _and_bolts() {
+yY
+y +x glRotatef ((Glfloat) elbow, 0.0, 0.0, 1.0);
+y +x
Draw_wheel();
+y +x glTranslatef (1.0, 0.0, 0.0);
Wheel 1 Wheel 1 For (int i=0; i<3; i++) { Command
glPushMatrix(); +z X glPushMatrix ();
order
glRotatef(120*i, 0.0, 0.0, 1.0);
+z glScalef (2.0, 0.4, 1.0);
Bolt11 Bolt12 Bolt13 Bolt11 Bolt12 Bolt13
glTranslatef(0.0, 1.0, 0.0);
+z
draw_bolt(); glutWireCube (1.0);
glPopMatrix(); glPopMatrix ();
}
Z
}
glPopMatrix ();
Using a Display List
Store OpenGL commands for later execution
Integer index, identify this display
list, generated by glGenLists( )
Gluint listName = glGenLists(1);
glNewList(listName, GL_COMPILE);
glColor3f(1.0, 0.0, 0.0); What Color??
glBegin(GL_POLYGON);
glVertex2f(0.0, 0.0); glColor3f(0.0, 1.0, 0.0);
glVertex2f(1.0, 0.0); for (i=0; i<6; i++)
glVertex2f(0.0, 1.0); glCallList(listName);
glEnd();
glTranslatef(1.5, 0.0, 0.0);
glEndList();