Vulkan

物体坐标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.

posted on 2012-07-09 16:09  Vulkan  阅读(567)  评论(0编辑  收藏  举报

导航