Opengl deferred lighting shader

947 Views Asked by At

I just started to learning OpenGL 3.1 and I'm trying to implement deferred shading to my engine(framework?). I wrote simple shaders for first stage, lighting stage and deferred stage. Lighting stage takes the diffuse color from deferred texture and saves it in lighting texture. Deferred stage draws the lighting texture. In lighting shader is a bug and scene is very strange. It looks like this, and it should look like this. Lighting stage vertex shader:

#version 150

in vec4 vertex;

out vec2 position;

void main(void)
{
   gl_Position = vertex*2-1;
   gl_Position.z = 0.0;

   position.xy = vertex.xy;
}

Lighting stage fragment shader:

#version 150

in vec2 position;

uniform sampler2D diffuseTexture;
uniform sampler2D positionTexture;

out vec4 lightingOutput;

void main()
{
    vec4 diffuse = texture(diffuseTexture, vec2(position.x, position.y));
    vec4 position = texture(positionTexture, position.xy);

    vec4 ambient = vec4(0.05, 0.05, 0.05, 1.0) * diffuse;

    lightingOutput = diffuse;
}

That's what I render in lighting stage:

static const GLfloat _vertices[] =
{
    -1.0f, -1.0f, 0.0f,
    1.0f, -1.0f, 0.0f,
    0.0f,  1.0f, 0.0f,

    0.0f, 1.0f, 0.0f,
    1.0f, -1.0f, 0.0f,
    1.0f,  1.0f, 0.0f,
};

And that's how I render it:

glUseProgram( programID[2] );

glEnableVertexAttribArray(vertexID[1]);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(
    vertexID[1],
    3,                  // size
    GL_FLOAT,           // type
    GL_FALSE,           // normalized?
    0,                  // stride
    (void*)0            // array buffer offset
    );

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, positionTexture);
glUniform1i(positionTextureID[1], 1);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, diffuseTexture);
glUniform1i(diffuseTextureID[1], 0);

glDrawArrays( GL_TRIANGLES, 0, 6 );

glDisableVertexAttribArray(vertexID[1]);

If you need all the code it's here www.dropbox.com/s/hvfe4v4pb1pfxb3/code.zip.

How to fix that strange problem?

0

There are 0 best solutions below