I am having sucess with creating a framebuffer, for offscreen rendering, with multisampling and with a stencil buffer attached, but having both I fail at.
Trying to create a multisampled stencil buffered framebuffer fails with an GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT. The following code works as expected for non multisampling, but fails when multisampling is enabled.
static GLuint createOffScreenFrameBuffer(int width, int height, int numSamples)
{
GLuint fboID, colorBufferID,depth_stencil_texture;
GLint maxSamples;
glGetIntegerv(GL_MAX_SAMPLES_EXT, &maxSamples);
if(maxSamples<numSamples)
throw gcnew System::Exception(“Requested number of multi samples not supported by video format”);
glGenFramebuffersEXT(1, &fboID);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboID);
glGenRenderbuffersEXT(1, &colorBufferID);
glGenTextures(1, &depth_stencil_texture);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, depth_stencil_texture);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_DEPTH24_STENCIL8_EXT, width, height, 0, GL_DEPTH_STENCIL_EXT, GL_UNSIGNED_INT_24_8_EXT, NULL);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
if(numSamples>1) //here we say that 1 multisample is equal to no multisample. It is just ONE sample
{
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, colorBufferID);
glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER_EXT, multiSampleCount, GL_RGBA8, width, height);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, colorBufferID);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_RECTANGLE_ARB , depth_stencil_texture, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_RECTANGLE_ARB , depth_stencil_texture, 0);
}
else
{
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, colorBufferID);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA, width, height);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, colorBufferID);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_RECTANGLE_ARB , depth_stencil_texture, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_RECTANGLE_ARB , depth_stencil_texture, 0);
}
GLuint status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if(status!=GL_FRAMEBUFFER_COMPLETE_EXT)
throw gcnew System::Exception(“Failed to create off screen framebuffer”);
return fboID;
}