GLSL shader Specular Highlight problem

Started by
1 comment, last by glJack 19 years, 2 months ago
Hi Everyone! My problem is: (see image) http://www.gamedev.ru/images/show.php?id=3709 The highlight has to be in the place where blue arrow points, but it appears in the place, where the red arrow is. I thought that problem might be linear interpolation of Half Angle vector across the vertices causing the problem, but I made HL vector to be calculated per fragment, and that didn't help. Who knows, what the problem can be? spot_bump.vert attribute vec3 tangent, binormal; varying vec3 v_HalfAngleTan, v_Light, v_LightTan, v_EyeTan; void main() { gl_Position = gl_ModelViewMatrix*gl_Vertex; gl_TexCoord[0] = gl_MultiTexCoord0; // diffuse map coord gl_TexCoord[1] = gl_MultiTexCoord0; // normal map coord gl_TexCoord[3] = gl_TextureMatrix[0] * gl_Position; // light spot tex coord vec4 l_pos = gl_LightSource[0].position; vec3 v_Normal = normalize(gl_NormalMatrix*gl_Normal); // normal to eye space vec3 v_Tangent = normalize(gl_NormalMatrix*tangent); // tangent to eye space vec3 v_Binormal = normalize(gl_NormalMatrix*binormal); // binormal to eye space mat3 tangent_bas = mat3( // in column major order v_Tangent.x, v_Binormal.x, v_Normal.x, v_Tangent.y, v_Binormal.y, v_Normal.y, v_Tangent.z, v_Binormal.z, v_Normal.z); v_EyeTan = normalize(tangent_bas * -vec3(gl_Position)); v_Light = l_pos.xyz - gl_Position.xyz; v_LightTan = tangent_bas * v_Light; v_HalfAngleTan = (normalize(v_LightTan) + v_EyeTan) * 0.5; gl_Position = gl_ProjectionMatrix * gl_Position; } spot_bump.frag uniform sampler2D tex_unit_0; uniform sampler2D tex_unit_1; uniform sampler2D tex_unit_3; varying vec3 v_HalfAngleTan, v_Light, v_LightTan; #define LIGHT gl_LightSource[0] void main() { vec3 light_diff = gl_LightSource[0].diffuse.xyz; float len = length(v_Light); vec3 a_coeff = vec3(LIGHT.constantAttenuation, LIGHT.linearAttenuation, LIGHT.quadraticAttenuation); float atten = 1.0/(a_coeff[0]+a_coeff[1]*len + a_coeff[2] *len*len); vec3 v_Light_n = normalize(v_LightTan); vec4 diffuse = texture2D(tex_unit_0, gl_TexCoord[0].xy); vec4 _normal = texture2D(tex_unit_1, gl_TexCoord[1].xy); vec3 normal = normalize(vec3(_normal*2.0 - 1.0)); // unpacking normal bool lit = v_LightTan.z > 0.0; vec2 intens = vec2( dot(normal, v_Light_n), dot(normalize(v_HalfAngleTan), normal) ); intens = float(lit) * max(vec2(0,0), intens); intens[1]=0.6 * pow(intens[1], 100.0); intens = atten * intens; // LIGHT SPOT COLOR vec3 spot_color = vec3(0,0,0); vec3 tex_coord = gl_TexCoord[3].xyz / gl_TexCoord[3].w; vec2 temp = abs(tex_coord.xy); if(temp.x < 1.0 && temp.y <1.0 && gl_TexCoord[3].w > 0.0) { vec2 tex_coord_2D = tex_coord.xy * 0.5 + vec2(0.5); spot_color = texture2D(tex_unit_3, tex_coord_2D ).rgb; } spot_color.rgb *= light_diff; gl_FragColor = vec4(vec3(spot_color)*vec3(intens[0]*vec3(diffuse)+intens[1]) + // DIFFUSE + SPECULAR vec3(diffuse) * 0.2 // ABMIENT ,1.0); }
Advertisement
A couple things:


Screenie Linkified

Specular Equation

I think the highlight looks good even if its off (are you positive its off?) I dont know enough about the math behind the lighting calculations to help you, but the link above SHOULD help you

hope that helps
-Dan
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."
Thanks, Dan. I looked at these articles, but I didn't find anything new there. The code above seems to implement all the stuff mentioned in articles. I don't know what the problem is.

You're right, the highlight looks good in this way either, but I want it to be correct.

This topic is closed to new replies.

Advertisement