0

I write C# code to draw cubes surrounded by lines on the edges using orthogonal projection using OpenGL

Code used at form load

    private void glControl_window_Load(object sender, EventArgs e)
    {
        GL.Enable(EnableCap.DepthTest);
        GL.DepthFunc(DepthFunction.Less);
        GL.Enable(EnableCap.DepthClamp);
        GL.Enable(EnableCap.CullFace);
        GL.FrontFace(FrontFaceDirection.Ccw);
        GL.CullFace(CullFaceMode.Back);
        GL.Enable(EnableCap.Normalize);
     }

Code used for window paint

    private void glControl_window_Paint(object sender, PaintEventArgs e)
    {
      GL.Clear(ClearBufferMask.ColorBufferBit | 
      ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);

            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();

            float aspectRatio;
            if (Height == 0 || Width == 0) aspectRatio = 1f; 
             else aspectRatio = Width / (float)Height;
            double W = 20 * aspectRatio / CameraZoomScale
            , H = 20 / CameraZoomScale;
            GL.Ortho(-0.5 * W, 0.5 * W, -0.5 * H, 0.5 * H, -600.0f, 600.0f);


        GL.MatrixMode(MatrixMode.Modelview);

        Matrix4 mv = Matrix4.LookAt(0, 0, 15, 0, 0, 0, 0, 1, 0);
        // target coord 0,0,0 and eye coord 0,0,15

        //Drawlines
        GL.Enable(EnableCap.PointSmooth);
        GL.Enable(EnableCap.LineSmooth);
        GL.Enable(EnableCap.PolygonSmooth);
        GL.Hint(HintTarget.LineSmoothHint,HintMode.Nicest);
        GL.Enable(EnableCap.PolygonOffsetFill);
        GL.Begin(PrimitiveType.Lines);
        .
        .
        .
        GL.End();

        //Drawlines
        GL.Begin(PrimitiveType.Triangles);
        .
        .
        GL.End();
    }

The following image shows how depth test precision is lost. https://i.stack.imgur.com/NCVzj.jpg

when I replace

 GL.Ortho(-0.5 * W, 0.5 * W, -0.5 * H, 0.5 * H, -600.0f, 600.0f);

by the following

 GL.Ortho(-0.5 * W, 0.5 * W, -0.5 * H, 0.5 * H, -10.0f, 10.0f);

Then, Every thing is drawn nice. But I have cube that has Z_coordinates equals 600 and another with Z_coordinates equals 0 that need to use frustm 600

Is there another solution to draw all objects without loosing depth precision?

  • How many bits does your depth buffer have? Think of it like this: If you have 8 bits, then you can store 256 values. If the range is [-600, 600], then these 256 values span 1200 units in depth. To be distinguishable, two depth values have to be 1200/256 units away from each other, otherwise they produce the same depth values. (This is simplified since depth values are not linear, but you get the point). – BDL Dec 30 '17 at 11:57
  • When you have a cube at 0 and a cube at 600, then why not use [0, 600] instead of [-600, 600]. – BDL Dec 30 '17 at 11:58
  • I did't set a parameter for buffer bits. its the default of openGL. – Ahmed Shaban Dec 30 '17 at 12:04
  • [0, 600] may be some better but didnt solve the problem. I used this value to show and cube behind the camera – Ahmed Shaban Dec 30 '17 at 12:05
  • [Maybe this helps](https://stackoverflow.com/questions/29420616/32-bit-depth-buffer-in-opentk-glcontrol) – BDL Dec 30 '17 at 13:14
  • many thanks BDL. I used the following this.glControl_window = new OpenTK.GLControl(new OpenTK.Graphics.GraphicsMode(new OpenTK.Graphics.ColorFormat(8,8,8,8), 24)); instead of this.glControl_window = new OpenTK.GLControl(); and this solved the problem. but lines become poor quality. – Ahmed Shaban Dec 30 '17 at 15:40
  • this the new situation where lines became less qualtiy https://i.stack.imgur.com/WVXxb.jpg – Ahmed Shaban Dec 30 '17 at 15:53
  • Why do you enable `PolygonOffsetFill` but never seem to set a proper polygon offset? – derhass Dec 30 '17 at 18:58
  • thanks all. thanks stackoverflow. it is working now after adding the foolowing before draw lines GL.PolygonOffset(1.0f, 2.003f); – Ahmed Shaban Dec 31 '17 at 23:24

0 Answers0