基于 Ubuntu Qt的OpenGL编程----(一) 三角形
作者:互联网
原Joey De Vries 的现代OpenGL教程链接如下
运行结果如下:
1创建第一个工程
最后的项目工程如下:
2 .qt集成了opengl相关函数的模块,我们要想使用qt只需要在.pro文件中增添opengl模块即可,如下图所示:
Qt对OpenGL的支持需要public继承QOpenGLWidget类,还要protected继承QOpenGLFunctions_3_3_Core(因为我所用opengl版本为3.3,所以继承QOpenGLFunctions_3_3_Core),然后重写这三个函数
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QOpenGLWidget>
#include<QOpenGLExtraFunctions>
#include<QDebug>
#include<QOpenGLShaderProgram>
#include<QOpenGLFunctions_3_3_Core>
class Triangle : public QOpenGLWidget,protected QOpenGLFunctions_3_3_Core //qt对opengl的支持,必须public继承QOpenGLWidget类,protected继承QOpenGLExtraFunctions
{
Q_OBJECT
public:
Triangle();
~Triangle();
protected:
//重写这三个函数
virtual void initializeGL();//用来初始化opengl的资源和状态
virtual void resizeGL(int w, int h);//用来设置窗口大小
virtual void paintGL();//用来绘制窗口
private:
QOpenGLShaderProgram shaderProgram;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include<QMessageBox>
static unsigned int VAO,VBO,EBO;
Triangle::Triangle()
{
}
Triangle::~Triangle()
{
glDeleteVertexArrays(1,&VAO);
glDeleteBuffers(1,&VBO);
}
void Triangle::initializeGL()
{
this->initializeOpenGLFunctions();//初始化qt opengl部分
bool success = shaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex,":/triangles.vert");//加载定点着色器
if(!success)
{
QMessageBox::critical(this,"error","顶点着色器triangle.vert加载失败");
}
success = shaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment,":/triangles.frag");//加载片段着色器
if(!success)
{
QMessageBox::critical(this,"error","片段着色器triangle.frag加载失败");
}
success = shaderProgram.link();//把着色器链接到程序上
if(!success)
{
QMessageBox::critical(this,"error","着色器连接诶失败");
}
float vertices[]={ //坐标数组
-0.5f,-0.5f,0.0f, //左边顶点
0.5f,-0.5f,0.0f, //右边顶点
0.0f,0.5f,0.0f //上边顶点
};
glGenVertexArrays(1,&VAO);//生成一个顶点数组对象
glGenBuffers(1,&VBO);//生成一个顶点缓冲对象
glBindVertexArray(VAO);//通过VAO绑定VBO的顶点数据
glBindBuffer(GL_ARRAY_BUFFER,VBO);//把新建的VBO缓冲绑定到GL_ARRAY_BUFFER目标上
glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);//把之前定义的顶点数据复制到缓冲的内存中
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(GL_FLOAT),(void*)0);//告诉opengl如何解析顶点数据
glEnableVertexAttribArray(0);//启用顶点属性0
glBindBuffer(GL_ARRAY_BUFFER,0);//取消VBO的绑定
glBindVertexArray(0);//取消VAO的绑定
}
void Triangle::resizeGL(int w, int h)
{
glViewport(0,0,w,h);//控制渲染窗口的大小
}
void Triangle::paintGL()
{
glClearColor(0.2f,0.3f,0.3f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
shaderProgram.bind();
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES,0,3);
shaderProgram.release();
}
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Triangle w;
w.show();
return a.exec();
}
triangles.vert
#version 330 core
layout(location = 0)in vec3 aPos;
void main()
{
gl_Position = vec4(aPos.x,aPos.y,aPos.z,1.0f);
}
triangles.frag
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0f,0.0f,0.2f,1.0f);
}
标签:include,Triangle,Qt,OpenGL,void,----,顶点,GL,VAO 来源: https://blog.csdn.net/weixin_48239221/article/details/114241353