QT中学习Opengl---(旋转、平移、缩放)

前言:

本文的代码是 LearnOpenGL 中对应代码,这里提供学习,大家喜欢的可去官方网站去看看:

LearnOpenGL-CNicon-default.png?t=M1H3https://learnopengl-cn.readthedocs.io/zh/latest/ 本章简单记录,使用旋转、平移、缩放。矩阵相关计算是基础东西,记得以前还问老师,学习高代有啥用。哈哈,一切都会用的。

缩放:

  基本的矩阵变换:

 这样,对应的x y z 对应放大对应倍数。

平移

 基本的矩阵变换:

 这样就是平移对应位置了。

旋转

沿x轴旋转:

 沿y轴旋转:

 沿z轴旋转:

 任意轴:

 矩阵的组合

 我们有一个顶点(x, y,z), 我们希望将其缩放2倍, 然后用位移(1, 2, 3)来平移它。

 这里注意:矩阵乘法是不可交换的。我们可以记录方向是←方向添加矩阵就好就好,第一缩放,第二,在左边放置位移。

举例说明:

 正常状态:

 我们先缩放,然后向x移动0.4

 matrix.translate(0.4f,0.0,0.0);
 matrix.scale(0.5);

结果图: 

 

  我们先向x移动0.4,然后缩放。

matrix.scale(0.5);
matrix.translate(0.4f,0.0,0.0);

 

 我们发现,效果是不一样的。包括你使用旋转,代码使用的顺序与逻辑是相反过来的。

然后我们在加上旋转。

先缩放,在平移,在旋转。

matrix.rotate(45,0,0,1);
matrix.translate(0.4f,0.0,0.0);
matrix.scale(0.5);

 先缩放,在旋转,在平移 。

matrix.translate(0.4f,0.0,0.0);
matrix.rotate(45,0,0,1);
matrix.scale(0.5);

 本例中是采用qt类中的写法。书中采用glm库来对矩阵操作。

glm::mat4 trans;
trans = glm::rotate(trans, 90.0f, glm::vec3(0.0, 0.0, 1.0));
trans = glm::scale(trans, glm::vec3(0.5, 0.5, 0.5));

道理都是一样的。

代码部分:

 顶点着色器写法: 

#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.0);
        ourColor = aColor;
        TexCoord = vec2(aTexCoord.x, aTexCoord.y);
}

 片段着色器:

#version 330 core
out vec4 FragColor;

in vec3 ourColor;
in vec2 TexCoord;

// texture samplers
uniform sampler2D texture1;
uniform sampler2D texture2;

void main()
{
        // linearly interpolate between both textures (80% container, 20% awesomeface)
        FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2);
}

主要代码: 

#include "bkqopenglw.h"
#include<iostream>
#include <QDebug>

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

float vertices[] = {
        // positions          // colors           // texture coords
         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
    };


unsigned int indices[] = {  // note that we start from 0!
    0, 1, 3,  // first Triangle
    1, 2, 3   // second Triangle
};

BKQOpenglW::BKQOpenglW(QWidget *parent) : QOpenGLWidget(parent)
{
    m_Shape = Shape::None;
    setFocusPolicy(Qt::StrongFocus); //强制聚焦 这样时刻可以监视一些东西

}

BKQOpenglW::~BKQOpenglW()
{
    makeCurrent();
    glDeleteVertexArrays(1,&VAO);
    glDeleteBuffers(1,&VBO);
    doneCurrent();
}

void BKQOpenglW::drawShapes(BKQOpenglW::Shape shape)
{
    m_Shape = shape;
    update();
}

void BKQOpenglW::setWireFrame(bool b)
{
    makeCurrent();
    if(b)
    {
       glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    }
    else {
       glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    }
    update();
    doneCurrent();
}

void BKQOpenglW::initializeGL()
{
    initializeOpenGLFunctions();

    //    shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex,vertexShaderSource);
    //    shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment,fragmentShaderSource);
    shaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex,":/shader/shader.vs");
    shaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment,":/shader/shader.fs");
    shaderProgram.link();
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    //绑定ebo
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),indices,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);
    // texture coord attribute
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
    glEnableVertexAttribArray(2);

    //绑定纹理

    pTexture = new QOpenGLTexture(QImage(":/images/images/brickwall.jpg").mirrored());
    pTexture2 = new QOpenGLTexture(QImage(":/images/images/awesomeface.png").mirrored());
    shaderProgram.bind();
    shaderProgram.setUniformValue("texture1",0);
    shaderProgram.setUniformValue("texture2",1);
    // note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    // You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
    // VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
    glBindVertexArray(0);

}

void BKQOpenglW::resizeGL(int w, int h)
{
       glViewport(0,0,w,h);
}

void BKQOpenglW::paintGL()
{
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    QMatrix4x4 matrix;
    matrix.setToIdentity();
    matrix.translate(0.4f,0.0,0.0);
    matrix.rotate(45,0,0,1);
    matrix.scale(0.5);

    shaderProgram.bind();
    glBindVertexArray(VAO);
    switch (m_Shape) {
    case Triangle:
        glDrawArrays(GL_TRIANGLES,0,3);
        break;
    case Rect:
        pTexture->bind(0);
        pTexture2->bind(1);
        shaderProgram.setUniformValue("transform",matrix);
        glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);
        break;
    default:
        break;
    }

}

 

可以使用 QOpenGLWidget 类来创建一个 OpenGL 窗口,并使用 QMatrix4x4 类来实现旋转平移缩放。以下是一个简单的示例代码: ```python from PyQt5.QtWidgets import QApplication, QOpenGLWidget from PyQt5.QtGui import QOpenGLShader, QOpenGLShaderProgram, QMatrix4x4 from PyQt5.QtCore import Qt class OpenGLWidget(QOpenGLWidget): def initializeGL(self): self.shaderProgram = QOpenGLShaderProgram() self.shaderProgram.addShaderFromSourceFile(QOpenGLShader.Vertex, "vertexShader.glsl") self.shaderProgram.addShaderFromSourceFile(QOpenGLShader.Fragment, "fragmentShader.glsl") self.shaderProgram.link() self.vertices = [ -0.5, -0.5, 0.0, 0.5, -0.5, 0.0, 0.0, 0.5, 0.0 ] self.vbo = self.createBuffer(self.vertices) self.projectionMatrix = QMatrix4x4() self.projectionMatrix.perspective(60, self.width() / self.height(), 0.1, 100.0) self.viewMatrix = QMatrix4x4() self.viewMatrix.translate(0.0, 0.0, -3.0) self.modelMatrix = QMatrix4x4() def paintGL(self): self.shaderProgram.bind() self.shaderProgram.setUniformValue("projectionMatrix", self.projectionMatrix) self.shaderProgram.setUniformValue("viewMatrix", self.viewMatrix) self.shaderProgram.setUniformValue("modelMatrix", self.modelMatrix) self.vbo.bind() self.shaderProgram.setAttributeBuffer("vertexPosition", Qt.GL_FLOAT, 0, 3) self.shaderProgram.enableAttributeArray("vertexPosition") self.glDrawArrays(Qt.GL_TRIANGLES, 0, 3) self.vbo.release() self.shaderProgram.release() def resizeGL(self, width, height): self.projectionMatrix.setToIdentity() self.projectionMatrix.perspective(60, width / height, 0.1, 100.0) def createBuffer(self, data): vbo = QOpenGLBuffer(QOpenGLBuffer.VertexBuffer) vbo.create() vbo.bind() vbo.allocate(data, len(data) * 4) vbo.release() return vbo ``` 这个示例代码使用了一个简单的三角形作为渲染对象,使用了一个顶点着色器和一个片段着色器来实现 OpenGL 渲染。可以通过修改顶点着色器和片段着色器来实现不同的效果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值