#include
#include
#include
/* 初始化显示窗口大小 */
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;
//};
struct wcPt2D
{
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);
glClear(GL_COLOR_BUFFER_BIT); // 清空显示窗口
}
/* 构建3*3的单位矩阵 */
void matrix3x3SetIdentity(Matrix3x3 matIdent3x3)
{
GLint row, col;
for (row = 0; row < 3; row++)
for (col = 0; col < 3; col++)
matIdent3x3[row][col] = (row == col);
}
/* 变换矩阵m1前乘矩阵m2,储存结果到m2中 */
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];
//将矩阵matTemp复制进m2
for (row = 0; row < 3; row++)
for (col = 0; col < 3; col++)
m2[row][col] = matTemp[row][col];
}
/* 平移变换函数,平移量tx,ty */
void translate2D(GLfloat tx, GLfloat ty)
{
Matrix3x3 matTransl;
/* 初始化平移矩阵为单位矩阵 */
matrix3x3SetIdentity(matTransl);
matTransl[0][2] = tx;
matTransl[1][2] = ty;
/* 将平移矩阵前乘到复合矩阵matComposite中 */
matrix3x3PreMultiply(matTransl, matComposite);
}
/* 旋转变换函数,参数为中心点pivotPt和旋转角度theta */
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);
/* 将旋转矩阵前乘到复合矩阵matComposite中 */
matrix3x3PreMultiply(matRot, matComposite);
}
/* 比例变换函数,参数为基准点fixedPt和缩放比例sx、sy */
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;
/* 将缩放矩阵前乘到复合矩阵matComposite中 */
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 displayFcn(void)
{
/* 定义三角形的初始位置 */
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);
glColor3f(0.0, 0.0, 1.0); // 设置前景色为蓝色
triangle(verts); //显示蓝色三角形(变换前)
/* 初始化复合矩阵为单位矩阵 */
matrix3x3SetIdentity(matComposite);
/* 根据变换序列重建复合矩阵 */
//变换序列1:缩放变换
GLfloat sx = 0.5, sy = 0.5;//缩放比例
wcPt2D fixedPt = centroidPt;//缩放基准点
scale2D(sx, sy, fixedPt);
//变换序列2:旋转变换
wcPt2D pivPt = centroidPt;//旋转基准点
GLdouble theta = pi / 2.0;//旋转弧度
rotate2D(pivPt, theta);
//变换序列3:平移变换
GLfloat tx = 0.0, ty = 100.0;
translate2D(tx, ty);
/* 应用复合矩阵到三角形 */
transformVerts2D(nVerts, verts);
glColor3f(1.0, 0.0, 0.0); //重新设置前景色为红色
triangle(verts); //显示红色三角形(变换后)
glFlush();
}
void winReshapeFcn(GLint newWidth, GLint newHeight)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(xwcMin, xwcMax, ywcMin, ywcMax);
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(50, 50);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("二维几何变换实例-复合变换");
init();
glutDisplayFunc(displayFcn);
glutReshapeFunc(winReshapeFcn);
glutMainLoop();
}
#include
#include
#include
/* 初始化显示窗口大小 */
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);
glClear(GL_COLOR_BUFFER_BIT);
}
//class wcPt3D
//{
//public:
// GLfloat x, y, z;
//};
struct wcPt3D
{
GLfloat x, y, z;
};
/* 三维旋转变换,参数:旋转轴(由点p1和p2定义)和旋转角度(thetaDegrees)*/
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);
/*通过平移-旋转-平移复合变换序列完成任意轴的旋转(注意OpenGL中的反序表示)*/
//③移动p1到原始位置
glTranslatef(p1.x, p1.y, p1.z);
//②关于通过坐标原点的坐标轴旋转
glRotatef(thetaDegrees, vx, vy, vz);
//①移动p1到原点位置
glTranslatef(-p1.x, -p1.y, -p1.z);
}
/* 三维比例放缩变换,参数:比例系数sx、sy、sz和固定点fixedPt */
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 displayFcn(void)
{
/* 设置变换中心点位置 */
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;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); //清空变换矩阵为单位矩阵,恢复原始坐标系环境
/* 显示变换前几何对象 */
glColor3f(0.0, 0.0, 1.0); // 设置前景色为蓝色
glRecti(50, 100, 200, 150); //显示蓝色矩形(变换前)
/* 执行几何变换(注意以反序形式写出)*/
// ③平移变换
GLfloat tx = 0.0, ty = 100.0, tz = 0;
glTranslatef(tx, ty, tz);
glColor3f(1.0, 1.0, 0.0); //重新设置前景色为黄色
glRecti(50, 100, 200, 150); //显示黄色矩形(变换后)
// ②比例放缩变换
GLfloat sx = 0.5, sy = 0.5, sz = 1;
wcPt3D fixedPt = centroidPt;
scale3D(sx, sy, sz, fixedPt);
glColor3f(0.0, 1.0, 0.0); //重新设置前景色为绿色
glRecti(50, 100, 200, 150); //显示绿色矩形(变换后)
// ①旋转变换
wcPt3D p1, p2;
p1 = R_p1;
p2 = R_p2;
GLdouble thetaDegrees = 90;
rotate3D(p1, p2, thetaDegrees);
/* 显示变换后几何对象 */
glColor3f(1.0, 0.0, 0.0); //重新设置前景色为红色
glRecti(50, 100, 200, 150); //显示红色矩形(变换后)
glFlush();
}
void winReshapeFcn(GLint newWidth, GLint newHeight)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(xwcMin, xwcMax, ywcMin, ywcMax);
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(50, 50);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("三维几何变换实例-OpenGL版复合变换");
init();
glutDisplayFunc(displayFcn);
glutReshapeFunc(winReshapeFcn);
glutMainLoop();
}
以上是opengl的学习笔记 仅供参考哦