Vertex distortion of large plane from certain angles

Viewed 64

I'm trying to write a shader that creates a grid on my ground plane which is rather large (vertex coordinates are around 1000.0 or more). It was working fine until I realized that from a certain view angle some of the vertices seem "shifted":

enter image description here

When breaking it down, it becomes clear that the shader itself is not the problem. The same thing happens when stripping the shader of almost everything and just showing the vertex coordinates as color:

#version 330 core
in vec3 VertexPos;

out vec4 outColor;

void main()
{
    outColor = vec4(VertexPos.xz, 0, 1);
}

enter image description here

The shift becomes worse when I move the camera closer and gets better when I move it further away (or disappears if I move it slightly left or right). Now the position and angle aren't arbitrary. The plane is simply made up of two triangles creating a quad. However for reasons it is not drawn with 4 vertices and 6 indices but instead with 6 vertices and 6 indices. So it is actually drawn like this (the gap isn't really there of course):

enter image description here

As you might have guessed the shift happens at the edge where the two triangles meet. It also seems to me that it only happens when this edge is perfectly horizontal in my final image. To avoid the problem I could scale down the plane quite a bit (which I don't want) or probably draw it with only four vertices (haven't tried it though). Nonetheless I'd really like to find the root of the problem. I suspect it has something to do with floating point precision when clipping the vertices that are outside the screen or something like that but I can't quite put my finger on it.

Any ideas?

EDIT: The vertex shader. Nothing special going on here really:

#version 330 core

layout(location = 0) in vec3 position;

layout(std140) uniform GlobalMatrices
{
    mat4 viewProjection;
    vec3 camPos;
    vec3 lightCol;
    vec3 lightPos;
    vec3 ambient;
};

uniform mat4 transform;

out vec3 VertexPos;

void main()
{
    vec4 transformed = transform * vec4(position, 1.0);
    VertexPos = transformed.xyz;
    gl_Position = viewProjection * transformed;
}

EDIT 2: According to renderdoc there's nothing wrong with my vertex attributes or coordinates either: enter image description here

0 Answers
Related