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.mzph.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有三种基本工作模式…