Search code examples
c++openglmathcameraclip

OpenGL near plane is clipping too much


I'm having some issues with my camera, where the near plane seems to be too far even when I have it set to 0.1 or lower. It seems like there is already some offset value. So you can't really get close enough an arbitrary object in the scene.

Below is a visual appearance of the bug. enter image description here

The black triangle shown is the clipping. Currently I'm using a perspective matrix and here is the code for that.

Matrix4x4 Matrix4x4::Perspective(Float fov, Float aspectRatio, Float near, Float far)
{
    Matrix4x4 result(1.0f);

    Float q = 1.0f / tan(toRadians(0.5f * fov));
    Float a = q / aspectRatio;

    Float b = (near + far) / (near - far);
    Float c = (2.0f * near * far) / (near - far);

    result.elements[0 + 0 * 4] = a;
    result.elements[1 + 1 * 4] = q;
    result.elements[2 + 2 * 4] = b;
    result.elements[2 + 3 * 4] = -1.0f;
    result.elements[3 + 2 * 4] = c;

    return result;
}

I don't feel that the bug is from the maths class. This is because the maths code are mainly from another project that I've been working on. And works perfectly fine from there. I also don't suspect that its the way that I render it (forward renderer). I believe my pointers for that are good since I am able to move and rotate the camera via mouse and keyboard.

But! What I suspect is the buffers. The OpenGL buffers. But I'm not entirely sure.

I hope someone gives me some advice as to how I can hunt and tackle this bug down.

Thank you in advance.


Solution

  • "glEnable(GL_DEPTH_CLAMP)" Solved The Issue