OpenGL ES3使用MSAA(多重采样抗锯齿)的方法
昨晚花费了我2个多小时的时间终于把OpenGL ES3.0中的MSAA给搞定了。在OpenGL ES2.0中,Khronos官方没有引入标准的MSAA全屏抗锯齿的方法,而Apple则采用了自己的GL_APPLE_framebuffer_multisample的扩展来实现MSAA。在iOS中,OpenGL ES3.0之前使用MSAA的方法可以参见Apple的官方OpenGL ES开发者指南,写得非常详细:
而对于OpenGL ES3.0,GL_APPLE_framebuffer_multisample扩展已经失效,不能再使用了。于是我在网上搜了许多资料,不过有帮助的不多,比较有方向性的文章是OpenGL官方wiki上关于多重采样的介绍:https://www.opengl.org/wiki/Multisampling
不过这篇文章针对的是OpenGL,与OpenGL ES稍微有些差异。于是本人借助Apple的文档结合这篇官维,终于把它捣鼓出来了。
其实,大部分代码与Apple官方所描述的差不多,有几个需要改动的地方:
1、要包含头文件<OpenGLES/ES3/gl.h>。如果是之前的OpenGL ES2.0,那么所包含的是<OpenGLES/ES2/gl.h>和<OpenGLES/ES2/glext.h>。
2、带‘APPLE’、‘EXT’以及‘OES’后缀的函数以及常量都没有了。改起来非常简单,直接把后缀给删了即可,比如原来的‘glRenderbufferStorageMultisampleAPPLE’改为‘glRenderbufferStorageMultisample’;原来的‘GL_RGBA8_OES’改为‘GL_RGBA8’。
3、在绘制时,用‘glBlitFramebuffer’来取代‘glResolveMultisampleFramebufferAPPLE’。
4、用‘glInvalidateFramebuffer’来取代‘glDiscardFramebufferEXT’。这个接口非常有用!使用和没使用速度能相差1倍之多!这里得感谢Apple的Xcode以及OpenGL ES Analysis的profile工具,使得我能查到之前的glDiscardFramebufferEXT被啥取代了……否则,如果包含<OpenGLES/ES2/glext.h>然后调用glDiscardFramebufferEXT也没啥问题。不过直接用官方标准的接口会更可靠些,至少更有可移植性些,呵呵。
下面我提供比较完整的使用范例(带有部分的Objective-C代码):
先是头文件
// MyGLLayer.h
// CADemo
//
// Created by Zenny Chen on 14-8-19.
// Copyright (c) 2014年 Adwo. All rights reserved.
// @import QuartzCore; #import <OpenGLES/ES3/gl.h> @interface MyGLLayer : CAEAGLLayer
{
@private /* The pixel dimensions of the backbuffer */
GLint mBackingWidth;
GLint mBackingHeight; EAGLContext *mContext; /* OpenGL names for the renderbuffer and framebuffers used to render to this view */
GLuint mFramebuffer, mRenderbuffer, mDepthRenderbuffer; GLuint mMSAAFramebuffer, mMSAARenderbuffer, mMSAADepthRenderbuffer; CADisplayLink *mDisplayLink;
}
我们看到以上代码定义了两组FBO和RBO,一组是用于绘制到目标窗口的(不带MSAA的),另一组是用于图形渲染的,采用MSAA。在最后绘制时会把MSAA的FBO像素拷贝到单样本的FBO,用于显示。
以下是源文件的主要代码片段:
- (instancetype)init
{
self = [super init]; self.opaque = YES; self.contentsScale = [UIScreen mainScreen].scale; // Optionally configure the surface properties of the rendering surface by assigning a new dictionary of
// values to the drawableProperties property of the CAEAGLLayer object.
self.drawableProperties = @{
kEAGLDrawablePropertyRetainedBacking : @NO,
kEAGLDrawablePropertyColorFormat : kEAGLColorFormatRGBA8
}; // Set OpenGL ES context,use GL ES3 profile
mContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3]; return self;
} - (BOOL)createFramebuffer
{
// Create the framebuffer and bind it so that future OpenGL ES framebuffer commands are directed to it.
glGenFramebuffers(, &mFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer); // Create a color renderbuffer, allocate storage for it, and attach it to the framebuffer.
glGenRenderbuffers(, &mRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, mRenderbuffer); // Create the color renderbuffer and call the rendering context to allocate the storage on our Core Animation layer.
// The width, height, and format of the renderbuffer storage are derived from the bounds and properties of the CAEAGLLayer object
// at the moment the renderbufferStorage:fromDrawable: method is called.
[mContext renderbufferStorage:GL_RENDERBUFFER fromDrawable:self];
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, mRenderbuffer); // Retrieve the height and width of the color renderbuffer.
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &mBackingWidth);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &mBackingHeight); // Perform similar steps to create and attach a depth renderbuffer.
glGenRenderbuffers(, &mDepthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, mDepthRenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, mBackingWidth, mBackingHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, mDepthRenderbuffer); // The following is MSAA settings
glGenFramebuffers(, &mMSAAFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, mMSAAFramebuffer); glGenRenderbuffers(, &mMSAARenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, mMSAARenderbuffer);
// 4 samples for color
glRenderbufferStorageMultisample(GL_RENDERBUFFER, , GL_RGBA8, mBackingWidth, mBackingHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, mMSAARenderbuffer); glGenRenderbuffers(, &mMSAADepthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, mMSAADepthRenderbuffer);
// 4 samples for depth
glRenderbufferStorageMultisample(GL_RENDERBUFFER, , GL_DEPTH_COMPONENT16, mBackingWidth, mBackingHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, mMSAADepthRenderbuffer); // Test the framebuffer for completeness.
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
return NO;
} glViewport(, , mBackingWidth, mBackingHeight); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Do other settings... return YES;
} - (void)drawLayer:(CADisplayLink*)link
{
glBindFramebuffer(GL_FRAMEBUFFER, mMSAAFramebuffer);
glBindRenderbuffer(GL_RENDERBUFFER, mMSAARenderbuffer); // Draw something here...
[self drawModels]; glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebuffer);
glBindFramebuffer(GL_READ_FRAMEBUFFER, mMSAAFramebuffer); #if 0
// OpenGL ES 2.0 Apple multisampling // Discard the depth buffer from the read fbo. It is no more necessary.
glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER, , (GLenum[]){GL_DEPTH_ATTACHMENT}); glResolveMultisampleFramebufferAPPLE(); glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER, , (GLenum[]){GL_COLOR_ATTACHMENT0});
#else
// OpenGL ES3.0 Core multisampling // Discard the depth buffer from the read fbo. It is no more necessary.
glInvalidateFramebuffer(GL_READ_FRAMEBUFFER, , (GLenum[]){GL_DEPTH_ATTACHMENT}); // Copy the read fbo(multisampled framebuffer) to the draw fbo(single-sampled framebuffer)
glBlitFramebuffer(, , mBackingWidth, mBackingHeight, , , mBackingWidth, mBackingHeight, GL_COLOR_BUFFER_BIT, GL_NEAREST); glInvalidateFramebuffer(GL_READ_FRAMEBUFFER, , (GLenum[]){GL_COLOR_ATTACHMENT0});
#endif glBindRenderbuffer(GL_RENDERBUFFER, mRenderbuffer); // Assuming you allocated a color renderbuffer to point at a Core Animation layer, you present its contents by making it the current renderbuffer
// and calling the presentRenderbuffer: method on your rendering context.
[mContext presentRenderbuffer:GL_RENDERBUFFER];
}
大致使用流程如上述代码所示。我用11寸的MacBook Air上模拟器看,效果十分明显(因为MacBook Air不是retina屏)。上述demo中使用了4个样本,基本够用了。
如果各位要看非MSAA版本,只需要把drawLayer:方法下面第一行代码改为:‘glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);’;然后把对glBlitFramebuffer的调用给注释掉即可,非常方便~
OpenGL ES3使用MSAA(多重采样抗锯齿)的方法的更多相关文章
- Unity3D学习(七):Unity多重采样抗锯齿设置无效的解决办法
前言 学习Shader的过程中发现模型锯齿严重,于是去Edit--Project Settings--Quality选项下将反锯齿设置为了8X Multi Sampling.结果没有任何改变,如图: ...
- Unity3d 超级采样抗锯齿 Super Sampling Anti-Aliasing
Super Sampling Anti-AliasingSSAA算是在众多抗锯齿算法中比较昂贵的一种了,年代也比较久远,但是方法比较简单,主要概括为两步1. 查找边缘2. 模糊边缘这是一种 ...
- 多重采样(MultiSample)下的FBO反锯齿 【转】
在三维渲染的过程中,锯齿总是让人讨厌的东西.抗锯齿的一种采用方式是多重采样,本文主要小记一下FBO与多重采样的关系.——ZwqXin.com 首先,关于FBO(Frame Buffer Object) ...
- openGL线型和线宽以及线的抗锯齿
openGL线型和线宽以及线抗锯齿 一. 线宽 Opengl的线宽设置:glLineWidth(width); width为float类型值,在0~10.0,大于10以上按10来处理. 若开启线的反走 ...
- osg如何设置抗锯齿(反走样,反锯齿)
首先抗锯齿是什么? 举个最简单的例子 你用windows画图软件画一根直线(准确说这个叫做线段),当水平或者垂直的时候,如下图,这是绝对完美的 但是当线段出现倾斜时,就无法做到完美了此时就会出现锯齿 ...
- 处理 CALayer 变形后的抗锯齿问题
处理锯齿当然要用抗锯齿,iOS 可以通过修改 Plist 实现全局抗锯齿,但是这样容易出现性能问题. 所以就要使用对单个 Layer 开启抗锯齿的方法 layer.allowsEdgeAntialia ...
- 【Three.js】模型抗锯齿处理
1.锯齿消除方法 three.js参考使用官方demo发现模型渲染有锯齿,这种情况在旋转视角时候就非常明显. 抗锯齿的方法,很简单,只需要配置render两个属性即可: renderer = new ...
- 在qt的QOpenGLWidget开启opengl的抗锯齿
在QOpenGLWidget的构造函数添加下面几句代码即可 QSurfaceFormat surfaceFormat; surfaceFormat.setSamples();//多重采样 setFor ...
- OpenGL之抗锯齿 以及 线宽的设置
转自原文 OpenGL之抗锯齿 以及 线宽的设置 抗锯齿 1.线的抗锯齿 glEnable(GL_LINE_SMOOTH); //启用 glHint(GL_LINE_SMOOTH,GL_NICEST) ...
随机推荐
- python算法与数据结构-插入排序算法(34)
一.插入排序的介绍 插入排序的工作方式非常像人们排序一手扑克牌一样.开始时,我们的左手为空并且桌子上的牌面朝下.然后,我们每次从桌子上拿走一张牌并将它插入左手中正确的位置.为了找到一张牌的正确位置,我 ...
- 学习markdown(一)
转:https://www.jianshu.com/p/81e1608ea2d8 ----------------------------------------------------------- ...
- 最快速的办法解决MySQL数据量增大之后翻页慢问题
MySQL最易碰到的性能问题就是数据量逐步增大之后的翻页速度变慢的额问题,而且越往后翻页速度越慢,如果用最快速的办法解决,以下就是解决办法,简单方便. 1.问题现状 现有MySQL数据表 event_ ...
- LA 3704细胞自动机——循环矩阵&&矩阵快速幂
题目 一个细胞自动机包含 $n$ 个格子,每个格子的取值为 $0 \sim m-1$.给定距离 $d$,则每次操作是将每个格子的值变为到它的距离不超过 $d$ 的所有格子的在操作之前的值的和除以 $m ...
- PostgreSQL物理坏块和文件损坏案例分享
作者简介 王睿操,平安好医数据库架构岗,多年postgresql数据库运维开发工作.曾就职于中国民航信息,迪卡侬.对其他数据库产品也有一定的涉猎. 背景 笔者最近发现很多朋友经常遇到PostgreSQ ...
- noi.ac #42 模拟
\(des\) 二维平面上存在 \(m\) 个点,每个点会对该点的 \(8\) 个方向上的最近的点产生影响 问每个点会被影响多少次 \(sol\) 过每个点会产生 \(4\) 条线段 保存每条线段的斜 ...
- 【概率论】5-7:Gama分布(The Gamma Distributions Part II)
title: [概率论]5-7:Gama分布(The Gamma Distributions Part II) categories: - Mathematic - Probability keywo ...
- linux 最大TCP连接数限制
----------------------------------------------问题--------------------------------------------- 前几日碰到问 ...
- GoCN每日新闻(2019-09-24)
1. Go 搭建的高效网页爬虫:https://creekorful.me/building-fast-modern-web-crawler/ 2. Go 时区处理:https://medium.co ...
- 内核中通过进程PID获取进程的全部路径
目录 一丶简介 二丶原理 1.原理 2.代码实现. 一丶简介 我们遇到的Dos路径.如果想转化为NT路径(也就是 C:\xxxx)类似的格式 需要自己实现. 具体原理如下: 二丶原理 1.原理 1.使 ...