object to world space

Started by
7 comments, last by hoLogramm 19 years, 6 months ago
I am wondering what would be the best way to translate a vertice in object space to absolute world space. For example, imagine the following code: glLoadIdentity(); glTranslatef( 5, 0, 0 ); glTranslatef( 5, 0, 0 ); glBegin( GL_POINTS ); glVertex3f( 0,0,0 ); glEnd(); The object space coordinate of the point would be at 0,0,0, but the absolute worldspace coordinate would be 10,0,0. I believe I can just retrieve the modelview matrix and multiply it with the vertex, but I am not sure how to carry out the operation, for example the vertex needs to have 4 coordinates, should I make the 4th be 1?
Advertisement
with a modelview transformation you can ignore anything that ends up in the 4th component, so you can either use a 3 component vector or make the 4th component 1.
Ok, though it may not seem strikingly obvious, my experience with matrix operations is somewhat limited (sans the use of helper functions like glTranslatef, glRotatef, etc. )

However, I have done a little research, and I am wondering if the following would do what I want. Lets let x,y,z represent the values of a coordinate in object space which I want to translate in the following:

float m[16] = glGetFloatv( GL_MODELVIEW );
world_x = x*m[0] + y*m[4] + z*m[8] + 1*m[12];
world_y = x*m[1] + y*m[5] + z*m[9] + 1*m[13];
world_z = x*m[2] + y*m[6] + z*m[10]+ 1*m[14];

In other words, does this code effectively multiply the modelview matrix with my vertex ( I realize I am not computing the 4th component of the resultant vertex, but it will not be used )
that'll do it
That is correct.

But I personally would manage the Modelview Transformations only for learning purposes to get more aquainted with Matrices, etc.
But when working with huge datasets performance is very important and so i would recommend to stick to the glTranslate*, glRotate* and glScale* Functions. They might seem to create overhead but they are much more efficient than a single call to glMultMatrix*.
Quote:Original post by hoLogramm
...glTranslate*, glRotate* and glScale* Functions. They might seem to create overhead but they are much more efficient than a single call to glMultMatrix*.


Really? Can you tell why?
I have never benchmarked these methods, by I would imagine multmatrix to the fastest, by far!
| Stein Nygård - http://steinware.dk |
Ok I have experienced a problem. At the beginning of each scene, I call the function gluLookAt and so when I multiply my vertex by the modelview matrix I get a position that doesn't take into account by view transformation....

Would this be solved by using my code from before and then furthur multiplying that by the matrix used from my gluLookAt function?
Quote:Original post by hoLogramm
They might seem to create overhead but they are much more efficient than a single call to glMultMatrix*.

Wrong. Read here.
You should never let your fears become the boundaries of your dreams.
@all readers. follow link from above post

glTranslate and glRotate each are twice as fast than a call to glMultMatrix.
However glRotate is slower and need nearly twice as much time than glMultMatrix.

This topic is closed to new replies.

Advertisement