# 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.

## 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 on other sites
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 on other sites
I would consider writing generic routine for inverting matrices, it's quite easy using gauss elimination

##### 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 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 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 on other sites
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 on other sites
Quote:
 Original post by Anonymous PosterHi, 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 matrix3. 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 youAndrew

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 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 on other sites
gluLookat should update your projection matrix

view matrix should be proj*modelview matrix

1. 1
Rutin
47
2. 2
3. 3
4. 4
5. 5

• 10
• 27
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633405
• Total Posts
3011683
• ### Who's Online (See full list)

There are no registered users currently online

×