即Blinn-Phong 光照模型, 关于此模型的更多信息请google.
参照:http://www.lighthouse3d.com/tutorials/glsl-tutorial/directional-lights-ii/
源代码:http://download.csdn.net/detail/netrookie/4306817
主要看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_mv_mat; uniform mat4 m_model_it; uniform MaterialParameters m_material_attr; uniform LightSourceParameters m_light_attr; attribute vec4 m_position; attribute vec3 m_normal; varying vec4 m_out_color; void main() { vec3 normal, light_dir; vec3 position_mv; vec3 view_dir; vec3 H; float NdotL,NdotHV; vec4 specular; light_dir = normalize(vec3(m_light_attr.position)); normal = normalize(vec3(m_model_it * vec4(m_normal, 0.0))); NdotL = max(dot(normal, light_dir), 0.0); position_mv = vec3(m_mv_mat * m_position); view_dir = vec3(0, 0, 0) - position_mv; H = normalize(view_dir - light_dir); NdotHV = max(dot(normal, H), 0.0); specular = m_light_attr.ambient * m_material_attr.ambient + m_material_attr.specular * m_light_attr.specular * pow(NdotHV, m_material_attr.shininess); m_out_color = specular; gl_Position = m_mvp_mat * m_position; }
截图: