Lecture2 v1
Lecture2 v1
• Example
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMultMatrixf(N); /* apply transformation N */
glMultMatrixf(M); /* apply transformation M */
glMultMatrixf(L); /* apply transformation L */
glBegin(GL_POINTS);
glVertex3f(v); /* draw transformed vertex v */
glEnd();
• The first three elements of the first three columns are just directional
vectors that represent the orientation (vectors here are used to
represent a direction) of the x-, y-, and z-axes in space.
• For most purposes, these three vectors are always at 90° angles
from each other, and are usually each of unit length (unless you are
also applying a scale or shear).
– The mathematical term for this is orthonormal when the vectors are unit
length, and
– orthogonal when they are not.
// Load an identity matrix
GLfloat m[] = { 1.0f, 0.0f, 0.0f, 0.0f, // X Column
0.0f, 1.0f, 0.0f, 0.0f, // Y Column
0.0f, 0.0f, 1.0f, 0.0f, // Z Column
0.0f, 0.0f, 0.0f, 1.0f }; // Translation
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(m);
glLoadIdentity();
glTranslatef(0, 0, -10.0);
glRotatef(x_Angle, 1.0, 0.0, 0.0);
glRotatef(y_Angle, 0.0, 1.0, 0.0);
glRotatef(z_Angle, 0.0, 0.0, 1.0);
drawCube();
glutSwapBuffers();
}
Duality of Modeling and Viewing Transformation
if(bGlulookat){
glTranslatef(0, 0, -10.0);
// Translate the object by 10 units in -ve z-direction.
printf("\n Using glTranslate().");
}
else {
gluLookAt(0, 0, 10, 0, 0, -1, 0, 1, 0);
// Specify the same camera position
//and orientation as above using gluLookAt()
printf("\n Using gluLookAt().");
}
pilotView.c
• Suppose you're writing a flight
simulator and you'd like to display
the world from the point of view of
the pilot of a plane.
{
glRotated(roll, 0.0, 0.0, 1.0);
glRotated(pitch, 0.0, 1.0, 0.0);
glRotated(yaw, 1.0, 0.0, 0.0);
glTranslated(-planex, -planey, -planez);
}
Polar camera type
• Spherical/Polar
Coordinates based
camera moves about
the surface of a
sphere, always
looking at the Origin.
• Radius of the sphere
is changeable.
Performing Your Own Transformations
Transform.cpp
void RenderScene(void) {
M3DMatrix44f transformationMatrix; // Storage for rotation matrix
static GLfloat yRot = 0.0f; // Rotation angle for animation
yRot += 0.5f;
//With the exception of the angle being in radians instead of degrees, this is almost exactly like the
OpenGL function glRotate.
transformationMatrix[12] = 0.0f;
transformationMatrix[13] = 0.0f;
transformationMatrix[14] = -2.5f;
// The function DrawTorus does the necessary math to generate the torus’s geometry and takes as an
argument a 4×4 transformation matrix to be applied to the vertices.
DrawTorus(transformationMatrix);
objectVertex[0] = ?
objectVertex[1] = ?
objectVertex[2] = ?
glVertex3fv(transformedVertex);
• The TRANSFORM sample program is
very inefficient.
glPushMatrix();
glRotatef(-yRot * 2.0f, 0.0f, 1.0f, 0.0f);
glTranslatef(1.0f, 0.0f, 0.0f);
glutSolidSphere(0.1f, 13, 26);
//void gltDrawSphere(GLfloat fRadius, GLint iSlices, GLint iStacks);
//This effect makes the sphere appear to revolve around the origin in front of us.
glPopMatrix();
A frustum is a truncated section of a pyramid viewed from the narrow end to the
broad end.
void glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top,
GLdouble near, GLdouble far); the frustum doesn't have to be symmetrical,
• void gluPerspective(GLdouble fovy, GLdouble
aspect, GLdouble zNear, GLdouble zFar);
Creates a matrix for a symmetric perspective-
view
A Simple Example: Drawing a Cube
A Transformed Cube (cube.c)
#include <GL/glut.h>
#include <stdlib.h>
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity (); /* clear the matrix */
/* viewing transformation */
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glScalef (1.0, 2.0, 1.0); /* modeling transformation */
glutWireCube (1.0);
glFlush ();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
//glOrtho (-1.0, 1.0, -5.0, 5.0, 1.5, 20.0);
glMatrixMode (GL_MODELVIEW);
}
Viewing Volume Clipping
• After the vertices of the objects in the
scene have been transformed by the
modelview and projection matrices, any
vertices that lie outside the viewing volume
are clipped.
• You can specify additional clipping planes
and locate them wherever you choose;
Viewport Transformation
• The viewport transformation corresponds to the stage
where the size of the developed photograph is chosen.
Do you want a wallet-size or a poster-size photograph?
• The viewport is measured in window coordinates.
• void glViewport(GLint x, GLint y, GLsizei width, GLsizei
height);
• The (x, y) parameter specifies the lower left corner of the
viewport
• By default, the initial viewport values are (0, 0, winWidth,
winHeight), where winWidth and winHeight are the size
of the window.
• The aspect ratio of a viewport should
generally equal the aspect ratio of the viewing
volume. If the two ratios are different, the
projected image will be distorted as it's
mapped to the viewport.
• For example, this sequence maps a square image onto a
square viewport:
gluPerspective(myFovy, 1.0, myNear, myFar);
glViewport(0, 0, 400, 400);
•
Troubleshooting Transformations
draw_wheel();
for(i=0;i<5;i++){
glPushMatrix();
glRotatef(72.0*i,0.0,0.0,1.0);
glTranslatef(3.0,0.0,0.0);
draw_bolt();
glPopMatrix();
}
}
draw_body_and_wheel_and_bolts()
{
draw_car_body();
glPushMatrix();
glTranslatef(40,0,30); /*move to first wheel position*/
draw_wheel_and_bolts();
glPopMatrix();
glPushMatrix();
glTranslatef(40,0,-30); /*move to 2nd wheel position*/
draw_wheel_and_bolts();
glPopMatrix();
... /*draw last two wheels similarly*/
}
Additional Clipping Planes
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glutWireSphere(1.0, 20, 16); /* draw sun */
glRotatef ((GLfloat) year, 0.0, 1.0, 0.0);
glTranslatef (2.0, 0.0, 0.0);
glRotatef ((GLfloat) day, 0.0, 1.0, 0.0);
glutWireSphere(0.2, 10, 8); /* draw smaller planet */
glPopMatrix();
glutSwapBuffers();
}
• Try adding a moon to the planet. Or try several
moons and additional planets.
– Hint: Use glPushMatrix() and glPopMatrix() to save
and restore the position and orientation of the
coordinate system at appropriate moments. If you're
going to draw several moons around a planet, you
need to save the coordinate system prior to
positioning each moon and restore the coordinate
system after each moon is drawn.
• Try tilting the planet's axis.
A Robot Arm
robot.c
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef (-1.0, 0.0, 0.0);
glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (2.0, 0.4, 1.0);
glutWireCube (1.0);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
glutObjects.c
• glutWireSphere(GLdouble radius, Glint slices, Glint
stacks)
– Slices: The number of subdivisions around the Z axis (similar to
lines of longitude).
– Stacks: The number of subdivisions along the . axis (similar to
lines of latitude).
• glutSolidSphere( )
• glutWireCube(GLdoublesize)
• glutSolidCube(GLdoublesize)
• glutWireTeapot(GLdoublesize)
• glutSolidTeapot(GLdoublesize)
• …