Jump to content
  • Advertisement
Sign in to follow this  
MARS_999

OpenGL How to do a transformation from camera space to world space?

This topic is 4352 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to do a transformation from camera space to world space. I am using OpenGL and have looked around the net, and through all my fancy game programming books and come up with at best I need to use the inverse of the modelview matrix? I am thinking this is wrong.... Any help would be greatly appreciated. Thanks

Share this post


Link to post
Share on other sites
Advertisement
In most applications the modelview matrix is really just the view matrix since all your objects are already in world-space. And you're correct, you just take the inverse of this matrix to go from camera-space to world-space. As a matter of fact, the view matrix is typically the inverse of a simpler matrix which you can build directly from your camera's orientation and location. Just stick your camera's up/right/forward/translation vectors in a matrix and you've got yourself the transformation.

Share this post


Link to post
Share on other sites
Hmm I am not getting the result I was looking for and I think it has to do with my matrix library being wrong but would like a second opinion...

Code I use to get the camera space to world space matrix

Matrix4x4 inverseMatrix;
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
inverseMatrix.UploadMatrix(matrix);
inverseMatrix.Inverse();
plane = inverseMatrix.VectorMatrixMultiply(plane);




inline void Inverse(void)
{
float det = 0.0f;
float oodet = 0.0f;
Matrix4x4 I;

I.matrix[0] = det3x3(matrix[5], matrix[6], matrix[7], matrix[9], matrix[10], matrix[11], matrix[13], matrix[14], matrix[15]);
I.matrix[1] = -det3x3(matrix[1], matrix[2], matrix[3], matrix[9], matrix[10], matrix[11], matrix[13], matrix[14], matrix[15]);
I.matrix[2] = det3x3(matrix[1], matrix[2], matrix[3], matrix[5], matrix[6], matrix[7], matrix[13], matrix[14], matrix[15]);
I.matrix[3] = -det3x3(matrix[1], matrix[2], matrix[3], matrix[5], matrix[6], matrix[7], matrix[9], matrix[10], matrix[11]);

I.matrix[4] = -det3x3(matrix[4], matrix[6], matrix[7], matrix[8], matrix[10], matrix[11], matrix[12], matrix[14], matrix[15]);
I.matrix[5] = det3x3(matrix[0], matrix[2], matrix[3], matrix[8], matrix[10], matrix[11], matrix[12], matrix[14], matrix[15]);
I.matrix[6] = -det3x3(matrix[0], matrix[2], matrix[3], matrix[4], matrix[6], matrix[7], matrix[12], matrix[14], matrix[15]);
I.matrix[7] = det3x3(matrix[0], matrix[2], matrix[3], matrix[4], matrix[6], matrix[7], matrix[8], matrix[10], matrix[11]);

I.matrix[8] = det3x3(matrix[4], matrix[5], matrix[7], matrix[8], matrix[9], matrix[11], matrix[12], matrix[13], matrix[15]);
I.matrix[9] = -det3x3(matrix[0], matrix[1], matrix[3], matrix[8], matrix[9], matrix[11], matrix[12], matrix[13], matrix[15]);
I.matrix[10] = det3x3(matrix[0], matrix[1], matrix[3], matrix[4], matrix[5], matrix[7], matrix[12], matrix[13], matrix[15]);
I.matrix[11] = -det3x3(matrix[0], matrix[1], matrix[3], matrix[4], matrix[5], matrix[7], matrix[8], matrix[9], matrix[11]);

I.matrix[12] = -det3x3(matrix[4], matrix[5], matrix[6], matrix[8], matrix[9], matrix[10], matrix[12], matrix[13], matrix[14]);
I.matrix[13] = det3x3(matrix[0], matrix[1], matrix[2], matrix[8], matrix[9], matrix[10], matrix[12], matrix[13], matrix[14]);
I.matrix[14] = -det3x3(matrix[0], matrix[1], matrix[2], matrix[4], matrix[5], matrix[6], matrix[12], matrix[13], matrix[14]);
I.matrix[15] = det3x3(matrix[0], matrix[1], matrix[2], matrix[4], matrix[5], matrix[6], matrix[8], matrix[9], matrix[10]);

det = (matrix[0] * I.matrix[0]) + (matrix[4] * I.matrix[1]) + (matrix[8] * I.matrix[2]) + (matrix[12] * I.matrix[3]);

if(det)
oodet = 1.0f / det;

for(int i = 0; i < 16; i++)
matrix = I.matrix * oodet;
}

Vector4D Matrix4x4::VectorMatrixMultiply(Vector4D v)
{
Vector4D out;

out.x = (v.x * matrix[0]) + (v.y * matrix[4]) + (v.z * matrix[8]) + matrix[12];
out.y = (v.x * matrix[1]) + (v.y * matrix[5]) + (v.z * matrix[9]) + matrix[13];
out.z = (v.x * matrix[2]) + (v.y * matrix[6]) + (v.z * matrix[10]) + matrix[14];

return out;
}



I am trying to do a clip plane based on Eric L paper, but haven't been able to get it to work as of yet....

Share this post


Link to post
Share on other sites
By world space, I assume you mean model space, as there is no separate world space in OpenGL.

And, yes, if you have a value in eye space (i e, before perspective transform and division) then you can multiply by the inverse of the modelview, and you end up back in model space.

If you have a post-transform value, then what you want is gluUnproject() (which basically multiplies by the inverse of both the projection and modelview matrices, in addition to un-projecting from the viewport)

Share this post


Link to post
Share on other sites
Quote:
Original post by hplus0603
By world space, I assume you mean model space, as there is no separate world space in OpenGL.

And, yes, if you have a value in eye space (i e, before perspective transform and division) then you can multiply by the inverse of the modelview, and you end up back in model space.

If you have a post-transform value, then what you want is gluUnproject() (which basically multiplies by the inverse of both the projection and modelview matrices, in addition to un-projecting from the viewport)


Hmm not sure, we are on the same page, but here is what Eric sent me and what I am trying to accomplish.

It looks like the problem is that your clip plane isn't sent to ModifyProjectionMatrix() in camera-space coordinates. Calculate the camera-space plane as C' = C*M, where M is the 4x4 transformation from camera space to world space

Share this post


Link to post
Share on other sites
Don't know for sure what the problem is, but your plane transform looks a little suspect.

First of all, your VectorMatrixMultiply() takes (ostensibly) a 4D vector as an argument, but then assumes it has a w component of 1. In effect this is really a confusingly named point transform function.

It appears you're representing your plane using a 4D vector, but it's not clear from your example if or how you're using the w component. Is it the d of the plane equation? Or perhaps -d? Or perhaps 0, with 'plane' only representing the normal?

If it's -d, remember that to transform a plane by a matrix M, you transform a 4D vector representing it by (MT)-1, not M itself. Although I'm not positive, this may be why you're not getting the results you expect.

If you're still having trouble with this, perhaps you could show how 'plane' is computed, and what the w component represents.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!