OpenGL——二维几何变换
平移、旋转、缩放的实现
#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——二维几何变换的更多相关文章
- [opengl] 画一个可移动的自行车 二维几何变换(平移、旋转、缩放)
#include <cmath> #include "glut.h" #include "iostream" using namespace std ...
- [图形学] Chp8.4 OpenGL 二维观察函数——视口
这节有几个显示窗口的控制函数,可以调整视口,创建子窗口,最小化为图标,设置图标名称,隐藏显示等. gluOrtho2D (xwmin, xwmax, ywmin, ywmax); // 定义二维裁剪窗 ...
- 【opengl】OpenGL中三维物体显示在二维屏幕上显示的变换过程
转自:http://blog.sina.com.cn/s/blog_957b9fdb0100zesv.html 为了说明在三维物体到二维图象之间,需要经过什么样的变换,我们引入了相机(Camera)模 ...
- openGL实现二维图形和三维图形
openGL是一个强大的底层图形库,其命令最初的时候使用C语言实现的.openGL定义了一个图形程序接口,常用于制作处理三维图像,功能强大,调用方便,在图像处理十分受欢迎. 实现图形主要使用的是ope ...
- OpenGL屏幕二维坐标转化成三维模型坐标
我们把OpenGL里模型的三维坐标往二维坐标的转化称为投影,则屏幕上的二维坐标往三维坐标转化则可以称为反投影,下面我们来介绍一下反投影的方法. 主要是gluUnProject函数的使用,下面是代码: ...
- VC6下OpenGL 开发环境的构建外加一个简单的二维网络棋盘绘制示例
一.安装GLUT 工具包 GLUT 不是OpenGL 所必须的,但它会给我们的学习带来一定的方便,推荐安装. Windows 环境下的GLUT 本地下载地址:glut-install.zip(大小约为 ...
- OpenGL中的二维编程——从简单的矩形开始
一.OpenGL的组成 图元函数(primitive function)指定要生成屏幕图像的图元.包括两种类型:可以在二维.三维或者四维空间进行定义的几何图元,如多边形:离散实体:位图. 属性函数(a ...
- Win窗口坐标二维坐标与OpenGl的世界坐标系的之间的相互转换
Win窗口坐标二维坐标与OpenGl的世界坐标系的转换 几何处理管线擅长于使用视图和投影矩阵以及用于裁剪的视口把顶点的世界坐标变换为窗口坐标. 但是,在有些情况下,需要逆转这个过程.一种常见的情形是: ...
- VC、OpenGL、ArcGIS Engine开发的二维三维结合的GIS系统
一.前言 众所周知,二维GIS技术发展了近四十年,伴随着计算机软硬件以及关系型数据库的飞速发展,二维GIS技术已日臻完善.在对地理信息的分析功能上有着无可比拟的优势.一些宏观的地理信息,一维的地理信息 ...
随机推荐
- 在vi中搜索字符串,替换字符串
在vi中搜索一个字符串: 输入命令:"vi test.txt"用vi打开一个文本文件. 输入命令:"/spider"用ESC键进入命令模式,然后输入一个&quo ...
- Window 任务栏清除历史记录
Window任务栏上的历史记录在很多情况下是很方便的[可以很快速的执行命令] 但如果历史记录太多可能会影响使用,经过我N长时间的研究和网络上的搜索 终于找到了清除的方法,网络上通过IE删除等方式都是无 ...
- 【转】JavaScript 的装载和执行
承接前面一篇文章<浏览器的渲染原理简介> ,本文来说下JavaScript的装载和执行. 通常来说,浏览器对于 JavaScript 的运行有两大特性: 1) 载入后马上执行 2) 执行时 ...
- [Python设计模式] 第8章 学习雷锋好榜样——工厂方法模式
github地址:https://github.com/cheesezh/python_design_patterns 简单工厂模式 v.s. 工厂方法模式 以简单计算器为例,对比一下简单工厂模式和工 ...
- Java调用Elasticsearch API查询及matchPhraseQuery和matchQuery的区别
一.引入依赖 <!--Elasticsearch client--> <!-- https://mvnrepository.com/artifact/org.elasticsearc ...
- MySql之修改操作与进阶
一:更新特定行 UPDATE tableName SET 列名 = 值,列名 = 值... WHERE 条件; 二:使用子查询更新数据 UPDATE tableName SET 列名 = SELECT ...
- 谈谈MySQL死锁之二 死锁检测和处理源码分析
这一篇主要是通过一个实验来进行描述,过程是比较枯燥的. 实验准备 create table test_lock(id int auto_increment primary key ,stock int ...
- Google 发布的15个 Android 性能优化典范
2015年伊始,Google发布了关于Android性能优化典范的专题,一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍了Android系统中有关 ...
- aaronyang的百度地图API之LBS云与.NET开发 Javascript API 2.0【把数据存到LBS云2/2】
中国的IT 需要无私分享和贡献的人,一起努力 本篇博客来自地址:http://www.cnblogs.com/AaronYang/p/3672898.html,请支持原创,未经允许不许转载 1.新建一 ...
- SNF快速开发平台MVC-Grid++集成打印
一.显示效果: 二.程序实现: 1.先要在 打印模版程序 给指定页面进行在线设计打印模版 2.在使用的程序当中,增加如下代码即可.程序上是可以挂多个打印模版的,程序页面的代码不用动直接可以读取到打印模 ...