Trouble getting a GBuffer up and running

Started by
-1 comments, last by CirdanValen 9 years, 11 months ago

I'm working towards implementing a deferred lighting system in my 2d game, and have run into trouble with my gbuffer.

GBuffer code: http://pastebin.com/4xrcJ5c3

Geometry pass shaders:


// VERTEX SHADER

#version 330

layout(location=0) in vec4 inPosition;
layout(location=1) in vec2 UV;
uniform mat4 transformMatrix;

out vec2 TexCoords;

void main(void) {
    gl_Position = transformMatrix*inPosition;
    TexCoords = UV;
}

// FRAGMENT SHADER

#version 330

uniform sampler2D diffuseTexture;
uniform sampler2D normalsTexture;
in vec2 TexCoords;

layout(location=0) out vec3 outDiffuse;
layout(location=1) out vec3 outNormals;

void main(void) {
    vec4 texel = texture2D(diffuseTexture, TexCoords);
    outDiffuse = vec3(1.0);
    outNormals = vec3(1.0, 0.0, 0.0);
}

Light pass shaders


// VERTEX SHADER

#version 330

layout(location=0) in vec4 inPosition;
layout(location=1) in vec2 UV;
uniform mat4 transformMatrix;

out vec2 TexCoords;

void main(void) {
    gl_Position = transformMatrix*inPosition;
    TexCoords = UV;
}

// FRAGMENT SHADER

#version 330

uniform sampler2D diffuseTexture;
uniform sampler2D normalsTexture;
in vec2 TexCoords;

out vec4 outColor;

void main(void) {
    outColor = vec4(texture2D(diffuseTexture, TexCoords).rgb, 1.0);
}


I am rendering out all of my geometry to the gbuffer/framebuffer through the geometry shader, which right now I have set to only output white pixels for testing. After rendering to the gbuffer, I render a rectangle the size of the screen using the light pass shader. What I expect to be happening is getting white rectangles from the first gbuffer pass, however all I am getting is a black screen. If I change outColor in the light pass to white, I get a white screen so I know that portion is working fine. So something is wrong with either reading or writing data to the gbuffer.


mGBuffer.writeBind(mRenderWindow.context());
ae::RenderState *state = mGBuffer.state();
state->pushShader(mDeferredGeomShader);
state->useOrthographicProj();
state->useCamera(mCamera);
mMap.draw(mRenderWindow.state());
mSpriteRenderSystem.renderSprites(mRenderWindow.state());
mGBuffer.writeUnbind(mRenderWindow.context());


mRenderWindow.state()->pushShader(mDeferredLightShader);
mDeferredLightShader->setUniform("diffuseTexture", (GLuint)0);
mDeferredLightShader->setUniform("normalsTexture", (GLuint)1);

mGBuffer.readBind();
mRenderWindow.state()->pushModelMatrix(glm::translate(glm::mat4(1.f), glm::vec3(1.f,
1.f, 0.f)));
mScreenRect->draw(mRenderWindow.state());
mRenderWindow.state()->popModelMatrix();
mGBuffer.readUnbind();
mRenderWindow.state()->popShader();

EDIT:

Okay, one of the problems was in my initialization code for the gbuffer, I was initializing texture 0 twice. Another problem was when I was rendering to the gbuffer, I was using the wrong state. It seems to be working now, however not all my objects are showing up in the gbuffer (the sprite is, but the map is not)

EDIT2:

Okay, the map was not appearing because there is no alpha value in the gbuffer textures...so the map layers that had no tiles were overwriting the bottom layer.

This topic is closed to new replies.

Advertisement