Introduction
This article explains the two transformations - Viewing and Modeling.
The viewing transformation
The viewing transformation is analogous to positioning and aiming a camera. The viewing transformation is specified with gluLookAt()
. The arguments for this command indicate where the camera (or eye position) is placed, where it is aimed, and which way is up. The arguments used here place the camera at (0, 0, 5), aim the camera lens towards (0, 0, 0), and specify the up-vector as (0, 1, 0). The up-vector defines a unique orientation for the camera.
In this code example, before the viewing transformation can be specified, the current matrix is set to the identity matrix with glLoadIdentity()
. This step is necessary since most of the transformation commands multiply the current matrix by the specified matrix and then set the result to be the current matrix. If you don't clear the current matrix by loading it with the identity matrix, you continue to combine previous transformation matrices with the new one you supply. In some cases, you do want to perform such combinations, but you also need to clear the matrix sometimes.
The modeling transformation
You use the modeling transformation to position and orient the model. For example, you can rotate, translate, or scale the model - or perform some combination of these operations. glScalef()
is the modeling transformation that is used. The arguments for this command specify how scaling should occur along the three axes. If all the arguments are 1.0, this command has no effect. In the demo application the polygon is drawn twice as large in the y direction. Thus, if one corner of the polygon had originally been at (3.0, 3.0, 3.0), that corner would wind up being drawn at (3.0, 6.0, 3.0).
My previous article on OpenGL - Getting started with OpenGL, showed the basic framework for drawing in a window.
The OnPaint()
for the demonstration project of this article looks like this:
void OnPaint()
{
this->Invalidate(TRUE);
hdc = ::BeginPaint(hwnd,&ps);
wglMakeCurrent( hdc, hRC );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef( 0.0f, 0.0f, -5.0f );
static GLfloat vertices[] = {
1.0,-0.5,0.5,
1.0,0.5,-1.0,
0.5,1.0,-1.0,
0.5,-1.0,1.0
};
glEnableClientState(GL_VERTEX_ARRAY); glColor3f(0.0,0.0,0.0); glVertexPointer(3,GL_FLOAT,0,vertices);
glPolygonMode(GL_FRONT,GL_LINE); glPolygonMode(GL_BACK,GL_FILL); glLoadIdentity ();
gluLookAt (vx1,vy1,vz1,vx2,vy2,vz2,vx3,vy3,vz3);
glScalef((float)mx,(float)my,(float)mz);
glBegin(GL_POLYGON);
for(int i=0;i<4;i++)
glArrayElement (i); glEnd();
glDisableClientState(GL_VERTEX_ARRAY);
glFlush();
wglMakeCurrent( NULL, NULL );
::EndPaint(hwnd,&ps);
}
About the demo application
The demo application allows you to specify the co-ordinates for the transformations and accordingly the image can be viewed. Just change the x co-ordinate to -1 in the modeling transformation dialog box to view the back face of the polygon.