OpenGL实现抗锯齿效果

OpenGL实现抗锯齿效果

首先创建普通FBO,再创建一个MSAAFBO,将要描画物描画在MSAAFBO上,然后将MSAAFBO的颜色copy到普通FBO上,最后将普通FBO呈现在屏幕。

 

1、创建普通BFO

glGenTextures(1, &textureId);

glBindTexture(GL_TEXTURE_2D, textureId);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, TEXTURE_WIDTH, TEXTURE_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);

glBindTexture(GL_TEXTURE_2D, 0);

 

GLuint rboId;

glGenRenderbuffers(1, &rboId);

glBindRenderbuffer(GL_RENDERBUFFER, rboId);

glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, TEXTURE_WIDTH, TEXTURE_HEIGHT);

glBindRenderbuffer(GL_RENDERBUFFER_EXT, 0);

 

GLuint fboId;

glGenFramebuffers(1, &fboId);

glBindFramebuffer(GL_FRAMEBUFFER, fboId);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureId, 0);

glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rboId);

glBindFramebuffer(GL_FRAMEBUFFER, 0);

 

 

创建MSAABFO

glGenFramebuffers(1, &mMSAAFramebuffer);

glBindFramebuffer(GL_FRAMEBUFFER, mMSAAFramebuffer);

glGenRenderbuffers(1, &mMSAARenderbuffer);

glBindRenderbuffer(GL_RENDERBUFFER, mMSAARenderbuffer);

glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_RGBA8, mBackingWidth, mBackingHeight);

glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, mMSAARenderbuffer);

glGenRenderbuffers(1, &mMSAADepthRenderbuffer);

glBindRenderbuffer(GL_RENDERBUFFER, mMSAADepthRenderbuffer);

glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_DEPTH_COMPONENT16, Width, Height);

glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, mMSAADepthRenderbuffer);

glBindFramebuffer(GL_FRAMEBUFFER, 0);

 

拷贝MSAAFBO到普通FBO

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboId);

glBindFramebuffer(GL_READ_FRAMEBUFFER, mMSAAFramebuffer);

glBindFramebuffer(GL_FRAMEBUFFER, 0);

glInvalidateFramebuffer(GL_READ_FRAMEBUFFER, 1, (GLenum[]){GL_DEPTH_ATTACHMENT});

glBlitFramebuffer(0, 0, Width, Height, 0, 0, Width, Height, GL_COLOR_BUFFER_BIT, GL_NEAREST);

glInvalidateFramebuffer(GL_READ_FRAMEBUFFER, 1, (GLenum[]){GL_COLOR_ATTACHMENT0});

发布了12 篇原创文章 · 获赞 1 · 访问量 6906
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览