Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert local object coordinates to world coordinates?

Tags:

c++

opengl

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?

like image 669
Shazad Maved Avatar asked May 06 '11 16:05

Shazad Maved


People also ask

How do you convert local coordinates to global coordinates?

gCoord = local2globalcoord( lclCoord ) converts the local rectangular coordinates lclCoord to the global rectangular coordinates gCoord . gCoord = local2globalcoord( lclCoord , option ) converts local coordinates to global coordinates using the coordinate transformation type option .

Which matrix is used to convert world coordinates to cameras coordinates?

We also use 4x4 matrices to transform cameras. Therefore, we can also convert points from world space to camera space. Computing the coordinates of a point from camera space onto the canvas, can be done using perspective projection.

What are world coordinates unity?

In particular, Unity offers two ways to see (and set) coordinates: World coordinates: These are absolute coordinates of where the game object is located (by absolute, I mean with respect to the world frame, which is considered to be absolute in the game)


1 Answers

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 OpenGL 2.1 reference pages.


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:

T  = [ 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 homogenous coordinates first; 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.

like image 80
Jay Conrod Avatar answered Oct 09 '22 19:10

Jay Conrod