基于Qt的OpenGL编程(3.x以上GLSL可编程管线版)---(七)摄像机

本篇目的是学习如何在OpenGL自由观察,即设计一个类似于3dMAX,Blender,或Unity3D中常见的摄像机观察机制。具有可以用键盘前后左右上下的视角控制功能,和鼠标拖移视角的功能。

Vries的原教程地址如下,https://learnopengl-cn.github.io/01%20Getting%20started/09%20Camera/关于OpenGL函数的详细解析及摄像机这一知识点描述请看这个教程,本篇旨在对Vires基于visual studio的编程思想做Qt平台的移植)

Qt开发平台:5.8.0

编译器:Desktop Qt 5.8.0 MSVC2015_64bit 


(一)视角环绕


让我们尝试将视角动起来,像上图所示。其实很简单,在上篇学习教程的代码中只要引入lookat()//(一个封装好的类摄像机函数,具体解析看Vires的教程啦),稍微修改一下widget.cpp函数即可。

项目组织如下:


widget.cpp

#include "widget.h"
GLuint VBO, VAO;

Triangle::Triangle(){
    this->setWindowTitle("Camera");
}

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

void Triangle::initializeGL(){
    //着色器部分
    core = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
    ourShader = new Shader(":/shaders/vertexshadersource.vert", ":/shaders/fragmentshadersource.frag");

    //VAO,VBO数据部分
    float vertices[] = {
        -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
         0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
         0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
         0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,

        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
         0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
        -0.5f,  0.5f,  0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,

        -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
        -0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
        -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

         0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
         0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
         0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
         0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
         0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
         0.5f, -0.5f, -0.5f,  1.0f, 1.0f,
         0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
         0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,

        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
         0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
        -0.5f,  0.5f,  0.5f,  0.0f, 0.0f,
        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f
    };


    core->glGenVertexArrays(1, &VAO);//两个参数,第一个为需要创建的缓存数量。第二个为用于存储单一ID或多个ID的GLuint变量或数组的地址
    core->glGenBuffers(1, &VBO);

    core->glBindVertexArray(VAO);

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

    core->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
    core->glEnableVertexAttribArray(0);

    // texture coord attribute
    core->glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
    core->glEnableVertexAttribArray(1);

    //纹理
    //第一张箱子
    texture1 = new QOpenGLTexture(QImage(":/textures/res/textures/container.jpg").mirrored(), QOpenGLTexture::GenerateMipMaps); //直接生成绑定一个2d纹理, 并生成多级纹理MipMaps
    if(!texture1->isCreated()){
        qDebug() << "Failed to load texture" << endl;
    }
    texture1->setWrapMode(QOpenGLTexture::DirectionS, QOpenGLTexture::Repeat);// 等于glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    texture1->setWrapMode(QOpenGLTexture::DirectionT, QOpenGLTexture::Repeat);//    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    texture1->setMinificationFilter(QOpenGLTexture::Linear);   //等价于glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    texture1->setMagnificationFilter(QOpenGLTexture::Linear);  //     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


    //第二张笑脸
    texture2 = new QOpenGLTexture(QImage(":/textures/res/textures/smile.png").mirrored(), QOpenGLTexture::GenerateMipMaps); //直接生成绑定一个2d纹理, 并生成多级纹理MipMaps
    if(!texture2->isCreated()){
        qDebug() << "Failed to load texture" << endl;
    }
    texture2->setWrapMode(QOpenGLTexture::DirectionS, QOpenGLTexture::Repeat);// 等于glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    texture2->setWrapMode(QOpenGLTexture::DirectionT, QOpenGLTexture::Repeat);//    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    texture2->setMinificationFilter(QOpenGLTexture::Linear);   //等价于glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    texture2->setMagnificationFilter(QOpenGLTexture::Linear);  //     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    //设置纹理单元编号
    ourShader->use();
    ourShader->setInt("texture1", 0);
    ourShader->setInt("texture2", 1);

    //开启计时器,返回毫秒
    time.start();

    //给着色器变量赋值
    QMatrix4x4 projection;
   // view.translate(QVector3D(0.0f, 0.0f, -3.0f));
    projection.perspective(45.0f, (GLfloat)width()/(GLfloat)height(), 0.1f, 100.0f);

    ourShader->use();
    //ourShader->setMat4("view", view);
    ourShader->setMat4("projection", projection);

    //开启状态
    core->glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    core->glEnable(GL_DEPTH_TEST);
}

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

QVector3D cubePositions[] = {       //为了省事,设为全局函数,若嫌封装性不好,改为成员变量指针,在initializeGL()重新赋值就好
  QVector3D( 0.0f,  0.0f,  -1.0f), //小小改动一下将z轴的0.0f变为-1.0f
  QVector3D( 2.0f,  5.0f, -15.0f),
  QVector3D(-1.5f, -2.2f, -2.5f),
  QVector3D(-3.8f, -2.0f, -12.3f),
  QVector3D( 2.4f, -0.4f, -3.5f),
  QVector3D(-1.7f,  3.0f, -7.5f),
  QVector3D( 1.3f, -2.0f, -2.5f),
  QVector3D( 1.5f,  2.0f, -2.5f),
  QVector3D( 1.5f,  0.2f, -1.5f),
  QVector3D(-1.3f,  1.0f, -1.5f)
};

void Triangle::paintGL(){
    core->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

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

    ourShader->use();

    QMatrix4x4 view;
    GLfloat radius = 15.0f;
    GLfloat camX = sin(((GLfloat)time.elapsed())/1000) * radius; //返回的毫秒太大了,除以1000小一些
    GLfloat camZ = cos(((GLfloat)time.elapsed())/1000) * radius;
    view.lookAt(QVector3D(camX, 0.0f, camZ), QVector3D(0.0f, 0.0f, 0.0f), QVector3D(0.0f, 1.0f, 0.0f));
    ourShader->setMat4("view", view);

    for(GLuint i = 0; i != 10; ++i){
        QMatrix4x4 model;
        model.translate(cubePositions[i]);
        model.rotate(20.0f * i, cubePositions[i]);//这里角度改为固定角度


        ourShader->setMat4("model", model);
        core->glBindVertexArray(VAO);
        core->glDrawArrays(GL_TRIANGLES, 0, 36);
    }

    update();
}
 
 

以下是没有改动过的函数:

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QOpenGLWidget>
#include <QDebug>
#include <QOpenGLFunctions_3_3_Core>
#include "shader.h"
#include <QOpenGLTexture>
#include <QTime>    //增添头文件

class Triangle : public QOpenGLWidget
{
public:
    Triangle();
    GLuint a;
    ~Triangle();
protected:
    virtual void initializeGL();
    virtual void resizeGL(int w, int h);
    virtual void paintGL();
private:
    Shader *ourShader;
    QOpenGLTexture *texture1;
    QOpenGLTexture *texture2;

    QOpenGLFunctions_3_3_Core *core;
    QTime time; //增添QTime对象,替代glfwGetTime()函数
};

#endif // WIDGET_H

shader.h

#ifndef SHADER_H
#define SHADER_H

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

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

  void use(){
    shaderProgram->bind();
  }

  //还是把设置着色器uniform变量操作写成Shader里的inline成员函数管理,真的方便很多。
  void setMat4(const QString& name, const QMatrix4x4& value){
      GLuint loc = shaderProgram->uniformLocation(name);
      shaderProgram->setUniformValue(loc, value);
  }

  void setInt(const QString& name, const GLint& value){
      GLuint loc = shaderProgram->uniformLocation(name);
      shaderProgram->setUniformValue(loc, value);
  }
};

#endif // SHADER_H

shader.cpp

#include "shader.h"

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

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

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

Shader::~Shader(){
    delete shaderProgram;
}

main.cpp

#include "widget.h"
#include <QApplication>

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

    return a.exec();
}

vertexshadersource.vert

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

out vec2 TexCoord;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main(){
  gl_Position = projection * view * model * vec4(aPos, 1.0f);
  TexCoord = aTexCoord;
}

fragmentshadersource.frag

#version 330 core
out vec4 FragColor;

in vec2 TexCoord;

uniform sampler2D texture1;
uniform sampler2D texture2;

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

(二)自由移动

初步实现,使用键盘前后左右上下控制视角的功能。如下图所示

这次仅修改widget.h与widget.cpp函数


widget.h

为封装性考虑,将Vires放在cpp文件中的全局变量cameraPos,cameraFront等变量设为了成员变量

#ifndef WIDGET_H
#define WIDGET_H

#include <QOpenGLWidget>
#include <QDebug>
#include <QOpenGLFunctions_3_3_Core>
#include "shader.h"
#include <QOpenGLTexture>
#include <QTime>    //增添头文件
#include <QVector3D>

class Triangle : public QOpenGLWidget
{
public:
    Triangle();
    GLuint a;
    ~Triangle();
protected:
    virtual void initializeGL();
    virtual void resizeGL(int w, int h);
    virtual void paintGL();
    void keyPressEvent(QKeyEvent *event);

private:
    Shader *ourShader;
    QOpenGLTexture *texture1;
    QOpenGLTexture *texture2;

    QOpenGLFunctions_3_3_Core *core;
    QTime time; //增添QTime对象,替代glfwGetTime()函数

    QVector3D cameraPos;
    QVector3D cameraFront;
    QVector3D cameraUp;
    GLfloat deltaTime;
    GLfloat lastFrame;
};

#endif // WIDGET_H

widget.cpp

因为没有不再实现箱子的旋转动画效果了,按理说应该删除paintGL()函数里的update()刷新函数,以节省资源,可按Vries大神的思想,deltaTime,即帧与帧之间的间隔时间又必须在放循环里才能得到。参考官方给的示例,对与循环机制,是靠一个计时器timer不断调用刷新函数update()实现的,与把update()放在paintGL()里本质上没什么区别,区别就是timer是每隔20毫秒刷新一次,另一个则是取决于电脑性能本身,性能好就刷新快,差就刷新慢。所以思想上还是跟Vries看起,把update()放在paintGL()里吧。



//示例里的一段函数,每隔20秒,执行一次rotateOneStep()函数,函数里执行rotateBy()函数,而这个函数里包含有update()刷新函数。

#include "widget.h"
#include <QKeyEvent>
GLuint VBO, VAO;

Triangle::Triangle(){
    this->setWindowTitle("Camera");
}

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

void Triangle::initializeGL(){
    //着色器部分
    core = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
    ourShader = new Shader(":/shaders/vertexshadersource.vert", ":/shaders/fragmentshadersource.frag");

    //VAO,VBO数据部分
    float vertices[] = {
        -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
         0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
         0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
         0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,

        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
         0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
        -0.5f,  0.5f,  0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,

        -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
        -0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
        -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

         0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
         0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
         0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
         0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
         0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
         0.5f, -0.5f, -0.5f,  1.0f, 1.0f,
         0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
         0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,

        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
         0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
        -0.5f,  0.5f,  0.5f,  0.0f, 0.0f,
        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f
    };


    core->glGenVertexArrays(1, &VAO);//两个参数,第一个为需要创建的缓存数量。第二个为用于存储单一ID或多个ID的GLuint变量或数组的地址
    core->glGenBuffers(1, &VBO);

    core->glBindVertexArray(VAO);

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

    core->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
    core->glEnableVertexAttribArray(0);

    // texture coord attribute
    core->glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
    core->glEnableVertexAttribArray(1);

    //纹理
    //第一张箱子
    texture1 = new QOpenGLTexture(QImage(":/textures/res/textures/container.jpg").mirrored(), QOpenGLTexture::GenerateMipMaps); //直接生成绑定一个2d纹理, 并生成多级纹理MipMaps
    if(!texture1->isCreated()){
        qDebug() << "Failed to load texture" << endl;
    }
    texture1->setWrapMode(QOpenGLTexture::DirectionS, QOpenGLTexture::Repeat);// 等于glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    texture1->setWrapMode(QOpenGLTexture::DirectionT, QOpenGLTexture::Repeat);//    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    texture1->setMinificationFilter(QOpenGLTexture::Linear);   //等价于glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    texture1->setMagnificationFilter(QOpenGLTexture::Linear);  //     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


    //第二张笑脸
    texture2 = new QOpenGLTexture(QImage(":/textures/res/textures/smile.png").mirrored(), QOpenGLTexture::GenerateMipMaps); //直接生成绑定一个2d纹理, 并生成多级纹理MipMaps
    if(!texture2->isCreated()){
        qDebug() << "Failed to load texture" << endl;
    }
    texture2->setWrapMode(QOpenGLTexture::DirectionS, QOpenGLTexture::Repeat);// 等于glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    texture2->setWrapMode(QOpenGLTexture::DirectionT, QOpenGLTexture::Repeat);//    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    texture2->setMinificationFilter(QOpenGLTexture::Linear);   //等价于glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    texture2->setMagnificationFilter(QOpenGLTexture::Linear);  //     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    //设置纹理单元编号
    ourShader->use();
    ourShader->setInt("texture1", 0);
    ourShader->setInt("texture2", 1);

    //开启计时器,返回毫秒
    time.start();

    //给着色器变量赋值
    QMatrix4x4 projection;
    projection.perspective(45.0f, (GLfloat)width()/(GLfloat)height(), 0.1f, 100.0f);

    ourShader->use();
    ourShader->setMat4("projection", projection);

    //开启状态
    core->glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    core->glEnable(GL_DEPTH_TEST);

    //emmm ,本来成员变量的初始化应该放在构造函数里,不过这些变量和OpenGL相关,就放在initializeGL()里方便管理;
    cameraPos = QVector3D(0.0f, 0.0f,  3.0f);
    cameraFront = QVector3D(0.0f, 0.0f, -1.0f);
    cameraUp = QVector3D(0.0f, 1.0f,  0.0f);
    deltaTime = 0.0f;
    lastFrame = 0.0f;

}

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

QVector3D cubePositions[] = {       //为了省事,设为全局函数,若嫌封装性不好,改为成员变量指针,在initializeGL()重新赋值就好
  QVector3D( 0.0f,  0.0f,  -1.0f), //小小改动一下将z轴的0.0f变为-1.0f
  QVector3D( 2.0f,  5.0f, -15.0f),
  QVector3D(-1.5f, -2.2f, -2.5f),
  QVector3D(-3.8f, -2.0f, -12.3f),
  QVector3D( 2.4f, -0.4f, -3.5f),
  QVector3D(-1.7f,  3.0f, -7.5f),
  QVector3D( 1.3f, -2.0f, -2.5f),
  QVector3D( 1.5f,  2.0f, -2.5f),
  QVector3D( 1.5f,  0.2f, -1.5f),
  QVector3D(-1.3f,  1.0f, -1.5f)
};

void Triangle::paintGL(){
    GLfloat currentFrame = (GLfloat)time.elapsed()/100;
    deltaTime = currentFrame - lastFrame;
    lastFrame = currentFrame;


    core->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

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

    ourShader->use();

    QMatrix4x4 view;
    view.lookAt(cameraPos, cameraPos+cameraFront, cameraUp);
    ourShader->setMat4("view", view);

    for(GLuint i = 0; i != 10; ++i){
        QMatrix4x4 model;
        model.translate(cubePositions[i]);
        model.rotate(20.0f * i, cubePositions[i]);//这里角度的设置仍然为时间,如果按照Vires大神写的i*20, 箱子不会旋转


        ourShader->setMat4("model", model);
        core->glBindVertexArray(VAO);
        core->glDrawArrays(GL_TRIANGLES, 0, 36);
    }

    update();
}

void Triangle::keyPressEvent(QKeyEvent *event)
{
    GLfloat cameraSpeed = 2.5 * deltaTime;
    if(event->key() == Qt::Key_W){
        cameraPos += cameraFront * cameraSpeed;
    }
    if(event->key() == Qt::Key_S){
        cameraPos -= cameraFront * cameraSpeed;
    }
    if(event->key() == Qt::Key_A){
        cameraPos -= (QVector3D::crossProduct(cameraFront, cameraUp).normalized()) * cameraSpeed;
    }
    if(event->key() == Qt::Key_D){
        cameraPos += (QVector3D::crossProduct(cameraFront, cameraUp).normalized()) * cameraSpeed;
    }
    if(event->key() == Qt::Key_E){ //上升
        cameraPos += cameraUp * cameraSpeed;
    }
    if(event->key() == Qt::Key_Q){ //下降
        cameraPos -= cameraUp * cameraSpeed;
    }
}

(三)鼠标输入与缩放

按住鼠标左键进行视角的拖拉移动,使用鼠标滚轮放大缩小视角,emmm,还是因为没有glfw函数,无法实现Vires教程中的锁定鼠标光标功能,即下图函数。(不实现也好,这个锁定光标操作有点反人类,在(四)中,会对这个操作进行改善)


整体鼠标左键与滚轮操作,鼠标操作过程中可以同时进行键盘操作,如下图所示:


在(一)中的所有代码中,仅修改widget.h与widget.cpp文件,

以为我们的类是继承自 Qwidget的,所以只要在proteced域内,重载mouseMoveEvent(QMouseEvent *event)即可使用鼠标事件,详见注释

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QOpenGLWidget>
#include <QDebug>
#include <QOpenGLFunctions_3_3_Core>
#include "shader.h"
#include <QOpenGLTexture>
#include <QTime>    //增添头文件
#include <QVector3D>

class Triangle : public QOpenGLWidget
{
public:
    Triangle();
    GLuint a;
    ~Triangle();
protected:
    virtual void initializeGL();
    virtual void resizeGL(int w, int h);
    virtual void paintGL();
    void keyPressEvent(QKeyEvent *event);   //键盘事件
    void mouseMoveEvent(QMouseEvent *event);//鼠标事件
    void wheelEvent(QWheelEvent *event);    //滚轮事件

private:
    Shader *ourShader;
    QOpenGLTexture *texture1;
    QOpenGLTexture *texture2;

    QOpenGLFunctions_3_3_Core *core;
    QTime time; //增添QTime对象,替代glfwGetTime()函数

    QVector3D cameraPos;
    QVector3D cameraFront;
    QVector3D cameraUp;
    GLfloat deltaTime;
    GLfloat lastFrame;

    GLboolean firstMouse;
    GLfloat yaw;
    GLfloat pitch;
    GLfloat lastX;
    GLfloat lastY;
    GLfloat fov;

};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include <QKeyEvent>
GLuint VBO, VAO;

Triangle::Triangle(){
    this->setWindowTitle("Camera");
}

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

void Triangle::initializeGL(){
    //着色器部分
    core = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
    ourShader = new Shader(":/shaders/vertexshadersource.vert", ":/shaders/fragmentshadersource.frag");

    //VAO,VBO数据部分
    float vertices[] = {
        -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
         0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
         0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
         0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,

        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
         0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
        -0.5f,  0.5f,  0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,

        -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
        -0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
        -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

         0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
         0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
         0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
         0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
         0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
         0.5f, -0.5f, -0.5f,  1.0f, 1.0f,
         0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
         0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,

        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
         0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
        -0.5f,  0.5f,  0.5f,  0.0f, 0.0f,
        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f
    };


    core->glGenVertexArrays(1, &VAO);//两个参数,第一个为需要创建的缓存数量。第二个为用于存储单一ID或多个ID的GLuint变量或数组的地址
    core->glGenBuffers(1, &VBO);

    core->glBindVertexArray(VAO);

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

    core->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
    core->glEnableVertexAttribArray(0);

    // texture coord attribute
    core->glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
    core->glEnableVertexAttribArray(1);

    //纹理
    //第一张箱子
    texture1 = new QOpenGLTexture(QImage(":/textures/res/textures/container.jpg").mirrored(), QOpenGLTexture::GenerateMipMaps); //直接生成绑定一个2d纹理, 并生成多级纹理MipMaps
    if(!texture1->isCreated()){
        qDebug() << "Failed to load texture" << endl;
    }
    texture1->setWrapMode(QOpenGLTexture::DirectionS, QOpenGLTexture::Repeat);// 等于glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    texture1->setWrapMode(QOpenGLTexture::DirectionT, QOpenGLTexture::Repeat);//    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    texture1->setMinificationFilter(QOpenGLTexture::Linear);   //等价于glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    texture1->setMagnificationFilter(QOpenGLTexture::Linear);  //     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


    //第二张笑脸
    texture2 = new QOpenGLTexture(QImage(":/textures/res/textures/smile.png").mirrored(), QOpenGLTexture::GenerateMipMaps); //直接生成绑定一个2d纹理, 并生成多级纹理MipMaps
    if(!texture2->isCreated()){
        qDebug() << "Failed to load texture" << endl;
    }
    texture2->setWrapMode(QOpenGLTexture::DirectionS, QOpenGLTexture::Repeat);// 等于glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    texture2->setWrapMode(QOpenGLTexture::DirectionT, QOpenGLTexture::Repeat);//    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    texture2->setMinificationFilter(QOpenGLTexture::Linear);   //等价于glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    texture2->setMagnificationFilter(QOpenGLTexture::Linear);  //     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    //设置纹理单元编号
    ourShader->use();
    ourShader->setInt("texture1", 0);
    ourShader->setInt("texture2", 1);

    //开启计时器,返回毫秒
    time.start();

    //开启状态
    core->glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    core->glEnable(GL_DEPTH_TEST);

    //emmm ,本来成员变量的初始化应该放在构造函数里,不过这些变量和OpenGL相关,就放在initializeGL()里方便管理;
    cameraPos = QVector3D(0.0f, 0.0f,  3.0f);
    cameraFront = QVector3D(0.0f, 0.0f, -1.0f);
    cameraUp = QVector3D(0.0f, 1.0f,  0.0f);
    deltaTime = 0.0f;
    lastFrame = 0.0f;

    //与鼠标相关的摄像机变量初始化
    firstMouse = true;
    yaw   = -90.0f;	// 偏航角如果是0.0f,指向的是 x轴正方向,即右方向,所以向里转90度,初始方向指向z轴负方向
    pitch =  0.0f;
    lastX =  800.0f / 2.0;
    lastY =  600.0 / 2.0;
    fov   =  45.0f;
}

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

QVector3D cubePositions[] = {       //为了省事,设为全局函数,若嫌封装性不好,改为成员变量指针,在initializeGL()重新赋值就好
  QVector3D( 0.0f,  0.0f,  -1.0f), //小小改动一下将z轴的0.0f变为-1.0f
  QVector3D( 2.0f,  5.0f, -15.0f),
  QVector3D(-1.5f, -2.2f, -2.5f),
  QVector3D(-3.8f, -2.0f, -12.3f),
  QVector3D( 2.4f, -0.4f, -3.5f),
  QVector3D(-1.7f,  3.0f, -7.5f),
  QVector3D( 1.3f, -2.0f, -2.5f),
  QVector3D( 1.5f,  2.0f, -2.5f),
  QVector3D( 1.5f,  0.2f, -1.5f),
  QVector3D(-1.3f,  1.0f, -1.5f)
};

void Triangle::paintGL(){
    GLfloat currentFrame = (GLfloat)time.elapsed()/100;
    deltaTime = currentFrame - lastFrame;
    lastFrame = currentFrame;


    core->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

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

    ourShader->use();

    QMatrix4x4 projection;
    projection.perspective(fov, (GLfloat)width()/(GLfloat)height(), 0.1f, 100.f);
    ourShader->setMat4("projection", projection);

    QMatrix4x4 view;
    view.lookAt(cameraPos, cameraPos+cameraFront, cameraUp);
    ourShader->setMat4("view", view);

    for(GLuint i = 0; i != 10; ++i){
        QMatrix4x4 model;
        model.translate(cubePositions[i]);
        model.rotate(20.0f * i, cubePositions[i]);//这里角度的设置仍然为时间,如果按照Vires大神写的i*20, 箱子不会旋转


        ourShader->setMat4("model", model);
        core->glBindVertexArray(VAO);
        core->glDrawArrays(GL_TRIANGLES, 0, 36);
    }

    update();
}

void Triangle::keyPressEvent(QKeyEvent *event)
{
    GLfloat cameraSpeed = 2.5 * deltaTime;
    if(event->key() == Qt::Key_W){
        cameraPos += cameraFront * cameraSpeed;
    }
    if(event->key() == Qt::Key_S){
        cameraPos -= cameraFront * cameraSpeed;
    }
    if(event->key() == Qt::Key_A){
        cameraPos -= (QVector3D::crossProduct(cameraFront, cameraUp).normalized()) * cameraSpeed;
    }
    if(event->key() == Qt::Key_D){
        cameraPos += (QVector3D::crossProduct(cameraFront, cameraUp).normalized()) * cameraSpeed;
    }
    if(event->key() == Qt::Key_E){ //上升
        cameraPos += cameraUp * cameraSpeed;
    }
    if(event->key() == Qt::Key_Q){ //下降
        cameraPos -= cameraUp * cameraSpeed;
    }
}

void Triangle::mouseMoveEvent(QMouseEvent *event)
{
    GLfloat xpos = event->pos().x();
    GLfloat ypos = event->pos().y();

    if (firstMouse)
        {
            lastX = event->pos().x();
            lastY = event->pos().y();
            firstMouse = false;
        }

        GLfloat xoffset = xpos - lastX;
        GLfloat yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top
        lastX = xpos;
        lastY = ypos;

        GLfloat sensitivity = 0.01f; // change this value to your liking
        xoffset *= sensitivity;
        yoffset *= sensitivity;

        yaw += xoffset;
        pitch += yoffset;

        // make sure that when pitch is out of bounds, screen doesn't get flipped
        if (pitch > 89.0f)
            pitch = 89.0f;
        if (pitch < -89.0f)
            pitch = -89.0f;

        QVector3D front(cos(yaw) * cos(pitch), sin(pitch), sin(yaw) * cos(pitch));
        cameraFront = front.normalized();
}

void Triangle::wheelEvent(QWheelEvent *event)
{
    QPoint offset = event->angleDelta();
    if(fov >= 1.0f && fov <=45.0f)
        fov -= ((GLfloat)offset.y())/20;
    if(fov < 1.0f)
        fov = 1.0f;
    if(fov > 45.0f)
        fov = 45.0f;
}

猜你喜欢

转载自blog.csdn.net/z136411501/article/details/79939695