QOpenGLWidget 立体渲染示例
该示例展示了如何创建一个支持立体渲染的基于QOpenGLWidget 的最小应用程序。
注: 支持立体渲染有一定的硬件要求,如支持立体渲染的显卡、3D 眼镜和特定的显示器。
注: 本例将两幅图像渲染到两个独立的缓冲区。通过 3D 眼镜观看图像时,会产生 3D 全息效果。
上图是渲染到左侧缓冲区的图像。
上图是渲染到右侧缓冲区的图像。
设置正确的曲面标志
要启用立体渲染,需要全局设置标志QSurfaceFormat::StereoBuffers 。仅仅在部件上设置是不够的,因为内部是如何处理标志的。最安全的做法是在启动应用程序之前,在 QSurfaceFormat::SetDefaultFormat 上进行设置。
QSurfaceFormat format; format.setDepthBufferSize(24); format.setStencilBufferSize(8); // Enable stereoscopic rendering support format.setStereo(true); QSurfaceFormat::setDefaultFormat(format);
两次渲染
设置QSurfaceFormat::StereoBuffers 后,paintGL() 将被调用两次,每个缓冲区一次。在 paintGL() 中,您可以调用 currentTargetBuffer() 来查询当前激活的目标缓冲区。
在下面的代码段中,我们稍微平移了一下矩阵,以避免顶点相互重叠。这是一个简单的示例,只是为了说明如果有必要的支持,在运行时你应该看到两个对象,一个在左边,一个在右边。
// Slightly translate the model, so that there's a visible difference in each buffer. QMatrix4x4 modelview; if (currentTargetBuffer() == QOpenGLWidget::LeftBuffer) modelview.translate(-0.4f, 0.0f, 0.0f); else if (currentTargetBuffer() == QOpenGLWidget::RightBuffer) modelview.translate(0.4f, 0.0f, 0.0f);
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.