rcapote
rcapote

Reputation: 1044

Combining two texture in fragment shader

I'm working on implementing deferred shading to my game. I have rendered the diffuse textures to a render target, and I have lighting rendered to a render target. Both of which I know are fine because I can render them straight to the screen with no problems. What I want to do is combine both the diffuse map and the light map in a shader to create a final image. Here is my current fragment shader, which results in a black screen.

#version 110

uniform sampler2D diffuseMap;
uniform sampler2D lightingMap;

void main()
{
    vec4 color = texture(diffuseMap, gl_TexCoord[0].st);
    vec4 lighting = texture(lightingMap, gl_TexCoord[0].st);

    vec4 finalColor = color;

    gl_FragColor = finalColor;
}

Shouldn't this result in the same thing as just straight up drawing the diffuse map?

I set the sampler2d with this method

void ShaderProgram::setUniformTexture(const std::string& name, GLint t) {
    GLint var = getUniformLocation(name);
    glUniform1i(var, t);
}

GLint ShaderProgram::getUniformLocation(const std::string& name) {
    if(mUniformValues.find(name) != mUniformValues.end()) {
        return mUniformValues[name];
    }

    GLint var = glGetUniformLocation(mProgram, name.c_str());
    mUniformValues[name] = var;

    return var;
}

EDIT: Some more information. Here is the code where I use the shader. I set the two textures, and draw a blank square for the shader to use. I know for sure, my render targets are working, as I said before, because I can draw them fine using the same getTextureId as I do here.

    graphics->useShader(mLightingCombinedShader);
    mLightingCombinedShader->setUniformTexture("diffuseMap", mDiffuse->getTextureId());
    mLightingCombinedShader->setUniformTexture("lightingMap", mLightMap->getTextureId());
    graphics->drawPrimitive(mScreenRect, 0, 0);
        graphics->clearShader();



void GraphicsDevice::useShader(ShaderProgram* p) {
    glUseProgram(p->getId());
}

void GraphicsDevice::clearShader() {
    glUseProgram(0);
}

And the vertex shader

#version 110 

varying vec2 texCoord;

void main() 
{ 
    texCoord = gl_MultiTexCoord0.xy;
    gl_Position = ftransform();

}

Upvotes: 0

Views: 4495

Answers (3)

rcapote
rcapote

Reputation: 1044

The problem ended up being that I didn't enable the texture coordinates for the screen rectangle I was drawing.

Upvotes: 0

Nicol Bolas
Nicol Bolas

Reputation: 473212

glUniform1i(var, t);

The glUniform functions affect the program that is currently in use. That is, the last program that glUseProgram was called on. If you want to set the uniform for a specific program, you have to use it first.

Upvotes: 0

SuperMaximo93
SuperMaximo93

Reputation: 2056

In GLSL version 110 you should use:

texture2D(diffuseMap, gl_TexCoord[0].st); // etc.

instead of just the texture function.

And then to combine the textures, just multiply the colours together, i.e.

gl_FragColor = color * lighting;

Upvotes: 1

Related Questions