겨울팥죽 여름빙수
article thumbnail



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)



profile

겨울팥죽 여름빙수

@여름빙수

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!