In my program I'm loading a cube from a GLTF file and display it using my own implementation of math objects like vectors, matrices, and quaternions.
That's what my program draws when I try to rotate the cube:
The X rotation:
and the Y rotation:
as you can see the Y rotation looks like if my cube was flat.
I tried to find the culprit but for now it eludes me. But I'm 100% sure that:
- it is not the GLTF loader fault as my tests shows me the data for the cube is loaded right
- it is not my math module fault as my tests again tell me the calculations are done right
- it only happens when I try to rotate around Y and Z axis. The X axis rotation somehow doesn't have got this problem
The only thing that I'm not sure are shaders. But my shader code is the simplest possible:
#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
uniform mat4 projection;
uniform mat4 view;
out vec3 fragNormal;
out float distance;
void main()
{
gl_Position = projection * view * vec4(position, 1.0);
vec4 tmp = view * vec4(position, 1.0);
distance = tmp.z + 10.0;
fragNormal = normal;
}
#version 330 core
in vec3 fragNormal;
in float distance;
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0f, 1.0f, 1.0f, 1.0f) * distance;
}
I really don't know where else could I look. Also, I wish to give you my code for the rest of my program, but at this point it would be plain impossible to put it in this post as my program has many lines of code.
So, how this error might happen?
EDIT:
My projection matrix is standard perspective with fov=45°, near=0.1, far=100.0 :
|1.29996 0 0 0 |
|0 2.41421 0 0 |
|0 0 -1.002 -0.2002 |
|0 0 -1 0 |
The view matrix changes: it is a translation matrix (x=0,y=0,z=-10) multiplied (from the right) with a rotation matrix (in the below listing around Y axis by some degree)
| 0.5 0 0 0 |
| 0 1 0 0 |
| -0.866026 0 0.5 -10 |
| 0 0 0 1 |
Rotation is taken from a quaternion, but it can also be calculated from a rotation matrix directly. The error happens no mater how I calculate the rotation matrix.