I have drawn the Barth sextic with Haskell OpenGL and there's a problem. I have also drawn it in R (with packages rgl
and misc3d
) and there's no problem. The two algorithms (in Haskell and R) are very similar. The Barth sextic is an implicit surface, and in Haskell I compute a triangulation of this surface with a marching cubes algorithm that I got by translating the one of misc3d
to Haskell and C. I use the vertex normals, each defined by the gradient of the implicit equation.
Here is the problem:
I don't want these black shadows. When I look at the back of the sextic, by a 180° rotation, there's no such shadow:
The full code is available in this Github repo. Here are the parts of the code relevant to the colors:
fuchsia :: Color4 GLfloat
fuchsia = Color4 1.00 0.00 1.00 1
discord :: Color4 GLfloat
discord = Color4 0.21 0.22 0.25 1
renderPrimitive Triangles $ mapM_ drawTriangle triangles
swapBuffers
where
drawTriangle ((v1,v2,v3), (n1,n2,n3)) = do
materialDiffuse FrontAndBack $= fuchsia
normal n1
vertex v1
normal n2
vertex v2
normal n3
vertex v3
clearColor $= discord
materialAmbient FrontAndBack $= black
materialDiffuse FrontAndBack $= white
materialEmission FrontAndBack $= black
lighting $= Enabled
lightModelTwoSide $= Enabled
light (Light 0) $= Enabled
position (Light 0) $= Vertex4 0 0 (-1000) 1
ambient (Light 0) $= white
diffuse (Light 0) $= white
specular (Light 0) $= white
I have tried to change the colors in this last piece of code, but no way to get rid of these shadows. Am I doing something bad with the colors? I am sure the normals are correct because this works in R. However the shadows appear where the surface is not smooth, so I'm wondering whether the issue is caused by the normals.
The R rendering:
Edit
I have managed to get rid of these shadows:
I don't really know how, I've done so many attempts... But anyway, now the problem is that the back of the sextic is too lighty:
Now the rendering is nice :-)
I don't know what caused the problem because I have done numerous changes... Here are the relevant parts of the code:
I have also changed the order of the vertices of each triangle:
This answer is a bit premature. I will investigate more later, and I'll edit to write my findings.
EDIT
Well, I have done further investigations and sadly, my conclusion is that I have no explanation: I have reverted all my changes, and no way to reproduce the issue !!
Now I use this shorter code:
The two
MatrixMode
are important.polygonSmooth
andrescaleNormal
were useless. I have also changed the position of the light but this is not the point which caused the issue. ThecullFace
is not necessary but it is good because there's no visible back face.