BLOG ARTICLE glFog | 1 ARTICLE FOUND

  1. 2014.09.12 GLSL, OpenGLES2.0 glFog



OpenGLES2.0 not support glFog. So If you want fog effect, you have to a code using glsl.


1. Calculate

  GL_FOG_MODE have 3 modes.


GL_LINEAR : f = (end - z)/(end - start)

GL_EXP :  f = exp(-density*z) 

GL_EXP2 : f = exp2(-(density*z)*(density*z))

Color = Color_source*f + (1-f)Color_fog



  start - fog start distionce(usually 0.0f)

  end - fog's end distance (usually 1.0f)

  Color_source is fragment color.

  Color_fog is fog-color set by GL_FOG_COLOR


2. GLSL Code

  Below show glsl code. It is very simple code

tmp_color is rgba color that texture color and source color modulated. 

enable_fog is set by glUniform1i c++ code. Default mode is GL_LINEAR. 

To get fragment's depth buffer value, I used gl_FragCoord.z that is [0-1].


void main(void)
{	
	if(enable_fog == 1)
	{
		float f = 0.0;
		float z = gl_FragCoord.z;
		float alpha = tmp_color.a;
		if(fog_mode == FOG_EXP)
		{
			f = clamp(exp(-fog_density*z),0.0, 1.0);
		}
		else if(fog_mode == FOG_EXP2)
		{
			f = clamp(exp((fog_density*z)*(fog_density*z)),0.0, 1.0);
		}
		else
		{
			float divide = fog_end - fog_start;
			if(divide != 0.0)
				f = clamp((fog_end - z)/(divide),0.0, 1.0);
		}
		tmp_color = f*tmp_color + (1.0 - f)*fog_color;
		tmp_color.a = alpha;
	}
	
	gl_FragColor = tmp_color;
}


Below show result,

(OpenGL Diary)



'NDK & OpenGL ES' 카테고리의 다른 글

OpenGLES2.0 GLSL, 2D Basic shader  (1) 2014.09.18
OpenGLES 2.0,GLSL lighting code  (0) 2014.09.15
GLSL, OpenGLES2.0 glFog  (0) 2014.09.12
NDK AdMob, Can't see admob on Framelayout  (0) 2014.09.10
GLSurfaceView & GLRenderer 설정  (0) 2014.04.15
이클립스 NDK설정  (0) 2014.04.15