平移、旋转、缩放的实现

#include<iostream>
#include <math.h>
#include<Windows.h>
#include <GL/glut.h> using namespace std; GLsizei winWidth = , winHeight = ; GLfloat xwcMin = 0.0, xwcMax = 225.0;
GLfloat ywcMin = 0.0, ywcMax = 225.0; class wcPt2D {
public:
GLfloat x, y;
}; typedef GLfloat Matrix3x3[][]; Matrix3x3 matComposite; const GLdouble pi = 3.14159; void init()
{
//窗口背景为白色
glClearColor(1.0, 1.0, 1.0, 0.0);
} void matrix3x3SetIdentity(Matrix3x3 matIdent3x3)
{
GLint row, col;
for (row = ; row < ; row++) {
for (col = ; col < ; col++) {
matIdent3x3[row][col] = (row == col);
}
}
} void matrix3x3PreMultiply(Matrix3x3 m1, Matrix3x3 m2)
{
GLint row, col;
Matrix3x3 matTemp; for (row = ; row < ; row++) {
for (col = ; col < ; col++) {
matTemp[row][col] = m1[row][] * m2[][col] + m1[row][] * m2[][col] +
m1[row][] * m2[][col];
}
} for (row = ; row < ; row++) {
for (col = ; col < ; col++) {
m2[row][col] = matTemp[row][col];
}
}
} void translate2D(GLfloat tx, GLfloat ty)
{
Matrix3x3 matTransl; matrix3x3SetIdentity(matTransl); matTransl[][] = tx;
matTransl[][] = ty; matrix3x3PreMultiply(matTransl, matComposite);
} void rotate2D(wcPt2D pivotPt, GLfloat theta)
{
Matrix3x3 matRot; matrix3x3SetIdentity(matRot); matRot[][] = cos(theta);
matRot[][] = -sin(theta);
matRot[][] = pivotPt.x * ( - cos(theta)) + pivotPt.y * sin(theta);
matRot[][] = sin(theta);
matRot[][] = cos(theta);
matRot[][] = pivotPt.y * ( - cos(theta)) - pivotPt.x * sin(theta); matrix3x3PreMultiply(matRot, matComposite);
} void scale2D(GLfloat sx, GLfloat sy, wcPt2D fixedPt)
{
Matrix3x3 matScale; matrix3x3SetIdentity(matScale); matScale[][] = sx;
matScale[][] = ( - sx) * fixedPt.x;
matScale[][] = sy;
matScale[][] = ( - sy) * fixedPt.y; matrix3x3PreMultiply(matScale, matComposite);
} void transformVerts2D(GLint nVerts, wcPt2D * verts)
{
GLint k;
GLfloat temp; for (k = ; k < nVerts; k++) {
temp = matComposite[][] * verts[k].x + matComposite[][] * verts[k].y + matComposite[][];
verts[k].y = matComposite[][] * verts[k].x + matComposite[][] * verts[k].y + matComposite[][];
verts[k].x = temp;
}
} void triangle(wcPt2D *verts)
{
glBegin(GL_TRIANGLES);
for (GLint k = ; k < ; k++) {
glVertex2f(verts[k].x, verts[k].y);
}
glEnd();
} void displayFcn()
{
GLint nVerts = ; wcPt2D verts[] = { {50.0,25.0},{150.0,25.0},{100.0,100.0} }; wcPt2D centroidPt; GLint xSum = , ySum = ; for (GLint k = ; 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 / ; glClear(GL_COLOR_BUFFER_BIT);
glColor3f(200.0 / 255.0, 200.0 / 255.0, 169.0 / 255.0);
triangle(verts); matrix3x3SetIdentity(matComposite); scale2D(sx, sy, fixedPt); transformVerts2D(nVerts, verts);
glColor3f(137.0 / 255.0, 190.0 / 255.0, 178.0 / 255.0);
triangle(verts); matrix3x3SetIdentity(matComposite);
rotate2D(pivPt, theta);
transformVerts2D(nVerts, verts);
glColor3f(69.0 / 255.0, 137.0 / 255.0, 148.0 / 255.0);
triangle(verts); matrix3x3SetIdentity(matComposite);
translate2D(tx, ty);
transformVerts2D(nVerts, verts);
glColor3f(178.0 / 255.0, 200.0 / 255.0, 187.0 / 255.0);
triangle(verts); glFlush();
} void winReshapeFcn(GLint newWidth, GLint newHeight)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(xwcMin, xwcMax, ywcMin, ywcMax);
glClear(GL_COLOR_BUFFER_BIT);
} int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(, ); glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("二维几何变换");
init();
glutDisplayFunc(displayFcn);
glutReshapeFunc(winReshapeFcn);
glutMainLoop(); system("pause");
return ;
}

运行结果

OpenGL——二维几何变换的更多相关文章

  1. [opengl] 画一个可移动的自行车 二维几何变换(平移、旋转、缩放)

    #include <cmath> #include "glut.h" #include "iostream" using namespace std ...

  2. [图形学] Chp8.4 OpenGL 二维观察函数——视口

    这节有几个显示窗口的控制函数,可以调整视口,创建子窗口,最小化为图标,设置图标名称,隐藏显示等. gluOrtho2D (xwmin, xwmax, ywmin, ywmax); // 定义二维裁剪窗 ...

  3. 【opengl】OpenGL中三维物体显示在二维屏幕上显示的变换过程

    转自:http://blog.sina.com.cn/s/blog_957b9fdb0100zesv.html 为了说明在三维物体到二维图象之间,需要经过什么样的变换,我们引入了相机(Camera)模 ...

  4. openGL实现二维图形和三维图形

    openGL是一个强大的底层图形库,其命令最初的时候使用C语言实现的.openGL定义了一个图形程序接口,常用于制作处理三维图像,功能强大,调用方便,在图像处理十分受欢迎. 实现图形主要使用的是ope ...

  5. OpenGL屏幕二维坐标转化成三维模型坐标

    我们把OpenGL里模型的三维坐标往二维坐标的转化称为投影,则屏幕上的二维坐标往三维坐标转化则可以称为反投影,下面我们来介绍一下反投影的方法. 主要是gluUnProject函数的使用,下面是代码: ...

  6. VC6下OpenGL 开发环境的构建外加一个简单的二维网络棋盘绘制示例

    一.安装GLUT 工具包 GLUT 不是OpenGL 所必须的,但它会给我们的学习带来一定的方便,推荐安装. Windows 环境下的GLUT 本地下载地址:glut-install.zip(大小约为 ...

  7. OpenGL中的二维编程——从简单的矩形开始

    一.OpenGL的组成 图元函数(primitive function)指定要生成屏幕图像的图元.包括两种类型:可以在二维.三维或者四维空间进行定义的几何图元,如多边形:离散实体:位图. 属性函数(a ...

  8. Win窗口坐标二维坐标与OpenGl的世界坐标系的之间的相互转换

    Win窗口坐标二维坐标与OpenGl的世界坐标系的转换 几何处理管线擅长于使用视图和投影矩阵以及用于裁剪的视口把顶点的世界坐标变换为窗口坐标. 但是,在有些情况下,需要逆转这个过程.一种常见的情形是: ...

  9. VC、OpenGL、ArcGIS Engine开发的二维三维结合的GIS系统

    一.前言 众所周知,二维GIS技术发展了近四十年,伴随着计算机软硬件以及关系型数据库的飞速发展,二维GIS技术已日臻完善.在对地理信息的分析功能上有着无可比拟的优势.一些宏观的地理信息,一维的地理信息 ...

随机推荐

  1. android: 动态加载碎片布局的技巧

    虽然动态添加碎片的功能很强大,可以解决很多实际开发中的问题,但是它毕竟只是在 一个布局文件中进行一些添加和替换操作.如果程序能够根据设备的分辨率或屏幕大小在运 行时来决定加载哪个布局,那我们可发挥的空 ...

  2. Hyperledger超级账本在Centos7下搭建运行环境

    超级账本(hyperledger)是Linux基金会于2015年发起的推进区块链数字技术和交易验证的开源项目,加入成员包括:荷兰银行(ABN AMRO).埃森哲(Accenture)等十几个不同利益体 ...

  3. JavaScript之Date日期对象扩展

    各种时间加减 收藏起来以备后用 //名称:日期加法函数 //参数:part(year.month.day.hour.minute.second.millisecond) //返回:Date对象 Dat ...

  4. 熬之滴水成石:最想深入了解的内容--windows内核机制(6)

    58 进程和线程(3) 说完进程再说说线程,线程相比于进程其实有更多可说的内容.首先实现线程调用的数据结构是个栈,该栈记录了调用方法的信息这里面也包括了函数调用及返回的地址.线程肯定是属于某个进程,其 ...

  5. ImageProcessor.Web,再也不用自己生成缩略图了

    1.什么是ImageProcessor.Web ImageProcessor.Web是基于ImageProcessor的web图像处理模块,允许开发者使用URL查询字符串参数的方式作为指令执行图像处理 ...

  6. ubuntu 登陆信息打印 -- motd

    新需求需要改变 Ubuntu 启动时的登录信息打印,根据搜索到的资料,找到了这里: luo[~]ssh luo@192.168.100.233 Press ^@ (C-Space) to enter ...

  7. 【Android】Android传感器

    1.加速度传感器2.磁场传感器3.方向传感器4.陀螺仪传感器5.重力传感器6.线性加速度传感器7.温度传感器8.光线传感器9.距离传感器10.压力传感器11.计步传感器 首先先查看测试的安卓机拥有的传 ...

  8. <script> 的defer和async

    <script src="../file.js" async="async"></script> file.js---- 仅仅只有ale ...

  9. [Big Data - ZooKeeper] ZooKeeper: A Distributed Coordination Service for Distributed Applications

    ZooKeeper ZooKeeper: A Distributed Coordination Service for Distributed Applications Design Goals Da ...

  10. linux每日命令(24):Linux 目录结构

    一. 简介 对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统只 ...