Specular light erratic in OpenGL - opengl

I am working on a shader where the fragment shader should work in the tangent space. It works just as expected for both the ambient and diffuse light, but the specular light is just plain weird. It seems that nearby fragments can have a lot or no light with no obvious reasons.
The vertex shader is:
#version 330 core
layout (location = 0) in vec3 inVertex;
layout (location = 1) in vec3 inNormal;
layout (location = 2) in vec2 inTexture;
layout (location = 3) in vec3 inTangent;
layout (location = 4) in vec3 inBitangent;
out vec3 FragmentPosition;
out vec2 TextureCoordinate;
out vec3 TangentLightDirection;
out vec3 TangentViewPosition;
out vec3 TangentFragmentPosition;
void main()
{
FragmentPosition = vec3(inVertex);
vec3 normal = normalize(inNormal);
gl_Position = vec4( inVertex, 1 );
TextureCoordinate = inTexture;
vec3 tangent = normalize(inTangent);
vec3 biTangent = normalize(inBitangent);
mat3 toTangentSpaceTransformation = transpose(mat3(tangent,biTangent,normal));
TangentFragmentPosition = toTangentSpaceTransformation * FragmentPosition;
TangentLightPosition = toTangentSpaceTransformation * vec3(0,1,1);
TangentFragmentPosition = toTangentSpaceTransformation * vec3(0,0,3);
}
And the fragment shader is:
#version 330 core
out vec4 FragColor;
in vec3 FragmentPosition;
in vec2 TextureCoordinate;
in vec3 TangentLightDirection;
in vec3 TangentViewPosition;
in vec3 TangentFragmentPosition;
uniform sampler2D Texture;
uniform sampler2D normalTexture;
void main() {
vec3 normal = vec3(0,0,1);
float shininess = 4;
vec3 phongVector = vec3(0.3,0.7,1);
vec4 color = texture(Texture,TextureCoordinate);
vec4 ambientLightColor = vec4(1,1,1,1);//vec4(normalOffset,1);
// Calculation of ambient light
vec4 sunLightColor = vec4(1,1,1,1);
vec3 sunLightDirection = normalize(TangentLightPosition);
vec4 ambientLight = phongVector[0] * ambientLightColor;
// Calculation of diffuse light
float diffuseConst = max(dot(normal,sunLightDirection),0.0);
vec4 diffuseLight = phongVector[1] * diffuseConst * sunLightColor;
// Calculation of specular light
vec3 viewDirection = normalize(TangentViewPosition - TangentFragmentPosition);
vec3 reflectionDirection = reflect(-sunLightDirection,normal);
float spec = pow(max(dot(reflectionDirection,viewDirection),0),shininess);
vec4 specularLight = phongVector[2] * spec * sunLightColor;
FragColor = (specularLight)*color;
}

It was a typo. tangentFragmentPosition was initialized twice, while tangentViewPosition was not initialized at all. Initizalizing tangentViewPosition gave the desired result.

Related

Mixing colours in OpenGL GLSL

I am trying to create a translucent , stained glass effect in OpenGL, using C++.
I use a normal map generated from a perlin noise texture. Refraction is calculated with respect to manipulated normal vectors and it works.
For stained glass, I am trying mixing the colour from skybox through refraction, with another colour. I tried this with a constant value, and that works too.
When I try to get the second colour from a texture, I do not see the object on screen. I can load the texture if i ignore refraction and mixing though.
here is my vertex shader :
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoord;
layout (location = 3) in vec3 aTangent;
layout (location = 4) in vec3 aBitangent;
out vec3 FragPos;
out vec2 texCoord;
out vec3 tangentLightPos;
out vec3 tangentViewPos;
out vec3 tangentFragPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
//normal
uniform vec3 lightPos;
uniform vec3 viewPos;
void main()
{
texCoord = aTexCoord;
FragPos = vec3(view * model * vec4(aPos, 1.0));
mat3 normalMatrix = transpose(inverse(mat3(model)));
vec3 T = normalize(normalMatrix * aTangent);
vec3 N = normalize(normalMatrix * aNormal);
T = normalize(T - dot(T, N) * N);
vec3 B = cross(N, T);
mat3 TBN = transpose(mat3(T, B, N));
tangentLightPos = TBN * lightPos;
tangentViewPos = TBN * viewPos;
tangentFragPos = TBN * FragPos;
gl_Position = projection * view * model * vec4(aPos, 1.0);
}
and my fragment shader :
#version 330 core
out vec4 FragColor;
in vec3 FragPos;
in vec2 texCoord;
in vec3 tangentLightPos;
in vec3 tangentViewPos;
in vec3 tangentFragPos;
uniform sampler2D texture1;
uniform sampler2D texture2;
uniform samplerCube skybox;
uniform vec3 cameraPos;
void main()
{
//normal map - normal
vec3 normalDir = texture(texture2, texCoord).rgb;
normalDir = normalize(normalDir * 2.0 - 1.0);
vec3 Incident = normalize(tangentFragPos - cameraPos);
vec3 RefractedDir = refract(Incident, normalDir, 1.0/1.2);
float DispersionIndex = 1.0 - dot(Incident, normalDir);
vec3 RefractedFragColor = vec3(texture(skybox, RefractedDir).rgb);
//FIXME:
vec3 texColor = texture(texture1, texCoord).rgb;
vec3 constTexColor = vec3(1.0, 1.0, 1.0);
FragColor = mix( vec4(RefractedFragColor, 1.0), vec4(texColor.rgb, 0.5), 0.2); // doesnt show object on screen
// FragColor = mix( vec4(RefractedFragColor, 1.0), vec4(constTexColor.rgb, 0.5), 0.2); --> works , shows a tint on glass
// FragColor = vec4(texColor, 1.0); ---> shows only texture
}
with constant value for color in mix :
with only texture:
Why does the mix not work with color from texture?

Phong lighting leaves stripes pattern

I am learning lighting on OpenGL, in this example, I used the phong method. Everything seems to be working as intended, but I notice that this method leaves some "stripes", especially in the diffuse part of the resulting color:
Vertex shader:
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 normal;
struct Light{
vec3 position;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
uniform mat4 Model;
uniform mat4 View;
uniform mat4 Projection;
uniform Light light;
out vec3 aNormal;
out vec3 FragPos;
out vec3 LightPos;
//Usually you would transform the input into coordinates that fall within OpenGL's visible region
void main()
{
aNormal = mat3(transpose(inverse(View * Model))) * normal;
FragPos = vec3(View * Model * vec4(aPos,1.0));
gl_Position = Projection * vec4(FragPos,1.0);
LightPos = vec3(View * vec4(light.position,1.0));
}
#version 330 core
out vec4 FragColor;
in vec3 aNormal;
in vec3 FragPos;
in vec3 LightPos;
struct Material{
vec3 ambient;
vec3 diffuse;
vec3 specular;
float shininess;
};
struct Light{
vec3 position;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
uniform Material material;
uniform Light light;
precision highp float;
void main()
{
vec3 ambient = material.ambient * light.ambient;
vec3 norm = normalize(aNormal);
//Difuse
vec3 lightDir = normalize(LightPos - FragPos);
float diff = max(dot(norm,lightDir),0.0);
vec3 diffuse = (diff * material.diffuse) * light.diffuse;
vec3 specular = vec3(0);
//Specular
if(diff > 0){
vec3 viewDir = normalize(-FragPos);
vec3 reflectDir = reflect(-lightDir,norm);
float spec = pow(max(dot(viewDir,reflectDir),0.0), material.shininess);
specular = light.specular * (spec * material.specular);
}
vec3 result = diffuse + (ambient + specular);
FragColor = vec4(result, 1.0);
}
Fragment shader:
I was wondering why that happens, I have a suspicion that is related to float precision, however I tried to use precision highp float; and it had no effect. What is causing this? Is there a way to fix it?

OpenGL returns -1 for an uniform that does exist and is used

I have this vertex shader:
#version 430 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec2 textureCoordinate;
layout (location = 2) in vec3 normal;
layout (location = 4) in vec3 tangent;
uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;
uniform vec3 lightPosition = vec3(0.0, 1.0, 0.0);
out vec2 texCoord;
out vec3 lightDirection;
out vec3 eyeDirection;
void main() {
vec4 P = modelViewMatrix * vec4(position, 1.0);
vec3 N = normalize(mat3(modelViewMatrix) * normal);
vec3 T = normalize(mat3(modelViewMatrix) * tangent);
vec3 B = cross(N, T);
vec3 L = lightPosition - P.xyz;
vec3 V = -P.xyz;
lightDirection = normalize(vec3(dot(V, T), dot(V, B), dot(V, N)));
eyeDirection = normalize(vec3(dot(V, T), dot(V, B), dot(V, N)));
texCoord = textureCoordinate;
gl_Position = projectionMatrix * P;
}
And this fragment shader:
#version 430 core
in vec2 texCoord;
in vec3 lightDirection;
in vec3 eyeDirection;
layout (binding = 0) uniform sampler2D tex;
layout (binding = 1) uniform sampler2D normalMap;
out vec4 color;
void main() {
vec3 ambient = vec3(0.1);
vec3 V = normalize(eyeDirection);
vec3 L = normalize(lightDirection);
vec3 N = normalize(texture(normalMap, texCoord)).rgb * 2.0 - vec3(1.0);
vec3 R = reflect(-L, N);
vec3 diffuseAlbedo = texture(tex, texCoord).rgb;
/*vec3 diffuseAlbedo = vec3(1.0);*/
vec3 diffuse = max(dot(N, L), 0.0) * diffuseAlbedo;
vec3 specular = vec3(0.0);
color = vec4(ambient + diffuse + specular, 1.0);
}
As you can see, the variable lightPosition is actually used in the calculations. But when I want to get the location of it via glGetUniformLocation, I get -1.
I looked through the other questions here on SO, like those ones:
glGetActiveUniform reports uniform exists, but glGetUniformLocation returns -1
glGetUniformLocation return -1 on nvidia cards
glGetUniformLocation() returning -1 even though used in vertex shader
Especially the last one - but in my case, lightPosition is used to calculate lightDirection and this is then used in the fragment shader. So it should not be removed.
Any ideas on what is going on here? Or how to debug this?
Also: I set a default value for the uniform. If I remove the default value, it still gives -1.
I am running Ubuntu 15.10 with Nvidia 340.96 drivers and a GF 710M card. I am running OpenGL 4.4 with core profile.
... As you can see, the variable lightPosition is actually used in the calculations. ...
You would think, right. Actually you're using it once in the Vertex Shader to assign a value to vec3 L, which is not actually used after that.
The compilation process for the shaders is very meticulous. You may have used it in a calculation in the Vertex Shader, but you didn't utilize the result L anywhere and it doesn't make any contribution to the final result calculated in the Fragment Shader, so it was "optimized away".
You must give some use to L.

Bump Mapping black fragments

Currently i try to implement Bump Mapping for my OpenGL Project.
The Problem is that same parts of my cube is black. Like shown in this picture :
I am almost certain that i just dont understand how the shaders works, so i used the shader from OpenGL Superbible .
Here is my code :
Vertex Shader
#version 330
layout(location = 0) in vec4 position;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec2 texCoord;
layout(location = 3) in vec3 tangent;
layout(location = 4) in vec3 bitangent;
uniform mat4 matModel;
uniform mat4 matNormal;
uniform mat4 matMVP;
uniform mat4 matMV;
uniform vec3 light_pos = vec3(0.0,0.0,100.0);
out VS_OUT{
vec3 eyeDir;
vec3 lightDir;
vec2 fragTexCoord;
} vs_out;
void main()
{
vec4 P = matMV*position;
vec3 V = P.xyz;
vec3 L = normalize(light_pos - P.xyz);
vec3 N = normalize(mat3(matNormal)*normal);
vec3 T = normalize(mat3(matNormal)*tangent);
vec3 B = cross(N,T);
vs_out.lightDir = normalize(vec3(dot(L,T),
dot(L,B),
dot(L,N)));
V = -P.xyz;
vs_out.eyeDir = normalize(vec3(dot(V,T),
dot(V,B),
dot(V,N)));
vs_out.fragTexCoord = texCoord;
gl_Position = matMVP * position;
}
And the fragment shader :
#version 330
uniform sampler2D diffuseTex;
uniform sampler2D heightTex;
uniform vec3 heightColor;
in vec3 fragNormal;
in vec2 fragTexCoord;
in vec3 tangent_out_normalized;
in vec3 bitangent_out;
in vec3 normal_out_normalized;
in VS_OUT{
vec3 eyeDir;
vec3 lightDir;
vec2 fragTexCoord;
}fs_in;
out vec4 outputColor;
void main()
{
vec3 V = normalize(fs_in.eyeDir);
vec3 L = normalize(fs_in.lightDir);
vec3 N = normalize(texture(heightTex,fs_in.fragTexCoord).rgb*2-vec3(1.0));
vec3 R = reflect(-L,N);
vec3 diffuse_albedo = texture(diffuseTex,fs_in.fragTexCoord).rgb;
vec3 diffuse =max(dot(N,L),0)*diffuse_albedo;
vec3 specular_albedo = vec3(1.0);
vec3 specular = max(pow(dot(R,V),25.0),0.0)*specular_albedo;
outputColor = vec4(diffuse+specular,1);
}
What am i missing?
It seems very wrong that you don't use fragNormal anywhere. You should use it to rotate the texture normal. To make it obvious, if the bump is flat you should still get the usual surface lighting.
The next strange thing is that you need to multiply you bump normal by 2 and subtract {1,1,1}. The normal should never flip and I suspect this is going on in your case. When it flips you will suddenly go from in light to in shadow, and that might cause the black areas.

opengl shader directional lights specular reflection increasing with distance

the title says it all.. using opengls built in lighting system, specularlight does not increase or decrease with distance from the object, but by shader implementation does.
Vertex Shader:
#version 330
layout (location = 0) in vec3 position;
layout (location = 1) in vec2 texCoord;
layout (location = 2) in vec3 normal;
out vec2 texCoord0;
out vec3 normal0;
out vec3 worldPos0;
uniform mat4 transform;
uniform mat4 normalRotation;
uniform mat4 transformProjected;
void main()
{
gl_Position = transformProjected * vec4(position, 1.0);
texCoord0 = texCoord;
normal0 = normalize((normalRotation * vec4(normal, 0.0))).xyz;
worldPos0 = (transform * vec4(position, 1.0)).xyz;
}
Fragment Shader:
#version 330
in vec2 texCoord0;
in vec3 normal0;
in vec3 worldPos0;
out vec4 fragColor;
struct BaseLight
{
vec3 colorDiffuse;
vec3 colorSpecular;
float intensityDiffuse;
};
struct DirectionalLight
{
BaseLight base;
vec3 direction;
};
uniform vec3 tint;
uniform sampler2D sampler;
uniform vec3 eyePos; // camera pos
uniform vec3 ambientLight;
uniform vec3 emissiveLight;
//material
uniform float specularIntensity;
uniform float specularPower;
uniform DirectionalLight directionalLight;
vec4 calcLight(BaseLight base,vec3 direction, vec3 normal)
{
float diffuseFactor = dot(normal, -direction);
vec4 diffuseColorFinal = vec4(0,0,0,0);
vec4 specularColorFinal = vec4(0,0,0,0);
if(diffuseFactor > 0)
{
diffuseColorFinal = vec4(base.colorDiffuse,1) * diffuseFactor * base.intensityDiffuse;
vec3 directionToEye = normalize(eyePos - worldPos0);
vec3 reflectDirection = normalize(reflect(direction, normal));
float specularFactor = dot(directionToEye, reflectDirection);
specularFactor = pow(specularFactor, specularPower);
if(specularFactor > 0)
specularColorFinal = vec4(base.colorSpecular,1) * specularFactor * specularIntensity;
}
//
return diffuseColorFinal + specularColorFinal;
}
void main()
{
vec4 colorD = texture(sampler, texCoord0.xy) * vec4(tint,1);
vec3 normal = normal0;
vec4 totalLight = vec4(ambientLight,1) + vec4(emissiveLight,1);
totalLight += calcLight(directionalLight.base,-directionalLight.direction,normal);
fragColor = colorD * totalLight;
}
As you can see from the 2 images the specular light takes up a larger surface area the farther the camera gets from the plane.In my test with opengls built in lighting, this doesnt happen. is there a way to fix this? im new to lighting, maybe this is normal for directional light sources? thanks for the help!
Im also setting my eyePos uniform to my cameraPos. i dont know if that helps.
Basically you need to have distance between the fragment and the light dist . This can be a problem for directional light though because you have only the direction and distant is assumed to be infinite. Maybe switch to point light?
when youo have the 'dist' you use a formula
att = 1.0 / (Kc + Kl*dist + Kq*dist^2)
Kc - constant attenuation
Kl - linear attenuation
Kq - quadratic attenuation
simpler version (only Kq used, rest set to 1.0):
float attenuation = 1.0 / (1.0 + light.attenuation * pow(distanceToLight, 2));
then in the lighting equation you basically multiply calculated color by this att factor:
vec4 finalColor = ambient + (diffuseColorFinal + specularColorFinal)*att
http://www.ozone3d.net/tutorials/glsl_lighting_phong_p4.php#part_4
http://tomdalling.com/blog/modern-opengl/07-more-lighting-ambient-specular-attenuation-gamma/