View Full Version : Simple Opengl matrix question

{MSX}

19-11-2005, 06:57 PM

I'm back with a simple question :P

Ok, let's say that i'm currently in a certain matrix, obtained after dozens of glTranslate and glRotate.

Now i draw something at position x,y,z.

What's the best way to obtain x,y,z in world coordinate (so inverting the transformation)?

Maybe if you've some code too it will be appreciated :P

JSoftware

19-11-2005, 07:45 PM

couldn't you just take the inverted matrix?

{MSX}

19-11-2005, 10:26 PM

couldn't you just take the inverted matrix?

Umm yes.. :P How do i do that ? :D

technomage

19-11-2005, 10:58 PM

if you head over to www.delphi3d.net the DOT framework has a very good geometry unit which has matrix invert, it should give you some pointers.

Paulius

19-11-2005, 11:03 PM

glGetFloatv(GL_MODELVIEW_MATRIX, @Matrix[0]);

and invert

JSoftware

19-11-2005, 11:05 PM

uhh. getting the inverse of a matrix is very hard! i surfed for several weeks to find out how. here's my vector and matrix code: http://graesdal.dk/mark1.zip

i just made it simple if you would want to compile it just straight away. you can remove cpu1 and vectors units if you don't need them. the matinverse function should be pretty easy to just cut and paste out of there if you need it..

happy coding,

Jeppe :roll: [/url]

{MSX}

20-11-2005, 09:50 AM

Thanks guys!

I've came out with this code:

var SAVEDVECT:TVector;

procedure SavePosition;

var

ModM, B : TMatrix;

begin

glGetFloatv(GL_MODELVIEW_MATRIX, @ModM[0]);

B := MatrixInverse(ModM);

SAVEDVECT := getVector(0,0,0);

SAVEDVECT := VectorMatrixMult(B, SAVEDVECT);

end;

After this i draw something in position SAVEDVECT..

But it doesn't work..

I was wondering if it was about camera.. Becouse when i draw stuff i draw after the camera setup..

Maybe i should use not only the inverse of the modelview, but also the camera matrix and do some calculation..

Am i right? any hint on what calculation do i need ? :P

User137

20-11-2005, 10:50 AM

I didn't propably quite understand your question, but to get "camera" position in moved position can be simply looked from 4x4 GL_MODELVIEW_MATRIX obtained with glGet.

[#,#,#,posX]

[#,#,#,posY]

[#,#,#,posZ]

[#,#,#,#]

If you are rendering something in a world, first camera is moved to the wanted position, then if you call glTranslate or glRotate for single objects, remember glPushMatrix before to store the camera location and glPopMatrix after the object to restore previus position.

JSoftware

20-11-2005, 10:52 AM

how do you apply your projection matrix?

the common mistake is just to apply the projection matrix while in modelview mode. if you then query the modelview matrix then you get both that and the projection matrix.

if that's not the answer then i have no idea(probably a bug in the code). multiplying a transformed vector by the matrix's inverse would untransform it :!:

User137

20-11-2005, 10:55 AM

If you multiply projected matrix with its inverse, wouldn't you get the same as glLoadIdentity would give?

{MSX}

20-11-2005, 11:26 AM

the common mistake is just to apply the projection matrix while in modelview mode. if you then query the modelview matrix then you get both that and the projection matrix.

if that's not the answer then i have no idea(probably a bug in the code). multiplying a transformed vector by the matrix's inverse would untransform it :!:

Umm maybe this is the problem..

I usually set up the camera without thinking of matrices.. :P

glLoadIdentity;

glTranslatef(0,6,10);

gluLookAt(campos.x, campos.y, campos.z, camtarget.x,camtarget.y,camtarget.z,0,1,0);

then i start drawing stuff without switching matrix..

How should it be done instead?

Maybe i've some problem with basis :P

{MSX}

20-11-2005, 11:41 AM

If you multiply projected matrix with its inverse, wouldn't you get the same as glLoadIdentity would give?

That's not what i'm doing.. i'd like to transform a vector instead.

the question is: if i'm in the middle of some transformation (glTranslate, Rotate etc), and i draw something in position 0,0,0, what's the absolute position of that point?

Paulius

20-11-2005, 11:58 AM

How do you test that it doesn?¢_~t work?

{MSX}

20-11-2005, 12:42 PM

How do you test that it doesn?¢_~t work?

I call the saveposition procedure i pasted before, in the middle of my rendering, trying to "unproject" the 0,0,0 vector into SAVEDVECT.

Then i return to "camera" matrix (that i calculated before), and i try to draw something at the SAVEDVECT position.

pseudocode:

glLoadIdentity

applyCamera

for each object:

glPush

transform matrix

drawObject

if it's the special object, call saveposition

glPop

translate to SAVEDVECT

drawsomething

Now the thing i draw sometimes enters and exits the screen, but it's not in the position i want it to be..

Paulius

20-11-2005, 01:00 PM

load identity matrix before translating to SAVEDVECT,

{MSX}

21-11-2005, 07:02 AM

load identity matrix before translating to SAVEDVECT,

Umm.. But that's not what i want.. i'd like the position to be in the same coordinate frame of the rest of the objects..

Indeed, i need to spawn objects in that position, that should be treated as any other..

User137

21-11-2005, 08:56 AM

For saving, how about getting modelview-matrix with glGet and later recall it with glLoadidentity and glMultMatrix?

Or maybe there was a way to directly write into modelview-matrix...

Paulius

21-11-2005, 12:56 PM

...Or maybe there was a way to directly write into modelview-matrix...

You can with glLoadMatrixf.

Anyway if it?¢_~s only transformed origin you want MSX you might as well just take it from the last row of the modelview matrix.

{MSX}

21-11-2005, 07:08 PM

ok, after lots of blind tries, i got it working.. but i don't know how :P

here's the working code:

procedure SavePosition;

var

ModM, B : TMatrix;

begin

glGetFloatv(GL_MODELVIEW_MATRIX, @ModM[0]);

savedvect.x:=modm[12];

savedvect.y:=modm[13];

savedvect.z:=modm[14];

CAMMATRIX := MatrixInverse(CAMMATRIX);

SAVEDVECT := VectorMatrixMult(CAMMATRIX, SAVEDVECT);

end;

Where CAMMATRIX is the modelview matrix saved just after camera setup.

Now if i spawn an object at SAVEDVECT, it goes to the right position :P

Who knows how it works ? :mrgreen:

JSoftware

21-11-2005, 08:00 PM

glGetFloatv(GL_MODELVIEW_MATRIX, @ModM[0]);

you get the current modelview matrix

savedvect.x:=modm[12];

savedvect.y:=modm[13];

savedvect.z:=modm[14];

savedvect contains the translation of the current matrix

CAMMATRIX := MatrixInverse(CAMMATRIX);

you inverse cammatrix

SAVEDVECT := VectorMatrixMult(CAMMATRIX, SAVEDVECT);

you multiply translation with inverse cammatrix... if the current matrix is multiplied with the cammatrix in some way then savedvect would contain translation of current matrix in local space

heh i took five minutes trying to figure out how to explain that and i'm not really sure it's correct :P

Powered by vBulletin® Version 4.2.5 Copyright © 2020 vBulletin Solutions Inc. All rights reserved.