Sign in to follow this  
BkoChan

local coordinated to world coordinates

Recommended Posts

Is there a way of translating local coordinates into world coordinates? Say I do the following... glTranslatef(0.0f, 0.0f, -5.0f); glTranslatef(0.0f, 0.0f, -10.0f); glTranslatef(0.0f, 0.0f, -5.0f); I know that when I draw an object that it is -20.0f into the screen, but obviously the coordinate system says 0.0f. Is there a way of using the current matrix to return the -20.0f that I need? Thanks in advance

Share this post


Link to post
Share on other sites
you can retrieve the current modelview matrix state with:

float mv_matrix[16]; //array to store the matrix in
glGetFloatv(GL_MODELVIEW_MATRIX , mv_matrix); // function to get the current matrix

if you want to get the current coordinate Z of the model, it's the 12th member in mv_matrix (your Z = mv_matrix[12])

about matrices: opengl matrix

Share this post


Link to post
Share on other sites
My advice is this:

OpenGL only maintains one matrix refered to the ModelView matrix. However, this is really a concatenation of two matrices - the View Matrix (the camera) and the Model Matrix (a model in the scene).

You should always in software track your own camera - for example, yaw,pitch and roll. At render time - use the attributes of your camera to construct the View Matrix.

Likewise for every object in your scene you should create a Model matrix and again at render time construct a Model Matrix.

Finally - the render loop can then concentate the two matrices as required by openGL

e.g.


RenderLoop:
glMatrixMode (GL_MODELVIEW)
camera -> Get View Matrix()


foreach model in scene
{
glPushMatrix()
float[16] m = aModel->GetModelMatrix()
glMultMatrixf (m)
glPopMatrix()
}

Share this post


Link to post
Share on other sites
Let me see if I understand then

glTranslatef( 0.0f, 0.0f, -1.0f );
glRotatef( 1.0f, 0.0f, 0.0f, 1.0f );
glTranslatef( 0.0f, 0.0f, -1.0f );
glRotatef( 1.0f, 0.0f, 0.0f, 1.0f );
glTranslatef( 0.0f, 0.0f, -1.0f );
// Draw object here

glGetFloatv(GL_MODELVIEW_MATRIX , modelview);
for( GLuint i = 0; i < 4; i++ ) {
std::cout << modelview[ i ] << " " << modelview[ i+4 ] << " " << modelview[ i+8 ] << " " << modelview[ i+12 ] << " " << std::endl;
}

Produces the following matrix....

0.999391 -0.0348995 0 0
0.0348995 0.999391 0 0
0 0 1 -3
0 0 0 1

So that -3 is the Z position, no matter how many translates and rotates I do?

Sorry for the newb style questions, I just want to make sure I understand

Share this post


Link to post
Share on other sites
I'm having a really tough time figuring what you want..

What makes you think that z will stay the same no matter how many times you translate it..? You're not asking too much, just try to be more clear about it..

If I got it right this time, you want to know how to make Z constant.?

Share this post


Link to post
Share on other sites
Sorry Ill try to be clearer. In this matrix

1 0 0 X
0 1 0 Y
0 0 1 Z
0 0 0 1

Are X,Y and Z the world coordinates of the current position no matter how many translates and rotates I do?

Share this post


Link to post
Share on other sites
With OpenGL its all relative. This is because opengl only keeps current state for one combined model+view matrix.

You will find life much much much simpler if you first take the time to do a bit of background reading - on Matrix Maths. Trust me ... computer graphics is hard stuff ... but it can be made simpler once you understand the basics.

Share this post


Link to post
Share on other sites
There are many single issues to understand before you can think in 3D space that way.

For example
glTranslatef( 0.0f, 0.0f, -1.0f );
glRotatef( 1.0f, 0.0f, 0.0f, 1.0f );
glTranslatef( 0.0f, 0.0f, -1.0f );
glRotatef( 1.0f, 0.0f, 0.0f, 1.0f );
glTranslatef( 0.0f, 0.0f, -1.0f );
shows the problem: Since you're rotating both times around the principle z axis it cannot effect the z co-ordinate at all. In 3D, rotation is done in a plane which means that for any rotation of a point a plane can be found so that the point remains in the plane although when being rotated. Often one says "rotation happens around an axis", which is correlated to the said plane since the axis is identical to the plane's normal. In your case the plane shows constant z. In matrix math, one can see that because the product (using column vectors)

[ cos(a) -sin(a) 0 ] [ x ]
[ sin(a) cos(a) 0 } * [ y ]
[ 0 0 1 } [ z ]
=>
z' := 0 * x + 0 * y + 1 * z
of a rotation matrix (around principal z axis) and a vector the resulting vector will always show the same z as its original, while x and y will change in general. Rotating around another axis will have other effects, of course, but you can see that knowing the background makes analysing the problem an easy job.

So, as BionicBytes has already suggested, you have to make sure you've understood the basics before starting to compose transformations:
* column vectors, row vectors, and transposition
* matrix - vector product
* non-commutativity of matrix product (!)
* position vectors and direction vectors (!)
* affine and homogeneous co-ordinates
* (homogeneous) matrix representations of translation, rotation, scaling
* sense of projection, view, and model matrices

I have no clue which if these topics you already do know. For sure we will help you in learning them (but not solely teaching you; please look also into other sources).

Share this post


Link to post
Share on other sites
Quote:
Original post by BkoChan
Sorry Ill try to be clearer. In this matrix

1 0 0 X
0 1 0 Y
0 0 1 Z
0 0 0 1

Are X,Y and Z the world coordinates of the current position no matter how many translates and rotates I do?


The answer is: yes. As soon as you do a modelview translation, the modelview matrix will be updated.

Share this post


Link to post
Share on other sites
Quote:
Original post by BkoChan
Sorry Ill try to be clearer. In this matrix

1 0 0 X
0 1 0 Y
0 0 1 Z
0 0 0 1

Are X,Y and Z the world coordinates of the current position no matter how many translates and rotates I do?

First, in such a matrix, [X Y Z 1]T could be understood as the origin of the current co-ordinate frame. It is not a position of a vertex or something similar. (I write this for clarification due to the term "current position".)

Second, it seems me from the posts above that the matrix you're meaning here results from a glGetFloatv with the GL_MODELVIEW_MATRIX parameter. Then no; they don't denote global (world) co-ordinates. As was stated already earlier by BionicBytes, the MODELVIEW matrix in OpenGL is a composite of the VIEW matrix and the MODEL matrix. In other words, the matrix transforms from a local co-ordinate frame (namely that of the model) to the global co-ordinate frame and furthur to the view co-ordinate frame. Mathematically, the transformation looks like
pV' := V * M * p
where V denotes the VIEW portion and M the MODEL portion. But the global co-ordinates would result if only transformed by the MODEL matrix:
pG' := M * p

However, it plays no role how many particular transformations are done to compose V or M. Your conclusion is correct in this regard.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this