following scenario:
i'm creating an OpenGL texture and a framebuffer and associated objects
GLuint frameBuffer = 0, colorBuffer, depthBuffer;
glGenFramebuffers(1, &frameBuffer);
int maskRenderingWidth = 1 * settings.width, maskRenderingHeight = 1 * settings.height;
//define texture
glUseProgram(screenMaskRenderingProgramId);
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, maskRenderingWidth, maskRenderingHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glUseProgram(offscreeenMaskRenderingProgramId);
//setup framebuffer stuff
glGenRenderbuffers(1, &colorBuffer);
glGenRenderbuffers(1, &depthBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, maskRenderingWidth, maskRenderingHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, colorBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, maskRenderingWidth, maskRenderingHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorBuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(1, DrawBuffers);
I then render a polygon to that framebuffer which i put into a texture. Afterwards I draw another polygon but this time to the screen buffer. Finally I span a quad on the whole screen and pass the uv coords (0,0), (0,1), (1,1) and (1,0) and the previously created texture to it.
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(textureLocation,0);
What i want to achieve is, if that texture has at some texture coords certain values i want to draw the corresponding texture values, otherwise i want the output on coord (u,v) to be
untouched.
if(textureProj(mask, uv_coords).a == 1) {
gl_FragColor = textureProj(mask, uv_coords);
}
After some trying i figured out that always everything from the texture is drawn. It seems my texture is somehow corrupt as isinf and isnan return true for textureProj(mask, uv_coords) for every uv_coord. How could that be possible and someone has an idea what might be broken here?
in vec4 uv_coords;
uniform sampler2D mask;
void main() {
vec4 c = textureProj(mask, uv_coords);
bvec4 inf = isinf(c);
if(inf.r == true && inf.g == true && inf.b == true && inf.a == true) {
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
});
This yields a completely white screen. Same applies for
inf = isnan(c)
Anyone has an idea whats going on here?
EDIT:
I forgot to mention that if I leave out the if clause in the fragment shader and just do
in vec4 uv_coords;
uniform sampler2D mask;
void main() {
vec4 c = textureProj(mask, uv_coords);
gl_FragColor = c; });
the texture is drawn properly. Makes it even more strange.
EDIT2:
ok, i've read that not setting gl_FragColor gives undefined behavior. so my approach will be to upload the two textures to that specific fragment shader and render everything in one pass
Related
I am trying to render to multiple render targets, I set up a float texture and bound it to COLOR_ATTACHMENT1. This all works well and the buffer is created and bound properly. The buffer is even drawn to if I let all the shaders draw to it (glDrawBuffers(..)) instead of just the shader I have set up to draw to it (layout (location = 1) out ..). The issue is the shader that should be altering the values doesn't. I know this using Nsight graphics to preview COLOR_ATTATCHMENT1:
(note the white pixels on the left are a border from nsight)
As we can see, the buffer has two writes but neither change the buffer at all. The glClear shouldn't change anything here (I think) because glDrawBuffers doesn't contain this buffer yet.
This is what COLOR_ATTATCHMENT1 looks like if I let every shader write to it (glDrawBuffers(..) //with both attatchments):
These shaders don't write to this attatchment at all (layout (location = 0) no location 1) and work perfectly, but again, the one shader that does write to it doesn't. The crates should be writing white pixels, they can be seen here:
(note: this screenshot has post processing applied)
I don't know it's not writing but every other shader has no problem. The framebuffer is set up properly:
The texture is set up with a format of GL_RGB32F because I want this to be a float texture with the viewspace normals, but the problem persists if I use GL_RGB or RGBA. The framebuffer is multisampled and all the textures are multisampled. This is the creation code (ommited glGen.....):
glBindFramebuffer(GL_FRAMEBUFFER, fboMultisample);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, colourTexMs); //..
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, sampleLevel, GL_RGB, windowWidth, windowHeight, GL_FALSE); //..
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //..
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //.. magnified ..
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, depthTexMs);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, sampleLevel, GL_DEPTH_COMPONENT32, windowWidth, windowHeight, GL_FALSE); //..
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //..
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //..
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, normalTexMs);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, sampleLevel, GL_RGB32F, windowWidth, windowHeight, GL_FALSE); //..
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //..
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //..
And this is the attachment code (fboMultisampled is still bound to GL_FRAMEBUFFER):
glBindTexture(GL_TEXTURE_2D, 0); //unbind textures for safety
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0); //unbind textures for safety
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, colourTexMs, 0); //attatch textures to the framebuffer
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D_MULTISAMPLE, depthTexMs, 0); //..
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D_MULTISAMPLE, normalTexMs, 0); //.. 0 = mipmap level
And this is the shader that should be writing to COLOR_ATTATCHMENT1 (result calculation is omitted):
layout (location = 0) out vec4 fragColor;
layout (location = 1) out vec3 viewNorm;
//..
void main()
{
//..
fragColor = vec4(result, 1.0f);
viewNorm = vec3(1.f);
}
So in short I don't understand why the shader isn't writing to COLOR_ATTATCHMENT1.
note: there aren't any OpenGL errors reported.
fixed it by outputting a vec4 in the shader even though the texture is rgb32F
I wanted to use 4 textures in my fragment shader. One is a cubemap, one is a rendered texture on a framebuffer and the last two are created from images.
When I try calling them in my fragment shader, I keep getting a different one than the one I called or the same one no matter which one I call (depending on different iterations I did when I was trying to fix it).
This is how I got it
GLuint FramebufferName = 0;
glGenFramebuffers(1, &FramebufferName);
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
GLuint renderedTexture;
glGenTextures(1, &renderedTexture);
glBindTexture(GL_TEXTURE_2D, renderedTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1024, 1024, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, renderedTexture, 0);
GLenum DrawBuffers[1]={GL_COLOR_ATTACHMENT0};
glDrawBuffers(1, DrawBuffers);
if ( glCheckFramebufferStatus ( GL_FRAMEBUFFER ) == GL_FRAMEBUFFER_COMPLETE )
{
glViewport(0, 0, 1024, 1024);
glClearColor( 0.4f, 0.4f, 0.4f, 1.0f );
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
ngl::ShaderLib* shader = ngl::ShaderLib::instance();
(*shader)["Normal"]->use();
m_transform.setPosition(0.0f, 0.0f, 0.0f);
loadMatricesToShader();
//drawing the quad
prim->draw("tex");
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0,0,1024,768);
// grab an instance of the shader manager
ngl::ShaderLib* shader = ngl::ShaderLib::instance();
( *shader )[ "PBR" ]->use();
glUniform1i(renderedTexture, 1);
//initialize environment map
initEnvironment();
initTexture(2, m_glossMapTex, "images/gloss.png");
glUniform1i(m_glossMapTex, 2);
initTexture(3, m_textMap, "images/alege.png");
glUniform1i(m_textMap, 3);
Also the initTexture and initEnvironment
void NGLScene::initTexture(const GLuint& texUnit, GLuint &texId, const char *filename) {
glActiveTexture(GL_TEXTURE0 + texUnit);
// Load up the image using NGL routine
ngl::Image img(filename);
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
glTexImage2D (
GL_TEXTURE_2D,
0,
img.format(),
img.width(),
img.height(),
0,
GL_RGB,
GL_UNSIGNED_BYTE,
img.getPixels());
// Set up parameters for our texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
void NGLScene::initEnvironment() {
glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
glActiveTexture (GL_TEXTURE0);
glGenTextures (1, &m_envTex);
glBindTexture(GL_TEXTURE_CUBE_MAP, m_envTex);
initEnvironmentSide(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, "images/sky_zneg.png");
initEnvironmentSide(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, "images/sky_zpos.png");
initEnvironmentSide(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, "images/sky_ypos.png");
initEnvironmentSide(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, "images/sky_yneg.png");
initEnvironmentSide(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, "images/sky_xneg.png");
initEnvironmentSide(GL_TEXTURE_CUBE_MAP_POSITIVE_X, "images/sky_xpos.png");
glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_AUTO_GENERATE_MIPMAP, GL_TRUE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
GLfloat anisotropy;
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &anisotropy);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropy);
// Set our cube map texture to on the shader so we can use it
ngl::ShaderLib *shader=ngl::ShaderLib::instance();
shader->use("PBR");
shader->setUniform("envMap", 0);
}
void NGLScene::initEnvironmentSide(GLenum target, const char *filename) {
// Load up the image using NGL routine
ngl::Image img(filename);
glTexImage2D (
target,
0,
img.format(),
img.width(),
img.height(),
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
img.getPixels()
);
}
I feel like I have a gap in my knowledge of texture units and setting up uniforms, or maybe there is something wrong with my uniforms in my fragment shader.
In your code there is a misunderstanding, how glUniform1i has to be used. If a values is assigned to a uniform, the the uniform has to be identified by the uniform location index. See Uniform (GLSL)
The fist parameter of glUniform1i has to be the location of the uniform and not the named texture object.
The location of a uniform can be set explicit, in shader by a Layout Qualifier
e.g.
GLSL
layout(location = 7) uniform sampler2D u_gloss;
C++
initTexture(2, m_glossMapTex, "images/gloss.png");
glUniform1i(7, 2); // uniform location 7
If the location of the uniform is not set by a layout qualifier, then the uniform location is set automatically when the program is linked. You can ask for this location by glGetUniformLocation:
e.g.
GLSL
uniform sampler2D u_gloss;
C++
GLuint program_obj = ... ; // ( *shader )[ "PBR" ]->???
GLint gloss_location = glGetUniformLocation(program_obj , "u_gloss");
initTexture(2, m_glossMapTex, "images/gloss.png");
glUniform1i(gloss_location, 2);
I am trying to create a depth map in OpenGL and for some reason the framebuffer wont write to the texture, I tried multiple things to fix it but it doesn't seem to work.
Here is what I did:
This is the generation of the framebuffer and the texture:
glGenFramebuffers(1, &_DepthMapFBO);
glBindFramebuffer(GL_FRAMEBUFFER, _DepthMapFBO);
glGenTextures(1, &_DepthMapTex);
glBindTexture(GL_TEXTURE_2D, _DepthMapTex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, DEPTH_TEXTURE_WIDTH, DEPTH_TEXTURE_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
//glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, _DepthMapTex, 0);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, _DepthMapTex, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
(In the above I tried replacing glTexImage2D with glTexStorage2D and I tried replace glFramebufferTexture with glFramebufferTexture2D, both didn't work)
Then (There is stuff before this but they work find, I checked) I rendered the framebuffer as follows:
_DepthProgram.Use();
// Setting uniforms here
glViewport(0, 0, DEPTH_TEXTURE_WIDTH, DEPTH_TEXTURE_HEIGHT);
glBindFramebuffer(GL_FRAMEBUFFER, _DepthMapFBO);
glClear(GL_DEPTH_BUFFER_BIT);
// Rendering the scene here
glBindFramebuffer(GL_FRAMEBUFFER, 0);
These are the shaders I use:
(Vertex)
#version 410 core
layout(location = 0) in vec3 Position;
uniform mat4 LightSpaceMatrix;
uniform mat4 Model;
void main()
{
gl_Position = LightSpaceMatrix * Model * vec4(Position, 1.0);
}
(Fragment)
#version 410 core
void main()
{
// gl_FragDepth = 0.0;
}
I tried rendering the texture to a plane and it came out totally white (I Checked there was supposed to be other values), As you can see in the fragment shader, I tried to explicitly write to the gl_FragDepth but it didn't change the texture, it kept it white (1.0).
I looked all over the internet and looked at learnopengl.com and everybody seem to be doing the same as me, did I miss something?
(I double checked and I am rendering the texture to the plane right, I replaced the glBindTexture with another one and it rendered the texture)
If I am missing some information please let me know.
EDIT: By the way, I don't get any error with glGetError.
I'm trying to visualize the depth texture attached to a custom fbo in opengl. I expected to see a "foggy" looking, greyscale output - the problem is that the output seems like it's the same as the color texture - I mean it's the exact same color texture. Am I actually rendering the same thing into 2 different textures?
Creating the depth texture:
glGenTextures(1, &m_DepthTxId);
glBindTexture(GL_TEXTURE_2D, m_DepthTxId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, (void*)nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Creating the fbo & attaching the color/depth textures:
glGenFramebuffers(1, &m_Id);
glBindFramebuffer(GL_FRAMEBUFFER, m_Id);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_ColorTxId, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_DepthTxId, 0);
m_DrawBuffers[0] = GL_COLOR_ATTACHMENT0;
glDrawBuffers(1, m_DrawBuffers);
The interesting thing is that without adding the depth texture, the objects are displayed in the order they were rendered (scene looks messed up) - but when I add the depth texture, everything looks fine as you would expect with depth testing.
But how come when I give the depth texture to the shader it just displays the color-texture? Am I rendering color data into my depth texture, or..?
Apologies, but I'm fairly new to working with fbos :P
Additional info:
Rendering the fbo-textures onto a quad:
glActiveTexture(GL_TEXTURE0); // Color
glBindTexture(GL_TEXTURE_2D, m_Fbo->getColorTxId()); // ColorTexture ID
glUniform1i(m_Shader->getColorSampler(), 0); // ColorSampler Location
glActiveTexture(GL_TEXTURE1); // Depth
glBindTexture(GL_TEXTURE_2D, m_Fbo->getDepthTxId()); // DepthTexture ID
glUniform1i(m_Shader->getDepthSampler(), 0); // DepthSampler Location
glBindVertexArray(m_Fbo->getVaoId());
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)0);
glBindVertexArray(0);
Getting the sampler locations from the shader:
m_Location_ColorTxSampler = glGetUniformLocation(m_ProgramId, "colorSampler");
m_Location_DepthTxSampler = glGetUniformLocation(m_ProgramId, "depthSampler");
Shader:
in vec2 uv;
uniform sampler2D colorSampler;
uniform sampler2D depthSampler;
out vec4 color;
void main()
{
color = vec4(texture2D(depthSampler, uv).rgb, 1.0);
}
To me the whole thing seems correct, unless the 2 samplers are at the same location.. I'm sure that's not possible
If you bind the depth texture to unit 1...
glActiveTexture(GL_TEXTURE1); // Depth
glBindTexture(GL_TEXTURE_2D, m_Fbo->getDepthTxId()); // DepthTexture ID
... you should not tell the shader to sample from unit 0:
glUniform1i(m_Shader->getDepthSampler(), 0); // DepthSampler Location
I'm attempting to have SSAA featured in my scene. Previously, I have setup the entire scene as an FBO, and it works fine (it renders perfectly on my screen). However as soon as I try to apply SSAA to it, the screen is always black, and I'm unsure why. I've followed a tutorial based on iOS but was fairly simple to copy to windows which can be found here
However as I said when trying a similar technique to that it doesnt work. I understand the theory/concept of SSAA but cannot seem to get it to work. Any help I'd really appreciate it as I can't find much material on it (it is mostly on MSAA or other A.A. techniques)
FBO Initialization
void InitSSAAFBO()
{
// Create the on-screen FBO.
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_RENDERBUFFER,
fboColTex);
glGenRenderbuffers(1, &bigColour);
glBindRenderbuffer(GL_RENDERBUFFER, bigColour);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8,
windowWidth * supersamplingLevel, windowHeight * supersamplingLevel);
glGenRenderbuffers(1, &bigDepth);
glBindRenderbuffer(GL_RENDERBUFFER, bigDepth);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24,
windowWidth * supersamplingLevel, windowHeight * supersamplingLevel);
glGenFramebuffers(1, &fboBig);
glBindFramebuffer(GL_FRAMEBUFFER, fboBig);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_RENDERBUFFER,
bigColour);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,
GL_DEPTH_ATTACHMENT,
GL_RENDERBUFFER,
bigDepth);
// Create a texture object and associate it with the big FBO.
glGenTextures(1, &offscreenTexture);
glBindTexture(GL_TEXTURE_2D, offscreenTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, windowWidth * supersamplingLevel, windowHeight * supersamplingLevel, 0,
GL_RGBA, GL_UNSIGNED_BYTE, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, offscreenTexture, 0);
GLenum demoFBOStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (demoFBOStatus != GL_FRAMEBUFFER_COMPLETE) {
fboOkay = false;
cout << "Could not successfully create framebuffer object to render texture!" << endl;
}
else {
fboOkay = true;
cout << "FBO successfully created" << endl;
}
// Unbind FBO for now! (Plug main framebuffer back in as rendering destination)
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
Rendering
void display(void) {
//Bind Large FBO
glBindFramebuffer(GL_FRAMEBUFFER, fboBig);
glBindRenderbuffer(GL_RENDERBUFFER, bigColour);
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, windowWidth * supersamplingLevel, windowHeight * supersamplingLevel);
glEnable(GL_DEPTH_TEST);
DrawScene();
//Bind small/Onscreen FBO
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glBindRenderbuffer(GL_RENDERBUFFER, fboColTex);
glViewport(0, 0, windowWidth, windowHeight);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glUseProgram(fboShader);
glBindVertexArray(quadVAO);
glBindTexture(GL_TEXTURE_2D, offscreenTexture);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
glutSwapBuffers();
}
fboShader (Vertex Shader)
#version 330 core
layout (location = 0) in vec2 position;
layout (location = 1) in vec2 texCoords;
out vec2 TexCoords;
void main()
{
gl_Position = vec4(position.x, position.y, 0.0f, 1.0f);
TexCoords = texCoords;
}
fboShader (Fragment Shader)
#version 330 core
in vec2 TexCoords;
out vec4 color;
uniform sampler2D screenTexture;
void main()
{
color = texture(screenTexture, TexCoords);
}