ConvertLEToBE为什么excel已停止工作作

c++ - How to convert local object coordinates to world coordinates? - Stack Overflow
Stack Overflow for Teams
A private, secure home for your team's questions and answers.
to customize your list.
This site uses cookies to deliver our services and to show you relevant ads and job listings.
By using our site, you acknowledge that you have read and understand our
Your use of Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.
I need a simple way to convert my objects coordinate into world coordinates, so that a can locate them in that coordinate system and do collision detection?
5,02074184
First, some background. In 3D graphics, you need to worry about several vector spaces:
Model space - these are usually the coordinates you specify to OpenGL
World space - coordinates are specified with respect to some central point in the world.
View space - coordinates are specified with respect to the camera
Projection space - everything on the screen fits in the interval [-1, +1] in each dimension.
Coordinates are specified homogeneously, so each vector has components (x, y, z, w), where w is a scaling factor. You can obtain coordinates in 3-space as (x/w, y/w, z/w). The scaling factor is needed for certain transformations like perspective projection that wouldn't be possible with non-homogenous coordinates.
4x4 matrices are needed to transform coordinates from one vector space to another. You could have three different matrices:
Model matrix (model to world)
View matrix (world to view)
Projection matrix (view to projection space)
You would project a coordinate C onto the screen using the formula:
C' = P * V * M * C
OpenGL internally maintains two matrices: modelview (model and view multiplied together), and projection. There is also a texture matrix we won't worry about. When you call glMatrixMode, you are switching between these matrices. You replace the current matrix with the identity matrix using glLoadIdentity. You apply transformations to the current matrix with functions like glTranslatef, glRotatef, or gluProjection. Each of these functions just creates a 4x4 matrix the implements that specific transformation, then multiplies the current matrix by it. You can see what the transformation matrices are in the .
Now for the actual answer. You need to maintain a 4x4 model matrix for each object in your scene. The model matrix will contain all the transformations needed to change model coordinates into world coordinates. For instance, every time you call glTranslate, you would update your model matrix:
= [ 1 0 0 x ]
[ 0 1 0 y ]
[ 0 0 1 z ]
[ 0 0 0 1 ]
M' = M * T
You can then use your model matrix to get coordinates into world space (make sure they are homogeno just set w = 1, if they aren't):
V' = V * M
Since you are maintaining these transformations in parallel, you don't actually need to maintain the OpenGL modelview matrix anymore. You can pass in your model matrix as a uniform to your shaders. You can maintain your own view and projection matrices in similar ways. This is required in recent versions of OpenGL; all of the matrix handling functions are deprecated.
18.6k1476104
You can use the product of the inverse of the virtual camera view matrix(V-1) times the ModelView Matrix (V*M) of the object to get World coordinates.
You get V just after the gluLookAt() function using:
glGetFloatv (GL_MODELVIEW_MATRIX, camViewMatrix)
then invert the matrix and store it, (used for multiply later in the code.)
You get V*M at the point where you are about to draw the object, just after all the glTranslatef(), glRotatef() and glScale() commands using the same GL function:
glGetFloatv (GL_MODELVIEW_MATRIX, objectsModelViewMatrix);
V-1 * ( V * M ) == M
Then multiple the two matrices, resulting matrix contains the position, m12 = x, m13 = y and m14 = z
more details with C code at:
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Post as a guest
Post as a guest
By clicking &Post Your Answer&, you acknowledge that you have read our updated ,
and , and that your continued use of the website is subject to these policies.
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabled}

我要回帖

更多关于 frostpunk已停止工作 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信