[Java] [OpenGL ES3.2] 多光源(2个)与雾化

[Java] [OpenGL ES3.2] 多光源(2个)与雾化,第1张

[Java] [OpenGL ES3.2] 多光源(2个)与雾化 着色器

usual.vert

#version 320 es
layout(location=0)in vec3 a_position;
layout(location=1)in vec4 a_color;
out vec4 v_color;
out vec3 v_position;
uniform mat4 u_projection;
uniform mat4 u_view;
uniform mat4 u_model;
void main()
{
gl_Position = u_projection * u_view * u_model * vec4(a_position,1.0f);
v_color = a_color;
v_position = vec3(u_model * vec4(a_position,1.0));
}

usual.frag

#version 320 es
precision mediump float;
uniform vec3 u_cameraposition;
uniform float u_fogstart;
uniform float u_fogend;
uniform vec4 u_fogcolor;
in vec4 v_color;
in vec3 v_position;
out vec4 o_color;
void main()
{
float distance = length(u_cameraposition - v_position);
float factor = (u_fogend - distance) / (u_fogend - u_fogstart);
factor = max(min(factor,1.0),0.0);
if(factor == 0.0)
{
o_color = u_fogcolor;
}
else
{
o_color = v_color * factor + u_fogcolor * (1.0 - factor);
}
}

illumination.vert

#version 320 es
layout(location=0)in vec3 a_position;
layout(location=1)in vec3 a_normal;
out vec3 v_normal;
out vec3 v_position;
uniform mat4 u_projection;
uniform mat4 u_view;
uniform mat4 u_model;
void main()
{
gl_Position = u_projection * u_view * u_model * vec4(a_position,1.0f);
v_normal = normalize((u_model * vec4(a_normal,1.0f)).xyz);
v_position = (u_model * vec4(a_position,1.0f)).xyz;
}

illumination.frag

#version 320 es
precision mediump float;
in vec3 v_normal;
in vec3 v_position;
out vec4 o_color;
uniform vec4 u_color;
uniform vec3 u_cameraposition;
uniform vec4 u_lightcolor;
uniform vec3 u_lightposition;
uniform float u_intensity;
uniform vec4 u_fogcolor;
uniform float u_fogstart;
uniform float u_fogend;
vec4 light(vec3 lightposition)
{
vec4 ambient = 0.1f * u_lightcolor;
vec3 lightdir = normalize(lightposition - v_position);
float diff = max(dot(v_normal,lightdir),0.0f);
vec4 diffues = diff * u_lightcolor;
vec3 cameradir = normalize(u_cameraposition - v_position);
vec3 reflectdir = normalize(reflect(-lightdir,v_normal));
float spec = pow(max(dot(cameradir,reflectdir),0.0f),u_intensity);
vec4 specular = spec * u_lightcolor;
return (ambient + diffues) * u_color + specular;
}
float fog()
{
float distance = length(u_cameraposition - v_position);
float factor = (u_fogend - distance) / (u_fogend - u_fogstart);
factor = max(min(factor,1.0),0.0);
return factor;
}
void main()
{
vec4 color1 = light(u_cameraposition);
vec4 color2 = light(u_lightposition);
float factor = fog();
vec4 lightresult = color1 + color2;
o_color = lightresult * factor + u_fogcolor * (1.0 - factor);
}

效果


提示

本例用了2个光源,位置分别是u_cameraposition和u_lightposition,得到片段的2个颜色后相加,在加上雾化,就是最后的颜色
u_lightposition是(0,1,0)
两个光源颜色什么的都一样,就偷懒了

雾化

雾化的方式不止一种,本例用的是线性雾化,即
结果=(结束距离-摄像机与顶点的距离)/(结束距离-开始距离)

//《OpenGL ES3.0编程指南》P157
float compute_fog()
{
	float f;
	
	//use eye Z as approximation
	if(fog_mode == GLI_FOG_MODE_LINEAR;
	{
		f = (fog_end - p_eye.z) / (fog_end - fog_start);
	}
	else if(fog_mode == GLI_FOG_MODE_EXP)
	{
		f = exp(-(p_eye.z * fog_density));
	}
	else
	{
		f = (p_eye.z * fog_density);
		f = exp(-(f * f));
	}
	f = clamp(f,c_zero,c_one);
	return f;
}

p.s.c_zero是0,c_one是1

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5708710.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存