博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
OpenGL图形编程实现2D变换
阅读量:7122 次
发布时间:2019-06-28

本文共 2033 字,大约阅读时间需要 6 分钟。

二维几何变换相比三维略简单一点但原理基本一致,下列代码实现了平移、缩放、旋转变换,并有一个复合变换矩阵保存所有变换计算后的结果,方便用其结果在OpenGL进行渲染

#include 
#include
class wcPt2D {public: GLfloat x, y;};typedef GLfloat Matrix3x3[3][3];const GLdouble pi = 3.14159;Matrix3x3 matComposite;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; }}

 

转载于:https://www.cnblogs.com/paralysis/p/10800965.html

你可能感兴趣的文章
京东测试之道,这些你早该知道!
查看>>
jQuery可放大预览的图片滑块
查看>>
SpringBoot连接Redis哨兵模式
查看>>
【WPF】ComboBoxItem的禁用
查看>>
HTML5_CSS3仿Google Play垂直菜单
查看>>
达观杯文本智能处理挑战赛 练手代码实现
查看>>
Tornado 简单入门教程(一)——Demo1
查看>>
Pgpool-II 最新小版本更新发布,PgSQL 负载均衡中间件
查看>>
数据传输加密方式总结
查看>>
U-Boot启动过程完全分析
查看>>
深入理解Java中的底层阻塞原理及实现
查看>>
shell编程之转义和引用
查看>>
云盾.态势感知情报生态合作发布
查看>>
PHP排序函数
查看>>
ora.proxy_advm
查看>>
GitHub在其网站实现中移除对jQuery的使用
查看>>
美国明尼苏达州大学研制出仿生眼原型
查看>>
这些年,我是如何当好一个技术支持的
查看>>
多个网站域名进行301跳转合并对SEO有什么影响
查看>>
Linux学习笔记1_用户和权限
查看>>