PDA

View Full Version : decomposing modelview matrix

debonair
11-06-2015, 09:49 AM
I am trying to get the sign of the scale for each axis. but it works only when all 3 axes are of same sign. here is the code I am using:

glm::vec3 Xaxis(matrix[0][0],matrix[0][1],matrix[0][2]);
glm::vec3 Yaxis(matrix[1][0],matrix[1][1],matrix[1][2]);
glm::vec3 Zaxis(matrix[2][0],matrix[2][1],matrix[2][2]);

double zs=glm::dot(glm::cross(Xaxis,Yaxis),Zaxis);
double ys=glm::dot(glm::cross(Zaxis,Xaxis),Yaxis);
double xs=glm::dot(glm::cross(Yaxis,Zaxis),Xaxis);

xs=std::signbit(xs);
ys=std::signbit(ys);
zs=std::signbit(zs);

which fails for this example:

float []mat={0.032254f, 0.000000f, 0.000000f, 0.000000f, 0.000000f, -0.0052254f, 0.000000f, 0.000000f, 0.000000f, 0.000000f, 0.4332254f, 0.000000f, 0.000000f, 0.000000f, 0.000000f, 1.000000f};

GClements
11-06-2015, 09:40 PM
I am trying to get the sign of the scale for each axis. but it works only when all 3 axes are of same sign.
A matrix is either right-handed (positive determinant) or left-handed (negative determinant). Rotations don't change the handedness, a scale will change the handedness if and only if an odd number of scale factors are negative.

You can't distinguish between e.g.

glScalef(-1, 1, 1);
glScalef(1, -1, 1);

and

glRotatef(180, 0, 0, 1)

because both produce exactly the same matrix (which is right-handed).