PDA

View Full Version : Problem using fog (not working on z-axis)



marcuiulian13
04-11-2013, 10:11 AM
Hello everyone! My first post here.
I am programming OpenGL using the LWJGL, so Java.

For one week now I'm struggling with a problem: I'm trying to implement fog in OpenGL, but it doesn't render correctly when I'm moving on the Z-axis.
I already read about the GL_PROJECTION vs. GL_MODELVIEW problem, but I can't manage to find any problem in my code.

Here is a video showing the problem: www.youtube.com/watch?v=o-tMnrhtmiA

Here is my initialisation code:


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45f, (float) Main.WIDTH / Main.HEIGHT, 0.1f, 64f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);

// Init fog
FloatBuffer fogColor = BufferUtils.createFloatBuffer(4);
fogColor.put(0.5f).put(0.5f).put(1.0f).put(1.0f).f lip();

glFogi(GL_FOG_MODE, GL_LINEAR);
glFog(GL_FOG_COLOR, fogColor);
glFogf(GL_FOG_DENSITY, 0.35f);
glHint(GL_FOG_HINT, GL_DONT_CARE);
glFogf(GL_FOG_START, 32f);
glFogf(GL_FOG_END, 64f);

glClearColor(0.5f, 0.5f, 1.0f, 1.0f);


And here is the camera update code:


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glRotatef(pitch, 1.0f, 0.0f, 0.0f);
glRotatef(yaw, 0.0f, 1.0f, 0.0f);
glTranslatef(-position.x, -position.y, -position.z);


Also, the rendering code:


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

camera.update();

glEnable(GL_FOG);
HallwayManager.getInstance().renderHallway();
glDisable(GL_FOG);


(the hallway is rendered using VBOs, if you need it, I will post it's rendering code)

marcuiulian13
04-12-2013, 01:05 PM
UP!! Sorry for double post, but this is a key feature in the project. :(

Dan Bartlett
04-12-2013, 08:46 PM
In fixed-function OpenGL the contribution due to fog/lighting/etc is calculated per-vertex, then interpolated between vertices. When you approach the middle of a large triangle, the vertices at the edges will be far away - in your example they will be calculated to be inside the fog. Since the fragments that are generated between the vertices are interpolated from vertices that appear to be in the fog, they will also be calculated to be in the fog.

Try using more vertices for per-vertex fog/lighting to have a better effect, or use per-pixel fog/lighting via shaders instead.