openGL坐标系包括旋转,平移,缩放被塞在一个矩阵里面。

坐标系之间的转换基础是矩阵的运算。

每个矩阵代表的坐标系,就是是原点坐标系通过旋转。平移,缩放得到的坐标系。

当一个矩阵右乘一个向量或是还有一个矩阵,意味着把右边的变换。变成相对于左边的矩阵坐标系之上。

假设把一个世界坐标的X转换到一个矩阵上,我们能够矩阵右乘这个坐标:

static float multiplyMX(Matrix4* matrix, float x) {
return matrix->m[0] * x + matrix->m[4] + matrix->m[8] + matrix->m[12];
}

假设把一个世界坐标Y转换到一个矩阵上。我们能够矩阵右乘这个坐标:

static float multiplyMY(Matrix4* matrix, float y) {
return matrix->m[1] + matrix->m[5] * y + matrix->m[9] + matrix->m[13];
}

假设把一个世界坐标点转换到一个矩阵上,我们能够矩阵右乘这个点:

static void multiplyMV4(Matrix4* matrix, float x, float y, float z, float w, Out(Vector4* result)) {
result->v[0] = matrix->m[0] * x + matrix->m[4] * y + matrix->m[8] * z + matrix->m[12] * w;
result->v[1] = matrix->m[1] * x + matrix->m[5] * y + matrix->m[9] * z + matrix->m[13] * w;
result->v[2] = matrix->m[2] * x + matrix->m[6] * y + matrix->m[10] * z + matrix->m[14] * w;
result->v[3] = matrix->m[3] * x + matrix->m[7] * y + matrix->m[11] * z + matrix->m[15] * w;
} static void multiplyMV3(Matrix4* matrix, float x, float y, float z, Out(Vector3* result)) {
result->v[0] = matrix->m[0] * x + matrix->m[4] * y + matrix->m[8] * z + matrix->m[12];
result->v[1] = matrix->m[1] * x + matrix->m[5] * y + matrix->m[9] * z + matrix->m[13];
result->v[2] = matrix->m[2] * x + matrix->m[6] * y + matrix->m[10] * z + matrix->m[14];
} static void multiplyMV2(Matrix4* matrix, float x, float y, Out(Vector2* result)) {
result->v[0] = matrix->m[0] * x + matrix->m[4] * y + matrix->m[8] + matrix->m[12];
result->v[1] = matrix->m[1] * x + matrix->m[5] * y + matrix->m[9] + matrix->m[13];
}

假设把一个世界坐标系转换到一个矩阵上,我们矩阵右乘这个矩阵:

static void multiplyMM(Matrix4* left, Matrix4* right, Out(Matrix4* result)) {
result->m[0] = left->m[0] * right->m[0] + left->m[4] * right->m[1] + left->m[8] * right->m[2] + left->m[12] * right->m[3];
result->m[1] = left->m[1] * right->m[0] + left->m[5] * right->m[1] + left->m[9] * right->m[2] + left->m[13] * right->m[3];
result->m[2] = left->m[2] * right->m[0] + left->m[6] * right->m[1] + left->m[10] * right->m[2] + left->m[14] * right->m[3];
result->m[3] = left->m[3] * right->m[0] + left->m[7] * right->m[1] + left->m[11] * right->m[2] + left->m[15] * right->m[3]; result->m[4] = left->m[0] * right->m[4] + left->m[4] * right->m[5] + left->m[8] * right->m[6] + left->m[12] * right->m[7];
result->m[5] = left->m[1] * right->m[4] + left->m[5] * right->m[5] + left->m[9] * right->m[6] + left->m[13] * right->m[7];
result->m[6] = left->m[2] * right->m[4] + left->m[6] * right->m[5] + left->m[10] * right->m[6] + left->m[14] * right->m[7];
result->m[7] = left->m[3] * right->m[4] + left->m[7] * right->m[5] + left->m[11] * right->m[6] + left->m[15] * right->m[7]; result->m[8] = left->m[0] * right->m[8] + left->m[4] * right->m[9] + left->m[8] * right->m[10] + left->m[12] * right->m[11];
result->m[9] = left->m[1] * right->m[8] + left->m[5] * right->m[9] + left->m[9] * right->m[10] + left->m[13] * right->m[11];
result->m[10] = left->m[2] * right->m[8] + left->m[6] * right->m[9] + left->m[10] * right->m[10] + left->m[14] * right->m[11];
result->m[11] = left->m[3] * right->m[8] + left->m[7] * right->m[9] + left->m[11] * right->m[10] + left->m[15] * right->m[11]; result->m[12] = left->m[0] * right->m[12] + left->m[4] * right->m[13] + left->m[8] * right->m[14] + left->m[12] * right->m[15];
result->m[13] = left->m[1] * right->m[12] + left->m[5] * right->m[13] + left->m[9] * right->m[14] + left->m[13] * right->m[15];
result->m[14] = left->m[2] * right->m[12] + left->m[6] * right->m[13] + left->m[10] * right->m[14] + left->m[14] * right->m[15];
result->m[15] = left->m[3] * right->m[12] + left->m[7] * right->m[13] + left->m[11] * right->m[14] + left->m[15] * right->m[15];
}

这就是利用矩阵, 把一个世界坐标系的坐标,转换到局部坐标系的方法。

那么。怎样把一个局部坐标系转换到世界坐标系呢?

这里须要得到局部坐标系相应矩阵的逆矩阵,这个矩阵包括了还原矩阵操作的变换。

然后,把逆矩阵当做左边的矩阵,去右乘局部坐标点, 我们就能够得到局部坐标变成世界坐标后的坐标。

static bool tryInvert(Matrix4* matrix, Out(Matrix4* result)) {
float a0 = matrix->m[0] * matrix->m[5] - matrix->m[1] * matrix->m[4];
float a1 = matrix->m[0] * matrix->m[6] - matrix->m[2] * matrix->m[4];
float a2 = matrix->m[0] * matrix->m[7] - matrix->m[3] * matrix->m[4];
float a3 = matrix->m[1] * matrix->m[6] - matrix->m[2] * matrix->m[5];
float a4 = matrix->m[1] * matrix->m[7] - matrix->m[3] * matrix->m[5];
float a5 = matrix->m[2] * matrix->m[7] - matrix->m[3] * matrix->m[6]; float b0 = matrix->m[8] * matrix->m[13] - matrix->m[9] * matrix->m[12];
float b1 = matrix->m[8] * matrix->m[14] - matrix->m[10] * matrix->m[12];
float b2 = matrix->m[8] * matrix->m[15] - matrix->m[11] * matrix->m[12];
float b3 = matrix->m[9] * matrix->m[14] - matrix->m[10] * matrix->m[13];
float b4 = matrix->m[9] * matrix->m[15] - matrix->m[11] * matrix->m[13];
float b5 = matrix->m[10] * matrix->m[15] - matrix->m[11] * matrix->m[14]; // Calculate the determinant.
float det = a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0; // Close to zero, can't invert.
if (fabs(det) < FLT_EPSILON) {
return false;
} float scalar = 1.0f / det; // Support the case where matrix == result result->m[0] = ( matrix->m[5] * b5 - matrix->m[6] * b4 + matrix->m[7] * b3) * scalar;
result->m[1] = (-matrix->m[1] * b5 + matrix->m[2] * b4 - matrix->m[3] * b3) * scalar;
result->m[2] = ( matrix->m[13] * a5 - matrix->m[14] * a4 + matrix->m[15] * a3) * scalar;
result->m[3] = (-matrix->m[9] * a5 + matrix->m[10] * a4 - matrix->m[11] * a3) * scalar; result->m[4] = (-matrix->m[4] * b5 + matrix->m[6] * b2 - matrix->m[7] * b1) * scalar;
result->m[5] = ( matrix->m[0] * b5 - matrix->m[2] * b2 + matrix->m[3] * b1) * scalar;
result->m[6] = (-matrix->m[12] * a5 + matrix->m[14] * a2 - matrix->m[15] * a1) * scalar;
result->m[7] = ( matrix->m[8] * a5 - matrix->m[10] * a2 + matrix->m[11] * a1) * scalar; result->m[8] = ( matrix->m[4] * b4 - matrix->m[5] * b2 + matrix->m[7] * b0) * scalar;
result->m[9] = (-matrix->m[0] * b4 + matrix->m[1] * b2 - matrix->m[3] * b0) * scalar;
result->m[10] = ( matrix->m[12] * a4 - matrix->m[13] * a2 + matrix->m[15] * a0) * scalar;
result->m[11] = (-matrix->m[8] * a4 + matrix->m[9] * a2 - matrix->m[11] * a0) * scalar; result->m[12] = (-matrix->m[4] * b3 + matrix->m[5] * b1 - matrix->m[6] * b0) * scalar;
result->m[13] = ( matrix->m[0] * b3 - matrix->m[1] * b1 + matrix->m[2] * b0) * scalar;
result->m[14] = (-matrix->m[12] * a3 + matrix->m[13] * a1 - matrix->m[14] * a0) * scalar;
result->m[15] = ( matrix->m[8] * a3 - matrix->m[9] * a1 + matrix->m[10] * a0) * scalar; return true;
}

是的有些矩阵是没有逆矩阵的,所以求逆矩阵的操作会失败。

世界坐标系的意义,就是坐标是相对于原点坐标系的。

局部坐标系的意义。就是坐标不是相对于原点坐标系。而是相对于某个详细的坐标系。

局部坐标系是能够通过上面的方法互相转换的。

那么怎样在局部坐标系之间互相转换呢?

我们无法把一个局部坐标系的坐标,一次就变化成还有一个局部坐标系上。

由于两个不同的局部坐标的坐标。都是相对于各自的坐标系。也就是參考系不同。

但。我们能够,把一个局部坐标系,转换到世界坐标系。以后再从世界坐标系转换到还有一个局部坐标系上。

坐标系转换的意义是什么?

假设我们可以恰当的选取坐标系。在进行坐标计算的时候,会简化非常多运算和思考的模型。

由于一个物体坐标的变化总是在父类坐标之内的,也就是相对于父类坐标系去变化。

这个父类坐标系。要么世界坐标系,要么就是某个详细的坐标系。

而我们这里讨论的坐标转换的模型是这种。

一个坐标终于呈如今屏幕上,我们假设改动了坐标的父坐标系,通过坐标系的转化。而保持这个坐标终于呈现的位置不变。

打一个例如

假设一个坐标(0, 0)在世界坐标系上,终于呈现出来的就是在(0, 0)点处。

我们如今把这个坐标,放到一个在(5, 5)处的坐标系内。这样这个坐标全部的数值都像相对于(5, 5)这个坐标系的。

那么,(0, 0)终于呈现的就是在(5, 5)处了,而不再原来的位置。

我们通过把这个坐标(0, 0)转换到(5, 5)的坐标系里,会得到新的坐标(-5, -5)这是相对于新坐标系的数值。

终于(-5, -5) 会呈如今(0, 0)的位置。

其实,在openGL绘制的时候。我们常常须要在各种不同的坐标系之间互相转换,可能是为了计算动画。可能是为了计算物理碰撞。

openGL 坐标系的互相转换的更多相关文章

  1. OpenGL坐标系之间的转换 http://blog.csdn.net/sac761/article/details/52179585

    1. OpenGL 渲染管线 OpenGL渲染管线分为两大部分,模型观测变换(ModelView Transformation)和投影变换(Projection Transformation).做个比 ...

  2. cocos2d-x 屏幕坐标系和OPenGL坐标系转换

    转自:http://home.cnblogs.com/group/topic/57609.html cocos2d坐标系(OPenGL坐标系):以左下角为原点,x向右,y向上 屏幕坐标系(androi ...

  3. [OpenGL]OpenGL坐标系和坐标变换

    OpenGL通过摄像机的模拟.要实现一个三维计算机图形重大转变,这是几何变换(模型转换-查看转型(两者统称为几何变换)).投影.作物转型.口变换等.同一时候,OpenGL还实现了矩阵堆栈等.理解掌握了 ...

  4. 各类坐标系相互之间的转换(84互转GC02,GC02互转BD09)

    在遥感行业我们经常会用到各类的坐标系相互之间的转换,常见的度分秒转化为度很简单,直接上代码: //经纬度 ////118度48分54.152秒=118+(48/60)+(54.152/3600)=11 ...

  5. 百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换(JS版代码)

    /** * Created by Wandergis on 2015/7/8. * 提供了百度坐标(BD09).国测局坐标(火星坐标,GCJ02).和WGS84坐标系之间的转换 */ //定义一些常量 ...

  6. ArcGIS中的坐标系定义与转换 (转载)

    原文:ArcGIS中的坐标系定义与转换 (转载) 1.基准面概念:  GIS中的坐标系定义由基准面和地图投影两组参数确定,而基准面的定义则由特定椭球体及其对应的转换参数确定,因此欲正确定义GIS系统坐 ...

  7. 百度地图和高德地图坐标系的互相转换 四种Sandcastle方法生成c#.net帮助类帮助文档 文档API生成神器SandCastle使用心得 ASP.NET Core

    百度地图和高德地图坐标系的互相转换   GPS.谷歌.百度.高德坐标相互转换 一.在进行地图开发过程中,我们一般能接触到以下三种类型的地图坐标系: 1.WGS-84原始坐标系,一般用国际GPS纪录仪记 ...

  8. GIS中的坐标系定义与转换

    GIS中的坐标系定义与转换 青岛海洋地质研究所 戴勤奋 2002-3-27 14:22:47 ----------------------------------------------------- ...

  9. OpenGL 坐标与矩阵转换

    1. OpenGL 渲染管线 OpenGL渲染管线分为两大部分,模型观测变换(ModelView Transformation)和投影变换(Projection Transformation).做个比 ...

随机推荐

  1. Extjs GridPanel 监听事件 行选中背景

    Extjs设置GridPanel选中行背景色和选中单元格背景色 var view = grid.getView(); view.getRow(index).style.backgroundColor  ...

  2. tomcat 的缓存机制

    事出做项目时一个jsp页面修改后一直没有读出来,后来仔细研究了下tomcat才发现 当请求jsp页面时,Tomcat会分派给JspServlet来处理,在jspServlet的方法 service() ...

  3. nginx: [error] invalid PID number “” in “/usr/local/var/run/nginx/nginx.pid”

    在Mac上用brew安装Nginx,然后修改Nginx配置文件,再重启时报出如下错误: nginx: [error] invalid PID number "" in " ...

  4. 怎么上传自己的代码/项目到自己的github仓库上

    创建新的git仓库   设置新仓库   创建完成后   复制git地址   现在已经有window git客户端了(地址:https://desktop.github.com/),但还是建议用git命 ...

  5. strstr实现

    // strstr.c查找完全匹配的子字符串 #include<stdio.h> #include<string.h> char *my_strstr(const char * ...

  6. 简明python教程 --C++程序员的视角(三):模块

    模块和包 1 python程序由包(package).模块(module)和函数组成.包是由一系列模块组成的集合.模块是处理某一类问题的函数和类的集合.函数是一段可以重复多次调用的代码. 2 pyth ...

  7. Top N之MapReduce程序加强版Enhanced MapReduce for Top N items

    In the last post we saw how to write a MapReduce program for finding the top-n items of a dataset. T ...

  8. python里的引用、浅拷贝、深拷贝

    参考: 1.http://wsfdl.com/python/2013/08/16/%E7%90%86%E8%A7%A3Python%E7%9A%84%E6%B7%B1%E6%8B%B7%E8%B4%9 ...

  9. 使用Jenkins和Jmeter搭建性能测试平台

    参考文档:http://blog.csdn.net/liuchunming033/article/details/52186157 jenkins的性能测试结果展现插件:https://wiki.je ...

  10. Windows 7系统垃圾清理自写程序

    系统清理.bat @echo off color 0a title windows7系统垃圾清理--- echo ★☆ ★☆ ★☆ ★☆ ★☆★☆★☆ ★☆ ★☆ ★☆ ★☆★ echo ★☆ ★☆ ...