QOpenGL入门教程03:设置格式

系列教程索引:QOpenGL入门教程索引

上一篇:QOpenGL入门教程02:输出版本号

QSurface是一个可渲染的抽象类,继承自QOffscreenSurface和QWindow。

  • size():设置接口大小。
  • format():设置渲染的特殊属性。

QSurfaceFormat代表QSurface的一种格式,包括颜色缓冲(、红、绿、蓝、alpha缓冲)、深度缓冲、多重采样数量等;

查询Qt参考手册,可以发现其提供以下接口

  • setAlphaBufferSize(int size) A通道大小
  • setBlueBufferSize(int size) B通道大小
  • setDepthBufferSize(int size) 深度缓冲大小
  • setGreenBufferSize(int size) G通道大小
  • setMajorVersion(int major) OpenGL大版本号
  • setMinorVersion(int minor) OpenGL小版本号
  • setOption(FormatOption option, bool on = true)
  • setOptions(QSurfaceFormat::FormatOptions options) 设置选项,立体缓冲、调试上下文、过时功能启用、通知
  • setProfile(OpenGLContextProfile profile) 设置OpenGL画像,核心、兼容
  • setRedBufferSize(int size) 设置颜色缓冲区内R通道大小
  • setRenderableType(RenderableType type) 设置渲染类型桌面OpenGL、ES、VG
  • setSamples(int numSamples) 设置每个像素的采样点数
  • setStencilBufferSize(int size) 设置模版缓冲区大小
  • setStereo(bool enable) 是否启用立体缓冲区,立体缓冲器提供了额外的颜色缓冲器来生成左眼和右眼的图像。
  • setSwapBehavior(SwapBehavior behavior) 设置缓冲区类型:默认、单缓冲区、双缓冲区、三缓冲区
  • setSwapInterval(int interval) 垂直回扫(Vertical Blank),指的是当显示器完成了一帧画面的扫描后开始回到开始重新扫描下一帧画面的过程。在电脑显示上,往往利用垂直回扫这个间隔时间,来执行一些额外的操作例如后台缓冲中的内容复制到前台,或者是页翻转。默认是在交换两个缓冲区时回扫一次
  • setVersion(int major, int minor) 指定OpenGL版本,默认为2.0

可放置的位置有两个,一个是main.cpp调用窗口的地方,另一个是GLWidget的构造函数中

main

1
2
3
4
5
6
7
8
9
10
11
12
13
int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QSurfaceFormat format;
format.setSamples(24);

GLWidget w;
w.setFormat(format);
w.show();

return a.exec();
}

Qt5.5.1下编译运行提示

1
QOpenGLWidget: Already initialized, setting the format has no effect

构造函数

1
2
3
4
5
6
7
8
9
10
11
GLWidget::GLWidget(QWidget *parent)
: QOpenGLWidget(parent)
, mFullScreen(false)
{
QSurfaceFormat format;
format.setSamples(24);

setFormat(format);

showNormal();
}

运行没有提示相关信息

后续使用此种方式。

完整代码位于:OpenGL_Beginner在QOpenGL下的3.format中

下一篇:QOpenGL入门教程04:基本图形


QOpenGL入门教程03:设置格式
https://feater.top/qt/setup-qopengl-surface-format/
作者
JackeyLea
发布于
2022年7月6日
许可协议