OpenGL Deferred Shading light pass

Started by
0 comments, last by WhiskyAndCode 5 years, 4 months ago

I have been having difficulty with many lights and deferred shading in opengl. Some users here helped me but I'm still unsuccessful. ( i posted in stackoverflow but i dont like how the site work, i prefer here)

I'm at a time trying to add lights to my scene but unfortunately it's to no avail.

Following the learnopengl deferred shading tutorial, several lights are shown but in the final screen quad shader, and I wanted to render my lights independently.

At the end of the session the author indicates how to do it, which is adding beads as "light source", as shown below:

zHDRR.png

But unfortunately my result was this (embarrassing HAHAHA):
SHIT.thumb.png.5d1585ab403e016875efe2b714cee2e2.png

How could I accumulate all the lights in my main scene? Why is not working ?

That is, each light was rendered on the ball along with the framebuffer.
A snippet of my code:


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shaderLightingPass.use();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, gPosition);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, gNormal);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, gAlbedoSpec);
// send light relevant uniforms
//for (unsigned int i = 0; i < lightPositions.size(); i++)
//{

    shaderLightingPass.setVec3("light.Position", lightPositions[0]);
    shaderLightingPass.setVec3("light.Color", lightColors[0]);
    // update attenuation parameters and calculate radius
    const float constant = 1.0; // note that we don't send this to the shader, we assume it is always 1.0 (in our case)
    const float linear = 0.7;
    const float quadratic = 1.8;
    shaderLightingPass.setFloat("light.Linear", linear);
    shaderLightingPass.setFloat("light.Quadratic", quadratic);


//}
shaderLightingPass.setVec3("viewPos", camera.Position);
// finally render quad
renderQuad();


glBindFramebuffer(GL_READ_FRAMEBUFFER, gBuffer);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); 
glBlitFramebuffer(0, 0, SCR_WIDTH, SCR_HEIGHT, 0, 0, SCR_WIDTH, SCR_HEIGHT, GL_DEPTH_BUFFER_BIT, GL_NEAREST);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

//where i add lights
AddLightSphere(shaderLightBox);

 

AddLightSphere Method:


void AddLightSphere(Shader shaderLightBox) {


    glm::mat4 model;
    glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
    glm::mat4 view = camera.GetViewMatrix();


    for (unsigned int i = 0; i < lightPositions.size(); i++)
    {
        shaderLightBox.use();
        shaderLightBox.setInt("gPosition", 0);
        shaderLightBox.setInt("gNormal", 1);
        shaderLightBox.setInt("gAlbedoSpec", 2);

        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, gPosition);
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, gNormal);
        glActiveTexture(GL_TEXTURE2);
        glBindTexture(GL_TEXTURE_2D, gAlbedoSpec);

        shaderLightBox.setMat4("projection", projection);
        shaderLightBox.setMat4("view", view);

        model = glm::mat4();
        model = glm::translate(model, lightPositions[i]);
        model = glm::scale(model, glm::vec3(0.5f));
        shaderLightBox.setMat4("model", model);

        shaderLightBox.setVec3("light.Position", lightPositions[i]);
        shaderLightBox.setVec3("light.Color", lightColors[i]);
        // update attenuation parameters and calculate radius
        const float constant = 1.0; 
        const float linear = 0.7;
        const float quadratic = 0.8;
        shaderLightBox.setFloat("light.Linear", linear);
        shaderLightBox.setFloat("light.Quadratic", quadratic);
        shaderLightBox.setVec3("viewPos", camera.Position);
        renderSphere();
        glUseProgram(0);


    }
    }

The ShaderLightBox (lightSphere would be correct) (Shader shaderLightBox("8.1.deferred_light_box.vs", "8.1.deferred_light_box.fs")):

Vertex:


layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoords;

uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;

out vec2 TexCoords;

void main()
{
    TexCoords = aTexCoords;
    gl_Position = projection * view * model * vec4(aPos, 1.0);
}

//////////////// FRAGMENT //////////////
out vec4 FragColor;

in vec2 TexCoords;

uniform sampler2D gPosition;
uniform sampler2D gNormal;
uniform sampler2D gAlbedoSpec;

struct Light {
    vec3 Position;
    vec3 Color;

    float Linear;
    float Quadratic;
};

uniform Light light;
uniform vec3 viewPos;

void main()
{             
    // retrieve data from gbuffer
    vec3 FragPos = texture(gPosition, TexCoords).rgb;
    vec3 Normal = texture(gNormal, TexCoords).rgb;
    vec3 Diffuse = texture(gAlbedoSpec, TexCoords).rgb;
    float Specular = texture(gAlbedoSpec, TexCoords).a;

    // then calculate lighting as usual
    vec3 lighting  = Diffuse * 0.5; // hard-coded ambient component
    vec3 viewDir  = normalize(viewPos - FragPos);

        // diffuse
        vec3 lightDir = normalize(light.Position - FragPos);
        vec3 diffuse = max(dot(Normal, lightDir), 0.0) * Diffuse * light.Color;
        // specular
        vec3 halfwayDir = normalize(lightDir + viewDir);  
        float spec = pow(max(dot(Normal, halfwayDir), 0.0), 16.0);
        vec3 specular = light.Color * spec * Specular;
        // attenuation
        float distance = length(light.Position - FragPos);
        float attenuation = 1.0 / (1.0 + light.Linear * distance + light.Quadratic * distance * distance);
        diffuse *= attenuation;
        specular *= attenuation;
        lighting += diffuse + specular;        

    FragColor = vec4(lighting, 1.0);
}

 

Has anyone experienced this and could guide me in this situation? I can not make the lights pile up to have a final result with all the lights.

For more details, I put the code here.

How could I accumulate all the lights in my main scene?

Please, I've been trying for a long time, have patience.

 

Thank u

This topic is closed to new replies.

Advertisement