Jump to content
  • Advertisement
Sign in to follow this  
xDan

OpenGL positioning camera + finding inverse matrix

This topic is 4833 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

Hello :) I have the opengl matrix used to put an object in a 3d world, now I would like to position the camera at this location. I *think* I need to calculate the inverse of this matrix and multiply this before drawing my objects. So my questions are: is this correct, and how do I calculate the inverse? Thankyou!

Share this post


Link to post
Share on other sites
Advertisement
Yes, if you have an object matrix, you can find the corresponding view matrix by inversion. Assuming your object matrix contains only a rotation followed by a translation, inverting is pretty easy. Using block notation, the original matrix is:

[R | T]
-------
[0 | 1]

The inverse is then:
[RT | RT*-T]
-----------
[0 | 1 ]
Let me know if you need any further info.

Share this post


Link to post
Share on other sites
I would consider writing generic routine for inverting matrices, it's quite easy using gauss elimination

Share this post


Link to post
Share on other sites
Quote:
I would consider writing generic routine for inverting matrices, it's quite easy using gauss elimination
True, but for practically any combination of scaling, rotation, and translation, the special-case solution is less expensive and can be easier to code.

Share this post


Link to post
Share on other sites
i provided inversion methods for both projection and modelview matrices in my matrix class



but one think i wonder about do opengl matrices always use post multiplications

e.g.:

modelviewmatrix*localorigin = worldorigin
proj * modelviewmatrix*localorigin = perspective coordiante ....?


Share this post


Link to post
Share on other sites
In my opinion is better to use a matrix projection/view of your own (then load them into GL).

You can also use the matreix inversion code you find in the mesa 3d source code

In particular you can take a look at src/glu/sgi/libutil/project.c


/// NOTE: code taken from src/glu/sgi/libutil/project.c (MESA 3D pub src code)
static int __gluInvertMatrixd(const GLdouble src[16], GLdouble inverse[16])
{
int i, j, k, swap;
double t;
GLdouble temp[4][4];

for (i=0; i<4; i++) {
for (j=0; j<4; j++) {
temp[j] = src[i*4+j];
}
}
__gluMakeIdentityd(inverse);

for (i = 0; i < 4; i++) {
/*
** Look for largest element in column
*/

swap = i;
for (j = i + 1; j < 4; j++) {
if (fabs(temp[j]) > fabs(temp)) {
swap = j;
}
}

if (swap != i) {
/*
** Swap rows.
*/

for (k = 0; k < 4; k++) {
t = temp[k];
temp[k] = temp[swap][k];
temp[swap][k] = t;

t = inverse[i*4+k];
inverse[i*4+k] = inverse[swap*4+k];
inverse[swap*4+k] = t;
}
}

if (temp == 0) {
/*
** No non-zero pivot. The matrix is singular, which shouldn't
** happen. This means the user gave us a bad matrix.
*/

return GL_FALSE;
}

t = temp;
for (k = 0; k < 4; k++) {
temp[k] /= t;
inverse[i*4+k] /= t;
}
for (j = 0; j < 4; j++) {
if (j != i) {
t = temp[j];
for (k = 0; k < 4; k++) {
temp[j][k] -= temp[k]*t;
inverse[j*4+k] -= inverse[i*4+k]*t;
}
}
}
}
return GL_TRUE;
}




where


static void __gluMakeIdentityd(GLdouble m[16])
{
m[0+4*0] = 1; m[0+4*1] = 0; m[0+4*2] = 0; m[0+4*3] = 0;
m[1+4*0] = 0; m[1+4*1] = 1; m[1+4*2] = 0; m[1+4*3] = 0;
m[2+4*0] = 0; m[2+4*1] = 0; m[2+4*2] = 1; m[2+4*3] = 0;
m[3+4*0] = 0; m[3+4*1] = 0; m[3+4*2] = 0; m[3+4*3] = 1;
}


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi, a little off topic but I will try :)
For some time I'm trying to understand all this transformation stuff in OpenGL and have some unclear things in my mind:

What is "object matrix" exacly? Is it another name for "model matrix"?

Are this statements true:

1. glTranslateX, glRotateX, glScaleX creates our current model matrix
(when we are in MODELVIEW mode)
2. perspective matrix is another name for projection matrix
3. view matrix defines viewing transformation?
4. gluLookAt(...) creates custom view matrix and multiplies it by current matrix, but even if we do not call gluLookAt(...) it is already used/defined - but it is just Identity, yes?

Thank you
Andrew

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Hi, a little off topic but I will try :)
For some time I'm trying to understand all this transformation stuff in OpenGL and have some unclear things in my mind:

What is "object matrix" exacly? Is it another name for "model matrix"?

Are this statements true:

1. glTranslateX, glRotateX, glScaleX creates our current model matrix
(when we are in MODELVIEW mode)
2. perspective matrix is another name for projection matrix
3. view matrix defines viewing transformation?
4. gluLookAt(...) creates custom view matrix and multiplies it by current matrix, but even if we do not call gluLookAt(...) it is already used/defined - but it is just Identity, yes?

Thank you
Andrew


1) Translate, Rotate, Scale simply multiply the current matrix by a translation, rotation or scaling matrix; you specify the current matrix with glMatrixMode()
So you can combine different operations

2) In my opinion you should see perspective as a particular case of projection (you can use also Ortho that is another type of projection)
For example I use a projection object from which I derived a perspective and a ortho object. From these I can extract the same matrices used by GL or calculate the projection from the matrix (so I've not to deal with a particular API although I use the same tranposed notation)
If you are interested too take a look at mesa 3D source code (a real treasure)(I've implemented mine from GL specification [dead] )

3) MODELVIEW matrix define view/object transform.

The formula used by GL is very simple

Projected_point2D = Projection_matrix * Modelview_matrix * Space_point3D;

Before going to screen the point2D is first normalized (again see glu code if interested)

4) I dont know if GL uses by default an IDENTITY (I admit I dont remember...);
if so yes...it's like you look along zaxis (z<0)

Share this post


Link to post
Share on other sites
Thanks for all the replies!
I need some time to digest them. :D I'll post again if I need any more help.

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!