Jump to content

  • Log In with Google      Sign In   
  • Create Account

Calling all IT Pros from Canada and Australia.. we need your help! Support our site by taking a quick sponsored surveyand win a chance at a $50 Amazon gift card. Click here to get started!


[C++, OpenGL, GLSL] Point light lits up objects like a directional light.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 iwoplaza   Members   -  Reputation: 174

Like
0Likes
Like

Posted 21 August 2014 - 07:39 AM

Hello everybody.

 

As I said in the topic, I have a problem with my shader for point lights. I followed a ton of tutorials about phong lighting and per fragment point lighting. Still, after all these tutorials, I still have this problem:

gkpIut.png

 

The light only lits up objects in the positive X direction.

 

Here is my shader code:

VERTEX SHADER

#version 120

varying vec2 texCoord;
varying vec3 position;
varying vec3 normal;
varying vec3 tangentSurf2light;
varying vec3 tangentSurf2view;

uniform vec3 lightPos;

void main()
{
	gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
	position = vec3(gl_ModelViewMatrix * gl_Vertex);
	normal = normalize(gl_NormalMatrix * gl_Normal);
	
	
	
	texCoord=gl_MultiTexCoord0.xy;
	
	vec3 tangent;
	vec3 v1=cross(gl_Normal,vec3(0.0,0.0,-1.0));
	vec3 v2=cross(gl_Normal,vec3(0.0,-1.0,0.0));
	if(length(v1)>length(v2)){
		tangent=v1;
	}else{
		tangent=v2;
	}
	
	vec3 t = normalize(gl_NormalMatrix * tangent);
	vec3 b = cross(normal,t);
	mat3 mat = mat3(t.x,b.x,normal.x,
					t.y,b.y,normal.y,
					t.z,b.z,normal.z);
	
	vec4 var = vec4(lightPos,1.0);
	vec3 lightLoc = vec3(gl_ModelViewMatrix * var);
	
	vec3 vector = normalize(lightLoc-position);
	tangentSurf2light=mat*vector;
	
	vector=normalize(-position);
	tangentSurf2view=mat*vector;
}
FRAGMENT SHADER

#version 120

uniform sampler2D texture;
uniform sampler2D normalmap;
uniform sampler2D specularmap;

uniform vec3 lightPos;

varying vec2 texCoord;
varying vec3 position;
varying vec3 normal;

uniform vec3 matDiffuse;
uniform vec3 matSpecular;

uniform vec3 lightAmbient;
uniform vec3 lightDiffuse;
uniform vec3 lightSpecular;

varying vec3 tangentSurf2light;
varying vec3 tangentSurf2view;

void main(){
	vec4 var = vec4(lightPos,1.0);
	vec3 lightLoc = vec3(gl_ModelViewMatrix * var);

	float dist=length(position-lightLoc);
	float att=1.0/(1.0+0.1*dist+0.01*dist*dist);
	
	vec3 ambient = vec3(texture2D(texture,texCoord)) * lightAmbient;
	
	
	vec3 surf2light = normalize(tangentSurf2light);
	vec3 norm = normalize(texture2D(normalmap,texCoord).xyz*2.0-1.0);
	float dcont = max(0.0, dot(norm,surf2light));
	vec3 diffuse = dcont * vec3(texture2D(texture,texCoord)) * lightDiffuse;
	
	
	vec3 surf2view = normalize(tangentSurf2view);
	vec3 reflection = reflect(-surf2light,norm);
	
	float scont = pow(max(0.0,dot(surf2view,reflection)),texture2D(specularmap,texCoord).x*50);
	vec3 specular = scont*lightSpecular*matSpecular*att;
	
	gl_FragColor = vec4(ambient+diffuse+specular,1.0);
}

Thanks in advance!



Sponsor:

#2 HappyCoder   Members   -  Reputation: 3139

Like
0Likes
Like

Posted 21 August 2014 - 02:08 PM

It appears that your lighting normal is tilted 90 degrees. Try setting the output color to be a few intermediate values, such as the surface normal, the light direction, and the texture normal. You then compare the output with what you expected the output to be.

 

 

Also, what direction in your scene is up? The y axis, or the z axis?



#3 iwoplaza   Members   -  Reputation: 174

Like
0Likes
Like

Posted 21 August 2014 - 02:28 PM

The Y axis is the up direction of my scene. 






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS