进入3D
shader头文件和cpp
#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
#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() { }
widget头文件和cpp
#ifndef WIDGET_H #define WIDGET_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 Widget; } class Widget : public QGLWidget { Q_OBJECT public: Widget(); ~Widget(); public slots: 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; QTimer updateTimer; }; #endif // WIDGET_H
#include "widget.h" #include "ui_widget.h" GLuint VBO, VAO, EBO; Widget::Widget() { } Widget::~Widget() { delete ourShader; core->glDeleteVertexArrays(1, &VAO); core->glDeleteBuffers(1, &VBO); core->glDeleteBuffers(1, &EBO); texture1->destroy(); texture2->destroy(); } void Widget::initializeGL() { //着色器部分 core = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>(); if (!core) { qWarning() << "Could not obtain required OpenGL context version"; exit(1); } ourShader = new Shader(":/shader/vertexshadersource.vert", ":/shader/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 }; 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); // texture coord attribute core->glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (void*)(6 * sizeof(float))); core->glEnableVertexAttribArray(2); //纹理1 texture1 = new QOpenGLTexture(QImage(":/image/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(":/image/smile.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); QMatrix4x4 model, view, projection; model.rotate(-55.0f, QVector3D(1.0f, 0.0f, 0.0f)); view.translate(QVector3D(0.0f, 0.0f, -3.0f)); projection.perspective(45.0f, (float)width()/(float)height(), 0.1f, 100.0f); ourShader->use(); ourShader->shaderProgram.setUniformValue("model", model); ourShader->shaderProgram.setUniformValue("view", view); ourShader->shaderProgram.setUniformValue("projection", projection); } void Widget::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(); ourShader->use(); core->glBindVertexArray(VAO); core->glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr); update(); } void Widget::resizeGL(int w, int h) { core->glViewport(0, 0, w, h); }
着色器
#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); }
#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; }
mian函数
#include "widget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }
更多的3D
修改的部分:
#include "widget.h" #include "ui_widget.h" GLuint VBO, VAO, EBO; Widget::Widget() { } Widget::~Widget() { delete ourShader; core->glDeleteVertexArrays(1, &VAO); core->glDeleteBuffers(1, &VBO); core->glDeleteBuffers(1, &EBO); texture1->destroy(); texture2->destroy(); } void Widget::initializeGL() { //着色器部分 core = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>(); if (!core) { qWarning() << "Could not obtain required OpenGL context version"; exit(1); } ourShader = new Shader(":/shader/vertexshadersource.vert", ":/shader/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 }; 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, 5*sizeof(GLfloat), nullptr); core->glEnableVertexAttribArray(0); //texture coord attribute core->glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), (void*)(3 * sizeof(float))); core->glEnableVertexAttribArray(1); //纹理1 texture1 = new QOpenGLTexture(QImage(":/image/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(":/image/smile.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); mtime.start(); QMatrix4x4 view, projection; // 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()/(float)height(), 0.1f, 100.0f); ourShader->use(); // ourShader->shaderProgram.setUniformValue("model", model); ourShader->shaderProgram.setUniformValue("view", view); ourShader->shaderProgram.setUniformValue("projection", projection); } void Widget::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 model; model.rotate((float)mtime.elapsed()/10, QVector3D(0.5f, 1.0f, 0.0f)); ourShader->use(); ourShader->shaderProgram.setUniformValue("model", model); core->glBindVertexArray(VAO); // core->glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr); core->glDrawArrays(GL_TRIANGLES, 0, 36); update(); } void Widget::resizeGL(int w, int h) { core->glViewport(0, 0, w, h); }
修改的着色器:
#version 330 core layout (location = 0) in vec3 aPos; //layout (location = 1) in vec3 aColor; layout (location = 1) 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; }
Z缓冲
initializeGL: core->glEnable(GL_DEPTH_TEST); paintGL: core->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
多个3D:
#include "widget.h" #include "ui_widget.h" GLuint VBO, VAO, EBO; Widget::Widget() { } Widget::~Widget() { delete ourShader; core->glDeleteVertexArrays(1, &VAO); core->glDeleteBuffers(1, &VBO); core->glDeleteBuffers(1, &EBO); texture1->destroy(); texture2->destroy(); } void Widget::initializeGL() { //着色器部分 core = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>(); if (!core) { qWarning() << "Could not obtain required OpenGL context version"; exit(1); } ourShader = new Shader(":/shader/vertexshadersource.vert", ":/shader/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 }; 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, 5*sizeof(GLfloat), nullptr); core->glEnableVertexAttribArray(0); //texture coord attribute core->glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), (void*)(3 * sizeof(float))); core->glEnableVertexAttribArray(1); //纹理1 texture1 = new QOpenGLTexture(QImage(":/image/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(":/image/smile.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); mtime.start(); QMatrix4x4 view, projection; // 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()/(float)height(), 0.1f, 100.0f); ourShader->use(); // ourShader->shaderProgram.setUniformValue("model", model); ourShader->shaderProgram.setUniformValue("view", view); ourShader->shaderProgram.setUniformValue("projection", projection); core->glEnable(GL_DEPTH_TEST); } QVector3D cubePositions[] = { QVector3D(0.0f, 0.0f, 0.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 Widget::paintGL() { core->glClearColor(0.2f, 0.3f, 0.3f, 1.0f); core->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ourShader->use(); core->glActiveTexture(GL_TEXTURE1); texture2->bind(); core->glActiveTexture(GL_TEXTURE0); texture1->bind(); ourShader->use(); // ourShader->shaderProgram.setUniformValue("model", model); core->glBindVertexArray(VAO); // core->glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr); for(unsigned int i=0;i<10;i++) { QMatrix4x4 model; model.translate(cubePositions[i]); model.rotate((float)mtime.elapsed()/10, QVector3D(0.5f, 1.0f, 0.0f)); ourShader->shaderProgram.setUniformValue("model", model); core->glDrawArrays(GL_TRIANGLES, 0, 36); } update(); } void Widget::resizeGL(int w, int h) { core->glViewport(0, 0, w, h); }
- 对GLM的
projection
函数中的FoV
和aspect-ratio
参数进行实验。看能否搞懂它们是如何影响透视平截头体的。- fov越小摄像机越近,fov越大距离越远。aspect-ratio是宽度比例,显示幅面不变的情况下。会有拉伸。
- 将观察矩阵在各个方向上进行位移,来看看场景是如何改变的。注意把观察矩阵当成摄像机对象。
- 场景反方向移动了
- 使用模型矩阵只让是3倍数的箱子旋转(以及第1个箱子),而让剩下的箱子保持静止。
void Widget::paintGL() { core->glClearColor(0.2f, 0.3f, 0.3f, 1.0f); core->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ourShader->use(); core->glActiveTexture(GL_TEXTURE1); texture2->bind(); core->glActiveTexture(GL_TEXTURE0); texture1->bind(); ourShader->use(); // ourShader->shaderProgram.setUniformValue("model", model); core->glBindVertexArray(VAO); // core->glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr); for(unsigned int i=0;i<10;i++) { QMatrix4x4 model; model.translate(cubePositions[i]); if(i%3 == 0) { float angle = 20.0f*i + (float)mtime.elapsed()/10; model.rotate(angle, QVector3D(0.5f, 1.0f, 0.0f)); } ourShader->shaderProgram.setUniformValue("model", model); core->glDrawArrays(GL_TRIANGLES, 0, 36); } update(); }