OpenGL MSAA多采样中的蒙版(stencil)使用
程序员文章站
2022-07-04 20:07:31
...
在OpenGL中进行MSAA多采样时,需要进行如下设置FBO
glGenFramebuffers(1, &mfbo);
glBindFramebuffer(GL_FRAMEBUFFER, mfbo);
//create color render buffer
glGenRenderbuffers(1, &colorBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, colorBuffer);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4 /* 4 samples */, GL_RGBA8, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorBuffer);
// multi sampled depth buffer
glGenRenderbuffers(1, &depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4 /* 4 samples */, GL_RGBA8, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);
GLenum result = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (result == GL_FRAMEBUFFER_COMPLETE) {
isMultiFBODone = true;
}
else {
isMultiFBODone = false;
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
如果程序中还需要使用蒙版缓冲(stencil buffer),则需要将此FBO设置为如下格式:
glGenFramebuffers(1, &mfbo);
glBindFramebuffer(GL_FRAMEBUFFER, mfbo);
//create color render buffer
glGenRenderbuffers(1, &colorBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, colorBuffer);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4 /* 4 samples */, GL_RGBA8, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorBuffer);
// multi sampled depth buffer
glGenRenderbuffers(1, &depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4 /* 4 samples */, GL_DEPTH24_STENCIL8, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);
GLenum result = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (result == GL_FRAMEBUFFER_COMPLETE) {
isMultiFBODone = true;
}
else {
isMultiFBODone = false;
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
然后在根据需要写关于蒙版的代码即可
上一篇: 【OpenGL学习笔记】2.创建窗口