I have a frame buffer with two textures attached, and the read calls:
Occlusion = texture2D(OcclusionTexture, TexCoord);
FragColor = texture2D(ColourTexture, TexCoord);
Return the same thing. They return the data that resides in the texture OcclusionTexture. Any help please, sorry for the wall of code.
Shader Code: Attachments
subroutine void RenderPassType();
subroutine uniform RenderPassType RenderPass;
uniform sampler2D ColourTexture;
uniform sampler2D OcclusionTexture;
layout(location = 0) out vec4 FragColor; // Actual fragment colour;
layout(location = 1) out vec3 OutColour;
layout(location = 2) out vec3 OcclusionColour;
Writing and reading
subroutine (RenderPassType) void Write()
{
// Writing
OcclusionColour = vec3(m_Light[0].Intensity);
OutColour = vec3(ApplyFog(ViewPos, Colour, fogColour, fogDistance));
}
subroutine (RenderPassType) void Read()
{
// Reading
vec4 Occlusion = texture2D(OcclusionTexture, TexCoord);
FragColor = texture2D(ColourTexture, TexCoord);
}
Code for generating the frame buffer and texture:
// Create and bind the FBO
glGenFramebuffers(1, &FBO);
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
// The depth buffer
glGenRenderbuffers(1, &DepthBuff);
glBindRenderbuffer(GL_RENDERBUFFER, DepthBuff);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT,
settings::GameSettings::GetSingleton()->Screen.x, settings::GameSettings::GetSingleton()->Screen.y);
// The color buffer
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &ScreenTex);
glBindTexture(GL_TEXTURE_2D, ScreenTex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, settings::GameSettings::GetSingleton()->Screen.x
, settings::GameSettings::GetSingleton()->Screen.y, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// The occlusion buffer
glActiveTexture(GL_TEXTURE1);
glGenTextures(1, &OcclusionTex);
glBindTexture(GL_TEXTURE_2D, OcclusionTex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, settings::GameSettings::GetSingleton()->Screen.x
, settings::GameSettings::GetSingleton()->Screen.y, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Attach the images to the framebuffer
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, DepthBuff);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, ScreenTex, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, OcclusionTex, 0);
GLenum drawBuffers[] = { GL_NONE, GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
glDrawBuffers(3, drawBuffers);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
SetUniform("ColourTexture", ScreenTex);
SetUniform("OcclusionTexture", OcclusionTex);
////////////////////////////////////////////////////// EDIT //////////////////////////////////////////////////////
First pass I render the scene to the textures. In the second pass I only read from the textures and do volumetric lighting calculations and then push the final frag colour out to attachment 0.
void Shader::SetUniform(char* string, int type) { GLuint varLocation = glGetUniformLocation(m_shaderProgramHandle, string); glUniform1iv(varLocation, 1, &type); }