ParagonArcade

01-22-2015, 06:30 AM

Please see the example video: http://www.twitch.tv/paragonarcade/b/614469614

So I am getting these hard lines when I have point lights in my scene. I've traced the source to the specular componenet but I can seem to fixed it. In the video, I adjust the fragment codes in steps to try to show the problem:

1. The specular reflections completely turned off. So just diffuse, everything is fine.

2. The specular reflections are turned on, and hard lines appear.

3. Showing the if statement on the dot product, I make the specularColor = red for the back-side(should not be illuminated by specular comp) and = blue for front-side(should be illuminated by specular comp)

4. Specular reflections turned off on accident. wrong fragment code. same as 1.

5. Back-side colored full red, front-side specular reflection turned off.

6. Back-side colored full red, front-side specular reflection turned on.

7. The original setup with specular reflections turned off only for the front-side.

Fragment Shader

#version 440

struct Attenuation

{

float Constant;

float Linear;

float Exponent;

};

struct DirectLight

{

vec3 Color;

vec3 Direction;

float Intensity;

};

struct PointLight

{

vec3 Color;

vec3 Position;

float Intensity;

float Range;

Attenuation Atten;

};

in vec3 vPosition;

in vec4 vColor;

in vec3 vNormal;

in vec2 vTexture;

uniform mat4 projection;

uniform vec3 eyePosition;

uniform sampler2D sampler;

uniform vec3 ambientLight;

uniform vec3 diffuse_Color;

uniform float specularPower;

uniform float specularIntensity;

uniform DirectLight dLight;

uniform int NUM_POINT_LIGHTS;

uniform PointLight pLights[4];

out vec4 frag_color;

vec3 calcDirectLight(float intensity, vec3 color, vec3 direction, vec3 normal)

{

//. Calculate Diffuse Component

float diffuseFactor = max(0.0f, dot(normal, -direction));

vec3 diffuseColor = color * intensity * diffuseFactor;

//. Calculate Specular Component

float specularFactor = 0.0f;

vec3 specularColor;

if(dot(normal, direction) > 0.0f)

{

specularColor = vec3(0.0f, 0.0f, 0.0f);

}

else

{

vec3 cameraPosition = (projection * vec4(eyePosition, 1.0f)).xyz;

specularFactor = max(0.0f, dot(normalize(vPosition - cameraPosition), reflect(direction, normal)));

specularFactor = pow(specularFactor, specularPower);

}

specularColor = color * specularIntensity * specularFactor;

return diffuseColor + specularColor;

}

vec3 calcPointLight(float intensity, vec3 color, vec3 position, vec3 normal, float range, float constant, float linear, float exponent)

{

vec3 lightDir = vPosition - position;

float lightDist = length(lightDir);

if(lightDist > range)

return vec3(0.0f,0.0f,0.0f);

lightDir = normalize(lightDir);

float attenuation = 1.0f / (0.00001f + constant + linear * lightDist + exponent * lightDist * lightDist);

vec3 diffuseColor = calcDirectLight(intensity, color, lightDir, normal) * attenuation;

return diffuseColor;

}

void main()

{

vec4 tColor = texture(sampler, vTexture.st);

vec3 totalLight = ambientLight * diffuse_Color;

totalLight += calcDirectLight(dLight.Intensity, dLight.Color, dLight.Direction, vNormal);

//. Calculate Point Lights

for(int i = 0; i < NUM_POINT_LIGHTS; i++)

{

totalLight += calcPointLight(pLights[i].Intensity, pLights[i].Color, (projection * vec4(pLights[i].Position, 1.0f)).xyz, vNormal, pLights[i].Range, pLights[i].Atten.Constant, pLights[i].Atten.Linear, pLights[i].Atten.Exponent);

}

vec4 fColor = clamp(tColor * vec4(totalLight, 1.0f), 0.0, 1.0);

frag_color = fColor;

}

In the video, I am editing the calcDirectLight portion of the fragment shader and please don't response about the inefficiencies of IF statemetns. I know IF statements are expensive and I don't care right now. I just want to get it working correctly and the examples without IF statements have not been working as well for me personally.

Is the normal Phong shading behavior or what? Thanks you in advanced.

So I am getting these hard lines when I have point lights in my scene. I've traced the source to the specular componenet but I can seem to fixed it. In the video, I adjust the fragment codes in steps to try to show the problem:

1. The specular reflections completely turned off. So just diffuse, everything is fine.

2. The specular reflections are turned on, and hard lines appear.

3. Showing the if statement on the dot product, I make the specularColor = red for the back-side(should not be illuminated by specular comp) and = blue for front-side(should be illuminated by specular comp)

4. Specular reflections turned off on accident. wrong fragment code. same as 1.

5. Back-side colored full red, front-side specular reflection turned off.

6. Back-side colored full red, front-side specular reflection turned on.

7. The original setup with specular reflections turned off only for the front-side.

Fragment Shader

#version 440

struct Attenuation

{

float Constant;

float Linear;

float Exponent;

};

struct DirectLight

{

vec3 Color;

vec3 Direction;

float Intensity;

};

struct PointLight

{

vec3 Color;

vec3 Position;

float Intensity;

float Range;

Attenuation Atten;

};

in vec3 vPosition;

in vec4 vColor;

in vec3 vNormal;

in vec2 vTexture;

uniform mat4 projection;

uniform vec3 eyePosition;

uniform sampler2D sampler;

uniform vec3 ambientLight;

uniform vec3 diffuse_Color;

uniform float specularPower;

uniform float specularIntensity;

uniform DirectLight dLight;

uniform int NUM_POINT_LIGHTS;

uniform PointLight pLights[4];

out vec4 frag_color;

vec3 calcDirectLight(float intensity, vec3 color, vec3 direction, vec3 normal)

{

//. Calculate Diffuse Component

float diffuseFactor = max(0.0f, dot(normal, -direction));

vec3 diffuseColor = color * intensity * diffuseFactor;

//. Calculate Specular Component

float specularFactor = 0.0f;

vec3 specularColor;

if(dot(normal, direction) > 0.0f)

{

specularColor = vec3(0.0f, 0.0f, 0.0f);

}

else

{

vec3 cameraPosition = (projection * vec4(eyePosition, 1.0f)).xyz;

specularFactor = max(0.0f, dot(normalize(vPosition - cameraPosition), reflect(direction, normal)));

specularFactor = pow(specularFactor, specularPower);

}

specularColor = color * specularIntensity * specularFactor;

return diffuseColor + specularColor;

}

vec3 calcPointLight(float intensity, vec3 color, vec3 position, vec3 normal, float range, float constant, float linear, float exponent)

{

vec3 lightDir = vPosition - position;

float lightDist = length(lightDir);

if(lightDist > range)

return vec3(0.0f,0.0f,0.0f);

lightDir = normalize(lightDir);

float attenuation = 1.0f / (0.00001f + constant + linear * lightDist + exponent * lightDist * lightDist);

vec3 diffuseColor = calcDirectLight(intensity, color, lightDir, normal) * attenuation;

return diffuseColor;

}

void main()

{

vec4 tColor = texture(sampler, vTexture.st);

vec3 totalLight = ambientLight * diffuse_Color;

totalLight += calcDirectLight(dLight.Intensity, dLight.Color, dLight.Direction, vNormal);

//. Calculate Point Lights

for(int i = 0; i < NUM_POINT_LIGHTS; i++)

{

totalLight += calcPointLight(pLights[i].Intensity, pLights[i].Color, (projection * vec4(pLights[i].Position, 1.0f)).xyz, vNormal, pLights[i].Range, pLights[i].Atten.Constant, pLights[i].Atten.Linear, pLights[i].Atten.Exponent);

}

vec4 fColor = clamp(tColor * vec4(totalLight, 1.0f), 0.0, 1.0);

frag_color = fColor;

}

In the video, I am editing the calcDirectLight portion of the fragment shader and please don't response about the inefficiencies of IF statemetns. I know IF statements are expensive and I don't care right now. I just want to get it working correctly and the examples without IF statements have not been working as well for me personally.

Is the normal Phong shading behavior or what? Thanks you in advanced.