前言
一 渲染器中的 mat4x4
uniform mat4x4 mat;
vec4 = 列矩阵
#version 330 core
layout(location=1) in vec3 aPos;
layout(location=2) in vec3 aColor;
layout(location=3) in vec2 aTextureCoord;
out vec4 ourcolor;
out vec2 texcoord;
uniform mat4x4 mat;
void main()
{
gl_Position = mat* vec4(aPos.x,aPos.y,aPos.z,1.0f); // mat对点直接作用
ourcolor = vec4(aColor.rgb,1.0f);
texcoord = aTextureCoord;
};
二 产生时间signal
链接时间事件:
QObject::connect(timer, &QTimer::timeout, this, &zlOpenGLWidget::drawTimer);
void zlOpenGLWidget::clickTimer()
{
setFocusPolicy(Qt::StrongFocus); // 强制可以获得消息
timer = new QTimer(this);
QObject::connect(timer, &QTimer::timeout, this, &zlOpenGLWidget::drawTimer);
timer->start(10);
}
三 调用update() 去更新 paintGL()
void zlOpenGLWidget::drawTimer()
{
update(); //其实就是调用 makeCurrent();+ paintGL(); + doneCurrent();
return;
}
四 绘制代码
- mat操作
mat.translate(trans,trans,0.f);
mat.rotate(rot,0.f,0.f,1.f);
mat.scale(sc);
上述代码功能: 缩放 -> 旋转 -> 移动
以上代码都是反正写的 [注意:上述代码就是正确的]
公式
T
r
a
n
s
l
a
t
i
o
n
×
R
o
t
a
t
e
×
S
c
a
l
e
×
V
A
先
作
用
,
C
最
后
作
用
:
C
×
B
×
A
×
V
Translation × Rotate × Scale × V \\ A先作用,C最后作用: C × B × A× V
Translation×Rotate×Scale×VA先作用,C最后作用:C×B×A×V
和V相互作用都是从右到左作用的
static int rot = 1;
static float trans = 0.f;
static float sc = 1.f;
rot ++;
trans += 0.001f;
sc *= 0.99;
QMatrix4x4 mat;
mat.translate(trans,trans,0.f);
mat.rotate(rot,0.f,0.f,1.f);
mat.scale(sc);
glClearColor(0.f,1.0f,1.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
textureWall->bind(0);
textureLe->bind(1);
textureSmall->bind(2);
program.bind();
program.setUniformValue("ourTexture1",0);
program.setUniformValue("ourTexture2",1);
program.setUniformValue("ourTexture3",2);
program.setUniformValue("mat",mat);
glBindVertexArray(VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);
glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);