OpenGL shadow map not render over cast shadow objects - c++

In my shadow map implementation for directional lights I have a collection of items which I iterate them to render their depth to a depth buffer then iterate them again to render them normally with the generated shadow map, each item has a bool property called castShadow which I test if true add it to the depth buffer, the problem is if the item cast shadow and rendered to the depth buffer it can't receive shadows at all, once I set the to false then it is not rendered as depth it start to receive shadows without problem.
so in render function first I render the depth buffer as following:
When the object cast shadow it doesn't receive shadows
When the object isn't cast shadow it receives shadows
void Engine::renderDepthMaps() {
if (sun != nullptr) {
if (sun->castShadow) {
glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
glClear(GL_DEPTH_BUFFER_BIT);
glm::vec3 pos = -500.0f * sun->direction;
glm::mat4 lightSpaceProjection = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, -10.0f, 1000.0f);
glm::mat4 lightSpaceView = glm::lookAt(pos, player->getPosition(), glm::vec3(0, 1, 0));
lightSpaceMatrix = lightSpaceProjection * lightSpaceView;
Shader& shader = shaders.getShader(DEPTH);
shader.use();
shaders.setDepthLightSpaceUniform(shader, lightSpaceMatrix);
for (item_it it = engineItems.begin(); it != engineItems.end(); ++it) {
if (it->get()->castShadow)
it->get()->renderDepth(deltaFirst);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
}
}
and the render function is:
void Engine::renderMeshes() {
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Shader& basicShader = shaders.getShader(BASIC);
basicShader.use();
sun->setUniforms(basicShader.getProgramID(), "directLights");
shaders.setLightSpaceUniform(basicShader, sun, lightSpaceMatrix, depthMap);
shaders.setShaderViewUniforms(basicShader, camera);
terrain->render(basicShader, deltaFirst);
for (item_it it = basicItems.begin(); it != basicItems.end(); ++it) {
if (it->get()->type != "terrain")
it->get()->render(basicShader, deltaFirst);
}
}
and the shaders are:
vertex:
#version 300 es
precision highp float;
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 texCoord;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform mat4 lightSpaceMatrix;
uniform mat3 normalMatrix;
out vec4 FragPosLightSpace;
out vec2 TexCoords;
out vec3 Normal;
out vec3 viewPos;
out vec3 fragPos;
void main(){
TexCoords = texCoord;
Normal=normalMatrix*normal;
fragPos = vec3(model * vec4(position,1.0f));
FragPosLightSpace =lightSpaceMatrix * model * vec4(position,1.0f);
gl_Position = projection * view * model * vec4(position,1.0f);
}
fragment:
#version 300 es
precision highp float;
out vec4 glFragColor;
vec2 poissonDisk[4] = vec2[](
vec2( -0.94201624, -0.39906216 ),
vec2( 0.94558609, -0.76890725 ),
vec2( -0.094184101, -0.92938870 ),
vec2( 0.34495938, 0.29387760 )
);
struct DirectLight {
vec3 direction;
vec3 color;
float intensity;
};
in vec3 Normal;
in vec2 TexCoords;
in vec3 fragPos;
in vec4 FragPosLightSpace;
uniform int has_texture;
uniform vec3 matDiffuse;
uniform sampler2D shadowMap;
uniform sampler2D mat_diffuse;
uniform sampler2D mat_specular;
uniform vec3 matSpecular;
uniform float shininess;
uniform DirectLight sun;
uniform int castShadow;
uniform vec3 viewPos;
void main(){
vec3 tex=vec3(1),spe=vec3(1);
if(has_texture==1){
tex=vec3(texture(mat_diffuse, TexCoords));
spe=vec3(texture(mat_specular,TexCoords));
}
vec3 diffColor = matDiffuse * tex;
vec3 specColor = matSpecular * spe;
vec3 ambient = vec3(0.4,0.4,0.4)*diffColor;
vec3 lightDir = normalize(-sun.direction);
float diff = max(dot(Normal,lightDir), 0.0);
vec3 diffuse = sun.color * diff * diffColor;
vec3 viewDir = normalize(viewPos - fragPos);
vec3 reflectDir = reflect(-lightDir, Normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess);
vec3 specular = spec * specColor * sun.color;
vec3 color = ambient + diffuse + specular;
float gamma = 2.2;
color.rgb = pow(color.rgb, vec3(1.0/gamma));
if(castShadow==1){
vec3 projCoords = FragPosLightSpace.xyz / FragPosLightSpace.w;
projCoords = projCoords * 0.5 + 0.5;
float shadow = 1.0;
for (int i=0;i<4;i++){
shadow -= 0.2*(1.0-texture( shadowMap, projCoords.xy + poissonDisk[i]/700.0).r);
}
if(projCoords.z > 1.0)
shadow = 0.0;
color *=shadow;
}
glFragColor=vec4(color,1.0f);
}
Another problem: the light space perspective matrix is glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, -10.0f, 1000.0f) and the shadow map size is 1024, when an object is outside that matrix it create very long shadow and is not in the correct direction as shown in the following image:

Related

Trying to implement shadow using OpenGL; depth texture seems correct but shadow is not displaying

trying to implement shadow. I checked my depth texture on a quad, and it seems correct, but the shadow is not displaying. I check my shadow vertex and fragment shaders, and I believe I have done the light space transformation correctly.
Here are my code.
directional light source matrix setup:
//light source states
glm::vec3 Window::lightColor = glm::vec3(0.9f, 0.9f, 0.9f);
glm::vec3 Window::lightDir = glm::vec3(-1.f, -1.f, 0.f);
glm::mat4 Window::lightView = glm::lookAt(glm::vec3(0.f) - glm::normalize(lightDir) * 15.f, glm::vec3(0.0f), glm::vec3(0.f, 1.f, 0.f));
float Window::near_plane = 0.01f;
float Window::far_plane = 50.1f;
float camWidth = 10.f;
glm::mat4 Window::lightProj = glm::ortho(-10.f, 10.f, -10.f, 10.f, Window::near_plane, Window::far_plane);
glm::mat4 Window::lightProjView = lightProj * lightView;
shadow drawing logic:
void Renderer::drawWithShadow(Object* obj) {
//set shader uniforms
Shader* shader = shadowShader;
shader->bind();
shader->setUniformMat4("model", obj->model);
shader->setUniformMat4("projView", projView);
shader->setUniformVec3("viewPos", eyePos);
//need another projection matrix
shader->setUniformMat4("lightSpaceMatrix", shadowProjView);
glcheck(glActiveTexture(GL_TEXTURE0));
glcheck(glBindTexture(GL_TEXTURE_2D, textID));
//light uniforms
shader->setUniformVec3("directionalLightDir", directionalLightDir);
shader->setUniformVec3("lightColor", lightColor);
glcheck(glBindVertexArray(obj->vao));
for (auto i = 0; i < obj->meshList.size(); i++) {
Mesh* mesh = obj->meshList[i];
prepMaterial(mesh->material, shader);
glcheck(glDrawElements(GL_TRIANGLES, mesh->size, GL_UNSIGNED_INT, (GLvoid*)(sizeof(GLuint) * mesh->vertexOffset)));
}
}
vert and frag shaders to prepare shadow depth textures
//vertex shader
#version 330 core
layout (location = 0) in vec3 position;
uniform mat4 projView;
uniform mat4 model;
void main() {
gl_Position = projView * model * vec4(position, 1.0);
}
//fragment shader
#version 330 core
void main()
{
}
vert and frag shaders to draw shadows with Phong lighting
//vertex shader
#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 texCoord;
out VS_OUT {
vec4 fragPos;
vec3 normal;
vec2 texCoord;
vec4 fragPosLightSpace;
} vs_out;
uniform mat4 projView;
uniform mat4 model;
uniform mat4 lightSpaceMatrix;
void main()
{
vs_out.fragPos = model * vec4(position, 1.0);
vs_out.normal = transpose(inverse(mat3(model))) * normal;
vs_out.texCoord = texCoord;
vs_out.fragPosLightSpace = lightSpaceMatrix * vs_out.fragPos;
gl_Position = projView * vs_out.fragPos;
}
//fragment shader
#version 330 core
uniform vec3 viewPos; //just the eye pos
uniform vec3 diffuseFactor; //kd
uniform vec3 ambientColor; //ka
uniform vec3 specColor; //ks
uniform float specHighlight; //ns, the larger this value is, the more apparent the light dot on the surface
uniform float dissolve; //d
//lights
uniform vec3 directionalLightDir;
uniform vec3 pointLightPos;
uniform vec3 lightColor;
uniform sampler2D shadowMap;
//uniform sampler2DShadow shadowMap;
in VS_OUT {
vec4 fragPos;
vec3 normal;
vec2 texCoord;
vec4 fragPosLightSpace;
} fs_in;
out vec4 fragColor;
float ShadowCalculation(vec4 fragPosLightSpace)
{
vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
vec2 shadowCoords;
shadowCoords.x = projCoords.x * 0.5 + 0.5;
shadowCoords.y = projCoords.y * 0.5 + 0.5;
float closestDepth = texture(shadowMap, shadowCoords).r;
float currentDepth = projCoords.z * 0.5 + 0.5;
float shadowValue = currentDepth + 0.00001 > closestDepth ? 1.0 : 0.0;
//if(currentDepth < 0.0)
//shadowValue = 0.0;
return shadowValue;
}
void main()
{
vec3 lightDir = normalize(-directionalLightDir);
vec3 norm = normalize(fs_in.normal);
//diffuse lighting
float diffStrength = max(dot(norm, lightDir), 0.0); // this calculates diffuse intensity based on angle
vec3 diffuse = lightColor * diffStrength * diffuseFactor;
//specular
vec3 viewDir = normalize(viewPos - fs_in.fragPos.xyz);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = 0.0;
if(specHighlight > 0.0) { // if specHighlight is < 0, pow might produce undefined result if base is also 0
spec = pow(max(dot(viewDir, reflectDir), 0.0), specHighlight);
}
vec3 specular = spec * specColor * lightColor;
float shadow = ShadowCalculation(fs_in.fragPosLightSpace);
//float shadow = textureProj(shadowMap, fs_in.fragPosLightSpace);
//vec3 result = ambientColor * 0.05 * lightColor + (diffuse + specular)*(1-shadow);
vec3 result = (diffuse + specular)*(1.0 - shadow);
fragColor = vec4(result, 1);
}
with just Phong shading, the scene looks like this:
Phong shading
when the scene is seen from the light source as depth value:
depth texture on quad
when I finally render the scene, it is mostly black; I made sure the far plane covers all of the bunnies:
render shadow

Omnidirectional shadows for directional light

when using a shadow map, the light projection (orthogonal) is used in the following way (similarly for other planets):
const glm::mat4 lightProjection = glm::ortho(-saturn->GetRadius() * 3.0f, saturn->GetRadius() * 3.0f, -saturn->GetRadius() * 3.0f, saturn->GetRadius() * 3.0f, camera.GetNear(), camera.GetFar());
const glm::mat4 lightView = glm::lookAt(_sun->GetPosition(), saturn->GetPosition(), glm::vec3(0.0, 1.0, 0.0));
const glm::mat4 lightSpaceMatrix = lightProjection * lightView;
saturn->SetLightSpaceMatrix(lightSpaceMatrix);
While rendering the planets, I change the lightSpaceMatrix, trying to recreate the pseudo omnidirectional light (in certain directions) in the following way:
void Application::RenderPlanetsAndSatellites(const Shader& shader) {
shader.Use();
for (const auto& renderableComponentPS : _renderableComponentsPS) {
shader.SetMat4("lightSpaceMatrix", renderableComponentPS.planet->GetLightSpaceMatrix());
...
renderableComponentPS.planet->SetShader(shader);
renderableComponentPS.planet->AdjustToParent(isTimeRun);
renderableComponentPS.planet->Render();
for (const auto& satellite : renderableComponentPS.satellites) {
satellite->SetShader(shader);
satellite->AdjustToParent(isTimeRun);
satellite->Render();
}
}
}
Vertex shader:
#version 460 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;
layout (location = 3) in vec3 aTangent;
layout (location = 4) in vec3 aBitangent;
out VS_OUT {
vec3 FragPos;
vec2 TexCoords;
vec3 TangentLightPos;
vec3 TangentViewPos;
vec3 TangentFragPos;
vec4 FragPosLightSpace;
} vs_out;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
uniform mat4 lightSpaceMatrix;
uniform vec3 lightPos;
uniform vec3 viewPos;
uniform float zCoef; // For log z-buffer (2.0 / log2(farPlane + 1.0))
void main() {
vs_out.FragPos = vec3(model * vec4(aPos, 1.0));
vs_out.TexCoords = aTexCoords;
mat3 normalMatrix = mat3(transpose(inverse(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));
vs_out.TangentLightPos = TBN * lightPos;
vs_out.TangentViewPos = TBN * viewPos;
vs_out.TangentFragPos = TBN * vs_out.FragPos;
vs_out.FragPosLightSpace = lightSpaceMatrix * vec4(vs_out.FragPos, 1.0);
gl_Position = projection * view * vec4(vs_out.FragPos, 1.0f);
// Log z-buffer [логарифмический z-буфер]
gl_Position.z = log2(max(1e-6, gl_Position.w + 1.0)) * zCoef - 1.0;
gl_Position.z *= gl_Position.w;
}
Shadow calculation in fragment shader:
float CalculateShadow(vec4 fragPosLightSpace) {
// Perform perspective divide
vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
// Transform to [0,1] range
projCoords = projCoords * 0.5 + 0.5;
// Get closest depth value from light's perspective (using [0,1] range fragPosLight as coords)
float closestDepth = texture(shadowMap, projCoords.xy).r;
// Get depth of current fragment from light's perspective
float currentDepth = projCoords.z;
vec3 lightDir = lightPos - fs_in.FragPos;
vec3 lightDirNorm = normalize(lightDir);
float shadow;
ApplyPCF(shadow, projCoords, currentDepth);
return shadow;
}
But for some reason this does not work. The planet that is closest to the sun begins to seem to cover all other planets:
So guys, I solved the problem...
The idea is to render a scene component (planet, its satellites, rings, etc.) into a shadow map, then immediately into a regular buffer, and then clear the shadow map (depth buffer) so that the planet closest to the sun does not cover all the others, etc.
Cleaning is necessary because only one shadow map is used, and without cleaning, the depth will be overlapped by objects from all over the scene.
Thus, by changing the lightSpaceMatrix, it can be created the impression that an omnidirectional light source is used in the scene.

Don't think my scene is lit properly? opengl

i'm trying to implement a lighting in my vertex shader however, there doesn't seem to be much change on the vertex's colour.
#version 330
structured layout for vertex attributes
layout (location = 0 ) in vec3 vertex_position;
layout (location = 1 ) in vec3 vertex_color;
layout (location = 2 ) in vec3 vertex_normal;
/// uniform variables
uniform mat4 ModelMatrix;
uniform mat4 ProjectionMatrix;
uniform mat4 ViewMatrix;
uniform mat4 NormalMatrix;
uniform vec3 AmbientFinal;
///struct to handle light info and material info
struct lightInfo
{
vec3 LightPosition;
vec3 ambientLightInt;
vec3 diffuseLightInt;
vec3 SpecularLightInt;
};
uniform lightInfo lights;
struct MaterialInfo
{
vec3 ka;
vec3 kd;
vec3 ks;
float Shineness;
};
uniform MaterialInfo material;
out vec3 colour;
lighting calculations in main
void main()
{
///light position in eye coordinates
vec4 lighteye = ViewMatrix * ModelMatrix * vec4(lights.LightPosition, 1.0f);
vec3 tnorm = normalize(mat3(NormalMatrix) * vertex_normal);
///position in eyecoords
vec4 eyeCoords = ViewMatrix*ModelMatrix* vec4(vertex_position, 1.f);
vec3 s = normalize(vec3(lighteye - eyeCoords));
vec3 v = normalize(-eyeCoords.xyz);
vec3 r = reflect(-s, tnorm);
vec3 ambient = material.ka * lights.ambientLightInt;
float sdotn = max(dot(s, tnorm),0.0);
vec3 diffuse = lights.diffuseLightInt * material.kd * sdotn;
vec3 spec = vec3(0.0);
if(sdotn > 0)
spec = lights.SpecularLightInt * material.ks * pow (max(dot(r,v), 0.0), material.Shineness);
gl_Position = ProjectionMatrix *ViewMatrix*ModelMatrix* vec4(vertex_position, 1.f);
colour = (diffuse + ambient + spec) + vertex_color;
}
produced output
ouput without lighting calculations
These are the variables
///lights and Mats
lightInfo lights;
lights.LightPosition = glm::vec3(0.f, 0.f, 2.f);
lights.ambientLightInt = glm::vec3(0.1f, 0.1f, 0.1f);
lights.diffuseLightInt = glm::vec3(1.f);
lights.SpecularLightInt = glm::vec3(1.f);
MaterialInfo mat;
mat.ka = glm::vec3(0.1f, 0.1f, 0.1f);
mat.kd = glm::vec3(0.5f, 0.5f, 0.5f);
mat.ks = glm::vec3(0.5f, 0.5f, 0.5f);
mat.Shineness = 30.f;
Prehaps my understanding of lighting is wrong? i don't get why the 3d object isn't really being affected by the lighting calculations

Directional light in worldSpace is dependent on viewMatrix

I try to get my glsl shader to calculate a directional light for me but I run into the problem that the direction seems to be dependend on the viewMatrix while I want to specify it in worldSpace.
My initial idea was to just multipy the worldSpace Vector with the viewMatrix(
Vector4f dir = new Vector4f(dirLight.direction, 1);
dir.mul(window.getCamera().viewMatrix);
) in the code before setting the direction uniform but it seems that the light still changes depended on my viewMatrix, so I obviously do something wrong.
the relevant code of my shader:
//vertex shader
layout (location =0) in vec3 position;
layout (location =1) in vec2 texCoord;
layout (location =2) in vec3 vertexNormal;
layout (location=3) in vec4 jointWeights;
layout (location=4) in ivec4 jointIndices;
out vec3 gmvVertexNormal;
out vec3 gmvVertexPos;
uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;
struct Material
{
vec3 color;
int hasTexture;
float reflectance;
};
uniform Material material;
void main()
{
vec4 mvPos = modelViewMatrix * vec4(position, 1.0);
gl_Position = vec4(position,1.0);
gmvVertexNormal = normalize(modelViewMatrix * vec4(vertexNormal, 0.0)).xyz;
gmvVertexPos = position;
}
//geometry shader
layout ( triangles ) in;
layout ( triangle_strip, max_vertices = 3) out;
uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;
out vec3 mvVertexNormal;
out vec3 mvVertexPos;
in vec3 gmvVertexNormal[3];
in vec3 gmvVertexPos[3];
vec3 calculateTriangleNormal(){
vec3 tangent = gl_in[1].gl_Position.xyz - gl_in[0].gl_Position.xyz;
vec3 bitangent = gl_in[2].gl_Position.xyz - gl_in[0].gl_Position.xyz;
vec3 normal = cross(tangent, bitangent);
return normalize(normal);
}
void main()
{
vec4 mvPos = modelViewMatrix * vec4(gmvVertexPos[0], 1.0);
gl_Position = projectionMatrix * mvPos;
mvVertexNormal=calculateTriangleNormal();
mvVertexPos=mvPos.xyz;
EmitVertex();
mvPos = modelViewMatrix * vec4(gmvVertexPos[1], 1.0);
gl_Position = projectionMatrix * mvPos;
mvVertexNormal=calculateTriangleNormal();
mvVertexPos=mvPos.xyz;
EmitVertex();
mvPos = modelViewMatrix * vec4(gmvVertexPos[2], 1.0);
gl_Position = projectionMatrix * mvPos;
mvVertexNormal=calculateTriangleNormal();
mvVertexPos=mvPos.xyz;
EmitVertex();
EndPrimitive();
}
//fragment shader
in vec3 mvVertexNormal;
in vec3 mvVertexPos;
struct DirectionalLight {
vec3 color;
vec3 direction;
float intensity;
};
const int MAX_DIRECTIONALLIGHT = 10;
uniform int USED_DIRECTIONALLIGHTS;
uniform DirectionalLight directionalLight[MAX_DIRECTIONALLIGHT];
vec4 calcDirectionalLight(DirectionalLight light, vec3 position, vec3 normal)
{
return calcLightColor(light.color, light.intensity, position, normalize(light.direction), normal);
}
vec4 calcLightColor(vec3 light_color, float light_intensity, vec3 position, vec3 to_light_dir, vec3 normal)
{
vec4 diffuseColor = vec4(0, 0, 0, 0);
vec4 specColor = vec4(0, 0, 0, 0);
// Diffuse Light
float diffuseFactor = max(dot(normal, to_light_dir), 0.0);
diffuseColor = vec4(light_color, 1.0) * light_intensity * diffuseFactor;
// Specular Light
vec3 camera_direction = normalize(- position);
vec3 from_light_dir = -to_light_dir;
vec3 reflected_light = normalize(reflect(from_light_dir , normal));
float specularFactor = max( dot(camera_direction, reflected_light), 0.0);
specularFactor = pow(specularFactor, specularPower);
specColor = light_intensity * specularFactor * material.reflectance * vec4(light_color, 1.0);
return (diffuseColor + specColor);
}
void main()
{
vec4 totalLight = vec4(0);
//directional Light
for (int i=0; i<USED_DIRECTIONALLIGHTS; i++) {
totalLight += calcDirectionalLight(directionalLight[i], mvVertexPos, mvVertexNormal);
}
//...
fragColor = vec4(ambientLight, 1.0) + totalLight;
}
I am kinda new to shader so I dont know what to do anymore.
To specify the effect I get: the directional light that should only come from one direction (in worldSpace) comes from different directions based on the viewMatrix
I feel stupid now. I found the answer just after posting.
The geometry shader passes the vertexNormal directly instead of mutiplying it with the modelViewMatrix.
So the answer is this:
mvVertexNormal=normalize(modelViewMatrix * vec4(calculateTriangleNormal(), 0.0)).xyz;
instead of this:
mvVertexNormal=calculateTriangleNormal();

GLSL phong shaders, debugging

I am looking for some help debugging my GLSL phong shading code. Here is my vertex shader:
layout(std140) uniform Matrices {
mat4 model[1024];
};
layout(location = 0) in vec4 vertexCoord;
layout(location = 2) in vec3 vertexNormal;
uniform mat4 view; // from lookAt()
uniform mat4 projection; // perspective projection
out vec3 Position;
out vec3 Normal;
out vec4 lightPosEye;
void main() {
mat4 modelView = view * model[gl_InstanceID];
mat3 normalMatrix = mat3(transpose(inverse(modelView)));
//mat3 normalMatrix = mat3(modelView);
vec4 lightPos = vec4(350, 350, 350, 1);
lightPosEye = modelView * lightPos;
Position = vec3(modelView * vertexCoord);
Normal = normalize(normalMatrix * vertexNormal);
gl_Position = projection * vec4(Position, 1.0);
}
and here is my fragment shader:
in vec3 Position;
in vec3 Normal;
in vec4 lightPosEye;
layout(location = 0) out vec4 FragColor;
vec3 ads() {
vec3 Ka = vec3(0, 0.5, 0);
vec3 Kd = vec3(0, 0.5, 0);
vec3 Ks = vec3(0, 0.1, 0);
vec3 intensity = vec3(0.3, 0.5, 0.0);
float shininess = 0.1;
vec3 n = normalize(Normal);
vec3 s = normalize(vec3(lightPosEye) - Position);
vec3 v = normalize(vec3(-Position));
vec3 r = reflect(-s, n);
return intensity * (Ka + Kd * max(dot(s, n), 0.0) + Ks * pow(max(dot(r,v), 0.0), shininess));
}
void main() {
FragColor = vec4(ads(), 1);
}
Here is a screenshot of the result (I am also rendering the normals with another geometry shader for debugging purposes):
The artifacts on the cube are wrong, and the "shadows" on the circle also moves a bit when I move the camera around (change the view matrix).
Any obvious errors in my GLSL code?