Skip to content

Instantly share code, notes, and snippets.

@feliwir
Created June 13, 2016 13:34
Show Gist options
  • Save feliwir/42a67a0fba706343e97a6260ad7d9802 to your computer and use it in GitHub Desktop.
Save feliwir/42a67a0fba706343e97a6260ad7d9802 to your computer and use it in GitHub Desktop.
/*
VERTEX SHADER FOR SIMPLE LIGHT MODELS
created by Daniel Jungblut, IWR Heidelberg, February 2008
refined by Bastian Rieck, IWR Heidelberg, May 2010
Simplified Blinn-Phong shading model for a single light source. Hit SPACE in
order to move the light source. All vector calculations should be made in
world coordinates.
The following functions might be helpful:
A = normalize(B) [normalizing a vector]
float s = dot(A, B) [dot product, scalar product]
float m = max(a, b) [maximum of two vectors]
float p = pow(a, b) [power function]
*/
void per_vertex_lighting(
float4 position : POSITION, // input coordinates (object space)
out float4 out_position : POSITION, // output coordinates ("screen" space)
uniform float3 translation,
float3 normal : NORMAL, // surface normal (object space)
out float4 out_color : COLOR, // output colour of vertex
uniform float4x4 modelview_proj, // concatenation of modelview and projection matrix
uniform float3 ambient_light, // ambient light colours des ambienten Lichts
uniform float3 light_color, // light colour
uniform float4 light_position, // light position (world coordinates)
uniform float4 camera_position, // viewer position (world coordinates)
uniform float3 Ke, // material properties: emitting part
uniform float3 Ka, // material properties: ambient part
uniform float3 Kd, // material properties: diffuse part
uniform float3 Ks, // material properties: specular part
uniform float exponent // material properties: exponent for specular term
)
{
out_position = mul(modelview_proj, position);
float3 P = position.xyz + translation; // vertex position in world coordinates
float3 N = normal; // normal in world coordinates
float F = 1.0f;
float D = 1.0f;
float3 emissive = Ke;
float3 ambient = Ka * ambient_light;
float3 L = normalize(light_position.xyz - P);
float diffuse_light = max(dot(N, L), 0);
float3 diffuse = Kd * light_color * diffuse_light;
float3 V = normalize(camera_position.xyz - P);
float3 H = normalize(L + V);
float3 R = reflect(L, N);
float VdotN = max(dot(V, N), 0.0);
float LdotN = max(dot(L, N), 0.0);
float NdotH = max(dot(N, H), 0.0);
float VdotH = max(dot(V, H), 0.000001);
float LdotH = max(dot(L, H), 0.000001);
float G1 = (2.0 * NdotH * VdotN) / VdotH;
float G2 = (2.0 * NdotH * LdotN) / LdotH;
float G = min(1.0, min(G1, G2));
float3 specular = G * F * D / max(3.14159265 * VdotN, 0.000001);
out_color.xyz = emissive + ambient + diffuse + specular;
out_color.w = 1.0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment