0
\$\begingroup\$

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

1

Position: x = 0, y = 0, z = 0
Rotation: x = 0, y = 45.0, z = 0

3

Position: x = 1.5, y = 0, z = 0
Rotation: x = 0, y = 0, z = 0

2

Position: x = 3.5, y = 0, z = 0
Rotation: x = 0, y = 0, z = 0

4

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.

\$\endgroup\$
2
  • 3
    \$\begingroup\$ You haven't identified what problem you want to solve. (If it's the third image "looking wrong", it might actually be right, but surprising with an empty scene. With a wide field of view, that's what happens.) \$\endgroup\$ Commented Jan 11, 2017 at 19:12
  • \$\begingroup\$ What is your field of view (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\$ Commented Jan 12, 2017 at 3:31

1 Answer 1

0
\$\begingroup\$

What you see is perspective distortion caused by wide Field Of View. With perspective camera, objects near the view edges get "stretched", compared with objects in view center. The more the FOV, the larger they "stretch".

You might want to reduce the FOV, or even try to switch to Orthographic camera, if you want objects to "stretch" less or keep their size/proportions constant.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .