Skip to main content
gamedev.net
🔒 Locked

problems with specular lighting

Started by Code-R Oct 27, 2005 at 7:55 PM 6 replies 800+ views
Original Post
Code-R
Code-R
I try to do the following code to add specular lighting: in the VertexShader:

HalfAngle = fvLightPosition + ViewVector;
pixel:

vec4 col = (TextureDiffuse * Kd * dot(normalize(Normal), normalize(LightVector)))* att;
 vec4 spec = (TextureDiffuse * Ks * pow(max(0.0, n_dot_h), SpecularPower) ) * att;
return col + spec;
It's not appearing as correct, proper specular. any ideas, anyone?
Aldenar
Aldenar
Maybe it's just the name, but instead of fvLightPosition, you probably want to use the direction from the current vertex to the light (also assuming that ViewVector is the direction from the vertex to the viewer).

HTH,
Aldenar
Code-R
Code-R
nope, still no luck
any ideas?
Aldenar
Aldenar
Hmmm.. pretty hard to tell without a screenshot, is there some specular or not at all? What shader model are you using (for example, the pow function in a pixel shader is very limited in SM1)?
paic
paic
Hi,

could you please post the entire shader ? Because you're using n_dot_h, but we don't know how you initialize, same goes for kd, ks.
Also, in the vertex shader, you write "HalfAngle = fvLightPosition + ViewVector;" but you don't seem to use it in your pixel shader :/ (at least in what you copied here ^^)

So post the entire shader, it will be easier to spot the potential errors ^^
Code-R
Code-R
VS Code:
uniform vec3 fvLightPosition;uniform vec4 ViewPosition;attribute vec3 rm_Tangent;varying vec3 ViewVector;varying vec3 LightVector;varying vec3 HalfAngle;   void main( void ){   gl_TexCoord[0] = gl_MultiTexCoord0;   vec4 NV = gl_Vertex;   NV = vec4(NV.xyz * 4.0, 1.0); //just for scaling   gl_Position = gl_ModelViewProjectionMatrix * NV;   vec4 fvObjectPosition = gl_ModelViewMatrix * NV;      ViewVector = ViewPosition.xyz - fvObjectPosition.xyz;   LightVector = fvLightPosition - gl_Vertex.xyz;   HalfAngle = LightVector + ViewVector;        vec3 fvNormal         = gl_NormalMatrix * gl_Normal;   vec3 fvTangent        = gl_NormalMatrix * rm_Tangent;      mat3 TBN;   TBN[0] = rm_Tangent;   TBN[1] = cross(rm_Tangent, gl_Normal);   TBN[2] = gl_Normal;         ViewVector *= TBN;   LightVector *= TBN;   HalfAngle *= TBN;}


PS Code:
uniform sampler2D DiffuseMap;uniform sampler2D NormalMap;uniform sampler2D HeightMap;uniform float ParallaxScale;uniform float ParallaxBias;uniform float LightRadius;uniform vec4 Color;varying vec3 ViewVector;varying vec3 LightVector;varying vec3 HalfAngle;uniform float Ks;uniform float Kd;uniform float SpecularPower;uniform float Ms;vec4 surface_light(vec3 _Normal, vec3 _LightVector, float _LightRadius, vec3 _ViewVector, vec4 _Diffuse){ float att = max(1.0 - (length(_LightVector) / _LightRadius),0.0); float n_dot_h = dot(_Normal, normalize(HalfAngle)); vec4 col = (_Diffuse * Kd * dot(normalize(_Normal), normalize(xsl_LightVector)))* att; float spec = (Ks * pow(max(0.0, n_dot_h), SpecularPower) ) * Ms; return col + spec;}void main(){ float HeightTex = texture2D(HeightMap, gl_TexCoord[0].xy).x; float Height = ParallaxScale * HeightTex - ParallaxBias; vec2 NewTexCoord = Height * normalize(ViewVector).xy + gl_TexCoord[0].xy;  vec3 Normal = texture2D(NormalMap, NewTexCoord.xy).xyz * 2.0 - 1.0; vec4 Diffuse = texture2D(DiffuseMap, NewTexCoord.xy); gl_FragColor = surface_light(Normal, LightVector, LightRadius, ViewVector, Diffuse*Color);}


[Edited by - Code-R on October 29, 2005 4:34:22 AM]
Aldenar
Aldenar
In the vertex program, you seem to be computing LightVector in model space, but ViewVector in view space (I must admit I don't know what "__xsl_" means though). I think that this may be the source of your problems.

Other than that, you are using a normalized version of the normal to compute N.L, but the un-normalized version of it for N.H.
Code-R
Code-R
Thanks! that seems to have fixed it! the "__xsl_" thing was an internal naming convection, shouldn't have made it here. I edited the post and fixed that. Thanks again and rating++!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.