Sie sind auf Seite 1von 57

Viewing

(OpenGL Book Ch 3)
Introduction
Viewing and Modeling Transformations
Projection Transformations
Viewport Transformation
Overview: The Camera Analogy
The Camera Analogy (cont.)
1. Positioning the camera
viewing transformation
2. Positioning the model
modeling transformation
3. Choose a camera lens and adjust zoom
projection transformation
4. Mapping to screen.
viewport transformation
An Example Code: cube.c
Viewing Transformation
glLoadIdentity(), gluLookAt()
Modeling Transformation
glScalef()
Projection Transformation
glFrustum()
Viewport Transformation
glViewport()
Transformation Commands
glMatrixMode(GLenum mode)
void glLoadIdentity(void)
void glLoadMatrix(const TYPE *m)
void glMultMatrix(const TYPE *m)
Transformation Commands
glMatrixMode(GLenum mode);
mode = GL_MODELVIEW,
GL_PROJECTION,
GL_TEXTURE
Transformation Commands
void glLoadIdentity(void)
sets current matrix to 4x4 identity matrix.

void glLoadMatrix(const TYPE *M)
sets the current matrix to M.

Transformation Commands
void glMultMatrix(const TYPE *M)

multiplies M to current matrix and stores
the result as the current matrix.
if current matrix is C, resulting matrix is
CM, not MC.
glvertex(v) is now processed by CMv.
Mathematical and Programming Notes
OpenGL uses column instead of row vectors
Let C be the current matrix and call
glMultMatrix*(M). After multiplication, the final
matrix is always CM.
Matrices are defined like this (use float m[16]);

Transformation Commands(cont.)
Example:
glLoadIdentity(); // C = I
glMultMatrixf(N); // C = N
glMultMatrixf(M); // C = NM
glMultMatrixf(L); // C = NML
glBegin(GL_POINTS);
glVertex3f(v); // N(M(Lv))
glEnd();
Last command operates first, First command operates last.
Viewing & Modeling Transformation
45 deg rotation around z-axis, and 10 unit
translation along +x





Order is Important!
Global or Local thinking
Code for the left hand side figure
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMultMatrixf(T); // trans
glMultMatrixf(R); // rot
draw_the_object();
Viewing & Modeling Transformation
45 deg rotation around z-axis, and 10 unit
translation along +x





Order is Important!
Global or Local thinking
Code for the left hand side figure
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMultMatrixf(R); // trans
glMultMatrixf(T); // rot
draw_the_object();
local thinking suitable for articulated figure
modeling
Demo
Rectangle rotate, translate and combination
Modeling Transformations
OpenGL has 3 Modeling Transformations
glTranslate*()
glRotate*()
glScale*()

They multiply proper matrix to current
matrix and the resulting matrix is loaded to
current matrix.
glTranslate*()
void glTranslate{fd}(x, y, z)
Move an object by given x, y, z values.
glRotate*()
void glRotate*(angle, x, y, z)
Rotate an object in counterclockwise direction
about the ray from the origin through the point
(x,y,z) by the angle value.
glScale*()
void glScale{fd}(x, y, z)
scale an object by x, y and z times
according to x, y and z-axis, respectively.
Ex) glScalef(2.0,-0.5,1.0);
Whats the result of the following code ?
glTranslated(1.0,0.0,0.0);
glScalef(2.0,1.0,1.0);
glRotated(45.0,0.0,0.0,1.0);
glBegin(GL_POLYGON);
glVertex2f( 0.0, 0.0 );
glVertex2f(1.0, 0.0 );
glVertex2f( 1.0, 1.0 );
glVertex2f( 0.0, 1.0 );
glEnd();
Demo
Animation three rotating triangles
Duality of Modeling and Viewing Trans
Relative effect
Translate an object by (0,0,-5) has the same
effect as translate camera by (0,0,5).
Viewing trans + Modeling trans
=> modelview transformation.

Viewing Transformations
OpenGL has 3 Viewing Transformations
glTranslate*()
glRotate*()
gluLookAt*()
gluLookAt(eyex, eyey, eyez,
centerx, centery, centerz,
upx, upy, upz)
gluLookAt(0,0,0, 0,0,-1, 0,1,0);
Viewing Transformation
Method 1.
Create your own special function.
Method 2.
Use transformation commands such as
glTranslate*() and glRotate*() functions.
Method 3.
Use Utility Library routine such as gluLookAt().
It is easy to understand and implement.
Viewing Transformations
gluLookat(GLdouble eyex, eyey, eyez, centerx, centery, centerz, upx,
upy, upz) - Is a GLU utility that:
Defines a viewing matrix of the desired viewpoint.
Multiplies it to the right of the current matrix (in our case the
Modelview matrix).
Viewing Transformations
(eyex,eyey,eyez) - is the point where the desired camera is located.
(centerx,centery,centerz) - is a point in space to which the camera aims
(will appear in the center of the projected image of the camera).
(upx,upy,upz) - is the camera up vector. This vector actually defines
which direction in the world will be the up direction in the projected
image of the camera:
) , , ( upz upy upx
) , , ( centerz centery centerx
) , , ( eyez eyey eyex
Image up
direction
Image
center
Example
void init(void) {
glClearColor(0,0,0,0);glShadeModel(GL_FLAT);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE : GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
Example (cont.)
void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 1, 1);
glLoadIdentity();
gluLookAt(0,0,5, 0,0,0, 0,1,0);
glScalef(1,2,1);
glutWireCube(1.0);
glFlush();
}
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1, 1, -1, 1, 1.5, 20);
glMatrixMode(GL_MODELVIEW);
}
Viewing, then Modeling
Example

glMatrixMode(GL_MODELVIEW);
gluLookAt(0,0,5, 0,0,0, 0,1,0);
glTranslatef(0,0,-1);
draw_triangle();

Projection Transformation
Perspective Projection
For natural image.
Use glFrustum() or gluPerspective()
Orthographic Projection
For applications such as creating architectural
blueprints and computer-aided design.
Use glOrtho() or gluOrtho2D()
glFrustum()
void glFrustum(left, right,
bottom, top, near, far);

gluPerspective()
void gluPerspective(fovy, aspect,
near, far);
An Example
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1, 1, 20);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(10,0,0);
draw_triangle();
Viewport Transformation
Determine position and size of final image.
Use glViewport() function.
Viewport Transformations
If the aspect ration of the viewing volume isnt equal to the
viewports aspect ratio a distortion will occur during the viewport
transformation (square will turn to be rectangles and circles will
turn to be ellipses).
Distorted example:
gluPerspective(fovy, 1.0, near, far);
glViewport(0,0,200,400) ;
since
Undistorted example:
gluPerspective(fovy, 1.0, near, far);
glViewport(0,0,400,400) ;
0 . 1 5 . 0 400 / 200 = =
glViewport(x,y,width,height)
Example:

glViewport(100, 100, 512, 512);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1, 1, 20);
glMatrixMode(GL_MODELVIEW);
.........
Viewport Transformations
The last mapping to window coordinate.
glViewport(GLint x, y, width, height) - Defines a pixel
rectangle in the window into which the final image
(projected frustum) is mapped.
(x,y) is the lower left corner of the viewport.
(width,height) is the size of the viewport rect.

Stages of Vertex Transformation
Manipulating the Matrix Stacks
To recall previous transformation, matrix
stack is used.
Manipulating the Matrix Stacks(cont.)
glPushMatrix()
<=> glPopMatrix()
Matrix Stacks
glPushMatrix(void) - Pushes all
matrices in the current stack
down one level.
glPopMatrix(void) - Pops the top
matrix off the current stack, losing
the topmost matrix!
(The current stack is determined
by glMatrixMode).
M5
M4
M3
M2
M1
M5
M4
M3
M2
M1
Pop
Push
M4
M3
M2
M1
M4
M4
M3
M2
M1
Current
matrix
level
Current
matrix
level
Example
draw_body_and_wheel_and_bolts() {
draw_car_body();
glPushMatrix();
glTranslatef(40, 0, 30);
draw_wheel_and_bolts();
glPotMatrix();
glPushMatrix();
glTranslatef(40, 0, -30);
draw_wheel_and_bolts();
glPotMatrix();

}
Example (cont.)
draw_wheel_and_bolts() {
draw_wheel();
for(i=0;i<5;i++) {
glPushMatrix();
glRotatef(72*i, 0, 0, 1);
glTranslatef(3, 0, 0);
draw_bolt();
glPotMatrix();
}
}

Matrix Stacks
OpenGL supports two stacks of matrices
Modelview matrix stack (32 4x4 matrices)
Projection matrix stack (2 4x4 matrices)
These stacks are useful for constructing hierarchical
models. For example a car made of its body and the
four wheels:
Rotate wheels
Rotate wheels
+ Rotate body
Matrix Stacks
Example code:
void drawCar() {
glMatrixMode(GL_MODELVIEW) ;
glTranslatef(x,y,z) ; /*/
glRotatef(car_ang, 0, 1, 0) ; /*/
draw_car_body() ;
glPushMatrix() ;
glTranslate(-1,0,1) ;
glRotatef(wheels_ang, 0, 1, 0) ;
draw_car_wheel() ;
glPopMatrix() ;
glPushMatrix() ;
glTranslate(1,0,1) ;
glRotatef(wheels_ang, 0, 1, 0) ;
draw_car_wheel() ;
glPopMatrix() ;
}
First we move and rotate the car
(body + wheels) - as it is the top
level in the hierarchy.
Next we push the stack - and
therefore store a copy.
Then we draw the right and left
wheels in their appropriate position
and orientation. Note that on each
wheel the transformation /*/ will
operate.
The last pop will retrieve the matrix
containing only the /*/
transformations.
Manipulating the Matrix Stacks(cont.)
How about a planetary system ?
Study Also Example 3-7 (robot.c)

Demo
Simple planetary system
Modeling Transformations
glTranslate{fd}(TYPE x, TYPE y, TYPE z) -
Multiplies the current matrix by a matrix that moves
(translates) vertices by x, y and z values.
How will such a matrix look like?




Question: In the above equations we took W=1 what would
happen if W was any non-zero (Real) number?
|
|
|
|
|
.
|

\
|
+
+
+
=
|
|
|
|
|
.
|

\
|
-
|
|
|
|
|
.
|

\
|
|
|
|
|
|
.
|

\
|
1 1 1 0 0 0
1 0 0
0 1 0
0 0 1
: since
1 0 0 0
1 0 0
0 1 0
0 0 1
z Z
y Y
x X
Z
Y
X
z
y
x
z
y
x
Projective Transformations
The projection matrix is responsible for the projective
transformation:
glMatrixMode(GL_PROJECTION) ;
The camera model we will use in OpenGL are:
Pinhole perspective projection
Orthographic projection
Projective Transformations
glFrustum(Gldouble left, right, bottom, top, near, far) - Create a
matrix for a perspective view and multiplies the current matrix
(near and far are non-zero positive).
Perspective Projection
glFrustum(l, r, b, t, n, f)

Projective Transformations
gluPerspective(GLdouble fovy, aspect, near, far) - Creates a matrix for
symmetric perspecive view.
fovy is the angle of field of view in the X-Z plane [0180].
aspect is the aspect ratio of the frustum (its width/height television/computer
screen is 4:3, Theatre wide screen is 16:9).
near and far are the distances from the clipping planes.
Projective Transformations
glOrtho(GLdouble left, right, bottom, top, near, far) - Creates a matrix
for an orthographic parallel viewing projection and multiplies the current
matrix (In this case near and far can be also negative).
Projection
plane
direction of
projection
Orthographic Projection
glOrtho(l, r, b, t, n, f )

Projective Transformations
gluOrtho2D(GLdouble left, right, bottom, top) - Creates a matrix for an
orthographic parallel viewing projection and multiplies the current
matrix. Where are near = -1.0 and far is 1.0. This is a simple case for
2D drawing using OpenGL. When glVertex2(x,y) is given a point with
coordinate (x,y,0) is actually defined and thus it is not clipped when
using gluOrtho2D.
End of Transformations

Das könnte Ihnen auch gefallen