4-4 代码实现 矩阵

前言

本节代码:下载地址

一 渲染器中的 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;
}

四 绘制代码

  1. 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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值