Optimized shaders
This commit is contained in:
parent
a540d6b167
commit
b7cb346354
2 changed files with 15 additions and 17 deletions
|
@ -7,33 +7,31 @@ varying vec3 normal, pos;
|
||||||
void main() {
|
void main() {
|
||||||
vec3 n, l, refl, eye;
|
vec3 n, l, refl, eye;
|
||||||
float NdotL, RdotE;
|
float NdotL, RdotE;
|
||||||
vec4 color, specularColor;
|
vec4 specularColor;
|
||||||
float dist, att, specularFactor;
|
float dist, distSq, att, specularFactor;
|
||||||
|
|
||||||
n = normalize(normal);
|
n = normalize(normal);
|
||||||
|
|
||||||
l = gl_LightSource[0].position.xyz - pos;
|
l = gl_LightSource[0].position.xyz - pos;
|
||||||
|
|
||||||
dist = length(l);
|
distSq = dot(l, l);
|
||||||
l /= dist;
|
|
||||||
|
|
||||||
/* compute the dot product between normal and normalized lightdir */
|
NdotL = dot(n, l);
|
||||||
NdotL = max(dot(n, l), 0.0);
|
|
||||||
|
|
||||||
if (NdotL > 0.0) {
|
if (NdotL > 0.0) {
|
||||||
att = 1.0 / (gl_LightSource[0].constantAttenuation +
|
dist = sqrt(distSq);
|
||||||
gl_LightSource[0].linearAttenuation * dist +
|
|
||||||
gl_LightSource[0].quadraticAttenuation * dist * dist);
|
|
||||||
color = att * (diffuse * NdotL + ambient);
|
|
||||||
|
|
||||||
refl = normalize(reflect(-l, n));
|
att = 1.0 / (gl_LightSource[0].quadraticAttenuation * distSq);
|
||||||
eye = normalize(-pos);
|
gl_FragColor = att * (diffuse * NdotL / dist + ambient) * texture2D(tex, gl_TexCoord[0].st);
|
||||||
|
|
||||||
RdotE = max(dot(refl, eye), 0.0);
|
refl = reflect(-l, n);
|
||||||
|
|
||||||
|
RdotE = -dot(refl, pos) / sqrt(dot(refl, refl) * dot(pos, pos));
|
||||||
|
|
||||||
|
if(RdotE > 0.0) {
|
||||||
specularFactor = att * pow(RdotE, gl_FrontMaterial.shininess);
|
specularFactor = att * pow(RdotE, gl_FrontMaterial.shininess);
|
||||||
specularColor = specularFactor * gl_FrontMaterial.specular * gl_LightSource[0].specular;
|
gl_FragColor += specularFactor * gl_FrontMaterial.specular * gl_LightSource[0].specular;
|
||||||
|
}
|
||||||
gl_FragColor = color * texture2D(tex, gl_TexCoord[0].st) + specularColor;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
gl_FragColor = vec4(0, 0, 0, 1);
|
gl_FragColor = vec4(0, 0, 0, 1);
|
||||||
|
|
|
@ -3,7 +3,7 @@ varying vec3 normal, pos;
|
||||||
|
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
normal = normalize(gl_NormalMatrix * gl_Normal);
|
normal = gl_NormalMatrix * gl_Normal;
|
||||||
|
|
||||||
pos = vec3(gl_ModelViewMatrix * gl_Vertex);
|
pos = vec3(gl_ModelViewMatrix * gl_Vertex);
|
||||||
|
|
||||||
|
|
Reference in a new issue