To Simulate A Fog In OpenGL ES Android.

In openGL ES, there are 3 fog modes that are available to us. These are displayed in the table below :

Flag Description

  • GL_EXP This is the simplest form of fog. Objects do not really appear to move in and out of the fog. This just creates a simple haze effect.
  • GL_EXP2 This is a more advanced version of the previous fog mode. Objects now appear to move in and out of the fog . You may notice that the fog does not appear completely realistic as a distinct line  can be seen at the point where the object is moving out of the fog.
  • GL_LINEAR This is the most realistic fog mode. Objects appear to come in and out of the fog properly, giving a realistic fog effect.
gl.glEnable(GL10.GL_FOG); // 1. to enable the fog
gl.glFogf(GL10.GL_FOG_MODE, GL10.GL_EXP2); //2. set the fog mode.
gl.glFogfv(GL10.GL_FOG_COLOR, fbbb); // 3. to set the fog color, it takes a framebuffer value.
gl.glFogf(GL10.GL_FOG_DENSITY, 0.1f); // 4. density of the foggy fog</span></span></p>

 

This function allows you to specify how important the look  speed of your effect is.

The first parameter can be a number of values such as GL_FOG_HINT, GL_LINE_SMOOTH_HINT,      GL_PERSPECTIVE_CORRECTION_HINT and GL_POINT_SMOOTH_HINT.
We will only be dealing with the GL_FOG_HINT flag.

The second parameter can be either GL_DONT_CARE, GL_FASTEST or GL_NICEST.
If we want our fog to look as good as possible, we would pass on GL_NICEST.
If we are concerned more with speed, we would pass on GL_FASTEST.
The default value is GL_DONT_CARE.
This will enhance the effect of the fog only if there is enough time to do so.

We pass GL_DONT_CARE onto the glHint function below.
This is not necessary as this is the default value anyway.
We are showing it here just so that you know that you can increase the fog effect further.

gl.glHint(GL10.GL_FOG_HINT, GL10.GL_DONT_CARE);
gl.glFogf(GL10.GL_FOG_START, 10.0f); //near and far distances
gl.glFogf(GL10.GL_FOG_END, 15.0f);

Well to use a framebuffer object in the line 3, you can refer this function. It takes a float array and returns a FB Object.

float fogColor[] = { 0.5f, 0.5f, 0.5f, 1.0f }; //a fog of gray color is used here,
//you can change the values and create fog colors as per your need.
FloatBuffer fbbb;

public MyRenderer(Context context) // the renderer class constructor
{
this.context = context;
.
.
.
fbbb = makeFloatBuffer(fogColor);
}

protected static FloatBuffer makeFloatBuffer(float[] arr)
{
ByteBuffer bb = ByteBuffer.allocateDirect(arr.length*4);
bb.order(ByteOrder.nativeOrder());
FloatBuffer fb = bb.asFloatBuffer();
fb.put(arr);
fb.position(0);
return fb;
}

 

The above example is working fine.

 

 

Post By: Siddharth Verma