当前位置: 首页 > news >正文

Qt5与现代OpenGL学习(四)X轴方向旋转60度

请添加图片描述

在这里插入图片描述
在这里插入图片描述
把上面两张图像放到D盘1文件夹内:
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;
}

triangle.h

#ifndef WIDGET_H
#define WIDGET_H#include <QOpenGLWidget>
#include <QDebug>
#include <QOpenGLFunctions_3_3_Core>
#include "shader.h"
#include <QOpenGLTexture>
#include <QTime>    //增添头文件#include <QOpenGLFunctions>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>class Triangle : public QOpenGLWidget,protected QOpenGLFunctions
{Q_OBJECTpublic:Triangle(QWidget* parent=0);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

triangle.cpp

#include "triangle.h"
GLuint VBO, VAO, EBO;Triangle::Triangle(QWidget* parent):QOpenGLWidget(parent)
{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 right0.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 triangle1, 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 attributecore->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));core->glEnableVertexAttribArray(1);// texture coord attributecore->glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));core->glEnableVertexAttribArray(2);//纹理//第一张箱子texture1 = new QOpenGLTexture(QImage("D:/1/1.png").mirrored(), QOpenGLTexture::GenerateMipMaps); //直接生成绑定一个2d纹理, 并生成多级纹理MipMapsif(!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("D:/1/10.png").mirrored(), QOpenGLTexture::GenerateMipMaps); //直接生成绑定一个2d纹理, 并生成多级纹理MipMapsif(!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.5f, 0.3f, 0.5f, 1.0f);//开启计时器,返回毫秒//time.start();QMatrix4x4 model, projection, view;model.rotate(-60.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);
}

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;
}

在mainwindow.ui中,添加QWidget,并提升为:Triangle

http://www.xdnf.cn/news/212311.html

相关文章:

  • 《Vue3学习手记7》
  • RVO2(C#版)源码分析
  • 什么是ICSP编程
  • [展示]集成式深度学习对音频降噪的基准测试BenchMark
  • 【图片识别改名】批量读取图片区域文字识别后批量改名,基于Python和腾讯云的实现方案
  • [随笔] 升级uniapp旧项目的vue、pinia、vite、dcloudio依赖包等
  • BG开发者日志429:故事模式的思路
  • Mac 创建QT按钮以及一些操作
  • iVX 可视化调试:重塑 AI 编程规则的变革
  • 【11408学习记录】考研英语语法核心:倒装句考点全解+真题演练
  • 如何用vivado导出pin delay
  • 第33周JavaSpringCloud微服务 多人协作下的调试
  • CSRF(cross-site request forgery)跨域请求访问
  • Reverse-WP记录9
  • 51la统计坏了吗?用悟空统计保障运营决策安全详解
  • 斯坦福课程 MSE 318/CME 338: Large-Scale Numerical Optimization
  • Linux权限拓展
  • Headers池技术在Python爬虫反反爬中的应用
  • Kotlin 常见问题
  • 简单音频比较
  • 数据库day-08
  • C#中winform窗体如何捕获键盘按键事件
  • 深度学习篇---模型权重变化与维度分析
  • 阿里云 OpenManus 实战:高效AI协作体系
  • “情况说明“以后,Unity XR 开发者如何选择?
  • HTTP(超文本传输协议)全面总结
  • 蓝桥杯 10. 凯撒加密
  • [C]基础14.字符函数和字符串函数
  • 网络原理—应用层和数据链路层
  • 指针(5)