qt android glsl,基于Qt的OpenGL學習(1)—— Hello Triangle

簡介

要學習OpenGL的話,強烈安利這個教程JoeyDeVries的learnopengl,這里是中文翻譯好的版本。教程中使用OpenGL是通過GLFW這個庫,而在Qt中對OpenGL封裝得很好,并且和GUI以及IO相關的處理Qt更便捷,學習起來更輕松。這里就對每篇教程,在Qt在分別直接使用OpenGL的函數和Qt封裝好的類以作對比。

教程中使用的OpenGL版本為3.3,在Qt中需要使用此版本的OpenGL只需要繼承類QOpenGLFunctions_3_3_Core即可。如果為了在不同設備上都能用OpenGL的話,Qt提供了類QOpenGLFunctions,這個類包含了大部分公共的函數,可能會有個別函數不能用。

對比說明

教程地址

原教程地址,相關知識可以點擊鏈接學習。

我的工程地址,準備后期每篇教程一個commit,查看本篇代碼 git checkout v1.1,喜歡就點個Star吧~

不同點

原教程關于ShaderProgram的讀取、鏈接很繁瑣,后面教程還專門寫了個類Shader,這里我直接使用Qt封裝好的addShaderFromSourceFile函數更方便。

Qt提供了QOpenGLShaderProgram、QOpenGLVertexArrayObject、QOpenGLBuffer這些類來處理OpenGL中的program、VAO、VBO。

運行結果

bccc565b5248

運行結果

使用OpenGL函數版

CoreFunctionWidget.h

#ifndef COREFUNCTIONWIDGET_H

#define COREFUNCTIONWIDGET_H

#include

#include

#include

#include

#include

class CoreFunctionWidget : public QOpenGLWidget

, protected /*QOpenGLExtraFunctions*/QOpenGLFunctions_3_3_Core

{

Q_OBJECT

public:

explicit CoreFunctionWidget(QWidget *parent = nullptr);

~CoreFunctionWidget();

protected:

virtual void initializeGL();

virtual void resizeGL(int w, int h);

virtual void paintGL();

private:

QOpenGLShaderProgram shaderProgram;

};

#endif // COREFUNCTIONWIDGET_H

CoreFunctionWidget.cpp

#include "CoreFunctionWidget.h"

#include

#include

static GLuint VBO, VAO, EBO;

CoreFunctionWidget::CoreFunctionWidget(QWidget *parent) : QOpenGLWidget(parent)

{

}

CoreFunctionWidget::~CoreFunctionWidget()

{

glDeleteVertexArrays(1, &VAO);

glDeleteBuffers(1, &VBO);

// glDeleteBuffers(1, &EBO);

}

void CoreFunctionWidget::initializeGL(){

this->initializeOpenGLFunctions();

bool success = shaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/triangle.vert");

if (!success) {

qDebug() << "shaderProgram addShaderFromSourceFile failed!" << shaderProgram.log();

return;

}

success = shaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/triangle.frag");

if (!success) {

qDebug() << "shaderProgram addShaderFromSourceFile failed!" << shaderProgram.log();

return;

}

success = shaderProgram.link();

if(!success) {

qDebug() << "shaderProgram link failed!" << shaderProgram.log();

}

//VAO,VBO數據部分

float vertices[] = {

0.5f, 0.5f, 0.0f, // top right

0.5f, -0.5f, 0.0f, // bottom right

-0.5f, -0.5f, 0.0f, // bottom left

-0.5f, 0.5f, 0.0f // top left

};

unsigned int indices[] = { // note that we start from 0!

0, 1, 3, // first Triangle

1, 2, 3 // second Triangle

};

glGenVertexArrays(1, &VAO);

glGenBuffers(1, &VBO);

glGenBuffers(1, &EBO);

// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).

glBindVertexArray(VAO);

glBindBuffer(GL_ARRAY_BUFFER, VBO);

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); //頂點數據復制到緩沖

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);

glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void*)0);//告訴程序如何解析頂點數據

glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, 0);//取消VBO的綁定, glVertexAttribPointer已經把頂點屬性關聯到頂點緩沖對象了

// remember: do NOT unbind the EBO while a VAO is active as the bound element buffer object IS stored in the VAO; keep the EBO bound.

// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other

// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.

glBindVertexArray(0); //取消VAO綁定

//線框模式,QOpenGLExtraFunctions沒這函數, 3_3_Core有

// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

}

void CoreFunctionWidget::resizeGL(int w, int h){

glViewport(0, 0, w, h);

}

void CoreFunctionWidget::paintGL(){

glClearColor(0.2f, 0.3f, 0.3f, 1.0f);

glClear(GL_COLOR_BUFFER_BIT);

shaderProgram.bind();

glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized

// glDrawArrays(GL_TRIANGLES, 0, 6);

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

shaderProgram.release();

}

使用Qt相關函數版

QtFunctionWidget.h

#ifndef QTFUNCTIONWIDGET_H

#define QTFUNCTIONWIDGET_H

#include

#include

#include

#include

#include

#include

#include

#include

class QtFunctionWidget : public QOpenGLWidget, protected QOpenGLFunctions

{

public:

QtFunctionWidget(QWidget *parent = nullptr);

~QtFunctionWidget() Q_DECL_OVERRIDE;

protected:

virtual void initializeGL() Q_DECL_OVERRIDE;

virtual void resizeGL(int w, int h) Q_DECL_OVERRIDE;

virtual void paintGL() Q_DECL_OVERRIDE;

private:

QOpenGLShaderProgram shaderProgram;

QOpenGLBuffer vbo, ebo;

QOpenGLVertexArrayObject vao;

};

#endif // QTFUNCTIONWIDGET_H

QtFunctionWidget.cpp

#include "QtFunctionWidget.h"

#include

QtFunctionWidget::QtFunctionWidget(QWidget *parent) : QOpenGLWidget (parent),

vbo(QOpenGLBuffer::VertexBuffer),

ebo(QOpenGLBuffer::IndexBuffer)

{

}

QtFunctionWidget::~QtFunctionWidget(){

makeCurrent();

vbo.destroy();

ebo.destroy();

vao.destroy();

doneCurrent();

}

void QtFunctionWidget::initializeGL(){

this->initializeOpenGLFunctions();

bool success = shaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/triangle.vert");

if (!success) {

qDebug() << "shaderProgram addShaderFromSourceFile failed!" << shaderProgram.log();

return;

}

success = shaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/triangle.frag");

if (!success) {

qDebug() << "shaderProgram addShaderFromSourceFile failed!" << shaderProgram.log();

return;

}

success = shaderProgram.link();

if(!success) {

qDebug() << "shaderProgram link failed!" << shaderProgram.log();

}

//VAO,VBO數據部分

GLfloat vertices[] = {

0.5f, 0.5f, 0.0f, // top right

0.5f, -0.5f, 0.0f, // bottom right

-0.5f, -0.5f, 0.0f, // bottom left

-0.5f, 0.5f, 0.0f // top left

};

unsigned int indices[] = { // note that we start from 0!

0, 1, 3, // first Triangle

1, 2, 3 // second Triangle

};

QOpenGLVertexArrayObject::Binder vaoBind(&vao);

vbo.create();

vbo.bind();

vbo.allocate(vertices, sizeof(vertices));

ebo.create();

ebo.bind();

ebo.allocate(indices, sizeof(indices));

int attr = -1;

attr = shaderProgram.attributeLocation("aPos");

shaderProgram.setAttributeBuffer(attr, GL_FLOAT, 0, 3, sizeof(GLfloat) * 3);

shaderProgram.enableAttributeArray(attr);

vbo.release();

// remember: do NOT unbind the EBO while a VAO is active as the bound element buffer object IS stored in the VAO; keep the EBO bound.

// ebo.release();

}

void QtFunctionWidget::resizeGL(int w, int h){

glViewport(0, 0, w, h);

}

void QtFunctionWidget::paintGL(){

glClearColor(0.2f, 0.3f, 0.3f, 1.0f);

glClear(GL_COLOR_BUFFER_BIT);

shaderProgram.bind();

{

QOpenGLVertexArrayObject::Binder vaoBind(&vao);

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

}

shaderProgram.release();

}

GLSL

triangle.vert

#version 330 core

layout(location = 0) in vec3 aPos;

void main(){

gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0f);

}

triangle.frag

#version 330 core

out vec4 FragColor;

void main(){

FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);

}

main.cpp

#include

#include "MainWindow.h"

#include "QtFunctionWidget.h"

#include "CoreFunctionWidget.h"

int main(int argc, char *argv[])

{

QApplication a(argc, argv);

// MainWindow w;

QtFunctionWidget w1;

CoreFunctionWidget w2;

w1.setWindowTitle(QObject::tr("QtFunction"));

w2.setWindowTitle(QObject::tr("CoreFunction"));

w1.show();

w2.show();

return a.exec();

}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/456984.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/456984.shtml
英文地址,請注明出處:http://en.pswp.cn/news/456984.shtml

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

相關文章

解決:Not Found: /favicon.ico

直接說解決辦法&#xff1a; &#xff08;1&#xff09;制作一個 favicon.ico圖標放在<head></head>標簽中 <link rel"shortcut icon" href"xxxxxxxxxx.ico" type"image/x-icon" /> <!--制作的圖標&#xff0c;使用hr…

多態方法調用的解析和分派

方法調用并不等同于方法執行&#xff0c;方法調用階段唯一的任務就是確定被調用方法的版本&#xff08;即調用哪一個方法&#xff09;&#xff0c;暫時還不涉及方法內部的具體運行過程。在程序運行時&#xff0c;進行方法調用是最普遍、最頻繁的操作&#xff0c;Class文件的編譯…

ES6:Set和Map

Set Set:類似數組&#xff0c;但是成員的值都是唯一的&#xff0c;沒有重復。Set本身是一個構造函數&#xff0c;用來生成Set數據結構。他包含的方法&#xff1a;add: 添加某個值&#xff0c;返回Set結構本身。delete: 刪除某個值&#xff0c;返回一個布爾值&#xff0c;表示是…

九九乘法表[循環嵌套]

#九九乘法表 # 1*11 # 1*22 2*24 # 1*33 2*36 3*39 # ...#循環嵌套 #行數 i 1 while i < 9:# 打印每行的內容j 1while j < i:print("%d * %d %3d " % (i, j, i * j), end)j 1print() # 換行i 1while嵌套&#xff1a;w 1 while w < 10: #外層循…

關于用VS寫C程序運行時出現燙字以及亂碼的問題的原因

最近在復習C語言寫程序時&#xff0c;突然碰到標題上的這種情況&#xff0c;后來經過上網查找以及逐步調試才發現原來是在打印數組的時候“越界”導致的&#xff0c;因為程序在默認初始化char類型的數組時&#xff0c;初始化的值是“燙”字&#xff0c;一般情況下是字符串未初始…

javascript函數調用的各種方法!!

在JavaScript中一共有下面4種調用方式&#xff1a; (1) 基本函數調用 (2)方法調用 (3)構造器調用 (4)通過call()和apply()進行調用 1. 基本函數調用 普通函數調用模式&#xff0c;如&#xff1a; JavaScript code?1234function fn(o){…… }fn({x:1});在基本函數調用中&#x…

ARM TK1 安裝kinect驅動

首先安裝usb庫 $ git clone https://github.com/libusb/libusb.git 編譯libusb需要的工具 $ sudo apt-get install autoconf autogen $ sudo apt-get install libtool $ sudo apt-get install libudev* 編譯安裝 $ sudo ./autogen.sh $ sudo make $ sudo make install $ sudo l…

如何在一個html頁面中提交兩個post,如何在同一個頁面上從Django和Ajax獲得多個post請求?...

我一整天都在為這事犯愁。似乎什么都沒用。這是我的情況。在我有一個Django表單&#xff0c;有兩個字段&#xff1a;redirect_from&#xff0c;redirect_to。此表單有兩個提交按鈕&#xff1a;Validate和{}。當頁面加載時&#xff0c;Submit被隱藏&#xff0c;只顯示Validate。…

大數據入門:各種大數據技術的介紹

大數據我們都知道hadoop&#xff0c;可是還會各種各樣的技術進入我們的視野&#xff1a;Spark&#xff0c;Storm&#xff0c;impala&#xff0c;讓我們都反映不過來。為了能夠更好的架構大數據項目&#xff0c;這里整理一下&#xff0c;供技術人員&#xff0c;項目經理&#xf…

高可用與負載均衡(5)之基于客戶端的負載均衡

什么是客戶端負載均衡 基于客戶端的負載均衡&#xff0c;簡單的說就是在客戶端程序里面&#xff0c;自己設定一個調度算法&#xff0c;在向服務器發起請求的時候&#xff0c;先執行調度算法計算出向哪臺服務器發起請求&#xff0c;然后再發起請求給服務器。 基于客戶端負載均衡…

Variant 與 內存泄露

http://blog.chinaunix.net/uid-10386087-id-2959221.html 今天遇到一個內存泄露的問題。是師兄檢測出來的。Variant類型在使用后要Clear否則會造成內存泄露&#xff0c;為什么呢&#xff1f; Google一下找到下面一篇文章&#xff0c;主要介紹了Com的內存泄露&#xff0c;中間有…

安裝安全類軟件進行了android簽名漏洞修補,魅族MX3怎么升級固件體驗最新比較穩定的版本...

魅族mx3固件怎么升級?flyme os系統會持續更新&#xff0c;升級魅族MX3手機系統需先下載MX3的升級固件&#xff0c;升級固件分為體驗版和穩定版。魅族MX3固件有體驗版和穩定版兩種&#xff0c;顧名思義&#xff0c;體驗版為最新版但相比穩定版來說存在更多的漏洞&#xff0c;升…

linux su切換用戶提示Authentication failture的解決辦法

由于ubtun系統默認是沒有激活root用戶的&#xff0c;需要我們手工進行操作&#xff0c;在命令行界面下&#xff0c;或者在終端中輸入如下命令&#xff1a; sudo passwd Password&#xff1a;你當前的密碼 Enter new UNIX password&#xff1a;這個是root的密碼 Retype new …

@property

class Person(object):def __init__(self, name,age):#屬性直接對外暴露#self.age age#限制訪問self.__age ageself.__name namedef getAge(self):return self.__agedef setAge(self,age):if age<0:age 0self.__age age#方法名為受限制的變量去掉雙下劃線propertydef a…

ubuntu入門知識

1、linux系統發展歷史 unix -> Linux -> ubuntu linux發展軌跡圖 2、ubuntu下載和安裝 推薦使用長期支持版本&#xff1a; 10.04,12.04,14.04或LTS版本 安裝環境VMware虛擬機 3、安裝之后創建root sudo passwd root 輸入root用戶密碼即可 4、安裝軟件&#xff1a; 更新軟…

html 二級試題,計算機二級考試WEB試題及答案

計算機二級考試WEB試題及答案當前主要的 WEB數據庫訪問技術有哪些?答&#xff1a;到目前為止&#xff0c;WEB數據庫訪問技術主要分為兩大類&#xff1a;(1)公共網關接口技術(CGI);CGI 是 WEB 服務器運行時外部程序的規范&#xff0c;按照 CGI 編寫的程序可以擴展服務器的功能&…

細數阿里云服務器的十二種典型應用場景

原文鏈接&#xff1a;http://click.aliyun.com/m/13910/免費開通大數據服務&#xff1a;https://www.aliyun.com/product/odps文章轉載&#xff1a;小白楊1990如今&#xff0c;阿里云的產品可謂是多種多樣&#xff0c;紛繁復雜。面對各種各樣的技術和產品&#xff0c;ECS、RDS、…

動態給實例添加屬性和方法

from types import MethodType#創建一個空類 class Person(object):__slots__ ("name","age","speak","height")per Person() #動態添加屬性&#xff0c;這體現了動態語言的特點(靈活&#xff09;per.name "tom" print(…

android導入項目出現style錯誤,menu錯誤

android導入項目出現style錯誤&#xff0c;menu錯誤 style //查看 res/values/styles.xml 下的報錯點。<style name"AppBaseTheme" parent"Theme.AppCompat.Light"> //把這個改成 <style name"AppBaseTheme" parent"android:The…

Vim的基本操作總結

最近在學習Linux基礎的時候&#xff0c;對Vim的基本操作時遇到很多問題&#xff0c;如編輯錯誤&#xff0c;無法退出Vim等。通過一系列的學習后才解決了這些問題&#xff0c;希望這個過程能對后來者有所幫助 先對Vim的三種模式做個大致的介紹&#xff1a; Vi有三種基本工作模式…