Fog

I’m thinking about trying to create fog for my game but I’m not sure how to go about it.
It’s only a 2D game that I’m experimenting with and so far I’ve been looking into OpenGL Fog but I haven’t been able to get that working just yet.

I basically followed this tutorial: http://www.swiftless.com/tutorials/opengl/fog.html just I’m unsure how to get that to work with 2D alongside SpriteBatches (Using LibGDX) and things.

Example of how I’m currently trying to achieve this:


public void drawFog(){
		float fogDensity = 0.3f;
		float fogColour[] = {0.5f, 0.5f, 0.5f, 1f};
		gl.glEnable (gl.GL_DEPTH_TEST);
		gl.glEnable(gl.GL_FOG);
		gl.glFogf(gl.GL_FOG_MODE, gl.GL_EXP2);
		gl.glFogfv(gl.GL_FOG_COLOR, fogColour, 0);
		gl.glFogf(gl.GL_FOG_DENSITY, fogDensity);
		gl.glHint(gl.GL_FOG_HINT, gl.GL_NICEST);
		gl.glLoadIdentity();
	}


public void render(float delta){

		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		gl.glClearColor(0f, 0f, 0f, 1f);
		
		gl.glViewport((int) glViewport.x, (int) glViewport.y, (int) glViewport.width, (int) glViewport.height);
		cam.update();
		originalCamera.update();
		batch.setProjectionMatrix(relProjection());
        
		drawFog();
		
		batch.begin();
		
		for(int i = 0; i < entities.size(); i++){
			entities.get(i).draw(batch);
		}
		
		batch.end();
	}

Fog depends on the depth buffer of a 3D scene.

Are you trying to achieve a haze across your screen? Or are you trying to have sprites fade out after a certain distance?

For those kind of effects (in 2D or 3D) you should learn GLSL:

You can probably get away with vertex colors if you need a GL11 solution, but it won’t look as good.

I’m basically trying to get a ‘misty’ look on top. Maybe zoom in/out features will be added later so if I can create a nice effect that fades things in/out - that would be pretty cool.

Take a transparent 128x128 image of some foggy/misty stuff, draw it at a much larger scale (linear filtering), and have it move across the screen. Then it will look like fog/clouds.

Otherwise start learning shaders if you want something more impressive.

Thanks! :slight_smile:

Checking out the GLSL tutorials from the link you gave me. I just don’t want to move directly to that yet though so I’ll probably take my time with it. Some really impressive looking stuff on heroku, too!

Stacking and sliding around a couple layers of mostly-transparent noise gives it a much more realistic effect than just one layer alone. Keep in mind no effect is free, and ones that involve a lot of blending tend to be much more expensive.