QOpenGLShaderProgram绘制文本文字hello, 给出列子
时间: 2023-05-26 22:01:10 浏览: 387
以下是使用QOpenGLShaderProgram在OpenGL中绘制文本的示例代码:
```cpp
#include <QApplication>
#include <QOpenGLWidget>
#include <QOpenGLShaderProgram>
#include <QOpenGLFunctions>
#include <QFontMetrics>
#include <QImage>
#include <QDebug>
class TextRenderer : public QOpenGLWidget, protected QOpenGLFunctions {
Q_OBJECT
public:
TextRenderer(QWidget *parent = nullptr) : QOpenGLWidget(parent) {}
protected:
void initializeGL() override {
initializeOpenGLFunctions();
// Compile vertex shader
const char *vshader_source =
"#version 330 core\n"
"in vec2 pos;\n"
"in vec2 texcoord;\n"
"out vec2 frag_texcoord;\n"
"void main() {\n"
" frag_texcoord = texcoord;\n"
" gl_Position = vec4(pos, 0.0, 1.0);\n"
"}\n";
vertex_shader_.compileSourceCode(vshader_source);
// Compile fragment shader
const char *fshader_source =
"#version 330 core\n"
"in vec2 frag_texcoord;\n"
"uniform sampler2D tex;\n"
"uniform vec4 color;\n"
"out vec4 frag_color;\n"
"void main() {\n"
" frag_color = texture(tex, frag_texcoord) * color;\n"
"}\n";
fragment_shader_.compileSourceCode(fshader_source);
// Link shader program
shader_program_.addShader(&vertex_shader_);
shader_program_.addShader(&fragment_shader_);
shader_program_.bindAttributeLocation("pos", 0);
shader_program_.bindAttributeLocation("texcoord", 1);
shader_program_.link();
// Get uniform locations
color_location_ = shader_program_.uniformLocation("color");
texture_location_ = shader_program_.uniformLocation("tex");
// Create texture
QImage image(256, 256, QImage::Format_ARGB32);
image.fill(0);
QPainter painter(&image);
painter.setFont(QFont("Arial", 60));
painter.setPen(QPen(Qt::white));
painter.drawText(image.rect(), Qt::AlignCenter, "Hello");
texture_.create();
texture_.setSize(image.width(), image.height());
texture_.setFormat(QOpenGLTexture::RGBA8_UNorm);
texture_.setWrapMode(QOpenGLTexture::ClampToEdge);
texture_.setMinificationFilter(QOpenGLTexture::Nearest);
texture_.setMagnificationFilter(QOpenGLTexture::Nearest);
texture_.allocateStorage();
texture_.setData(image);
// Get texture size
texture_width_ = static_cast<float>(image.width());
texture_height_ = static_cast<float>(image.height());
// Create vertex buffer
float vertex_data[] = {
// Position Texcoord
-0.5f, 0.5f, 0.0f, 1.0f,
0.5f, 0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 0.0f,
};
vertex_buffer_.create();
vertex_buffer_.bind();
vertex_buffer_.allocate(vertex_data, sizeof(vertex_data));
// Create index buffer
unsigned int index_data[] = {
3, 0, 1,
3, 1, 2,
};
index_buffer_.create();
index_buffer_.bind();
index_buffer_.allocate(index_data, sizeof(index_data));
// Enable vertex arrays
shader_program_.enableAttributeArray(0);
shader_program_.enableAttributeArray(1);
shader_program_.setAttributeBuffer(0, GL_FLOAT, 0, 2, sizeof(float) * 4);
shader_program_.setAttributeBuffer(1, GL_FLOAT, sizeof(float) * 2, 2, sizeof(float) * 4);
// Enable depth testing
glEnable(GL_DEPTH_TEST);
}
void resizeGL(int width, int height) override {
glViewport(0, 0, width, height);
projection_ = QMatrix4x4();
projection_.ortho(0.0f, static_cast<float>(width), static_cast<float>(height), 0.0f, -1.0f, 1.0f);
}
void paintGL() override {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Draw text
shader_program_.bind();
shader_program_.setUniformValue(color_location_, QColor(Qt::white));
shader_program_.setUniformValue(texture_location_, 0);
texture_.bind();
vertex_buffer_.bind();
index_buffer_.bind();
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
}
private:
QOpenGLShaderProgram shader_program_;
QOpenGLShader vertex_shader_{QOpenGLShader::Vertex};
QOpenGLShader fragment_shader_{QOpenGLShader::Fragment};
int color_location_;
int texture_location_;
QOpenGLTexture texture_;
float texture_width_;
float texture_height_;
QOpenGLBuffer vertex_buffer_;
QOpenGLBuffer index_buffer_;
QMatrix4x4 projection_;
};
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
TextRenderer renderer;
renderer.show();
return a.exec();
}
#include "main.moc"
```
在此示例中,`QOpenGLShaderProgram`被用于编译和链接GLSL着色器程序。它使用一个由纹理和颜色组成的“正方形”网格来显示文本。使用`QFontMetrics`类计算了文本的大小,并使用`QPainter`类在QImage中绘制了文本。然后通过`QOpenGLTexture`将`QImage`数据加载到OpenGL中。`QOpenGLBuffer`类用于存储网格数据,而`QMatrix4x4`类用于计算投影矩阵。最后,通过调用`glDrawElements`方法来渲染网格。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)