矩阵基础知识

要对矩阵进行运算,必须先要了解矩阵的计算公式,这个知识的内容涉及到了线性代数。

我们知道在Cocos2dx中,有关于平移,旋转,缩放等等操作,都必须要进行矩阵的乘法。

只需要一张图就能理解怎么计算矩阵的乘法了。

Cocos2dx中的代码实现

先来看cocos2dx中的代码实现:

.h文件的代码

  1. #include <stdio.h>
  2. #include "CCMath.h"
  3. #include "CCVector2.h"
  4. #include "CCVector3.h"
  5. #include "CCVector4.h"
  6. #include "CCPoint.h"
  7. #include "CCMatrix.h"
  8. //仿射矩阵
  9. //单位矩阵
  10. /**@{
  11. Affine transform
  12. a b 0
  13. c d 0
  14. tx ty 1
  15. Identity
  16. 1 0 0
  17. 0 1 0
  18. 0 0 1
  19. */
  20. struct AffineTransform {
  21. float a, b, c, d;
  22. float tx, ty;
  23. static const AffineTransform IDENTITY;
  24. };
  25. #endif /* CCAffineTransform_hpp */
  26. // 创建仿射矩阵
  27. AffineTransform AffineTransformMake(float a, float b, float c, float d, float tx, float ty);
  28. // 节点乘以仿射矩阵
  29. Vector2 PointApplyAffineTransform(const Vector2& point, const AffineTransform& t);
  30. // size乘以仿射矩阵
  31. CCSize SizeApplyAffineTransform(const CCSize& size, const AffineTransform& t);
  32. // rect乘以仿射矩阵
  33. CCRect RectApplyAffineTransform(const CCRect& rect, const AffineTransform& anAffineTransform);
  34. // rect乘以矩阵
  35. CCRect RectApplyTransform(const CCRect& rect, const Matrix& transform);
  36. // 节点乘以矩阵
  37. Vector2 PointApplyTransform(const Vector2& point, const Matrix& transform);
  38. /**
  39. Translation, equals
  40. 1 0 1
  41. 0 1 0 * affine transform
  42. tx ty 1
  43. */
  44. // 平移
  45. AffineTransform AffineTransformTranslate(const AffineTransform& t, float tx, float ty);
  46. /**
  47. Rotation, equals
  48. cos(angle) sin(angle) 0
  49. -sin(angle) cos(angle) 0 * AffineTransform
  50. 0 0 1
  51. */
  52. // 旋转
  53. AffineTransform AffineTransformRotate(const AffineTransform& aTransform, float anAngle);
  54. /**
  55. Scale, equals
  56. sx 0 0
  57. 0 sy 0 * affineTransform
  58. 0 0 1
  59. */
  60. // 缩放
  61. AffineTransform AffineTransformScale(const AffineTransform& t, float sx, float sy);
  62. /**Concat two affine transform, t1 * t2*/
  63. AffineTransform AffineTransformConcat(const AffineTransform& t1, const AffineTransform& t2);
  64. /**Compare affine transform.*/
  65. // 仿射矩阵是否和矩阵相等
  66. bool AffineTransformEqualToTransform(const AffineTransform& t1, const AffineTransform& t2);
  67. /**Get the inverse of affine transform.*/
  68. // 获取仿射矩阵的逆矩阵
  69. AffineTransform AffineTransformInvert(const AffineTransform& t);
  70. /**Concat Mat4, return t1 * t2.*/
  71. // 矩阵相乘
  72. Matrix TransformConcat(const Matrix& t1, const Matrix& t2);
  73. // 单位矩阵
  74. extern const AffineTransform AffineTransformIdentity;

.cpp的代码:


  1. #include "CCAffineTransform.h"
  2. #include <algorithm>
  3. #include <math.h>
  4. using namespace std;
  5. // 构造函数
  6. AffineTransform AffineTransformMake(float a, float b, float c, float d, float tx, float ty)
  7. {
  8. AffineTransform t;
  9. t.a = a; t.b = b; t.c = c; t.d = d; t.tx = tx; t.ty = ty;
  10. return t;
  11. }
  12. Vector2 PointApplyAffineTransform(const Vector2& point, const AffineTransform& t)
  13. {
  14. Vector2 p;
  15. p.x = (float)((double)t.a * point.x + (double)t.c * point.y + t.tx);
  16. p.y = (float)((double)t.b * point.x + (double)t.d * point.y + t.ty);
  17. return p;
  18. }
  19. Vector2 PointApplyTransform(const Vector2& point, const Matrix& transform)
  20. {
  21. Vector3 vec(point.x, point.y, 0);
  22. transform.transformPoint(&vec);
  23. return Vector2(vec.x, vec.y);
  24. }
  25. CCSize SizeApplyAffineTransform(const CCSize& size, const AffineTransform& t)
  26. {
  27. CCSize s;
  28. s.width = (float)((double)t.a * size.width + (double)t.c * size.height);
  29. s.height = (float)((double)t.b * size.width + (double)t.d * size.height);
  30. return s;
  31. }
  32. // 单位矩阵
  33. AffineTransform AffineTransformMakeIdentity()
  34. {
  35. return AffineTransformMake(1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
  36. }
  37. extern const AffineTransform AffineTransformIdentity = AffineTransformMakeIdentity();
  38. const AffineTransform AffineTransform::IDENTITY = AffineTransformMakeIdentity();
  39. CCRect RectApplyAffineTransform(const CCRect& rect, const AffineTransform& anAffineTransform)
  40. {
  41. float top = rect.getMinY();
  42. float left = rect.getMinX();
  43. float right = rect.getMaxX();
  44. float bottom = rect.getMaxY();
  45. Vector2 topLeft = PointApplyAffineTransform(Vector2(left, top), anAffineTransform);
  46. Vector2 topRight = PointApplyAffineTransform(Vector2(right, top), anAffineTransform);
  47. Vector2 bottomLeft = PointApplyAffineTransform(Vector2(left, bottom), anAffineTransform);
  48. Vector2 bottomRight = PointApplyAffineTransform(Vector2(right, bottom), anAffineTransform);
  49. float minX = min(min(topLeft.x, topRight.x), min(bottomLeft.x, bottomRight.x));
  50. float maxX = max(max(topLeft.x, topRight.x), max(bottomLeft.x, bottomRight.x));
  51. float minY = min(min(topLeft.y, topRight.y), min(bottomLeft.y, bottomRight.y));
  52. float maxY = max(max(topLeft.y, topRight.y), max(bottomLeft.y, bottomRight.y));
  53. return CCRect(minX, minY, (maxX - minX), (maxY - minY));
  54. }
  55. CCRect RectApplyTransform(const CCRect& rect, const Matrix& transform)
  56. {
  57. float top = rect.getMinY();
  58. float left = rect.getMinX();
  59. float right = rect.getMaxX();
  60. float bottom = rect.getMaxY();
  61. Vector3 topLeft(left, top, 0);
  62. Vector3 topRight(right, top, 0);
  63. Vector3 bottomLeft(left, bottom, 0);
  64. Vector3 bottomRight(right, bottom, 0);
  65. transform.transformPoint(&topLeft);
  66. transform.transformPoint(&topRight);
  67. transform.transformPoint(&bottomLeft);
  68. transform.transformPoint(&bottomRight);
  69. float minX = min(min(topLeft.x, topRight.x), min(bottomLeft.x, bottomRight.x));
  70. float maxX = max(max(topLeft.x, topRight.x), max(bottomLeft.x, bottomRight.x));
  71. float minY = min(min(topLeft.y, topRight.y), min(bottomLeft.y, bottomRight.y));
  72. float maxY = max(max(topLeft.y, topRight.y), max(bottomLeft.y, bottomRight.y));
  73. return CCRect(minX, minY, (maxX - minX), (maxY - minY));
  74. }
  75. AffineTransform AffineTransformTranslate(const AffineTransform& t, float tx, float ty)
  76. {
  77. return AffineTransformMake(t.a, t.b, t.c, t.d, t.tx + t.a * tx + t.c * ty, t.ty + t.b * tx + t.d * ty);
  78. }
  79. AffineTransform AffineTransformScale(const AffineTransform& t, float sx, float sy)
  80. {
  81. return AffineTransformMake(t.a * sx, t.b * sx, t.c * sy, t.d * sy, t.tx, t.ty);
  82. }
  83. AffineTransform AffineTransformRotate(const AffineTransform& t, float anAngle)
  84. {
  85. float sine = sinf(anAngle);
  86. float cosine = cosf(anAngle);
  87. return AffineTransformMake( t.a * cosine + t.c * sine,
  88. t.b * cosine + t.d * sine,
  89. t.c * cosine - t.a * sine,
  90. t.d * cosine - t.b * sine,
  91. t.tx,
  92. t.ty);
  93. }
  94. /* Concatenate `t2' to `t1' and return the result:
  95. t' = t1 * t2 */
  96. AffineTransform AffineTransformConcat(const AffineTransform& t1, const AffineTransform& t2)
  97. {
  98. return AffineTransformMake( t1.a * t2.a + t1.b * t2.c, t1.a * t2.b + t1.b * t2.d, //a,b
  99. t1.c * t2.a + t1.d * t2.c, t1.c * t2.b + t1.d * t2.d, //c,d
  100. t1.tx * t2.a + t1.ty * t2.c + t2.tx, //tx
  101. t1.tx * t2.b + t1.ty * t2.d + t2.ty); //ty
  102. }
  103. Matrix TransformConcat(const Matrix& t1, const Matrix& t2)
  104. {
  105. return t1 * t2;
  106. }
  107. /* Return true if `t1' and `t2' are equal, false otherwise. */
  108. bool AffineTransformEqualToTransform(const AffineTransform& t1, const AffineTransform& t2)
  109. {
  110. return (t1.a == t2.a && t1.b == t2.b && t1.c == t2.c && t1.d == t2.d && t1.tx == t2.tx && t1.ty == t2.ty);
  111. }
  112. AffineTransform AffineTransformInvert(const AffineTransform& t)
  113. {
  114. float determinant = 1 / (t.a * t.d - t.b * t.c);
  115. return AffineTransformMake(determinant * t.d, -determinant * t.b, -determinant * t.c, determinant * t.a,
  116. determinant * (t.c * t.ty - t.d * t.tx), determinant * (t.b * t.tx - t.a * t.ty) );
  117. }

讲解

看讲解:

1. CCAffineTransform

首先要知道定义了这么一个CCAffineTransform专门用来表示变换矩阵。

  1. struct CCAffineTransform {
  2. float a, b, c, d;
  3. float tx, ty;
  4. };

在代码中AffineTransformMake用来构造这个变换矩阵。

用图表示则是:

2. 单位矩阵

很明显,就是这个函数AffineTransformMakeIdentity。

3. 平移

AffineTransformTranslate,用公式来看就是:

4. 缩放

AffineTransformScale,用公式来看就是:

5. 旋转

AffineTransformRotate,用公式来看就是:

先要根据旋转角度去求出对应的值,再将矩阵右乘以变换矩阵

6. 连接

TransformConcat,直接相乘,用公式来看就是:

6. 逆

AffineTransformInvert,求矩阵的逆矩阵,通过公示可以计算得出:

这些是实现cocos2dx矩阵变换的基础,掌握这个很有必要。这里

参考

http://www.cnblogs.com/logicbaby/p/4282003.html

http://www.360doc.com/content/13/1223/22/110467_339624338.shtml

http://blog.csdn.net/runaying/article/details/13094553

其它

和矩阵的乘积,得到的结果就是经过变换后的矩阵。注意,顺序应该是缩放,渲染,平移,只要更改顺序得到的结果就会很不一样。

这就是几个坐标系在整个渲染过程的变换,经历三次矩阵变换,这就是著名的MVP矩阵的由来

  1. 本地坐标空间(local space):在建模软件中的坐标空间,物体本身的坐标空间。

  2. 世界坐标空间(world space):将所有模型摆放到一个世界空间中,这个模型在空间中的坐标。

  3. 观察坐标空间(view space):即摄像机范围空间

  4. 裁剪坐标空间(clip space):将不在摄像机范围内的模型裁剪出去后的空间

  5. 屏幕空间坐标(screen space):最终显示在屏幕上的坐标空间。

坐标系

  1. GL坐标系:Cocos2D以OpenglES为图形库,所以它使用OpenglES坐标系。GL坐标系原点在屏幕左下角,x轴向右,y轴向上。

  2. 屏幕坐标系:苹果的Quarze2D使用的是不同的坐标系统,原点在屏幕左上角,x轴向右,y轴向下。ios的屏幕触摸事件CCTouch传入的位置信息使用的是该坐标系。因此在cocos2d中对触摸事件做出响应前需要首先把触摸点转化到GL坐标系。可以使用CCDirector的convertToGL来完成这一转化。

  3. 世界坐标系:世界坐标系也叫做绝对坐标系,是游戏开发中的概念,它建立了描述其他坐标系所需要的参考框架。我们能够用世界坐标系来描述其他坐标系的位置,而不能用更大的,外部的坐标系来描述世界坐标系。cocos2d中的元素是有父子关系的层级结构,我们通过CCNode的position设定元素的位置使用的是相对与其父节点的本地坐标系而非世界坐标系。最后在绘制屏幕的时候cocos2d会把这些元素的本地坐标映射成世界坐标系坐标。世界坐标系和GL坐标系一致,原点在屏幕左下角,x轴向右,y轴向上。

  4. 本地坐标系:本地坐标系也叫做物体坐标系,是和特定物体相关联的坐标系。每个物体都有它们独立的坐标系,当物体移动或改变方向时,和该物体关联的坐标系将随之移动或改变方向。例如坐出租车的时候对驾驶员说“向左转”,我们使用的是车的物体坐标系,“前”、“后”、“左”、“右”只有在物体坐标系中才有意义。但如果我们说“向东开”,我们使用的就是世界坐标系了,无论是车内还是车外的人都知道应该向什么方向开。CCNode的position使用的就是父节点的本地坐标系,它和GL坐标系也是一致的,x轴向右,y轴向上,原点在父节点的左下角。如果父节点是场景树中的顶层节点,那么它使用的本地坐标系就和世界坐标系重合了。在CCNode对象中有几个方便的函数可以做坐标转换:convertToWorldSpace方法可以把基于当前节点的本地坐标系下的坐标转换到世界坐标系中。convertToNodeSpace方法可以把世界坐标转换到当前节点的本地坐标系中。注意这些方法转换的是基于当前节点的坐标,而一个节点的position所使用的坐标是基于它父节点的本地坐标,因此我们要把node的位置转换到世界坐标系中应该调用父节点的convertToWorldSpace函数 [node.parent convertToWorldSpace:[node position]]。几乎所有的游戏引擎都会使用本地坐标系而非世界坐标系来指定元素的位置,这样做的好处是当计算物体运动的时候使用同一本地坐标系的元素可以作为一个子系统独立计算,最后再加上坐标系的运动即可,这是物理研究中常用的思路。例如一个在行驶的车厢内上下跳动的人,我们只需要在每帧绘制的时候计算他在车厢坐标系中的位置,然后加上车的位置就可以计算出人在世界坐标系中的位置,如果使用单一的世界坐标系,人的运动轨迹就变复杂了。

OpenGL 模型视图投影矩阵 仿射矩阵的更多相关文章

  1. WEBGL学习【八】模型视图投影矩阵

    <!--探讨WEBGL中不同图形的绘制方法:[待测试2017.11.6]--> <!DOCTYPE HTML> <html lang="en"> ...

  2. OpenGL模型视图变换、投影变换、视口变换的理解

    OpenGL中不设置模型,投影,视口,所绘制的几何图形的坐标只能是-1到1(X轴向右,Y轴向上,Z轴垂直屏幕向外). 产生目标场景的过程类似于用照相机进行拍照: (1)把照相机固定在三角架上,并让他对 ...

  3. 简单理解OpenGL模型视图变换

    前几天学习了OpenGL的绘图原理(其实就是坐标的不停变换变换),看到网上有个比较好的例程,于是学习了下,并在自己感兴趣的部分做了注释. 首先通过glMatrixMode(GL_MODELVIEW)设 ...

  4. WEBGL学习【四】模型视图矩阵

    <html lang="zh-CN"> <!--服务器运行地址:http://127.0.0.1:8080/webgl/LearnNeHeWebGL/NeHeWe ...

  5. WebGL或OpenGL关于模型视图投影变换的设置技巧

    目录 1. 具体实例 2. 解决方案 1) Cube.html 2) Cube.js 3) 运行结果 3. 详细讲解 1) 模型变换 2) 视图变换 3) 投影变换 4) 模型视图投影矩阵 4. 存在 ...

  6. three.js中的矩阵变换(模型视图投影变换)

    目录 1. 概述 2. 基本变换 2.1. 矩阵运算 2.2. 模型变换矩阵 2.2.1. 平移矩阵 2.2.2. 旋转矩阵 2.2.2.1. 绕X轴旋转矩阵 2.2.2.2. 绕Y轴旋转矩阵 2.2 ...

  7. OpenGL(五) 三维变换之模型视图矩阵

    计算机三维图形学中,一个基本的任务是如何描述三维空间中一个物体位置的变化,也就是如何 描述物体的运动.通常情况下,物体位置的变化包含三个基本的变化:平移.旋转和缩放,物体的运动也可以用这三个基本的运动 ...

  8. OpenGL中glRotatef()函数究竟对矩阵做了什么

    OpenGL中glRotatef()函数究竟对矩阵做了什么 我们知道OpenGL中维持着两套矩阵,一个是模型视图矩阵(model view matrix),另一个是投影矩阵(projection ma ...

  9. Android OpenGL ES 开发(五): OpenGL ES 使用投影和相机视图

    OpenGL ES环境允许你以更接近于你眼睛看到的物理对象的方式来显示你绘制的对象.物理查看的模拟是通过对你所绘制的对象的坐标进行数学变换完成的: Projection - 这个变换是基于他们所显示的 ...

随机推荐

  1. 【转】WCF光芒下的Web Service

    WCF光芒下的Web Service 学习.NET的开发人员,在WCF的光芒照耀下,Web Service 似乎快要被人遗忘了.因为身边做技术的人一开口就是WCF多么的牛逼!废话不多,本人很久不写博客 ...

  2. Flask:初次使用Blueprints

    Windows 10家庭中文版,Python 3.6.4,Flask 1.0.2,Eclipse Oxygen.1a Release (4.7.1a),PyDev 6.3.2 本文为记录自己第一次使用 ...

  3. SQLAlchemy-介绍安装

    一:概述 SQLAlchemy的SQL工具包和对象关系映射是一个全面的工具集,用来处理数据库和Python. 它有几个不同的功能领域,可以单独使用或组合使用. 所示的主要组件,组件依赖关系组织成层: ...

  4. 1.SpringBoot之Helloword 快速搭建一个web项目

    背景: Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配 ...

  5. No.10 selenium学习之路之通过元素定位获取属性

    1. implicitly_wait()隐形等待.等待页面加载完成,作用是全局的. 时间可以设置的长,短时间也没有影响.直到设置的时间耗完 时间耗完也不会报错 2.获取title值 driver.ti ...

  6. jQuery使用JSONP时的错误处理

    概述 什么是域,简单来说就是协议+域名或地址+端口,3者只要有任何一个不同就表示不在同一个域.跨域,就是在一个域中访问另一个域的数据. 如果只是加载另一个域的内容,而不需要访问其中的数据的话,跨域是很 ...

  7. java根据日期获取周几和获取某段时间内周几的日期

    整理两个日期的方法. 根据日期获取当天是周几 /** * 根据日期获取当天是周几 * @param datetime 日期 * @return 周几 */ public static String d ...

  8. 一步一步学习IdentityServer3 (8)

    IdentityServer3结合Hangfire及Cookies中间件实现授权 Idr3数据库Token过期管理 GlobalConfiguration.Configuration.UseSqlSe ...

  9. 浏览器开启web通知。

    https://www.cnblogs.com/xcsn/p/7767092.html

  10. centos6.5/6.3升级安装ImageMagick7.0.1-1

    线上论坛和应用程序的验证码功能都是使用的ImageMagick,但是版本比较老(centos yum安装的ImageMagick6.5.9).接到最新漏洞预报,紧急升级! ImageMagick图象处 ...