Sign in to follow this  

are these shaders correct?

Recommended Posts

Hey, Just trying to apply per pixel lighting on a textured sphere. This is what I'm doing: Vertex Shader
#version 110

varying vec3 lightDir,normal, viewVec;
void main()
	vec3 ecPos = vec3(gl_ModelViewMatrix * gl_Vertex);
	viewVec = vec3(normalize(-ecPos));

	normal = normalize(gl_NormalMatrix * gl_Normal);
	lightDir = normalize(vec3(gl_LightSource[0].position));
	gl_TexCoord[0] = gl_MultiTexCoord0;
	gl_Position = ftransform();

Fragment Shader
#version 110

varying vec3 lightDir,normal, viewVec;
uniform sampler2D tex;

void main()
	vec3 ct,cf;
	vec4 texel;
	float intensity,at,af;
	vec3 nnormal = normalize(normal);
	intensity = max(dot(lightDir, nnormal),0.0);
	float specValue = clamp(dot(reflect(-normalize(lightDir), nnormal), normalize(viewVec)), 0.0, 1.0);
	specValue = pow(specValue, 6.0);
	cf = intensity * (gl_FrontMaterial.diffuse).rgb + 
					  (gl_FrontMaterial.ambient.rgb*0.5) + specValue;
	af = gl_FrontMaterial.diffuse.a;
	texel = texture2D(tex,gl_TexCoord[0].st);
	ct = texel.rgb;
	at = texel.a;
	gl_FragColor = vec4(ct * cf, at * af);	

this is what I get: are te shaders correct? I think the result is the expected for this kind of lighting, no? Huge thanks

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this