Good evening everyone!
First posting, let’s see how much I can do wrong (I hope this is at least the right sub-forum). Also, this post will be a bit longer, so please bear with me.
Disclaimer: Yes, I first had a look at Google before bothering you with this. Yes, I have cleared the depth buffer and tried many different depth funcs.
The problem: For a game, I want to render to a texture and apply some post-processing to it. The game is in 2D and I already applied render-to-texture to it, but apparently I did it wrong, because as soon as I switch the depth testing on (which I forgot), the screen is black. And yes, I need the depth testing because the game will have different layers.
For some reason, I can’t link a tutorial I applied before, so here’s my code:
Header:
GLuint screenRenderFBO, screenRenderTexture, screenRenderDepthFBO;
Initialisation, along with comments from tutorial:
// The framebuffer, which regroups 0, 1, or more textures, and 0 or 1 depth
glGenFramebuffers(1, &screenRenderFBO);
glBindFramebuffer(GL_FRAMEBUFFER, screenRenderFBO);
// The texture we're going to render to
glActiveTexture(GL_TEXTURE0 + RENDER_TO_TEXTURE_OFFSET);
glGenTextures(1, &screenRenderTexture);
// "Bind" the newly created texture : all future texture functions will modify this texture
glBindTexture(GL_TEXTURE_2D, screenRenderTexture);
// Give an empty image to OpenGL
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, BASIC_RES, BASIC_RES, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
// Poor filtering. Needed !
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
checkGLError();
// The depth buffer
glGenRenderbuffers(1, &screenRenderDepthFBO);
glBindRenderbuffer(GL_RENDERBUFFER, screenRenderDepthFBO);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, BASIC_RES, BASIC_RES);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, screenRenderDepthFBO);
// Set "renderedTexture" as our colour attachement #0
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, screenRenderTexture, 0);
// Set the list of draw buffers.
GLenum DrawBuffers[1] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers(1, DrawBuffers); // "1" is the size of DrawBuffers
// Always check that our framebuffer is ok
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
std::cout << "Framebuffer could not be created!" << std::endl;
}
Drawcall (own code + comments, tutorial not very precise here):
glBindFramebuffer(GL_FRAMEBUFFER, screenRenderFBO);
glViewport(0, 0, BASIC_RES, BASIC_RES); // small resolution
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST); // <----- this causes the complete scene to go black
glDepthFunc(GL_???); // Anything can be inserted here, GL_NOTEQUAL causes the frame to flicker (every 2nd frame image, the other black). GL_ALWAYS works, but that's not what I want...
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
drawObjects(); // scene rendered here
// Render to the screen
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glActiveTexture(GL_TEXTURE0 + RENDER_TO_TEXTURE_OFFSET);
toScreenShader->renderBegin(); // selfmade wrapper class for shader access
glUniform1i(toScreen_texture, RENDER_TO_TEXTURE_OFFSET);
glViewport(0, 0, windowSize[0], windowSize[1]); // real window is larger than texture
toScreenVbo->draw();
toScreenShader->renderEnd();
As the comment says, the depth test causes everything to go black. No openGL error was generated during the whole process.
Some questions here could be:
[ol]
[li]How can the depth test alone make the whole scene black, if the render to texture procedure itself seems to work?
[/li][li]Is the methodology wrong, i.e. are two FBOs and one textures right? I have tried other approaches with a depth texture, but without success. Or is there a completely other way to achieve this?
[/li][li]How can I insert all possible values into the depth func, and even (almost) contrary values like GEQUAL and LEQUAL always print a black screen?
[/li][/ol]
Does anyone have an idea what could be going on here? Any help would be much appreciated! If there’s anything I forgot to mention, please let me know.
Best regards,
Brokenmind