Jump to content
  • Advertisement
Sign in to follow this  
slicer4ever

per pixel lighting creating irregular shape?

This topic is 2226 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello everyone, i'm trying to implement per-pixel lighting for a simple scene, however, what i expect to be a circlular pattern around the light(the small green dot in the image), is instead looks like an irregular shaped diamond:

21mtctf.png

here is my vertex shader:


#version 150
struct Light{
vec3 m_Position;
vec4 m_Diffuse;
vec4 m_Ambient;
vec3 m_Attenen;
};
uniform mat4 ProjMatrix;
uniform mat4 ViewMatrix;
uniform mat4 MeshMatrix;
uniform Light Lights[1];

in vec3 i_Vertex;
in vec2 i_TexCoord;
in vec3 i_Normal;

out vec2 o_TexCoord;
out vec3 o_Normal, o_LightDir;
out float o_Distance;

void main(void){
mat4 ViewModel = ViewMatrix*MeshMatrix;
mat3 NormMatrix = transpose(inverse(mat3(ViewModel)));
o_Normal = normalize(NormMatrix*i_Normal);
vec4 v_Vertex = ViewModel*vec4(i_Vertex, 1.0f);
vec3 Aux = (ViewMatrix*vec4(Lights[0].m_Position,1.0f)).xyz-v_Vertex.xyz;
o_LightDir = normalize(Aux);
o_Distance = length(Aux);

o_TexCoord = i_TexCoord;
gl_Position = ProjMatrix*v_Vertex;
return;
}


here is my pixel shader:


#version 150
struct Light{
vec3 m_Position;
vec4 m_Diffuse;
vec4 m_Ambient;
vec3 m_Attenen;
};

uniform sampler2D Texture;
uniform vec4 g_Color;
uniform Light Lights[1];

in vec2 o_TexCoord;
in vec3 o_Normal, o_LightDir;
in float o_Distance;

out vec4 o_Color;
void main(void){
o_Color = Lights[0].m_Ambient*g_Color*texture2D(Texture, o_TexCoord);
float NdotL = max(dot(normalize(o_Normal), normalize(o_LightDir)), 0.0f);
if(NdotL>0.0f){
float Att = 1.0f/(Lights[0].m_Attenen.x+Lights[0].m_Attenen.y*o_Distance+Lights[0].m_Attenen.z*o_Distance*o_Distance);
o_Color+=Att*Lights[0].m_Diffuse*NdotL;
}
if(o_Color.a<=0.0f) discard;
o_Color.rgb*=o_Color.a;
}

Share this post


Link to post
Share on other sites
Advertisement
You are doing per-vertex distance calculations. Those should be moved to the pixel shader along with o_LightDir.

Aside from the existing outputs, you are supposed to send a view copy of the vertex positions from the vertex shader to the pixel shader in order to perform these calculations per-pixel. In order words, make v_Vertex an output.


L. Spiro

Share this post


Link to post
Share on other sites

You are doing per-vertex distance calculations. Those should be moved to the pixel shader along with o_LightDir.

Aside from the existing outputs, you are supposed to send a view copy of the vertex positions from the vertex shader to the pixel shader in order to perform these calculations per-pixel. In order words, make v_Vertex an output.


L. Spiro


ah, thanks L. Spiro, i had suspicions that might have been the issue, but wasn't certain.

one last question, is that i moved:

vec3 Aux = (ViewMatrix*vec4(Lights[0].m_Position,1.0f)).xyz-o_Vertex.xyz;

into the pixel shader, what concerns me is doing a matrix multiplication in the pixel shader, however, since both are uniforms, i'm assuming that the driver well probably optimize it out, but would it be better to multiply my light before submitting it?

Share this post


Link to post
Share on other sites
You should pre-multiply light values in every case, but especially so if you are doing per-pixel lighting. The light position, direction, etc., should all be in world-view space before reaching the shaders.


L. Spiro

Share this post


Link to post
Share on other sites

You should pre-multiply light values in every case, but especially so if you are doing per-pixel lighting. The light position, direction, etc., should all be in world-view space before reaching the shaders.


L. Spiro


alright, thanks for the info=-)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!