glsl之多重纹理演示
源代码:http://download.csdn.net/detail/netrookie/4316081
vertex shader:
struct MaterialParameters { vec4 emission; vec4 ambient; vec4 diffuse; vec4 specular; float shininess; }; struct LightSourceParameters { vec4 ambient; vec4 diffuse; vec4 specular; vec4 position; vec4 halfVector; vec3 spotDirection; float spotExponent; float spotCutoff; float spotCosCutoff; float constantAttenuation; float linearAttenuation; float quadraticAttenuation; }; uniform mat4 m_mvp_mat; uniform mat4 m_model_it; uniform MaterialParameters m_material_attr; uniform LightSourceParameters m_light_attr; attribute vec4 m_position; attribute vec3 m_normal; attribute vec2 a_texcoord; varying vec4 m_out_color; varying vec2 v_texcoord; void main() { vec3 normal, light_dir; float indensity; vec4 diffuse; v_texcoord = a_texcoord; light_dir = normalize(vec3(m_light_attr.position)); normal = normalize(vec3(m_model_it * vec4(m_normal, 0.0))); indensity = max(dot(normal, light_dir), 0.0); diffuse = m_material_attr.diffuse * m_light_attr.diffuse; m_out_color = (indensity + 0.15) * diffuse; gl_Position = m_mvp_mat * m_position; }
frag shader:
uniform sampler2D texture1; uniform sampler2D texture2; varying vec4 m_out_color; varying vec2 v_texcoord; void main() { vec4 src_color, curr_color; src_color = texture2D(texture1, v_texcoord); curr_color = texture2D(texture2, v_texcoord); gl_FragColor = ((1.0 - curr_color.a) * src_color + curr_color.a * curr_color) * m_out_color; }
截图: