View Full Version : Getting the position of the matrix

Hylke Donker

11-16-2004, 01:11 AM

Hello

I'm currently working on a collison detection thing(i've just started).

But in order to check wheter i hit something or not, i need to know the position of the matrix.

Because after i rotate the matrix(using glRotatef) around the y axis, the x position of the matrix wil obviously change.

So is there are function that tells me where my matrix is?

Thanx Hylke

mikael_aronsson

11-16-2004, 05:02 AM

Hi !

You can get the matrix from opengl with glGetFloatv or glGetDoublev, but this is not a good idea because you might stall the opengl pipeline and it is not very fast.

You would be much better of to keep a local copy of matrix yourself, you might even do all the operations on your own matrix and then just use that for opengl with glMultMatrix instead of using glTranslate/glRotate/glScale.

Mikael

Hylke Donker

11-16-2004, 10:06 AM

Originally posted by mikael_aronsson:

You would be much better of to keep a local copy of matrix yourself,But what do i need that copy for?

you might even do all the operations on your own matrix and then just use that for opengl with glMultMatrix instead of using glTranslate/glRotate/glScale.

MikaelSorry, but i dont really understand what you mean.

Silkut

11-16-2004, 11:18 AM

to copy the current matrix, you can use glPopMatrix, glPushMatrix, isn't it ? it's just an idea...

I think Mikael try to explain that glRotate/glScale and glTranslate are particular operation matrix, to calculate the matrix transformations yourself you should use glMultMatrix 'coz opengl don't have other functions like glTranslate, you must calculate everything , is it something like that Mikael ? :D (hey !!! i'm not a glMath guru)

Hylke Donker

11-17-2004, 09:28 AM

Originally posted by Gollum:

to copy the current matrix, you can use glPopMatrix, glPushMatrix, isn't it ? it's just an idea...yeah, but why would i want to copy a matrix(im using glLoadIdentity to clear my matrix)?

Maybe a simple (source code) example, to help me out?

I think Mikael try to explain that glRotate/glScale and glTranslate are particular operation matrix, to calculate the matrix transformations yourself you should use glMultMatrix 'coz opengl don't have other functions like glTranslate, you must calculate everything , is it something like that Mikael ? :D (hey !!! i'm not a glMath guru)The problem is that i have no idea how glMultMatrix works(i dont understand how it works), and why is matrix 4 x 4(my opengl guide is so damn vagua about the matrices, or its just my lack of English)?

Can anyone please explain it to me

Aeluned

11-17-2004, 10:05 AM

glMultMatrix multiplies the supplied matrix (parameter) by the current matrix on the stack.

If, for example, you have a matrix that defines rotation, you can multmatrix that matrix by the current modelview matrix - then when you go to draw your vertices they will be subjected to the composite matrix created by multmatrix. If for example the top of the modelview stack had a matrix that defined translation and you multmatrix with a matrix that defines rotation your vertices would undergo the composite transformation.

the reason matrices are defined as 4x4 and your points have a forth element (w) has something to do with defining infinity. The Euclidean coordinate system doesn't have the notion of infinity, but in computer graphics it is useful to have such a notion and so we use a homogeneous coordinate system. I don't remember exactly the mathematics of why things are the way they are, I have just accepted it and moved on.

I hope my rambling made some sense

b.t.w. can you explain what you mean by 'position of the matrix'? do you mean the transformations applied to your coordinate system? like the vectors that define the x,y and z axes?

Hylke Donker

11-18-2004, 09:37 AM

Originally posted by Aeluned:

glMultMatrix multiplies the supplied matrix (parameter) by the current matrix on the stack.

If, for example, you have a matrix that defines rotation, you can multmatrix that matrix by the current modelview matrix - then when you go to draw your vertices they will be subjected to the composite matrix created by multmatrix. If for example the top of the modelview stack had a matrix that defined translation and you multmatrix with a matrix that defines rotation your vertices would undergo the composite transformation.

Can you please post a very small source code example?

the reason matrices are defined as 4x4 and your points have a forth element (w) has something to do with defining infinity. The Euclidean coordinate system doesn't have the notion of infinity, but in computer graphics it is useful to have such a notion and so we use a homogeneous coordinate system. I don't remember exactly the mathematics of why things are the way they are, I have just accepted it and moved on.

I hope my rambling made some sense

Ok, thanx

b.t.w. can you explain what you mean by 'position of the matrix'? do you mean the transformations applied to your coordinate system? like the vectors that define the x,y and z axes?I say position of the matrix, cus, if i understand my opengl book correctly(they should publish a dutch one :-p), when you use glTranslate*() you move the matrix from the viewpoint.So what i mean is, what's the distance from the viewpoint to the matrix?

Aeluned

11-18-2004, 09:57 AM

no, that's not what happens...

when you call glTranslatef it modifies the current modelview matrix.

A vertex is passed through a "viewing transformation pipeline". Along the way it is

multiplied by a modelview matrix which is responsible for subjecting the vertex to transformations

such as scaling, translations and rotations. After the vertex's x,y,z,w values are multiplied by the matrix the result is the vertex's new position.

the vertex is then multiplied by the

projection matrix - another matrix responsible for projecting the point into screen space

(this matrix basically places the vertex onto a pixel on your monitor).

glTranslates,Rotates and Scales do nothing more

than augment the modelview matrix so that your

vertex is transformed accordingly -- matrices

don't move - they don't even have a position.

you should read up on transformations and give

yourself a crash course on linear algebra if you

aren't familiar with it already. linear algebra

can get pretty scary but you only need the real

basics to understand matrix transforms and

vectors and stuff like that.

code sample for glMultMatrix() ?

GLdouble myMatrix[16];

say i then load the values of 'myMatrix' with some transformation or whatever else i want.

glMatrixMode(GL_MODELVIEW);

glPushMatrix(); //save matrix, im about to

change it

glMultMatrix(myMatrix); //multiply current

modelview with myMatrix which replaces the

matrix on top of the modelview matrix stack so

i'll get different transforms now

//draw some stuff using this new matrix

glPopMatrix(); //restore the old matrixCheers.

Hylke Donker

11-19-2004, 05:59 AM

Originally posted by Aeluned:

no, that's not what happens...

when you call glTranslatef it modifies the current modelview matrix.

A vertex is passed through a "viewing transformation pipeline". Along the way it is

multiplied by a modelview matrix which is responsible for subjecting the vertex to transformations

such as scaling, translations and rotations. After the vertex's x,y,z,w values are multiplied by the matrix the result is the vertex's new position.

the vertex is then multiplied by the

projection matrix - another matrix responsible for projecting the point into screen space

(this matrix basically places the vertex onto a pixel on your monitor).

glTranslates,Rotates and Scales do nothing more

than augment the modelview matrix so that your

vertex is transformed accordingly -- matrices

don't move - they don't even have a position.But how do i call it when i translate?

Cus its not Translating the Matrix right?

you should read up on transformations and give

yourself a crash course on linear algebra if you

aren't familiar with it already. linear algebra

can get pretty scary but you only need the real

basics to understand matrix transforms and

vectors and stuff like that.I've asked my science teacher, but he says vectors and things like that aren't discussed yet with 3th class of high school, and my English is to poor to fully understand all those mathematical terms(i don't even fully understand my redbook, and skip al the mathy things cus i just dont understand them)

So i gues i gotta wait a lil longer :'(

code sample for glMultMatrix() ?

GLdouble myMatrix[16];

say i then load the values of 'myMatrix' with some transformation or whatever else i want.

glMatrixMode(GL_MODELVIEW);

glPushMatrix(); //save matrix, im about to

change it

glMultMatrix(myMatrix); //multiply current

modelview with myMatrix which replaces the

matrix on top of the modelview matrix stack so

i'll get different transforms now

//draw some stuff using this new matrix

glPopMatrix(); //restore the old matrixCheers.But dont you need to give a value to myMatrix[i] ?

And if i want to rotate, what values do i need to store in myMatrix to rotate?

Thanx Hylke

Aeluned

11-19-2004, 06:22 AM

what values do i need to store in myMatrix to rotate

:) , depends on what kind of rotation you want.

this is going to be almost impossible to explain if you're not familiar with linear algebra (matrix multiplications, vectors, etc...) and the transformation pipeline.

you'll learn these things in time - but yes it seems like you need to wait to learn a bit more stuff...

if you want to talk to me in realtime you can find me on IM as 'Aeluned'. I'd be more than happy to try and answer questions you might have.

b.t.w. : yes, the matrix would have to be loaded with some values - that was pseudocode.

Hylke Donker

11-20-2004, 02:51 AM

Originally posted by Aeluned:

:) , depends on what kind of rotation you want.

this is going to be almost impossible to explain if you're not familiar with linear algebra (matrix multiplications, vectors, etc...) and the transformation pipeline.

you'll learn these things in time - but yes it seems like you need to wait to learn a bit more stuff...But isn't there a simple way to figure out how much the x and z are translated when i rotate(in example 5 degrees)?

if you want to talk to me in realtime you can find me on IM as 'Aeluned'. I'd be more than happy to try and answer questions you might have.Just added you :-)

b.t.w. : yes, the matrix would have to be loaded with some values - that was pseudocode.Ok :-)

Powered by vBulletin® Version 4.2.3 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.