jenspfister's picture

Render-Problem with GL_LIGHTING enabled

Hey Guys.

I have some problems with the rendering, when I enable GL_LIGHTING.

In the image you can see the sphere has some points where you can see through the faces.

The Sphere is created in blender, exported as obj and imported (normals included).

The lighting is done here

            GL.Light(LightName.Light0, LightParameter.Position, new float[] { 1, 0, 500 });
            GL.Light(LightName.Light0, LightParameter.SpotDirection, new float[] { 0, 0, -1 });
            GL.Light(LightName.Light0, LightParameter.Diffuse, new Color4(1f, 1f, 1f, 1f));
            GL.Light(LightName.Light0, LightParameter.Specular, new Color4(0.1f, 0.1f, 0.1f, 1f));
            GL.Light(LightName.Light0, LightParameter.Ambient, new Color4(0.1f, 0.1f, 0.1f, 1f));

The Rendering of a single triangle is done by this code:
P1, P2, P3 are the points of the triangle.
N1, N2, N3 the corresponding normals.

            GL.ColorMaterial(MaterialFace.Front, ColorMaterialParameter.AmbientAndDiffuse);
            GL.Material(MaterialFace.Front, MaterialParameter.Specular, new Color4(1.0f, 1.0f, 1.0f, 0.2f));
            GL.Color4(0.2, 0.2, 1, 1);
            GL.Normal3(N1.X, N1.Y, N1.Z);
            GL.Vertex3(P1.X * Scaling, P1.Y * Scaling, P1.Z * Scaling);
            GL.Normal3(N2.X, N2.Y, N2.Z);
            GL.Vertex3(P2.X * Scaling, P2.Y * Scaling, P2.Z * Scaling);
            GL.Normal3(N3.X, N3.Y, N3.Z);
            GL.Vertex3(P3.X * Scaling, P3.Y * Scaling, P3.Z * Scaling);
            GL.Vertex3(P1.X * this.Scaling, P1.Y * this.Scaling, P1.Z * this.Scaling);
            GL.Vertex3(P2.X * this.Scaling, P2.Y * this.Scaling, P2.Z * this.Scaling);
            GL.Vertex3(P3.X * this.Scaling, P3.Y * this.Scaling, P3.Z * this.Scaling);

Why does is look that crappy?


Inline Images


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
avc81's picture

i guess: wrong normals?

jenspfister's picture

ok. added following code to calculate the normals by myself. same problem...

Vector3D U = P2 - P1;
Vector3D V = P3 - P1;
 Vector3D Normal = new Vector3D(U.Y * V.Z - U.Z * V.Y, U.Z * V.Z - U.X * V.Z, U.X * V.Y - U.Y * V.X);
GL.Normal3(Normal.X, Normal.Y, Normal.Z);
avc81's picture

is culling enabled? why gldepth func is set to GL_LEQUAL?

jenspfister's picture


Because LEQUAL gets me the best results...

You think it's something with the z-Buffer?

Its set to

avc81's picture

yes maybe. have u turned on backface culling? i'm thinking about overlapping (backface vs front face triangles)

avc81's picture

try also to play with the z buffer's bits number (e.g. from 24 to 16 or less and viceversa)

jenspfister's picture

Backface culling is disabled (enabling doesn't help either).
I tried different Z-Buffer values (8, 16, 24 bits) and AA settings (on, 8x) it won't help either.

anything else i can try? (except for direct3d ;-) )

avc81's picture

i'm sorry but that was all i could think about. let's see if someone else can figure out a solution.

p.s. if you turn off lighting all goes to the right place , right?

jenspfister's picture

Thank you very much avc81 !!!

I was so fixed on the thought that it's a lighting problem, but it wasn't... :-)
You were right with the z-Buffer.

Glu.Perspective(FieldOfView * CameraZoom, (double)ViewportSize.Width / ViewportSize.Height, 0f, 1000f);

I changed the near / far clipping planes to 300 / 1000 and now it works!

Can someone explain that?
The distance between the planes is 50.0, the depth buffer has 24 bits, which is a resolution of (1000/2^24) = 0,000059604644775390625.
And the 0.000...5 is definately smaller that 50.0.

left image: 0/1000, right image: 300/1000

avc81's picture

you're welcome. have a nice sphere ;) !
p.s. the zbuffer values' distribution is non-uniform.