I'm trying to translate 3D object in my game engine. I copied all formulas from my old, working game engine, where translation was working.
When I set position and rotation to 0, everything looks to be fine, but when I try to change position of my 3D object, something strange happens. I'm not sure if it is projection matrix issue, or maybe transform matrix issue.
Position: x = 0, y = 0, z = 0
Rotation: x = 0, y = 0, z = 0
Position: x = 0, y = 0, z = 0
Rotation: x = 0, y = 45.0, z = 0
Position: x = 1.5, y = 0, z = 0
Rotation: x = 0, y = 0, z = 0
Position: x = 3.5, y = 0, z = 0
Rotation: x = 0, y = 0, z = 0
I checked all formulas once again, checked transformation matrix output, projection matrix output, and everything seems to be fine.
Here is my vertex shader:
#version 400
in vec3 position;
uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
void main(void)
{
gl_Position = projectionMatrix * viewMatrix * transformationMatrix * vec4(position,1.0);
}
and fragment shader:
#version 400
uniform vec2 textureVec;
layout(location = 0) out vec3 out_Diffuse;
layout(location = 1) out vec3 out_Texture;
void main(void)
{
out_Diffuse = vec3(0.5, 0.5, 0.5);
out_Texture = vec3(textureVec, 0.0);
}
Projection matrix formula:
float aspectRatio = static_cast<float>(windowSize.x) / static_cast<float>(windowSize.y);
float yScale = static_cast<float>((1.0f / tanf(MathUtil::degToRad(fov / 2.0f))));
float xScale = yScale / aspectRatio;
float frustumLen = farPlane - nearPlane;
projectionMatrix.m00 = xScale;
projectionMatrix.m11 = yScale;
projectionMatrix.m22 = -((farPlane + nearPlane) / frustumLen);
projectionMatrix.m23 = -1.0f;
projectionMatrix.m32 = -((2.0f * nearPlane * farPlane) / frustumLen);
projectionMatrix.m33 = 0.0f;
View matrix formula:
viewMatrix.setIdentity();
viewMatrix.rotate(Vector3(1.0f, 0.0f, 0.0f), MathUtil::degToRad(transformComponent->rotation.x));
viewMatrix.rotate(Vector3(0.0f, 1.0f, 0.0f), MathUtil::degToRad(transformComponent->rotation.y));
viewMatrix.scale(Vector3(1.0f, -1.0f, 1.0f));
Vector3 negativeCam = Vector3::negate(transformComponent->position);
viewMatrix.translate(negativeCam);
And transformation matrix formula:
transformationMatrix.setIdentity();
transformationMatrix.translate(position);
transformationMatrix.rotate(Vector3(1.0f, 0.0f, 0.0f), MathUtil::degToRad(rotation.x));
transformationMatrix.rotate(Vector3(0.0f, 1.0f, 0.0f), MathUtil::degToRad(rotation.y));
transformationMatrix.rotate(Vector3(0.0f, 0.0f, 1.0f), MathUtil::degToRad(rotation.z));
transformationMatrix.scale(scale);
Same happens for "y" and "z" position axis, but object "rotates" in different axis.
I also checked my VAO loader, vertices, uniform values loader, and everything else. If you need more code, let me know.
fov
) set to? It looks like you're doing the equivalent of using a really wide angle lens up close to an object, which would cause this exact kind of distortion. \$\endgroup\$