PDA

View Full Version : Matrix & Rotation Headache



dabeav
02-24-2011, 04:16 PM
I have created a "Node" based system of rendering my objects. Each parent node pushes its translation / rotation matrix, renders itself, and then calls each sub node to do the same, after a leaf and all its children has been rendered it pops its matrix off the static, and then we continue on. No problems here, and everything renders as expected. Also I am not having a stack overflow or anything like that, only about 4 push/pops deep so far.

Since all my scene objects start from the origin I have created a collision detection routine that simply grabs the view matrix for each level of the node tree using glGetFloatv(GL_MODELVIEW_MATRIX, modelView) and then I transform the point (0,0,0) using the matrix to determine where the object center is in view space. This is where I seem to be having a problem.

If I have NO object rotations I get the proper world space coordinates, and everything works fine. However, as soon as I add rotations to an object (simply using glRotatef() I start getting odd results when I transform my origin point. Most of the time it returns the exact position I would get if there were no rotations involved, but if I add more rotations further up the tree I start getting a point that falls on a linear line roughly at 45 degrees to the origin.

Yet like I said, if I render an object with the same matrix it gets placed correctly..... Now I am not a matrix master, however as far as I understand it for any given object it has only 1 view matrix. There may be many view matrices on the stack, but they are all multiplied together to form the "current" view matrix. Thus only transforming my origin by the "current" view matrix should position my point where the object will be rendered.... Is that correct? Any ideas on what I am doing wrong?

dabeav
02-25-2011, 11:47 AM
Maybe someone would just be willing to double check my transformations real quick? I am not sure if I am using the correct transformations at the correct time.

This should take a locally defined point (xyz) and place it into world space correct?
glGetFloatv(GL_MODELVIEW_MATRIX, modelView);
WorldX=x*modelView[0]+y*modelView[4]+z*modelView[8] +modelView[12];
WorldY=x*modelView[1]+y*modelView[5]+z*modelView[9] +modelView[13];
WorldZ=x*modelView[2]+y*modelView[6]+z*modelView[10]+modelView[14];

bcthund
02-25-2011, 12:14 PM
Column major matrix muliply function using:

typedef float Matrix44f[16];

Function:


void Matrix::Multiply(Matrix44f &product, const Matrix44f a, const Matrix44f b )
{
#define A(row,col) a[(col<<2)+row]
#define B(row,col) b[(col<<2)+row]
#define P(row,col) product[(col<<2)+row]

for (int i = 0; i < 4; i++) {
float ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3);
P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0);
P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1);
P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2);
P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3);
}
}

Alfonse Reinheart
02-25-2011, 01:41 PM
This should take a locally defined point (xyz) and place it into world space correct?

No, if your equation is correct, it would place it in view space. That's why it's called the modelview matrix.