4.4 编程实例——三角形与矩形变换及动画
4.4.1 自定义矩阵变换实例——三角形变换

#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
GLsizei winWidth=600,winHeight=600;
GLfloat xwcMin=0.0,xwcMax=225.0;
GLfloat ywcMin=0.0,ywcMax=225.0;
class wcPt2D
{
public:
GLfloat x, y;
};
typedef GLfloat Matrix3x3 [3][3];
Matrix3x3 matComposite;
const GLdouble pi=3.14159;
void init (void)
{
glClearColor(1.0,1.0,1.0,0.0);
}
void matrix3x3SetIdentity(Matrix3x3 matIdent3x3)
{
GLint row,col;
for (row=0;row<3;row++)
for (col=0;col<3;col++)
matIdent3x3[row][col]=(row==col);
}
void matrix3x3PreMultiply(Matrix3x3 m1, Matrix3x3 m2)
{
GLint row, col;
Matrix3x3 matTemp;
for(row=0; row<3;row++)
for(col=0;col<3;col++)
matTemp[row][col]=m1[row][0]*m2[0][col]+m1[row][1]*m2[1][col] +m1[row][2]*m2[2][col];
for(row=0;row<3;row++)
for(col=0;col<3;col++)
m2[row][col]=matTemp[row][col];
}
void translate2D(GLfloat tx,GLfloat ty)
{
Matrix3x3 matTransl;
matrix3x3SetIdentity(matTransl);
matTransl[0][2]=tx;
matTransl[1][2]=ty;
matrix3x3PreMultiply(matTransl,matComposite);
}
void rotate2D(wcPt2D pivotPt, GLfloat theta)
{
Matrix3x3 matRot;
matrix3x3SetIdentity(matRot);
matRot[0][0]=cos(theta);
matRot[0][1]=-sin(theta);
matRot[0][2]=pivotPt.x*(1-cos(theta))+pivotPt.y*sin(theta);
matRot[1][0]=sin(theta);
matRot[1][1]=cos(theta);
matRot[1][2]=pivotPt.y*(1-cos(theta))-pivotPt.x*sin(theta);
matrix3x3PreMultiply(matRot,matComposite);
}
void scale2D(GLfloat sx,GLfloat sy,wcPt2D fixedPt)
{
Matrix3x3 matScale;
matrix3x3SetIdentity(matScale);
matScale[0][0]=sx;
matScale[0][2]=(1-sx)*fixedPt.x;
matScale[1][1]=sy;
matScale[1][2]=(1-sy)*fixedPt.y;
matrix3x3PreMultiply(matScale,matComposite);
}
void transformVerts2D(GLint nVerts,wcPt2D * verts)
{
GLint k;
GLfloat temp;
for(k=0;k<nVerts;k++)
{
temp=matComposite[0][0]*verts[k].x+matComposite[0][1]*verts[k].y +matComposite[0][2];
verts[k].y=matComposite[1][0]*verts[k].x+matComposite[1][1] *verts[k].y+matComposite[1][2];
verts[k].x=temp;
}
}
void triangle(wcPt2D * verts)
{
GLint k;
glBegin(GL_TRIANGLES);
for(k=0;k<3;k++)
glVertex2f(verts[k].x,verts[k].y);
glEnd();
}
void myDisplay ()
{
GLint nVerts=3;
wcPt2D verts[3]={{50.0,25.0},{150.0,25.0},{100.0,100.0}};
wcPt2D centroidPt;
GLint k,xSum=0,ySum=0;
for(k=0;k<nVerts;k++)
{
xSum+=verts[k].x;
ySum+=verts[k].y;
}
centroidPt.x=GLfloat(xSum)/GLfloat(nVerts);
centroidPt.y=GLfloat(ySum)/GLfloat(nVerts);
wcPt2D pivPt,fixedPt;
pivPt=centroidPt;
fixedPt=centroidPt;
GLfloat tx=0.0,ty=100.0;
GLfloat sx=0.5,sy=0.5;
GLdouble theta=pi/2.0;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,1.0);
triangle(verts);
matrix3x3SetIdentity(matComposite);
scale2D(sx,sy,fixedPt);
rotate2D(pivPt,theta);
translate2D(tx,ty);
transformVerts2D(nVerts,verts);
glColor3f(1.0,0.0,0.0);
triangle(verts);
glFlush();
}
void Reshape(GLint newWidth,GLint newHeight)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(xwcMin,xwcMax,ywcMin,ywcMax);
glClear(GL_COLOR_BUFFER_BIT);
}
void main(int argc, char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(50,50);
glutInitWindowSize(winWidth,winHeight);
glutCreateWindow("二维几何变换实例-复合变换");
init();
glutDisplayFunc(myDisplay);
glutReshapeFunc(Reshape);
glutMainLoop();
}
4.4.2 OpenGL几何变换实例——矩形变换

#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
GLsizei winWidth=600,winHeight=600;
GLfloat xwcMin=-300.0,xwcMax=300.0;
GLfloat ywcMin=-300.0,ywcMax=300.0;
void init (void)
{
glClearColor(1.0,1.0,1.0,0.0);
}
class wcPt3D
{
public:
GLfloat x, y, z;
};
void rotate3D (wcPt3D p1, wcPt3D p2, GLfloat thetaDegrees)
{
float vx = (p2.x - p1.x);
float vy = (p2.y - p1.y);
float vz = (p2.z - p1.z);
glTranslatef (p1.x, p1.y, p1.z);
glRotatef (thetaDegrees, vx, vy, vz);
glTranslatef (-p1.x, -p1.y, -p1.z);
}
void scale3D (GLfloat sx, GLfloat sy, GLfloat sz, wcPt3D fixedPt)
{
glTranslatef (fixedPt.x, fixedPt.y, fixedPt.z);
glScalef (sx, sy, sz);
glTranslatef (-fixedPt.x, -fixedPt.y, -fixedPt.z);
}
void myDisplay()
{
wcPt3D centroidPt,R_p1, R_p2;
centroidPt.x=50;
centroidPt.y=100;
centroidPt.z=0;
R_p1=centroidPt;
R_p2.x=50;
R_p2.y=100;
R_p2.z=1;
wcPt3D p1,p2,fixedPt;
p1= R_p1;
p2= R_p2;
fixedPt=centroidPt;
GLfloat tx=0.0,ty=100.0,tz=0;
GLfloat sx=0.5,sy=0.5,sz=1;
GLdouble thetaDegrees = 90;
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
glColor3f(0.0,0.0,1.0);
glRecti(50,100,200,150);
glTranslatef (tx, ty, tz);
scale3D (sx, sy, sz, fixedPt);
rotate3D (p1, p2, thetaDegrees);
glColor3f(1.0,0.0,0.0);
glRecti(50,100,200,150);
glFlush();
}
void Reshape(GLint newWidth,GLint newHeight)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(xwcMin,xwcMax,ywcMin,ywcMax);
glClear(GL_COLOR_BUFFER_BIT);
}
void main(int argc, char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(50,50);
glutInitWindowSize(winWidth,winHeight);
glutCreateWindow("二维几何变换实例-OpenGL版复合变换");
init();
glutDisplayFunc(myDisplay);
glutReshapeFunc(Reshape);
glutMainLoop();
}
4.4.3 变换应用实例——正方形旋转动画

#include <gl\glut.h>
#include <cmath>
GLfloat cx = 0.0f;
GLfloat cy = 0.0f;
GLfloat length = 0.5f;
GLfloat theta = 0.0f;
void myDisplay()
{
glClearColor(0.8f, 0.8f, 0.8f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glLoadIdentity();
glRotated(theta, 0.0, 0.0, 1.0);
glRectf(cx- length/2, cy - length / 2, cx + length / 2, cy + length / 2);
glutSwapBuffers();
}
void myIdle()
{
theta += 0.1f;
if (theta >= 360)
theta = 0.0f;
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("旋转的正方形");
glutDisplayFunc(myDisplay);
glutIdleFunc(myIdle);
glutMainLoop();
return 0;
}