物体坐标to世界坐标

源地址:http://sourceforge.net/p/openantz/wiki/Local_to_World_Coordinates/

真尼玛不容易,找了两天才有点眉目。。汗

Local to World Coordinates

This document is designed to explain how to determine the position of an object in World coordinates after it has undergone an arbitrary number of transformations, (glTranslatef, glRotatef and glScalef.)

这个文档被设计用来解释如何判断一个物体在进行各种变换后(平移,旋转,缩放)在世界坐标系中的位置。

该死的opengl没有提供从Local坐标系到world坐标系的转换机制,甚至它也没定义世界坐标系。它使用称为modelview的矩阵,这个矩阵是model矩阵和view矩阵的结合。

你可以通过model矩阵得到world坐标,(该矩阵包含the world translate poision 和scale rotation).这通过逆矩阵乘以MV矩阵得到。

OpenGL does not provide a mechanism for converting Local coordinates to World coordinates, (nor does it define any such coordinates). It uses something called the 'ModelView Matrix' (M*V), which is a combination of transformed local model coordinates known as a Model Matrix (M) and virtual camera position known as a View Matrix (V).

You can get the World coordinates by determining the Model Matrix, (which contains the World Translate position as well as scale and rotation.) This is achieved by multiplying the inverse camera view V-1 by MV:

V-1 * ( V * M ) == M

Then you have m12 = X , m13 = Y, and m14 = Z (in World Coordinates!)

ModelView

OpenGL Transformation Concepts:
http://www.songho.ca/opengl/gl_transform.html

A good discussion on this method and some other possible techniques can be found here:
http://www.gamedev.net/topic/530565-convert-local-to-world-coords-opengl/


Code Used in ANTz:

//load the identity matrix and position the virtual camera
glLoadIdentity();
gluLookAt ( camNode->translate.x, camNode->translate.y, camNode->translate.z,
camNode->translate.x + camNode->rotateVec.x,
camNode->translate.y + camNode->rotateVec.y,
camNode->translate.z + camNode->rotateVec.z,
upX, upY, upZ ); 

//now get the virtual Camera Matrix, V
glGetFloatv (GL_MODELVIEW_MATRIX, camData->matrix);

//then invert the matrix V, you can use any standard 4x4 matrix inversion method
npInvertMatrixf (camData->matrix, camData->inverseMatrix);
.....

//transformation code here, multiple glTranslatef(), glRotatef() and glScalef()
.....
//then get your local object ModelView Matrix
glGetFloatv (GL_MODELVIEW_MATRIX, modelView);

//ANTz uses a fast partial matrix multiplication which only calculates m12, m13 and m14
//however, you can use any standard 4x4 Matrix Multiplication routine
npLocalToWorld (&world, camData->inverseMatrix, modelView);

printf("%6.2f %6.2f %6.2fn", world.x, world.y, world.z );

*camData->matrix, camData->inverseMatrix and modelView are all simple 1D arrays of 16 floats, or a 4x4 matrix however you look at it.

转载于:https://www.cnblogs.com/Vulkan/archive/2012/07/09/7530289.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值