Lines not drawn correctly in OpenGL with fog?
Please take a look at this: http://imgur.com/a/S8eOC
The problem:
The grid is drawn perfectly fine without fog. When fog is enabled, the vertical lines disappear. I then rotate a little to the left and and all lines start to get invisible; if I had rotated even more, they would completely disappear. It seems that it depends on the viewing angle. I thought it could be a problem with the lighting but I turned that off and the result was the same.
I also tried different fog types but the results were the same. If I look straight dow开发者_StackOverflown (90º angle with the grid plane) I can clearly see all the grid lines.
What's pup with this behavior? Can it be fixed?
The code is very basic but here it is in case it's important:
main():
glEnable(GL_FOG);
glHint(GL_FOG_HINT, GL_NICEST);
renderScene():
float fogColor[3] = {0.8f, 0.8f, 0.8f};
glFogfv(GL_FOG_COLOR, fogColor);
glFogi(GL_FOG_MODE, GL_EXP2);
glFogf(GL_FOG_DENSITY, 0.01f);
Without knowing more about the way the lines are drawn, I'm going to assume that you're drawing a single line for each horizontal and vertical line on the grid. The Fog equations are evaluated at vertices only, so if your only vertices are at the very ends of the lines, you'll end up with peculiar artefacts, such as the ones you are seeing.
The main way to solve this is to introduce more vertices; build a vertex for every crossing point, and connect each one up to make your grid. This will result in many more lines and vertices, but you'll get the correct behavior.
I'll go into more detail below to help explain the concept of aliasing, which is what you're most likely seeing.
Remember that any values evaluated at vertices (such as vertex colors, normals, texture coordinates), will be interpolated linearly to cover the triangle/line in between them. Imagine a case where we want pixels in the center of the screen to be bright, and edge pixels to be dark. (A strictly theoretical case, but useful for explanation). Say we draw a line from one edge, through the middle, and to the other edge. Say we only do this with 2 vertices, one at each edge. We then calculate the brightness of both these vertices to be dark. Then, to draw the line, OpenGL interpolates between dark and dark to determine the color of the line. There's no way for the line to know it needs to be bright in the center.
So, if you add a vertex in the center, it would now calculate that the center is bright. The more vertices you add, the more accurate the brightness becomes.
Good luck and Have fun. :)
精彩评论