1

I have been following a Game Engine tutorial series curated by YouTuber 'thebennybox' and have ran into an issue. A cube mesh is supposed to be displayed on the screen and in this specific episode the goal is to fix streaching of the object because of unsquare aspect ratios. However when I have tried to implement the same code the cube dispersal entirely. I have gone over the material multiple times so eithe the issues is in other parts of the code or I have not yet spotted my mistake in replicating the code [from this video: https://youtu.be/cgaixZEaDCg].

Code Before Implementing the Matrix Perspective:

public void render() 
{
    shader.bind();
    shader.setUniform("transform", transform.getTransformation());
    mesh.draw();
}

And After:

public void render() 
{
    shader.bind();
    shader.setUniform("transform", transform.getProjectedTransformation());
    mesh.draw();
} 

This new transform procedure "getPerspectiveTransformation()" looks like this:

public Matrix4f getProjectedTransformation()
{
    Matrix4f transformationMatrix = getTransformation();
    Matrix4f projectionMatrix = new Matrix4f().initProjection(fov, width, height, zNear, zFar);

    return projectionMatrix.mul(transformationMatrix);
}

And subsequently calls 'initProjection' on the Matrix4f as well as the normal 'getTransformation' procedure which I know works fine alone:

public Matrix4f initProjection(float fov, float width, float height, float zNear, float zFar)
{
    float ar = width/height;
    float tanHalfFOV = (float)Math.tan(Math.toRadians(fov / 2));
    float zRange = zNear - zFar;

    m[0][0] = 1.0f / (tanHalfFOV * ar); m[0][1] = 0;                    m[0][2] = 0;    m[0][3] = 0;
    m[1][0] = 0;                        m[1][1] = 1.0f / tanHalfFOV;    m[1][2] = 0;    m[1][3] = 0;
    m[2][0] = 0;                        m[2][1] = 0;                    m[2][2] = (-zNear - zFar)/zRange;   m[2][3] = 2 * zFar * zNear / zRange;
    m[2][0] = 0;                        m[2][1] = 0;                    m[2][2] = 1;    m[2][3] = 0;

    return this;
}

I don't believe that the issue is in the order in which the two Matrices are multiplied and returned (third block of code), and I have not been able to deduce in which part(s) of the code the problem stems from.

If you have debugging advice or believe that the problem is in rudimentary pieces of code which I have not included then I will provide any of the other blocks.

EDIT: I am using OpenGL for rendering and JFrame for the window manager.

Triston
  • 23
  • 4
  • Shouldn't the last line of the matrix have first index "3" - eg, change `m[2][2] = 1;` to `m[3][2] = 1;` – racraman Jul 04 '19 at 01:00

1 Answers1

0

You have a typo when setting the final row of the matrix. You should be using index [3], but instead are re-using index [2] .

Your matrix should be :

m[0][0] = 1.0f / (tanHalfFOV * ar); m[0][1] = 0;                    m[0][2] = 0;    m[0][3] = 0;
m[1][0] = 0;                        m[1][1] = 1.0f / tanHalfFOV;    m[1][2] = 0;    m[1][3] = 0;
m[2][0] = 0;                        m[2][1] = 0;                    m[2][2] = (-zNear - zFar)/zRange;   m[2][3] = 2 * zFar * zNear / zRange;
m[3][0] = 0;                        m[3][1] = 0;                    m[3][2] = 1;    m[3][3] = 0;
racraman
  • 4,988
  • 1
  • 16
  • 16