Luan Xa
Luan Xa

Reputation: 59

Bad rendering in OpenGL Delphi

I have problem with rendering opengl.

with pfd do
begin
  nSize := SizeOf(PIXELFORMATDESCRIPTOR);
  nVersion := 1;
  dwFlags := PFD_DRAW_TO_WINDOW
          or PFD_SUPPORT_OPENGL
          or PFD_DOUBLEBUFFER;
  iPixelType := PFD_TYPE_RGBA;
  cColorBits := colorBits;
  cRedBits := 0;
  cRedShift := 0;
  cGreenBits := 0;
  cGreenShift := 0;
  cBlueBits := 0;
  cBlueShift := 0;
  cAlphaBits := 0;
  cAlphaShift := 0;
  cAccumBits := 0;
  cAccumRedBits := 0;
  cAccumGreenBits := 0;
  cAccumBlueBits := 0;
  cAccumAlphaBits := 0;
  cDepthBits := 16;
  cStencilBits := 0;
  cAuxBuffers := 0;
  iLayerType := PFD_MAIN_PLANE;
  bReserved := 0;
  dwLayerMask := 0;
  dwVisibleMask := 0;
  dwDamageMask := 0;
end;

glMaterialfv(GL_FRONT, GL_AMBIENT, @matAmbient);
glMaterialfv(GL_FRONT, GL_SPECULAR, @matSpecular);
glMaterialf(GL_FRONT, GL_SHININESS, 50.0);
glLightfv(GL_LIGHT0, GL_POSITION, @lightPosition);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, @lmAmbient);

glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);

OpenGl rendering very good if angle between 2 faces large and very bad if angle between 2 faces small, it appear gray color:

enter image description here

This is my full code and binary file: OpenglDelphiTest

I want disappear gray color. Please help me

Upvotes: 1

Views: 184

Answers (1)

Rabbid76
Rabbid76

Reputation: 210878

The issue is caused by z-figthing.

The depth buffer has a limited accuracy, which depends on the bits of the depth buffer (cDepthBits := 16;).
If the distance between the near plane and the far plane of the projection is very haigh, in compare to the "z-range" (z distance from the nearest point of the geometry to the furthest away point) of the geometry, then it can't be distinguished between the different z positions of the geometry, because they are represented by the same value in the depth buffer.

This is the effect which you can see in the image. The effect occurs when the geometry is close to each other. The covered geometry "shines" through the geometry in the front

To solve your issue you have to increase the distance to the near plane respectively decrease the distance to the far plane at the perspective projection and/or to increase the number of depth bits (e.g cDepthBits := 24;, cDepthBits := 32;).

Upvotes: 3

Related Questions