QOpenGLShader Class
QOpenGLShader 类可用于编译 OpenGL 着色器。更多内容...
| 头文件: | #include <QOpenGLShader> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS OpenGL) target_link_libraries(mytarget PRIVATE Qt6::OpenGL) |
| qmake: | QT += opengl |
| 继承自: | QObject |
- 所有成员列表(包括继承的成员)
- QOpenGLShader 属于3D 渲染 部分。
公共类型
| flags | ShaderType |
| enum | ShaderTypeBit { Vertex, Fragment, Geometry, TessellationControl, TessellationEvaluation, Compute } |
公共函数
| QOpenGLShader(QOpenGLShader::ShaderType type, QObject *parent = nullptr) | |
| virtual | ~QOpenGLShader() |
| bool | compileSourceCode(const char *source) |
| bool | compileSourceCode(const QByteArray &source) |
| bool | compileSourceCode(const QString &source) |
| bool | compileSourceFile(const QString &fileName) |
| bool | isCompiled() const |
| QString | log() const |
| GLuint | shaderId() const |
| QOpenGLShader::ShaderType | shaderType() const |
| QByteArray | sourceCode() const |
静态公共成员
| bool | hasOpenGLShaders(QOpenGLShader::ShaderType type, QOpenGLContext *context = nullptr) |
详细说明
此类支持使用 OpenGL 着色语言 (GLSL) 和 OpenGL/ES 着色语言 (GLSL/ES) 编写的着色器。
QOpenGLShader 和QOpenGLShaderProgram 使程序员无需关注顶点着色器和片段着色器的编译和链接细节。
Qt OpenGLShader 使用的所有数据都应为可信内容。着色器源代码在经过少量修改后,会被传递给底层 OpenGL 实现的编译器,从 Qt 的角度来看,该编译器是一个黑盒子。
警告: 建议应用程序 开发人员在将用户提供的内容传递给compileSourceFile() 等函数之前,仔细考虑其潜在影响。
另请参阅 QOpenGLShaderProgram 。
成员类型文档
enum QOpenGLShader::ShaderTypeBit
flags QOpenGLShader::ShaderType
此枚举指定正在创建的QOpenGLShader 的类型。
| 常量 | 常量值 | 描述 |
|---|---|---|
QOpenGLShader::Vertex | 0x0001 | 用 OpenGL 着色语言 (GLSL) 编写的顶点着色器。 |
QOpenGLShader::Fragment | 0x0002 | 用 OpenGL 着色语言 (GLSL) 编写的片段着色器。 |
QOpenGLShader::Geometry | 0x0004 | 使用 OpenGL 着色语言 (GLSL) 编写的几何着色器(需要 OpenGL >= 3.2 或 OpenGL ES >= 3.2)。 |
QOpenGLShader::TessellationControl | 0x0008 | 使用 OpenGL 着色语言 (GLSL) 编写的曲面细分控制着色器(需要 OpenGL >= 4.0 或 OpenGL ES >= 3.2)。 |
QOpenGLShader::TessellationEvaluation | 0x0010 | 用 OpenGL 着色语言 (GLSL) 编写的曲面细分评估着色器(需要 OpenGL >= 4.0 或 OpenGL ES >= 3.2)。 |
QOpenGLShader::Compute | 0x0020 | 用 OpenGL 着色语言 (GLSL) 编写的计算着色器(需要 OpenGL >= 4.3 或 OpenGL ES >= 3.1)。 |
ShaderType 类型是QFlags<ShaderTypeBit> 的 typedef。它存储 ShaderTypeBit 值的按“或”运算组合。
成员函数文档
[explicit] QOpenGLShader::QOpenGLShader(QOpenGLShader::ShaderType type, QObject *parent = nullptr)
创建一个指定type 的新QOpenGLShader对象,并将其附加到parent 上。如果不支持着色器程序,QOpenGLShaderProgram::hasOpenGLShaderPrograms()将返回false。
调用此构造函数后,通常需要接着调用compileSourceCode()或compileSourceFile()。
该着色器将与当前的QOpenGLContext 相关联。
另请参阅 compileSourceCode() 和compileSourceFile()。
[virtual noexcept] QOpenGLShader::~QOpenGLShader()
删除此着色器。如果该着色器已附加到QOpenGLShaderProgram 对象上,则实际的着色器将一直存在,直到QOpenGLShaderProgram 被销毁为止。
bool QOpenGLShader::compileSourceCode(const char *source)
设置此着色器的source 代码并进行编译。如果源代码编译成功,则返回true ,否则返回false。
另请参阅 compileSourceFile()。
bool QOpenGLShader::compileSourceCode(const QByteArray &source)
设置此着色器的source 代码并进行编译。如果源代码编译成功,则返回true ;否则返回false。
这是一个重载函数。
另请参阅 compileSourceFile()。
bool QOpenGLShader::compileSourceCode(const QString &source)
设置此着色器的source 代码并对其进行编译。如果源代码编译成功,则返回true ;否则返回false。
这是一个重载函数。
另请参阅 compileSourceFile()。
bool QOpenGLShader::compileSourceFile(const QString &fileName)
将此着色器的源代码设置为fileName 中的内容,并进行编译。如果能够打开文件并成功编译源代码,则返回true ;否则返回 false。
另请参阅 compileSourceCode()。
[static] bool QOpenGLShader::hasOpenGLShaders(QOpenGLShader::ShaderType type, QOpenGLContext *context = nullptr)
如果该系统支持类型为type 的着色器程序,则返回true ;否则返回 false。
context 用于解析 GLSL 扩展。如果context 为nullptr ,则使用QOpenGLContext::currentContext()。
bool QOpenGLShader::isCompiled() const
如果该着色器已编译,则返回true ;否则返回false。
另请参阅 compileSourceCode() 和compileSourceFile()。
QString QOpenGLShader::log() const
返回上次编译过程中发生的错误和警告。
另请参阅 compileSourceCode() 和compileSourceFile()。
GLuint QOpenGLShader::shaderId() const
返回与该着色器关联的 OpenGL 标识符。
另请参阅 QOpenGLShaderProgram::programId()。
QOpenGLShader::ShaderType QOpenGLShader::shaderType() const
返回此着色器的类型。
QByteArray QOpenGLShader::sourceCode() const
返回此着色器的源代码。
另请参阅 compileSourceCode()。
© 2026 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.