PDA

View Full Version : minus swap on sin fx matrix creates working code



technologist
11-29-2017, 06:14 PM
Hi,

I am making some custom matrices with some initial instruction on the Opengl thread. Its mostly turned into a math issue, so I've placed the question here; hope that's ok. My goal was to make the Euler rotational matrices "by hand" which took some additional internet research. In most of the three Euler angle matrices, the matrix only produces identical GLM results if the "-" in front of the sine fx is moved to the other sine fx in the matrix. Both methods and results are posted below. I've found it by empirical testing, not a formal proof. But its the only way it will work.


glm::mat4 rotx(float a) {
float s = std::sin(a);
float c = std::cos(a);
return glm::mat4(
1.0f,0.0f,0.0f,0.0f,
0.0f, c, -s, 0.0f,
0.0f, s, c, 0.0f,
0.0f,0.0f,0.0f,1.0f );
}
glm::mat4 roty(float a) {
float s = std::sin(a);
float c = std::cos(a);
return glm::mat4(
c, 0.0f, s, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
-s, 0.0f, c, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
};

glm::mat4 rotz(float a) {
float s = std::sin(a);
float c = std::cos(a);
return glm::mat4(
c,-s,0.0f,0.0f,
s,c,0.0f,0.0f,
0.0f,0.0f,1.0f,0.0f,
0.0f,0.0f,0.0f,1.0f );
};


GLM result:
Rotate vector custom fx on x axis:vec4(4.000000, -5.156932, 5.865666, 1.000000)
Rotate vector by GLM on x axis: vec4(4.000000, 6.699447, -4.014649, 1.000000)

Rotate vector custom fx on y axis:vec4(6.545196, 5.000000, -3.026618, 1.000000)
Rotate vector by GLM on y axis: vec4(-5.311184, 5.000000, 4.877635, 1.000000)

Rotate vector custom fx on z axis:vec4(-4.323152, 4.723384, 6.000000, 1.000000)
Rotate vector by GLM on y axis: vec4(5.557164, -3.180869, 6.000000, 1.000000)

If I swap minus sign positions, GLM compares nicely.


glm::mat4 rotx(float a) {
float s = std::sin(a);
float c = std::cos(a);
return glm::mat4(
1.0f,0.0f,0.0f,0.0f,
0.0f, c, s, 0.0f,
0.0f, -s, c, 0.0f,
0.0f,0.0f,0.0f,1.0f );
}
glm::mat4 roty(float a) {
float s = std::sin(a);
float c = std::cos(a);
return glm::mat4(
c, 0.0f, -s, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
s, 0.0f, c, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
};

glm::mat4 rotz(float a) {
float s = std::sin(a);
float c = std::cos(a);
return glm::mat4(
c,s,0.0f,0.0f,
-s,c,0.0f,0.0f,
0.0f,0.0f,1.0f,0.0f,
0.0f,0.0f,0.0f,1.0f );
};


Rotate vector custom fx on x axis:vec4(4.000000, 6.699447, -4.014649, 1.000000)
Rotate vector by GLM on x axis: vec4(4.000000, 6.699447, -4.014649, 1.000000)

Rotate vector custom fx on y axis:vec4(-5.311184, 5.000000, 4.877635, 1.000000)
Rotate vector by GLM on y axis: vec4(-5.311184, 5.000000, 4.877635, 1.000000)

Rotate vector custom fx on z axis:vec4(5.557164, -3.180869, 6.000000, 1.000000)
Rotate vector by GLM on y axis: vec4(5.557164, -3.180869, 6.000000, 1.000000)

GClements
11-29-2017, 07:37 PM
I am making some custom matrices with some initial instruction on the Opengl thread. Its mostly turned into a math issue, so I've placed the question here; hope that's ok. My goal was to make the Euler rotational matrices "by hand" which took some additional internet research. In most of the three Euler angle matrices, the matrix only produces identical GLM results if the "-" in front of the sine fx is moved to the other sine fx in the matrix.
Moved relative to what?

Bear in mind that GLM's matrix constructors take their arguments in column-major order, so the constructed matrix is transposed relative to how it appears in the code. The first four arguments form the first column of the constructed matrix, the next four the second column, and so on.

GLM's behaviour is consistent with GLSL and with historical OpenGL behaviour (glLoadMatrix() and glMultMatrix() require data in column-major order, glGetDoublev(GL_MODELVIEW_MATRIX) etc return data in column-major order. Newer versions provide some support for row-major order, but column-major order remains the default.

technologist
11-30-2017, 06:13 PM
Moved relative to what?

Bear in mind that GLM's matrix constructors take their arguments in column-major order, so the constructed matrix is transposed relative to how it appears in the code. The first four arguments form the first column of the constructed matrix, the next four the second column, and so on.

GLM's behaviour is consistent with GLSL and with historical OpenGL behaviour (glLoadMatrix() and glMultMatrix() require data in column-major order, glGetDoublev(GL_MODELVIEW_MATRIX) etc return data in column-major order. Newer versions provide some support for row-major order, but column-major order remains the default.

Yes, you're right. The other examples I encountered must have been row-major. The (-) sign was on the sine of the first row. Moving this to the second sin occurrence below it (my wimpy 'relative' term) w/GLM must have been due to the column-major attribute. I looked at 3 x 3 matrices as well and converted them...they were row-major as well.