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

Qt5与现代OpenGL学习(三)纹理

请添加图片描述
在这里插入图片描述

把第一张图放到D盘的1文件夹里面:1.png
triangle.h

#ifndef WIDGET_H
#define WIDGET_H#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>#include <QDebug>
#include <QOpenGLFunctions_3_3_Core>
#include "shader.h"
#include <QOpenGLTexture>  //新添项class Triangle : public QOpenGLWidget,protected QOpenGLFunctions
{Q_OBJECT
public: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 *texture;//新添项QOpenGLFunctions_3_3_Core *core;};#endif // WIDGET_H

triangle.cpp

#include "triangle.h"
GLuint VBO, VAO, EBO;Triangle::Triangle(QWidget* parent):QOpenGLWidget(parent)
{this->setWindowTitle("Texture");
}Triangle::~Triangle(){delete ourShader;core->glDeleteVertexArrays(1, &VAO);core->glDeleteBuffers(1, &VBO);core->glDeleteBuffers(1, &EBO);texture->destroy(); //纹理使用完 进行删除
}void Triangle::initializeGL(){//着色器部分core = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();ourShader = new Shader(":/triangle.vert", ":/triangle.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 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);//纹理texture = new QOpenGLTexture(QImage("D://1//1.png").mirrored(), QOpenGLTexture::GenerateMipMaps); //直接生成绑定一个2d纹理, 并生成多级纹理MipMapsif(!texture->isCreated()){qDebug() << "Failed to load texture" << endl;}texture->setWrapMode(QOpenGLTexture::DirectionS, QOpenGLTexture::Repeat);// 等于glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);texture->setWrapMode(QOpenGLTexture::DirectionT, QOpenGLTexture::Repeat);//    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);texture->setMinificationFilter(QOpenGLTexture::Linear);   //等价于glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);texture->setMagnificationFilter(QOpenGLTexture::Linear);  //     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);texture->setFormat(QOpenGLTexture::RGBFormat); //将纹理储存为rgb值core->glClearColor(0.2f, 0.3f, 0.3f, 1.0f);}void Triangle::resizeGL(int w, int h){core->glViewport(0, 0, w, h);
}void Triangle::paintGL(){core->glClear(GL_COLOR_BUFFER_BIT);texture->bind();ourShader->use();core->glBindVertexArray(VAO);core->glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);}

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();}};#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.addShader(&vertexShader);shaderProgram.addShader(&fragmentShader);success = shaderProgram.link();if(!success){qDebug() << "ERROR::SHADER::PROGRAM::LINKING_FAILED" << endl;qDebug() << shaderProgram.log() << endl;}
}Shader::~Shader(){
}

triangle.frag

#version 330 core
out vec4 FragColor;in vec3 ourColor;
in vec2 TexCoord;uniform sampler2D ourTexture;void main()
{FragColor = texture2D(ourTexture, TexCoord);
}

triangle.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;void main(){gl_Position = vec4(aPos, 1.0f);ourColor = aColor;TexCoord = aTexCoord;
}

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

相关文章:

  • Android 实现一个隐私弹窗
  • c网络库libevent的http常用函数的使用(附带源码)
  • 打造惊艳的渐变色下划线动画:CSS实现详解
  • Kotlin -> lateinit 和 lazy 详解
  • 聚焦智能体未来,领驭科技在微软创想未来峰会大放异彩
  • 按键精灵安卓ios辅助工具脚本:实用的文件插件(lua开源)
  • 私有知识库 Coco AI 实战(四):打造 ES 索引参数小助手
  • 前端漏洞不扫描理由
  • Linux systemd 从理论到实践:现代系统管理的核心工具
  • C++ 单例对象自动释放(保姆级讲解)
  • Hearts of Iron IV 钢铁雄心 4 [DLC 解锁] [Windows SteamOS macOS]
  • 机器学习-入门-决策树(1)
  • 第17节:传统分类模型-随机森林与决策树
  • day10 python机器学习全流程实践
  • Azure Synapse Dedicated SQL pool企业权限管理
  • 数据库操作
  • 轻松实现CI/CD: 用Go编写的命令行工具简化Jenkins构建
  • Java练习8
  • 【AlphaFold2】Feature extraction:提取特征,为模型输入做准备|Datapipeline讲解
  • 激光扫描仪的用途及优势
  • 吴志朴当选福建德化县人民政府县长
  • 言短意长|新能源领军者密集捐赠母校
  • 商务部新闻发言人就波音公司飞回拟交付飞机答记者问
  • 今年一季度全国社会物流总额达91万亿元,工业品比重超八成
  • 2025上海体育消费节启动,多形式联动打造体育消费盛宴
  • 超级干细胞有助改善生育治疗