• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# [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.

2 replies to this topic

### #1iwoplaza  Members

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:

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);
}


### #2HappyCoder  Members

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?

My current game project Platform RPG

### #3iwoplaza  Members

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.