(Vries的原教程地址如下,
https://learnopengl-cn.github.io/01%20Getting%20started/08%20Coordinate%20Systems/关于函数的具体解析及OpenGL中的坐标系统的具体解释请看这个教程,本篇旨在对Vires基于visual studio的编程思想做Qt平台的移植)
本篇目的是学习OpenGL里的坐标系统,model矩阵,view矩阵,projection矩阵,如何将处于3D坐标系的点,投影到2D的电脑窗口。由2D编程进入3D编程的世界。
(一)初探3D
将平面2D纹理,绕x轴旋转-55度,如下图所示。这里注意,OpenGL的坐标系是右手坐标系,即右为x轴正方向,上为y轴正方向,由屏幕指向你的方向为z轴正方向。旋转时,以逆时针为绕一轴旋转的正方向,顺时针为负方向。
以下为项目源代码,
项目组织管理如下:
相比之前的教程,修改shader.h, widget.cpp与vertexshadersource.vert文件,修改部分见注释。
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
vertexshadersource.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 model; uniform mat4 view; uniform mat4 projection; void main(){ gl_Position = projection * view * model * vec4(aPos, 1.0f); ourColor = aColor; TexCoord = aTexCoord; }
widget.cpp
这里我没将对着色器变量model,view,projection的赋值写进paintGL()函数。在我的思维里,一直认为
initializeGL()函数是初始化函数,在OpenGL开始时仅运行一次,
resizeGL()函数是在窗口大小改变时运行,
paintGL()函数类似于Vires教程里在while()循环里的函数,是不断刷新运行的。(虽然Qt里paintGL()函数只运行一次,想不断刷新,需要使用update()函数)。
所以,我将目前进赋值一次,且不再改编的model等矩阵变量的赋值过程,写进了initializeGL()里。
#include "widget.h" GLuint VBO, VAO, EBO; Triangle::Triangle(){ this->setWindowTitle("Coordinate System"); } Triangle::~Triangle(){ delete ourShader; core->glDeleteVertexArrays(1, &VAO); core->glDeleteBuffers(1, &VBO); core->glDeleteBuffers(1, &EBO); 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数据部分 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);//两个参数,第一个为需要创建的缓存数量。第二个为用于存储单一ID或多个ID的GLuint变量或数组的地址 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); core->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); core->glEnableVertexAttribArray(0); // color attribute core->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float))); core->glEnableVertexAttribArray(1); // texture coord attribute core->glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); core->glEnableVertexAttribArray(2); //纹理 //第一张箱子 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->shaderProgram->setUniformValue(ourShader->shaderProgram->uniformLocation("texture1"), 0);//等价于ourShader.setInt("texture1", 0) setInt函数,是Vries自写的函数,实际应用还是要先获取“texture1”的location,然后设值 ourShader->shaderProgram->setUniformValue(ourShader->shaderProgram->uniformLocation("texture2"), 1); core->glClearColor(0.2f, 0.3f, 0.3f, 1.0f); //开启计时器,返回毫秒 //time.start(); QMatrix4x4 model, projection, view; model.rotate(-55.0f, QVector3D(1.0f, 0.0f, 0.0f)); 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("model", model); ourShader->setMat4("view", view); ourShader->setMat4("projection", projection); } void Triangle::resizeGL(int w, int h){ core->glViewport(0, 0, w, h); } void Triangle::paintGL(){ core->glClear(GL_COLOR_BUFFER_BIT); core->glActiveTexture(GL_TEXTURE0); texture1->bind(); core->glActiveTexture(GL_TEXTURE1); texture2->bind(); ourShader->use(); core->glBindVertexArray(VAO); core->glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); update(); }
以下是没有改动过的函数:
fragmentshadersource.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); }
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; }
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
main.cpp
#include "widget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Triangle t; t.show(); return a.exec(); }
(二)3D立方体
得到一个旋转的立方体,如下图所示,emmmm,因为gif动图截取工具帧数的限制,看起来不是很流畅,但程序运行起来不是这样,很流畅的。
修改widget.cpp,vertexshadersource.vert与fragmentshadersource.cpp文件。
widget.cpp
#include "widget.h" GLuint VBO, VAO; Triangle::Triangle(){ this->setWindowTitle("Coordinate System"); } 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; 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); } 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(); QMatrix4x4 model; model.rotate((float)time.elapsed()/10, QVector3D(0.5f, 1.0f, 0.0f)); ourShader->use(); ourShader->setMat4("model", model); core->glBindVertexArray(VAO); core->glDrawArrays(GL_TRIANGLES, 0, 36); update(); }
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.cpp
#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); }
学到现在这一步,照着Vries的教程,按Qt风格修改真的不是很大的问题。
(三)更多的立方体
让我们渲染更多的立方体。如下图所示,emmm,gif动图还不是很流程,与程序无关,
简单修改widget.cpp,对Vires大神的变量值稍加改动,详见注释。
widget.cpp
#include "widget.h" GLuint VBO, VAO; Triangle::Triangle(){ this->setWindowTitle("Coordinate System"); } 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; 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[] = { 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(); for(GLuint i = 0; i != 10; ++i){ QMatrix4x4 model; model.translate(cubePositions[i]); model.rotate((float)time.elapsed()/10, cubePositions[i]);//这里角度的设置仍然为时间,如果按照Vires大神写的i*20, 箱子不会旋转 ourShader->use(); ourShader->setMat4("model", model); core->glBindVertexArray(VAO); core->glDrawArrays(GL_TRIANGLES, 0, 36); } update(); }