-3

I draw a 3D shape with both fill mode and lines mode "wire frame", I get intersection between triangles and lines.

I enable the depth buffer before I renderGL.Enable(EnableCap.DepthTest); , this is the code of drawing the model:

            GL.BindVertexArray(VAO);

            Vector4 color;
            int colorLoc = GL.GetUniformLocation(programID, "color");

            if (BorderThickness > 0)
            {
                color = new Vector4((float)BorderColor.R / 255, (float)BorderColor.G / 255, (float)BorderColor.B / 255, (float)BorderColor.A / 255);
                GL.Uniform4(colorLoc, color.X, color.Y, color.Z, color.W);
                GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
                GL.LineWidth(BorderThickness);
                GL.DrawArrays(PrimitiveType.LineStrip, 0, _positions.Count);
            }

            color = new Vector4((float)FillColor.R / 255, (float)FillColor.G / 255, (float)FillColor.B / 255, (float)FillColor.A / 255);
            GL.Uniform4(colorLoc, color.X, color.Y, color.Z, color.W);
            GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
            GL.DrawArrays(PrimitiveType.Triangles, 0, _positions.Count);

            GL.BindVertexArray(0);

I got the following result:

enter image description here

The result after adding glPolygonOffset enter image description here

It fix the issue but the lines are not clear.

Community
  • 1
  • 1
Mohamed Moussa
  • 103
  • 1
  • 13
  • 5
    My crystal ball says you don't have enough Z-buffer precision. How do you construct your perspective matrix? – HolyBlackCat Jan 07 '19 at 13:23
  • This is my projection matrix code `projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)width / (float)height, 0.1f, 2000f);` – Mohamed Moussa Jan 07 '19 at 13:43
  • @Rabbid76 which distance? – Mohamed Moussa Jan 07 '19 at 13:44
  • @MohamedMoussa: The distance between farplane (2000f) and nearplane (0.1f) determines how much precision you have. Try to reduce the farplane to a smaller value. – BDL Jan 07 '19 at 13:45
  • @BDL I made it 1100 which is the closest number to the furthest face in the model, the problem is still exist – Mohamed Moussa Jan 07 '19 at 13:51
  • And try to put the close plane to a bigger value, see if that helps. – florent teppe Jan 07 '19 at 13:55
  • 2
    You can also play around with [glPolygonOffset](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glPolygonOffset.xhtml). Try to offset the lines a bit towards the camera such that they have a depth-difference to the model. – BDL Jan 07 '19 at 13:56
  • @BDL I added the following code `GL.Enable(EnableCap.PolygonOffsetLine); GL.PolygonOffset(-0.5f, 1.0f); GL.Disable(EnableCap.PolygonOffsetLine);` I got the result attached – Mohamed Moussa Jan 07 '19 at 14:35
  • 1
    @BDL I believe it's not the distance, but `far/near` or something similar. – HolyBlackCat Jan 07 '19 at 14:52
  • 1
    @HolyBlackCat: I guess it's not so easy since the z-buffer is usually not linear for perspective projections. But (assuming linear z-buffer), the distance between near and far has to be stored in 2^depth_bits different values. The larger the distance gets, the larger is the depth-range every single bit-combination has to represent. The problems arise when to actually different depth-values are mapped to the same depth-buffer value. – BDL Jan 07 '19 at 14:58
  • 1
    @MohamedMoussa much better is to move znear as far as you can ... moving zfar has not as big impact ... also check how much bits you allocate for depth buffer if it is 16 then you got only 2^16 Z values possible. In ideal conditions and with linear distribution of precision the depth step would be `~(2000-0.1)/65535= 0.03` but if you account for the logarithmic distirbution then the error will grow very high very soon (error is based on `zfar/znear`) after the Znear ... try `znear=2.0, zfar=2000` if it changes something (might be needed to move the camera a bit) also try 24bit depth buffer. – Spektre Jan 07 '19 at 20:56
  • @MohamedMoussa if you are using a lot of the dynamic range of the depth (not just the near zone) see [Linear Depth buffer](https://stackoverflow.com/a/42515399/2521214) for such scenes is linear depth buffer much much better – Spektre Jan 08 '19 at 07:21
  • @Spektre Your comment solved my problem, Thank you – Mohamed Moussa Jan 08 '19 at 08:52

1 Answers1

1

If the vertex that is far away is at a distance of 1100, then it is unlikely that you need a close plane at 0.1f precision.

If we look at the model, all the points seem to be in a similar scale. This kind of issue looks a lot like a Z-fighting issue.

Try

projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)width / (float)height, 10f, 2000f)

You will be significantly more precise at the scale you are working. You'll get clipping if your points are less then 10 away from the camera, but considering how big the mesh is, it shouldn't be an issue.

Since Z-buffer's precision is logarithmic, the close you are to the far plane, the lower your precision will be and the more Z-fighting you'll get. Considering your points are very close to the far plane, this would explain the issue we are seeing. By bringing the close plane to an order of magnitude a bit closer to the one of your vertices the issues should disappear.

florent teppe
  • 529
  • 5
  • 13