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.pswp.cn/diannao/81163.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/81163.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/81163.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【Machine Learning Q and AI 讀書筆記】- 02 自監督學習

Machine Learning Q and AI 中文譯名 大模型技術30講&#xff0c;主要總結了大模型相關的技術要點&#xff0c;結合學術和工程化&#xff0c;對LLM從業者來說&#xff0c;是一份非常好的學習實踐技術地圖. 本文是Machine Learning Q and AI 讀書筆記的第2篇&#xff0c;對應原…

using var connection = connectionFactory.CreateConnection(); using var 是什么意思

在 .NET 中&#xff0c;??垃圾回收&#xff08;Garbage Collection, GC&#xff09;?? 確實是自動管理內存的機制&#xff0c;但它 ??僅適用于托管資源&#xff08;Managed Resources&#xff09;??&#xff08;如類實例、數組等&#xff09;。然而&#xff0c;對于 ?…

Multicore-TSNE

文章目錄 TSNE使用scikit-learn庫使用Multicore-TSNE庫安裝方法基本使用方法采用不同的距離度量 其他資料 TSNE t-Distributed Stochastic Neighbor Embedding (t-SNE) 是一種高維數據的降維方法&#xff0c;由Laurens van der Maaten和Geoffrey Hinton于2008年提出&#xff0…

SI5338-EVB Usage Guide(LVPECL、LVDS、HCSL、CMOS、SSTL、HSTL)

目錄 1. 簡介 1.1 EVB 介紹 1.2 Si5338 Block Diagram 2. EVB 詳解 2.1 實物圖 2.2 基本配置 2.2.1 Universal Pin 2.2.2 IIC I/F 2.2.3 Input Clocks 2.2.4 Output Frequencies 2.2.5 Output Driver 2.2.6 Freq and Phase Offset 2.2.7 Spread Spectrum 2.2.8 快…

Spring AI應用系列——基于OpenTelemetry實現大模型調用的可觀測性實踐

一、項目背景與目標 在AI應用日益復雜的今天&#xff0c;大模型服務&#xff08;如語言理解和生成&#xff09;的性能監控和問題排查變得尤為關鍵。為了實現對大模型調用鏈路的可觀測性&#xff08;Observability&#xff09;管理&#xff0c;我們基于 Spring Boot Spring AI…

Spyglass:官方Hands-on Training(一)

相關閱讀 Spyglasshttps://blog.csdn.net/weixin_45791458/category_12828934.html?spm1001.2014.3001.5482 本文是對Spyglass Hands-on Training中第一個實驗的翻譯&#xff08;有刪改&#xff09;&#xff0c;Lab文件可以從以下鏈接獲取。Spyglass Hands-on Traininghttps:…

PCB設計工藝規范(三)走線要求

走線要求 1.走線要求2.固定孔、安裝孔、過孔要求3.基準點要求4.絲印要求 1.走線要求 印制板距板邊距離:V-CUT 邊大于 0.75mm&#xff0c;銑槽邊大于0.3mm。為了保證 PCB 加工時不出現露銅的缺陷&#xff0c;要求所有的走線及銅箔距離板邊:V-CUT邊大于 0.75mm&#xff0c;銑槽邊…

抓取工具Charles配置教程(mac電腦+ios手機)

mac電腦上的配置 1. 下載最新版本的Charles 2. 按照以下截圖進行配置 2.1 端口號配置&#xff1a; 2.2 https配置 3. mac端證書配置 4. IOS手機端網絡配置 4.1 先查看電腦上的配置 4.2 配置手機網絡 連接和電腦同一個wifi&#xff0c;然后按照以下截圖進行配置 5. 手機端證書…

【CSS】精通Flex布局(全)

目錄 1. flex布局體驗 1.1 傳統布局 與 flex布局 1.2 初體驗 2. flex布局原理 2.1 布局原理 3. flex布局父項常見屬性 3.1 常見父項屬性 3.2 屬性值 3.3 justify-content 設置主軸上的子元素排列方式 3.4 flex-wrap設置子元素是否換行 3.5 align-items 設置側軸上的…

力扣第447場周賽

這次終于趕上力扣的周賽了, 賽時成績如下(依舊還是三題 )&#xff1a; 1. 統計被覆蓋的建筑 給你一個正整數 n&#xff0c;表示一個 n x n 的城市&#xff0c;同時給定一個二維數組 buildings&#xff0c;其中 buildings[i] [x, y] 表示位于坐標 [x, y] 的一個 唯一 建筑。 如…

AI中常用概念的理解

1. RAG&#xff08;檢索增強生成&#xff09; 通俗理解&#xff1a;就像你寫作業時&#xff0c;先查課本 / 百度找資料&#xff0c;再根據資料寫答案&#xff0c;而不是純靠記憶瞎編。 AI 模型&#xff08;比如 ChatGPT&#xff09;回答問題時&#xff0c;先去 “數據庫 / 互聯…

SQLServer多版本兼容Java方案和數據采集

Maven引入 <dependency><groupId>com.microsoft.sqlserver</groupId><artifactId>sqljdbc4</artifactId><version>4.0</version></dependency><dependency><groupId>net.sourceforge.jtds</groupId><ar…

【每日八股】復習 Redis Day4:線程模型

文章目錄 復習 Redis Day4&#xff1a;線程模型介紹一下 Redis 的線程模型核心線程模型&#xff08;Redis 6.0 之前&#xff09;Redis 6.0 的多線程改進Redis 真的是單線程嗎&#xff1f;Redis 的線程模型剖析 上一篇 Redis 的應用我今天才完成&#xff0c;因此明天一并復習 Re…

樹莓派智能攝像頭實戰指南:基于TensorFlow Lite的端到端AI部署

引言&#xff1a;嵌入式AI的革新力量 在物聯網與人工智能深度融合的今天&#xff0c;樹莓派這一信用卡大小的計算機正在成為邊緣計算的核心載體。本文將手把手教你打造一款基于TensorFlow Lite的低功耗智能監控設備&#xff0c;通過MobileNetV2模型實現實時物體檢測&#xff0…

vs2019編譯occ7.9.0時,出現fatal error C1060: compiler is out of heap space

問題描述 visual studio 2019編譯opencascade 7.9.0時&#xff0c;出現編譯錯誤 fatal error C1060: compiler is out of heap space 解決方案 修改vs2019并行編譯的線程個數&#xff0c;默認是12個&#xff0c;我改成了4個&#xff0c;問題解決 Tools > Project and Sol…

vue跨域問題總結筆記

目錄 一、Websocket跨域問題 1.nginx配置 2.VUE CLI代理 3.env.development配置 4.nginx日志 5.解決 一、解決跨域的幾種常用方法 1.Vue CLI代理 2.JSONP 3.WebSocket 4.NGINX解決跨域問題 6.Java解決跨域 二、Vue跨域問題詳解 1. 什么是跨域 2. 跨域的例子 3.…

數據結構篇:線性表的另一表達—鏈表之單鏈表(下篇)

目錄 1.前言 2.是否使用二級指針 3.插入/刪除 3.1 pos位置前/后插入 3.2 查找函數 3.3 pos位置刪除 3.4 pos位置后面刪除 3.5 函數的銷毀 4.斷言問題 4.1 斷言pphead 4.2 斷言*pphead 5.三個文件的代碼 5.1 頭文件 5.2 具體函數實現 5.3 測試用例 1.前言 之前是講…

完美解決react-native文件直傳阿里云oss問題一

前言 通常情況下&#xff0c;作為前后端分離的項目來說&#xff0c;文件上傳是最尋常的功能之一。雖然每個公司選擇的文件管理云庫各不相同&#xff0c;但實現思路基本一致。我所在公司使用阿里云oss文件管理&#xff0c;之前服務端做了透傳&#xff0c;但是由于每個測試環境的…

5.運輸層

5. 運輸層 1. 概述 第2~4章依次介紹了計算機網絡體系結構中的物理層、數據鏈路層和網絡層&#xff0c;它們共同解決了將主機通過異構網絡互聯起來所面臨的問題&#xff0c;實現了主機到主機的通信然而在計算機網絡中實際進行通信的真正實體&#xff0c;是位于通信兩端主機中的…

告別手動時代!物聯網軟件開發讓萬物自動互聯

清晨&#xff0c;智能窗簾隨著陽光自動拉開&#xff1b;運動時&#xff0c;手表精準記錄著健康數據&#xff1b;回到家&#xff0c;室溫早已調節至最舒適狀態...這些場景的實現&#xff0c;都離不開物聯網軟件開發的技術支撐。在智能家居軟件開發、智能穿戴軟件開發、醫療器械軟…