felselva/mathc

pmatrix_rotation_axis functions incorrectly.

Ankush-p opened this issue · 1 comments

Whilst writing a simple OpenGL (3.3) program with a rotating cube, I noticed that the function pmatrix_rotation_axis() (and by extension matrix_rotation_axis()) seems to behave incorrectly - I found my cube stretching as the angle started to increase towards 180 deg (converted to radians using to_radians()), using a perspective projection. I attempted to fix the problem, and have included the code below - as well as the results with the original function and the results with the modified function.

Original pmatrix_rotation_axis() function:

image

My "Fixed" version of the pmatrix_rotation_axis() function:
image

The code to "fix" the function:

	float c = cos(angle);
	float s = sin(angle);
	float one_c = 1 - c;
	float x = a->x, y = a->y, z = a->z;
	float xx = x * x;
	float yy = y * y;
	float zz = z * z;
	float xy = x * y;
	float xz = x * z;
	float yz = y * z;
	float L = (xx + yy + zz); /* Length/Magnitude of vector */
	float sqrtL = sqrt(L);

	pmatrix_identity(result);

	result->m11 = (xx + (yy + zz) * c) / L;
	result->m12 = (xy * one_c - a->z * sqrtL * s) / L;
	result->m13 = (xz * one_c + a->y * sqrtL * s) / L;

	result->m21 = (xy * one_c + a->z * sqrtL * s) / L;
	result->m22 = (yy + (xx + zz) * c) / L;
	result->m23 = (yz * one_c - a->x * sqrtL * s) / L;

	result->m31 = (xz * one_c - a->y * sqrtL * s) / L;
	result->m32 = (yz * one_c + a->x * sqrtL * s) / L;
	result->m33 = (zz + (xx + yy) * c) / L;

@Ankush-p thank you very much! I'm adding your name in the CONTRIBUTORS.md.