1
votes

I'm rendering a big scene using opengl via lwjgl. When I set my projection matrix's zFar to a small number like 100, it clips my view frustum at that distance. But if I set it as 1000 or bigger, this setting is ignored and OpenGL just clips at about 500 units distance. This happens for all my shaders and the most interesting fact is that this clipping happens axis aligned, so it only depends on the distance from the camera along the axis, not diagonal distances.

What am I doing wrong? Is there a setting in OpenGL to limit the view at a certain point? THis happens on both, my Intel and my Nvidia card. I'm using "modern OpenGL" an am managing the matricies myself.

private void createProjectionMatrix()
{
    float aspectRatio = (float) Display.getWidth() / (float) Display.getHeight();
    float y_scale = (float) ((1f / Math.tan(Math.toRadians(FOV / 2f))) * aspectRatio);
    float x_scale = y_scale / aspectRatio;
    float frustum_length = FAR_PLANE - NEAR_PLANE;

    projectionMatrix = new Matrix4f();
    projectionMatrix.m00 = x_scale;
    projectionMatrix.m11 = y_scale;
    projectionMatrix.m22 = - ((FAR_PLANE + NEAR_PLANE) / frustum_length);
    projectionMatrix.m23 = -1;
    projectionMatrix.m32 = -((2 * NEAR_PLANE * FAR_PLANE) / frustum_length);
    projectionMatrix.m33 = 0;
}

Is there any way a view Matrix can cap the view frustrum?

EDIT: Vertex Shader:

#version 400 core

uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 transformationMatrix;

uniform vec3 lightPosition[4];

uniform float fogDensity;
uniform float fogGradient;

uniform vec4 plane;

uniform mat4 lightSpaceMatrix;

in vec3 position;
in vec2 textureCoords;
in vec3 normal;

out vec2 pass_textureCoords;
out vec3 surfaceNormal;
out vec3 toLightVector[4];
out vec3 toCameraVector;
out float visibility;
out vec4 FragPosLightSpace;


void main(void)
{
    vec4 worldPosition = transformationMatrix * vec4(position, 1.0);

    gl_ClipDistance[0] = dot(worldPosition, plane);

    vec4 positionRelativeToCam = viewMatrix * worldPosition;
    gl_Position = projectionMatrix * viewMatrix * worldPosition;

    pass_textureCoords = textureCoords;

    surfaceNormal = (transformationMatrix * vec4(normal, 0.0)).xyz;
    for(int i = 0;i < 4;i++)
    {
        toLightVector[i] = lightPosition[i] - worldPosition.xyz;
    }
    toCameraVector = (inverse(viewMatrix) * vec4(0.0, 0.0, 0.0, 1.0)).xyz - worldPosition.xyz;

    float distanceToCam = length(positionRelativeToCam.xyz);
    visibility = exp(-pow((distanceToCam * fogDensity), fogGradient));
    visibility = clamp(visibility, 0.0, 1.0);

    FragPosLightSpace = lightSpaceMatrix * worldPosition;
}

Commenting out gl_ClipDistance[0] doesn't change anything.

Here's an image, this is not the end of the world, this is the terrain being clipped along the axes, if you move you see more. Also you can see how the terrain under the water is clipped. Models, water or particles, that use different shaders are clipped, too.

image here

1
How small is your zNear?HolyBlackCat
0.1f cause smaller one's are not rendered on AMD cards. I'll try bigger numbers.Geosearchef
Still happening with a zNear of 50.Geosearchef
Projection matrix looks ok. What does the vertex shader look like? Posting a screenshot would help too.Robinson

1 Answers

1
votes

Thanks for everyone trying to help, i realized my fault. The world isn't getting clipped, the skybox is to small so it renders over the world.