QOpenGLWidget 绘制YUV和空心矩形,给出代码
时间: 2023-05-20 20:05:40 浏览: 109
以下是一个简单的 QOpenGLWidget 绘制 YUV 和空心矩形的示例代码:
```cpp
void MyOpenGLWidget::initializeGL()
{
initializeOpenGLFunctions();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
void MyOpenGLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
// 绘制 YUV 图像
glBindTexture(GL_TEXTURE_2D, yuvTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, yData);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, uvTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RG, width / 2, height / 2, 0, GL_RG, GL_UNSIGNED_BYTE, uvData);
glUniform1i(yuvSamplerLoc, 0);
glUniform1i(uvSamplerLoc, 1);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// 绘制空心矩形
glUseProgram(rectProgram);
glBindVertexArray(rectVAO);
glUniform4f(rectColorLoc, 1.0f, 0.0f, 0.0f, 1.0f);
glDrawArrays(GL_LINE_LOOP, 0, 4);
}
void MyOpenGLWidget::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
}
void MyOpenGLWidget::createYUVTexture()
{
glGenTextures(1, &yuvTexture);
glBindTexture(GL_TEXTURE_2D, yuvTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
void MyOpenGLWidget::createUVTexture()
{
glGenTextures(1, &uvTexture);
glBindTexture(GL_TEXTURE_2D, uvTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
void MyOpenGLWidget::createRectProgram()
{
rectProgram = glCreateProgram();
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
const char* vsSource = "#version 330 core\n"
"layout (location = 0) in vec2 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos, 0.0, 1.0);\n"
"}\n";
const char* fsSource = "#version 330 core\n"
"uniform vec4 color;\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = color;\n"
"}\n";
glShaderSource(vs, 1, &vsSource, NULL);
glCompileShader(vs);
glShaderSource(fs, 1, &fsSource, NULL);
glCompileShader(fs);
glAttachShader(rectProgram, vs);
glAttachShader(rectProgram, fs);
glLinkProgram(rectProgram);
glDeleteShader(vs);
glDeleteShader(fs);
rectColorLoc = glGetUniformLocation(rectProgram, "color");
}
void MyOpenGLWidget::createRectVAO()
{
GLfloat vertices[] = {
-0.5f, 0.5f,
0.5f, 0.5f,
0.5f, -0.5f,
-0.5f, -0.5f
};
glGenVertexArrays(1, &rectVAO);
glBindVertexArray(rectVAO);
GLuint rectVBO;
glGenBuffers(1, &rectVBO);
glBindBuffer(GL_ARRAY_BUFFER, rectVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (void*)0);
glEnableVertexAttribArray(0);
}
```
其中,yuvTexture 和 uvTexture 是 YUV 图像的纹理 ID,yData 和 uvData 是 YUV 图像的数据,width 和 height 是图像的宽度和高度。rectProgram 是绘制空心矩形的着色器程序 ID,rectVAO 是空心矩形的顶点数组对象 ID,rectColorLoc 是颜色 uniform 变量的位置。在 paintGL 函数中,首先绑定 YUV 纹理和 UV 纹理,然后绘制 YUV 图像,最后绘制空心矩形。在 createYUVTexture、createUVTexture、createRectProgram 和 createRectVAO 函数中,分别创建 YUV 纹理、UV 纹理、着色器程序和顶点数组对象。
阅读全文