|
Objectives
|
To perform transformations on 2D objects using OpenGL programming
|
OpenGL
transformation functions
|
|
OpenGL maintains a
4x4 transformation matrix that is multiplied by every (x,y,z) value sent to
glVertex(). This is sometimes referred to as the CT (current transform). The CT
can be modified by the following functions:
- glLoadIdentity();
- glTranslate{fd}( dx, dy, dz );
- glScale{fd}( sx, sy, sz);
- glRotate{fd}( angleInDegrees, axisXvalue, axisYvalue, axisZvalue);
|
How to use the OpenGL transformation functions
|
|
There are two
different 4x4 matrices used by OpenGL to produce an image. They are:
- Projection matrix - used to "project" a 3D image onto a 2D plane to create an image.
- ModelView matrix - used to transform objects and the camera to create the desired scene and view.
Commands like glTranslate, glRotate, and glScale will modify the active
matrix. It is the programmer's responsibility to make sure the appropriate
matrix is "active." The active matrix is determined by calls to
glMatrixMode(), such as:
- glMatrixMode(GL_PROJECTION); // make the projection matrix active
- glMatrixMode(GL_MODELVIEW); // make the modelview matrix active
2) The transformations caused by glRotate, glTranslate, and glScale are
applied to the CT (current transform). They will accumulate continually unless
you reset the matrix back to the Identity matrix. If you want to start a new
set of transformations, then call glLoadIdentity(), as in the following
example:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// start a new sequence of transforms
glTranslatef(-3.0, -4.0, 0.0);
glRotatef(90.0, 1.0, 0.0, 0.0);
drawObject1();
glLoadIdentity();
// start a new sequence of transforms
glTranslatef(4.0,
2.0, 1.0);
glScalef(2.0, 1.0,
1.0);
drawObject2();
|
Programming Exercises
|
|
1.
Implement
an Opengl interactive graphics program that draws a triangle which can be
translated by pressing 'l', 'r', 'u', or 'd' keys.
You can draw a triangle with equal side 1 unit:
//-------------------------------------------------------------------
float midX = 0.5;
float midY = sqrt(3)/2.0;
void drawTriangle(void)
{
glBegin(GL_POLYGON);
glColor3f ( 1.0, 0.0,
0.0);
glVertex2f( 0.0, 0.0);
glColor3f ( 0.0, 1.0, 0.0);
glVertex2f( midX, midY);
glColor3f ( 0.0, 0.0,
1.0);
glVertex2f( 1.0, 0.0);
glEnd();
}
//-------------------------------------------------------------------
2.
Modify
your programs from the previous questions so that the triangle now will
translate and rotate when the user presses the 'l', 'r', 'u', or 'd' keys (for
translation) and 'c' (for clockwise rotation),'a' (for anti-clockwise
rotation),
.................
#include <glut.h>
#include <math.h>
float midX = 0.5;
float midY = sqrt(3.0)/2.0;
float PI=3.1415;
//-------------------------------------------------------------------
void drawTriangle(void)
{
glBegin(GL_POLYGON);
glColor3f ( 1.0, 0.0, 0.0);
glVertex2f( 0.0, 0.0);
glColor3f ( 0.0, 1.0, 0.0);
glVertex2f( midX, midY);
glColor3f ( 0.0, 0.0, 1.0);
glVertex2f( 1.0, 0.0);
glEnd();
}
//-------------------------------------------------------------------
void Transform(GLfloat tx, GLfloat ty)
{
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
glTranslatef(tx, ty, 0.0);
drawTriangle();
glFlush();
glutSwapBuffers();
}
//-------------------------------------------------------------------
void myDisplay(void)
{
glClear( GL_COLOR_BUFFER_BIT );
drawTriangle();
glFlush();
glutSwapBuffers();
}
//-------------------------------------------------------------------
void mySpecialKey(int key, int x, int y)
{
GLfloat tx = 0.0, ty = 0.0;
switch(key)
{
case GLUT_KEY_UP : ty += 0.05; break;
case GLUT_KEY_DOWN : ty -= 0.05; break;
case GLUT_KEY_RIGHT : tx += 0.05; break;
case GLUT_KEY_LEFT : tx -= 0.05; break;
case GLUT_KEY_HOME : glLoadIdentity();
// drawTriangle();
break;
}
Transform(tx, ty);
glutPostRedisplay();
}
//-------------------------------------------------------------------
void myInit(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0); // set the background to black
glColor3f(1.0f, 1.0f, 1.0f); // set the drawing color to white
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 3.0, 0.0, 3.0);
}
//-------------------------------------------------------------------
void main( int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB );
glutInitWindowSize(450,450);
glutInitWindowPosition(0,0);
glutCreateWindow("One Triangle");
glutDisplayFunc(myDisplay);
glutSpecialFunc(mySpecialKey);
myInit();
glutMainLoop();
}
ليست هناك تعليقات:
إرسال تعليق