Shader Problem

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Kalda
Posts: 47
Joined: Wed Aug 23, 2006 1:38 pm
Location: Prostejov, Czech Republic
Contact:

Shader Problem

Post by Kalda »

Hello! I have small problem.

Shot from editor:
Image
and from irrlicht:
Image

Why is model in irrlicht darker?
I think there is problem with calculating lightAttenuation variable, but where?

Vertex:

Code: Select all

varying	vec3 g_lightVec;
varying	vec3 g_viewVec;

uniform vec4 lightPosition;

void main()
{
	gl_Position = ftransform();
	gl_TexCoord[0] = gl_MultiTexCoord0;
	
	vec3 fvTangent  = -vec3(abs(gl_Normal.y) + abs(gl_Normal.z), abs(gl_Normal.x), 0); 
    vec3 fvBinormal = cross(fvTangent,gl_Normal);   
    fvTangent=gl_NormalMatrix*cross(fvBinormal,gl_Normal); 
    fvBinormal=gl_NormalMatrix*fvBinormal;

	
	mat3 TBN_Matrix = gl_NormalMatrix * mat3(fvTangent, fvBinormal, gl_Normal);
	vec4 mv_Vertex = gl_ModelViewMatrix * gl_Vertex;
	g_viewVec = vec3(-mv_Vertex) * TBN_Matrix ;	

	vec4 lightEye = gl_ModelViewMatrix *  lightPosition;
	vec3 lightVec =0.02* (lightEye.xyz - mv_Vertex.xyz);				
	g_lightVec = lightVec * TBN_Matrix; 
}
and fragment:

Code: Select all

uniform sampler2D NormalHeight; //normalmap+heightmap
uniform sampler2D Base; //diffuse
uniform sampler2D Glow; //glow
uniform sampler2D Specular;//specular

uniform bool  UseGlow;
uniform float GlowMultiplier;

uniform vec2 cBumpSize;// = 0.02 * vec2 (2.0, -1.0);
uniform vec3 lightColor;

varying	vec3 g_lightVec;
varying	vec3 g_viewVec;

void main()
{
   
	float LightAttenuation = clamp(1.0 - dot(g_lightVec, g_lightVec), 0.0, 1.0);
	vec3 lightVec = normalize(g_lightVec);
	vec3 viewVec = normalize(g_viewVec);
	
	float height = texture2D(NormalHeight, gl_TexCoord[0].xy).a;
	height = height * cBumpSize.x + cBumpSize.y;
	
	vec2 newUV = gl_TexCoord[0].xy; + viewVec.xy * height;
	vec4 color_base = texture2D(Base,newUV);
	
	if(UseGlow)
	{
		vec4 glowColor  = texture2D(Glow,newUV)*clamp(GlowMultiplier,0.75,10.0);
		color_base.rgb += glowColor.rgb;
		color_base.a   *= glowColor.a;
	}
	
	vec3 bump = texture2D(NormalHeight, newUV.xy).rgb * 2.0 - 1.0;
	bump = normalize(bump);
	
	float base = texture2D(NormalHeight, newUV.xy).a;	
	float diffuse = clamp(dot(lightVec, bump), 0.0, 1.0);
	float specular = pow(clamp(dot(reflect(-viewVec, bump), lightVec), 0.0, 1.0), 16.0);
	
	float bd = base * diffuse;
	//vec3 spec = texture2D(Specular,newUV.xy).rgb * pow(specular,4.0) + (0.7 * vec3(bd));
	
	vec3 specular_color = texture2D(Specular,newUV.xy).rgb;
	specular_color *= 2.0 * specular;
	specular_color += vec3(bd)*1.5;
	//vec3 spec = bd +0.7* specular * color_base.a);
	
	gl_FragColor = vec4(color_base.rgb * lightColor //vec3
					//* (diffuse * base +0.7* specular * color_base.a)  //float
					* specular_color
					* LightAttenuation
					,color_base.a);  // float
	if(UseGlow)
	{
		vec4 glowColor  = texture2D(Glow,newUV)*clamp(GlowMultiplier,0.75,10.0);
		gl_FragColor.rgb += glowColor.rgb;
		gl_FragColor.a   *= glowColor.a;
	}	
}
Thx
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Its the light vector.

Sometimes in irrlicht I find that the normals in shaders are inverted, so try changing some of the gl_Normal to -glNormal and see if that helps.
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
Post Reply