Qt 封装了变换矩阵为 QMatrix4x4 类,类似于二维的 QTransform。
//顶点着色器
#version 450 core
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec3 aColor;
layout(location = 2) in vec2 aTexCord;
out vec3 ourColor;
out vec2 TexCord;
uniform mat4 theMatrix;//4维变换矩阵
void main()
{
gl_Position = theMatrix * vec4(aPos, 1.0f);
ourColor = aColor;
TexCord = aTexCord;
}
//片段着色器
#version 450 core
out vec4 FragColor;
in vec3 ourColor;
in vec2 TexCord;
uniform sampler2D textureWall;
uniform sampler2D textureSmile;
void main()
{
FragColor = mix(texture(textureWall,TexCord),
texture(textureSmile,TexCord),0.5);
}
#include "widget.h"
#include <QOpenGLShaderProgram>
#include <QTimer>
#include <QTime>
#include <QOpenGLTexture>
unsigned int VBO, VAO,EBO;
float vertices[] =
{
//位置 颜色 纹理坐标
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f
};
unsigned int indices[] =
{
0, 1, 3,
1, 2, 3
};
Widget::Widget(QWidget *parent) : QOpenGLWidget(parent)
{
auto timer = new QTimer(this);
connect(timer,&QTimer::timeout,this,[this]{update();});
timer->start(100);
}
Widget::~Widget()
{
makeCurrent();
glDeleteBuffers(1,&VBO);
glDeleteBuffers(1,&EBO);
glDeleteVertexArrays(1,&VAO);
doneCurrent();
}
void Widget::initializeGL()
{
initializeOpenGLFunctions();
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), nullptr);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, 0);
shaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex,":/shapes.vert");
shaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment,":/shapes.frag");
if(!shaderProgram.link())
qDebug()<<"着色器程序编译错误信息:"<<shaderProgram.log();
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
textureWall = new QOpenGLTexture(QImage(":/image/wall.jpg").mirrored());
textureSmile = new QOpenGLTexture(QImage(":/image/awesomeface.png").mirrored());
shaderProgram.bind();
shaderProgram.setUniformValue("textureWall",0);//纹理单元0
shaderProgram.setUniformValue("textureSmile",1);//纹理单元1
glBindVertexArray(0);
}
void Widget::paintGL()
{
QMatrix4x4 matrix;
uint time = QTime::currentTime().msec();
matrix.translate(0.5,-0.5,0);
matrix.rotate(time, 0.0f, 0.0f, 1.0f);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
shaderProgram.bind();
shaderProgram.setUniformValue("ratio",0.8f);
shaderProgram.setUniformValue("theMatrix", matrix);
glBindVertexArray(VAO);
textureWall->bind(0);
textureSmile->bind(1);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
}