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技术已日臻完善.在对地理信息的分析功能上有着无可比拟的优势.一些宏观的地理信息,一维的地理信息 ...
随机推荐
- POI HSSFCellStyle 设置 Excel 单元格样式
POI中可能会用到一些需要设置EXCEL单元格格式的操作小结: 先获取工作薄对象: HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb ...
- EBS R12视图打不到数据
兩個原因引起的,一個是語言,一個是oualter session set nls_language ='AMERICAN';alter session set nls_language='SIMPLI ...
- 手把手教你 Mockito 的使用
什么是 Mockito Mockito 是一个强大的用于 Java 开发的模拟测试框架, 通过 Mockito 我们可以创建和配置 Mock 对象, 进而简化有外部依赖的类的测试.使用 Mockito ...
- Fortran中的指针使用
Fortran中的指针如何使用,功能怎样,下面的的5个例子足可以让你明白一切! 对于单个值,用起来很简单,但是无法体现指针的强大功能, 示例1: program test_pointer_1 impl ...
- [Mockito] Mock List interface
In this post, we are going to see more functionalities in Mockito. Mock a List interface: @Test publ ...
- grid - gap
grid-gap默认还有两个参数 如果grid写默认方式,则行.列都会使用相同的单位 如果grid写两个参数,则行和列各自生效 如果grid写行方式,则仅有行生效 如果grid写列方式,则仅有列生 ...
- chromium 使用 flash
这又是个月经问题,几乎重装一次系统就得在搞一次. 1. chromium 使用的 flash 下载 https://get.adobe.com/cn/flashplayer/otherversions ...
- cas 资源
http://blog.sina.com.cn/s/blog_6fda308501012tk2.html http://www.blogjava.net/xmatthew/archive/2008/0 ...
- 搭建 WordPress 个人博客
系统要求:centos 6.8 64位 一. 准备 LNMP 环境 LNMP 是 Linux.Nginx.MySQL 和 PHP 的缩写,是 WordPress 博客系统依赖的基础运行环境.我们先来准 ...
- Linux使用过程中常见问题及其解决方法
“我不怕问题的出现,相反,我喜欢问题,因为我知道这是一种成长............” 1,ubuntu中文输入法的安装: 今天重装了英文版的ubuntu,而发现中文输入法并没有自动安装好,于是搜了 ...