QOpenGLShader Class

QOpenGLShader 类允许编译 OpenGL 着色器。更多

头文件: #include <QOpenGLShader>
CMake: find_package(Qt6 REQUIRED COMPONENTS OpenGL)
target_link_libraries(mytarget PRIVATE Qt6::OpenGL)
qmake: QT += opengl
继承: QObject

公共类型

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 使程序员免于编译和链接顶点与片段着色器的细节。

QOpenGLShader 使用的所有数据都是可信内容。着色器源代码可能在经过最小的修改后被传递给底层 OpenGL 实现的编译器,从 Qt 的角度来看,编译器是一个黑盒子。

警告: 建议应用程序开发人员在向compileSourceFile() 等函数传递用户提供的内容之前,仔细考虑潜在的影响。

另请参阅 QOpenGLShaderProgram

成员类型文档

枚举 QOpenGLShader::ShaderTypeBit
flags QOpenGLShader::ShaderType

这个枚举指定了正在创建的QOpenGLShader 的类型。

常量描述
QOpenGLShader::Vertex0x0001用 OpenGL 着色语言(GLSL)编写的顶点着色器。
QOpenGLShader::Fragment0x0002以 OpenGL 着色语言(GLSL)编写的片段着色器。
QOpenGLShader::Geometry0x0004以 OpenGL 着色语言 (GLSL) 编写的几何着色器(要求 OpenGL >= 3.2 或 OpenGL ES >= 3.2)。
QOpenGLShader::TessellationControl0x0008以 OpenGL 着色语言 (GLSL) 编写的细分控制着色器(要求 OpenGL >= 4.0 或 OpenGL ES >= 3.2)。
QOpenGLShader::TessellationEvaluation0x0010以 OpenGL 着色语言 (GLSL) 编写的细分评估着色器(要求 OpenGL >= 4.0 或 OpenGL ES >= 3.2)。
QOpenGLShader::Compute0x0020用 OpenGL 着色语言 (GLSL) 编写的计算着色器(要求 OpenGL >= 4.3 或 OpenGL ES >= 3.1)。

ShaderType 类型是QFlags<ShaderTypeBit> 的类型定义。它存储 ShaderTypeBit 值的 OR 组合。

成员函数文档

[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 扩展。如果contextnullptr ,则使用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().

© 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.