Like a lot of people, I have been working through the NeHe tutorials. In
#16, they introduce fog. I tried to port the code correctly, but either I
didn't do it right, or fog is not working correctly in LWJGL. Here is the
code I have to set up fog:
GL.glEnable(GL.GL_TEXTURE_2D);
// Enable Texture Mapping
GL.glBlendFunc(GL.GL_SRC_ALPHA,GL.GL_ONE); //
Set The Blending Function For Translucency
GL.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //
This Will Clear The Background Color To Black
GL.glClearDepth(1.0);
// Enables Clearing Of The Depth Buffer
GL.glDepthFunc(GL.GL_LESS);
// The Type Of Depth Test To Do
GL.glEnable(GL.GL_DEPTH_TEST);
// Enables Depth Testing
GL.glShadeModel(GL.GL_SMOOTH);
// Enables Smooth Color Shading
GL.glMatrixMode(GL.GL_PROJECTION);
// Select The Projection Matrix
GL.glLoadIdentity();
// Reset The Projection Matrix
GLU.gluPerspective( 45.0f, (float) Display.getWidth() / (float)
Display.getHeight(), 0.1f, 100.0f); // Calculate The Aspect Ratio Of The
Window
GL.glMatrixMode(GL.GL_MODELVIEW);
// Select The Modelview Matrix
GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); // Really
Nice Perspective Calculations
ByteBuffer temp=ByteBuffer.allocateDirect(16);
temp.order(ByteOrder.nativeOrder());
temp.asFloatBuffer().put(lightAmbient).flip();
GL.glLightfv(GL.GL_LIGHT1, GL.GL_AMBIENT, temp.asFloatBuffer());
// Setup The Ambient Light
temp.asFloatBuffer().put(lightDiffuse).flip();
GL.glLightfv(GL.GL_LIGHT1, GL.GL_DIFFUSE, temp.asFloatBuffer());
// Setup The Diffuse Light
temp.asFloatBuffer().put(lightPosition).flip();
GL.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION,temp.asFloatBuffer());
// Position The Light
GL.glEnable(GL.GL_LIGHT1);
// Enable Light One
GL.glFogi(GL.GL_FOG_MODE, fogMode[fogfilter]); // Fog Mode
temp.asFloatBuffer().put(fogColor).flip();
GL.glFog(GL.GL_FOG_COLOR, temp.asFloatBuffer()); //
Set Fog Color
GL.glFogf(GL.GL_FOG_DENSITY, 0.35f);
// How Dense Will The Fog Be
GL.glHint(GL.GL_FOG_HINT, GL.GL_DONT_CARE);
// Fog Hint Value
GL.glFogf(GL.GL_FOG_START, 1.0f);
// Fog Start Depth
GL.glFogf(GL.GL_FOG_END, 5.0f);
// Fog End Depth
GL.glEnable(GL.GL_FOG);
// Enables GL_FOG
What is happening is there is a foggy layer around the cube, but there is no fog anywhere else in the scene. Any ideas?
Thanks. 8)
I'll try and look into it tonight - you'll just have to wait ~ 10 hours :)
Fog is not actually drawn - all it does is modulate the triangles that are drawn with the fog colour depending on their Z depth.
In order to make the scene look foggy you have to call glClearColor() with the fog colour - and miraculously everything will look foggy!
Cas :)
Whoops, my bad.
That did the trick.
Thanks princec.
8)