viewing and transformation
DESCRIPTION
Viewing and Transformation. The Camera Analogy 1/2. Set up tripod and pointing the camera at the scene (viewing transformation) . Arrange the scene to be photographed into the desired composition (modeling transformation). The Camera Analogy 2/2. - PowerPoint PPT PresentationTRANSCRIPT
CGGM Lab. Tan-Chi Ho 2001
Viewing and Transformation
CGGM Lab. Tan-Chi Ho 2001
The Camera Analogy1/2
Set up tripod and pointing the camera at the scene (viewing transformation).
Arrange the scene to be photographed into the desired composition (modeling transformation).
CGGM Lab. Tan-Chi Ho 2001
The Camera Analogy2/2
Choose a camera lens or adjust the zoom (projection transformation).
Determine how large you want the final photograph to be (viewport transformation).
CGGM Lab. Tan-Chi Ho 2001
Overview
ModelviewMatrix
ModelviewMatrix
ViewTransformation
ViewTransformation
ModelviewMatrix
ModelviewMatrix
PerspectiveDivision
eye coordinat
es
clip coordinat
es
normalized device
coordinates
window coordinat
es
XYZW
Object coordinat
es
VertexVertex
CGGM Lab. Tan-Chi Ho 2001
Transformations1/12
The Viewing Transformation To positioning and aiming the camera. Use gluLookAt() to indicate where the ca
mera is placed and aimed. If gluLookAt() was not called, the camera
has a default position at the origin, points down the negative Z-axis, and an up-vector of (0, 1, 0).
CGGM Lab. Tan-Chi Ho 2001
Transformations2/12
gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx, GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy, GLdoubpe upz);
eyex, eyey, eyez is where the camera is positioned.
centerx, centery, centerz is where the camera look at.
Upx, upy, upz is the up-vector of the camera.
CGGM Lab. Tan-Chi Ho 2001
Transformations3/12
The Modeling Transformation To position and orient the models.
Rotate, translate, scale and some combination of these operations.
In OpenGL, modeling and viewing transformation are combined into the modelview matrix before the transformation are applied.
CGGM Lab. Tan-Chi Ho 2001
Transformations4/12
glTranslate{fd}(TYPE x,TYPE y,TYPE z); Multiplies current matrix by a matrix that mo
ves an object by x,y,z glRotate{fd}(TYPR angle,TYPE x,TYPR y,
TYPE z); Multiplies current matrix by a matrix that rot
ates an object in a counterclockwise direction about the ray from origin to (x,y,z) with angle as the degrees.
CGGM Lab. Tan-Chi Ho 2001
Transformations5/12
glScale{fd}(TYPE x,TYPE y,TYPE z); Multiplies current matrix by a matrix that scal
es an object along axes.
CGGM Lab. Tan-Chi Ho 2001
Transformations6/12
The Projection Transformation Determine what the field of view(or
viewing volume) is and how objects are projected onto the screen.
Two types of projections are provided: Perspective projection Orthographic projection
CGGM Lab. Tan-Chi Ho 2001
Transformations7/12
Perspective Projection glFrustum(GLdouble left, GLdouble rig
ht, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far);
CGGM Lab. Tan-Chi Ho 2001
Transformations8/12
gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble near, GLdouble far);
CGGM Lab. Tan-Chi Ho 2001
Transformations9/12
Orthographic Projection glOrtho(Gldouble left, Gldouble right,
Gldouble bottom, Gldouble top, Gldouble near, Gldouble far);
gluOrtho2D(Gldouble left, Gldouble right, Gldouble bottom, Gldouble top);
For 2D projection matrix The Z coordinates for objects are assumed to
lie between –1.0 and 1.0.
CGGM Lab. Tan-Chi Ho 2001
Transformations10/12
CGGM Lab. Tan-Chi Ho 2001
Transformations11/12
The Viewport Transformation Transform the final image into some regi
on of the window. The viewport is measured in window coo
rdinates. glViewport(GLint x, GLint y, GLsizei wi
dth, GLsizei height); Initial values are (0, 0, winWidth, winHeight).
CGGM Lab. Tan-Chi Ho 2001
Transformations12/12
Switch between matrix modes glMatrixMode(GLenum mode);
Switch between modelview, projection, tecture matrix mode.(GL_MODELVIEW, GL_PROJECTION, GL_TEXTURE)
Current matrix mode is a state variable. glLoadIdentity();
Set current matrix to the 4x4 identity matrix. Always call this after change matrix mode.
CGGM Lab. Tan-Chi Ho 2001
The Matrix Manipulation glLoadIdentity(); glLoadMatrix{f,d}( const TYPE* m ); glMultMatrix{f,d}( const TYPE* m ); glPushMatrix(); glPopMatrix();
Stack operation of matrix is very useful for constructing a hierarchical structures.
Ex: a car with four wheels.
CGGM Lab. Tan-Chi Ho 2001
Transformation Program1/5
#include <GL/glut.h>
static GLfloat year=0.0f, day=0.0f;
void init(){
glClearColor(0.0, 0.0, 0.0, 0.0);}
void GL_display(){
// clear the bufferglClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);glPushMatrix();
glutWireSphere(1.0, 20, 16); // the SunglRotatef(year, 0.0, 1.0, 0.0);
CGGM Lab. Tan-Chi Ho 2001
Transformation Program2/5
glTranslatef(3.0, 0.0, 0.0);glRotatef(day, 0.0, 1.0, 0.0);glutWireSphere(0.5, 10, 8); // the Planet
glPopMatrix();// swap the front and back buffersglutSwapBuffers();
}void GL_reshape(GLsizei w, GLsizei h){
// viewport transformationglViewport(0, 0, w, h);
// projection transformationglMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 1.0, 20.0);
CGGM Lab. Tan-Chi Ho 2001
Transformation Program3/5
// viewing and modeling transformationglMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(0.0, 3.0, 5.0, // eye
0.0, 0.0, 0.0, // center 0.0, 1.0, 0.0); // up
}
// GLUT idle functionvoid GL_idle(){
day += 10.0;if(day > 360.0)
day -= 360.0;year += 1.0;if(year > 360.0)
year -= 360.0;// recall GL_display() functionglutPostRedisplay();
}
CGGM Lab. Tan-Chi Ho 2001
Transformation Program4/5
// GLUT keyboard functionvoid GL_keyboard(unsigned char key, int x, int y){
switch(key){case 'd': day += 10.0;
if(day > 360.0)day -= 360.0;
glutPostRedisplay();break;
case 'y': year += 1.0;if(year > 360.0)
year -= 360.0;glutPostRedisplay();break;
case 'a': // assign idle functionglutIdleFunc(GL_idle);break;
case 'A': glutIdleFunc(NULL);break;
case 27: exit(0);}
}
CGGM Lab. Tan-Chi Ho 2001
Transformation Program5/5
int main(int argc, char** argv){
glutInit(&argc, argv);glutInitWindowSize(500, 500);glutInitWindowPosition(0, 0);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);glutCreateWindow("Planet");init();glutDisplayFunc(GL_display);glutReshapeFunc(GL_reshape);glutKeyboardFunc(GL_keyboard);glutMainLoop();
}
CGGM Lab. Tan-Chi Ho 2001
Any Question?
?