0
votes

I'm a bit of a novice at OpenGL and I've implemented lighting to a scene with instanced asteroids circling a planet. For some reason, the only output I'm getting seems to be the ambient lighting for the scene. I can't get diffuse or specular to work.

Here's an image of the scene as it is (very hard to see...)ambient only

The fragment shader code is below. FragPos, Normal and TexCoords are as you'd expect. texture_diffuse1 is the planet/asteroid texture depending on the shader used. LightPos can be moved, it's represented by the smaller planet in the image (so directly above the centre planet) and viewPos is the camera position.

Any more information needed please let me know, but there rest of the code is really straight forward (vertex shader really standard, assimp loaded models drawn in the scene, asteroids instanced 200,000 times).

Thanks Jon

Fragment Shader:

`

#version 330 core
out vec4 fragColor;

in VS_OUT{
    vec3 FragPos;
    vec3 Normal;
    vec2 TexCoords;
} fs_in;

uniform sampler2D texture_diffuse1;
uniform vec3 lightPos;
uniform vec3 viewPos;

void main()
{
    vec3 color = texture(texture_diffuse1, fs_in.TexCoords).rgb;

    //Ambient
    vec3 ambient = 0.05f * color;

    //Diffuse
    vec3 lightDir = normalize(lightPos - fs_in.FragPos);
    vec3 normal = normalize(fs_in.Normal);
    float diff = max(dot(lightDir, normal), 0.0);
    vec3 diffuse = diff * color;

    //Specular

    vec3 viewDir = normalize(viewPos - fs_in.FragPos);  

    float spec = 0.0f;
    vec3 halfwayDir = normalize(lightDir + viewDir);
    spec = pow(max(dot(normal, halfwayDir), 0.0), 32.0f);

    vec3 specular = vec3(0.3f) * spec * color;

    float distance = length(lightPos - fs_in.FragPos);
    float attenuation = 1.0f/distance;

    //fragColor = vec4(ambient + (diffuse + specular), 1.0f);
    fragColor = vec4( pow(ambient + attenuation * (diffuse + specular),vec3(1.0f/2.2f)), 1.0f);
}

`

1
If your light source and your planet are 300 units away your planet will be only affected by 1/300~0,003 of your light. Is this intended?Shiro
It was part of a tutorial on attenuation mixed with gamma correction. I did remove that and it didn't make a difference though.Jonathan Cain
I have a program implementing a solar system. No instancing in there, but there is lightning (also shadows). You can take a look and compare, maybe you notice what's wrong. The code is here: github.com/aromanro/SolarSystemAdrian Roman
Ah you legend Adrian, thank you, I had the light direction the wrong way round! It's Frag - lightpos, and I had lightpos - frag!! If you want credit for the answer then more than happy to give you it, please add it to the answersJonathan Cain
Hey, so turns out I was wrong ... again, but I did discover using your shaders that it was in fact the vertex shader that was wrong, I had not calculated the normal vectors with respect to the model matrices before passing them to the fragment shader! But thanks again, without your example I'd have never have found it.Jonathan Cain

1 Answers

0
votes

It seems that the direction of lightning was wrong. Instead of lightPos - fs_in.FragPos, fs_in.FragPos - lightPos was needed. I'll let here the link to my code that helped, to make it more visible, maybe it will help others, too: https://github.com/aromanro/SolarSystem

edit

JonCain: It also turned out the matrix for the Normals was not being applied properly before being passed from the Vertex Shader.