变换

1、矩阵具有坐标变换的作用,例如:左乘一个旋转矩阵,实现点的坐标旋转,左乘一个平移矩阵实现,点的平移

2、一个点可以同时串联相乘几个变换矩阵,实现坐标连续变换,根据左乘规则,右边矩阵先作用于点,作用顺序从右往左,例如:C*B*A*point(注:C,B,A分别为变换矩阵,point为一个vector3的位置向量,即点的x,y,z坐标)运算式子,矩阵A先作用于point,其次是B,C

3、QMatrix4x4矩阵类本身就支持translate,rotate,projection等连续变换函数,那么问题来了,这些变换的作用顺序是怎么样的,实际上和上面说的矩阵作用规则是一样的。

        顺序是按照后变换,先作用的,具体如下,

        matrix.perspective(45,w/h,0.1,100);     // 透视矩阵变换

        matrix.translate(0.8,0,-5);                    // 平移变换

        matrix.rotate(30,0,1,0)                        // 绕着y轴旋转30度

        matrix*point;                                     // 注:此时这给点是先执行了旋转,再平移,最后透视处理。

                                                                 // 而不是从上往下顺序,先透视,再平移和旋转


上面代码作用等同于如下代码:

        matrix1.perspective(45,w/h,0.1,100);     // 用一个单独矩阵,透视矩阵变换

        matrix2.translate(0.8,0,-5);                    // 用另一个单独矩阵,平移变换

        matrix3.rotate(30,0,1,0)                         // 再用一个单独矩阵,绕着y轴旋转30度
        matrix1*matrix2*matrix3*point;             // 先执行了旋转,再平移,最后透视处理
————————————————
版权声明:本文为CSDN博主「biao2488890051」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/kangkanglhb88008/article/details/80537544

 

变换测试代码:

变换
#ifndef TRIANGLE_H
#define TRIANGLE_H

#include <QWidget>
#include <QGLWidget>
#include <QOpenGLShader>
#include <QOpenGLShaderProgram>
#include <QDebug>
#include <QOpenGLFunctions>
#include <QOpenGLFunctions_3_3_Core>
#include <QTime>
#include <QtMath>
#include <QTimer>
#include <QOpenGLTexture>

#include "shader.h"

namespace Ui {
class Triangle;
}

class Triangle : public QGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT

public:
    explicit Triangle();
    ~Triangle();
public slots:
    void slotUpdateTimer();
protected:
    virtual void initializeGL();
    virtual void paintGL();
    virtual void resizeGL(int w, int h);
private:
    QOpenGLFunctions_3_3_Core *core;
    QOpenGLTexture *texture1, *texture2;
    Shader *ourShader;
    QTime time;
    QTimer updateTimer;
};

#endif // TRIANGLE_H
triangle.h

triangle.cpp:

变换
#include "triangle.h"
#include "ui_triangle.h"

GLuint VBO, VAO, EBO;

Triangle::Triangle()
{

}

Triangle::~Triangle()
{
    delete ourShader;
    core->glDeleteVertexArrays(1, &VAO);
    core->glDeleteBuffers(1, &VBO);
    core->glDeleteBuffers(1, &EBO);
    texture1->destroy();
    texture2->destroy();
}

void Triangle::slotUpdateTimer()
{
    update();
}

void Triangle::initializeGL()
{
    //着色器部分
    core = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
    if (!core) {
          qWarning() << "Could not obtain required OpenGL context version";
          exit(1);
    }
    ourShader = new Shader(":/vertexshader.vert", ":/fragmentshader.frag");

    connect(&updateTimer, &QTimer::timeout, this, &Triangle::slotUpdateTimer);

    //VAO,VBO数据部分
    GLfloat vertices[] = {
        0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f, // top right //注意新的数据,有纹理单元
        0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f, // bottom right
       -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f, // bottom left
       -0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f  // top left
    };

    GLuint indices[] = {
        0, 1, 3, // first triangle
        1, 2, 3  // second triangle
    };

    core->glGenVertexArrays(1, &VAO);
    core->glGenBuffers(1, &VBO);
    core->glGenBuffers(1, &EBO);

    core->glBindVertexArray(VAO);

    core->glBindBuffer(GL_ARRAY_BUFFER, VBO);
    core->glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    core->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    core->glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
    //position attribute
    core->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), nullptr);
    core->glEnableVertexAttribArray(0);
    //color attribute
    core->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (void*)(3 * sizeof(float)));
    core->glEnableVertexAttribArray(1);

    core->glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (void *)(6*sizeof(float)));
    core->glEnableVertexAttribArray(2);

    //纹理1
    texture1 = new QOpenGLTexture(QImage(":/box.jpg").mirrored(), QOpenGLTexture::GenerateMipMaps); //直接生成绑定一个2d纹理, 并生成多级纹理MipMaps
    if(!texture1->isCreated()) {
        qDebug()<<"Failed to load texture" << endl;
    }
    texture1->setWrapMode(QOpenGLTexture::DirectionS, QOpenGLTexture::Repeat);
    texture1->setWrapMode(QOpenGLTexture::DirectionT, QOpenGLTexture::Repeat);

    texture1->setMinificationFilter(QOpenGLTexture::Linear);
    texture1->setMagnificationFilter(QOpenGLTexture::Linear);
    texture1->setFormat(QOpenGLTexture::RGBFormat);


    texture2 = new QOpenGLTexture(QImage(":/awesomeface.png").mirrored(), QOpenGLTexture::GenerateMipMaps); //直接生成绑定一个2d纹理, 并生成多级纹理MipMaps
    if(!texture2->isCreated()) {
        qDebug()<<"Failed to load texture" << endl;
    }
    texture2->setWrapMode(QOpenGLTexture::DirectionS, QOpenGLTexture::Repeat);
    texture2->setWrapMode(QOpenGLTexture::DirectionT, QOpenGLTexture::Repeat);

    texture2->setMinificationFilter(QOpenGLTexture::Linear);
    texture2->setMagnificationFilter(QOpenGLTexture::Linear);
    texture2->setFormat(QOpenGLTexture::RGBFormat);
    ourShader->use();
    ourShader->shaderProgram.setUniformValue("texture1", 0);
    ourShader->shaderProgram.setUniformValue("texture2", 1);

    time.start();
//    updateTimer.start(20);
}

void Triangle::paintGL()
{
    core->glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    core->glClear(GL_COLOR_BUFFER_BIT);

    ourShader->use();
    core->glActiveTexture(GL_TEXTURE1);
    texture2->bind();
    core->glActiveTexture(GL_TEXTURE0);
    texture1->bind();

    QMatrix4x4 transform;
    transform.translate(QVector3D(0.5f, -0.5f, 0.0f));
    transform.rotate((float)time.elapsed()/10, QVector3D(0.0f, 0.0f, 1.0f));
    ourShader->use();
    ourShader->shaderProgram.setUniformValue(ourShader->shaderProgram.uniformLocation("transform"), transform);

    core->glBindVertexArray(VAO);
    core->glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);

    update();
}

void Triangle::resizeGL(int w, int h)
{
    core->glViewport(0, 0, w, h);
}
triangle.cpp

shader.h

变换
#ifndef SHADER_H
#define SHADER_H

#include <QObject>
#include <QDebug>
#include <QOpenGLShader>
#include <QOpenGLShaderProgram>
#include <QString>

class Shader : public QObject
{
    Q_OBJECT
public:
    explicit Shader(const QString& vertexSourcePath, const QString &fragmentSourcePath);
    ~Shader();
    QOpenGLShaderProgram shaderProgram;

    void use() {
        shaderProgram.bind();
    }
signals:

public slots:
};

#endif // SHADER_H
shader.h

shader.cpp

变换
#include "shader.h"

Shader::Shader(const QString &vertexSourcePath, const QString &fragmentSourcePath)
{
    QOpenGLShader vertexShader(QOpenGLShader::Vertex);
    bool success = vertexShader.compileSourceFile(vertexSourcePath);
    if(!success){
        qDebug() << "ERROR::SHADER::VERTEX::COMPILATION_FAILED" << endl;
        qDebug() << vertexShader.log() << endl;
    }

    QOpenGLShader fragmentShader(QOpenGLShader::Fragment);
    success  =fragmentShader.compileSourceFile(fragmentSourcePath);
    if(!success){
        qDebug() << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED" << endl;
        qDebug() << fragmentShader.log() << endl;
    }

    shaderProgram.addShader(&vertexShader);
    shaderProgram.addShader(&fragmentShader);
    success = shaderProgram.link();
    if(!success){
         qDebug() << "ERROR::SHADER::PROGRAM::LINKING_FAILED" << endl;
         qDebug() << shaderProgram.log() << endl;
    }
}

Shader::~Shader()
{

}
shader.cpp

main.cpp

变换
#include "triangle.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Triangle w;
    w.show();

    return a.exec();
}
main.cpp

fragmentshader.frag

变换
#version 330 core
out vec4 FragColor;

in vec3 ourColor;
in vec2 TexCoord;

uniform sampler2D texture1;
uniform sampler2D texture2;

void main()
{
    FragColor = mix(texture2D(texture1, TexCoord), texture2D(texture2, TexCoord), 0.2f);
}
fragmentshader.frag

vertexshader.vert

变换
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;

out vec3 ourColor;
out vec2 TexCoord;

uniform mat4 transform;

void main(){
  gl_Position = transform * vec4(aPos, 1.0f);
  ourColor = aColor;
  TexCoord = aTexCoord;
}
vertexshader.vert

 

上一篇:自考新教材-p328_5(1)


下一篇:Leetcode 120.三角形最小路径和(Triangle)