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?