使用 opengl es 2.0 glsl 实现阴影和高光效果,写出顶点和片元着色气代码
时间: 2023-03-15 10:50:17 浏览: 137
opengl 实现影子效果
3星 · 编辑精心推荐
顶点着色器:attribute vec4 a_position;
attribute vec2 a_texCoord;
attribute vec4 a_normal;varying vec2 v_texCoord;
varying vec4 v_normal;uniform mat4 u_mvpMatrix;void main()
{
gl_Position = u_mvpMatrix * a_position;
v_texCoord = a_texCoord;
v_normal = a_normal;
}片元着色器:precision mediump float;varying vec2 v_texCoord;
varying vec4 v_normal;uniform sampler2D u_sampler;
uniform vec3 u_ambientColor;
uniform vec3 u_lightLocation;
uniform vec3 u_lightColor;void main()
{
vec4 color = texture2D(u_sampler, v_texCoord);
vec3 normal = normalize(v_normal.xyz);
vec3 lightDirection = normalize(u_lightLocation - v_normal.xyz);
float nDotL = max(dot(lightDirection, normal), 0.0);
vec3 diffuse = u_lightColor * color.rgb * nDotL;
vec3 ambient = u_ambientColor * color.rgb;
gl_FragColor = vec4(diffuse + ambient, color.a);
}
阅读全文