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: - YouTube
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).flip();
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)