当前位置:网站首页>04 -- QT OpenGL two sets of shaders draw two triangles
04 -- QT OpenGL two sets of shaders draw two triangles
2022-07-03 19:19:00 【Qingsong 0527】
qmyopenglwidget.h
#ifndef QMYOPENGLWIDGET_H
#define QMYOPENGLWIDGET_H
#include <QWidget>
#include <QOpenGLWidget>
#include <QOpenGLFunctions_3_3_Core>
#include <QOpenGLShaderProgram>
class QMyOpenglWidget : public QOpenGLWidget, QOpenGLFunctions_3_3_Core
{
Q_OBJECT
public:
explicit QMyOpenglWidget(QWidget* parent = nullptr);
protected:
virtual void initializeGL();
virtual void resizeGL(int w, int h);
virtual void paintGL();
public:
void cretaeShader(QOpenGLShaderProgram& program, QString vShaderFile, QString fShaderFile);
void initTrianggleData(GLuint &vao, float* vertices, GLuint &ebo);
private:
unsigned int vao1;
unsigned int vao2;
unsigned int ebo1;
unsigned int ebo2;
QOpenGLShaderProgram program1;
QOpenGLShaderProgram program2;
signals:
public slots:
};
#endif // QMYOPENGLWIDGET_H
qmyopenglwidget.cpp
#include "qmyopenglwidget.h"
QMyOpenglWidget::QMyOpenglWidget(QWidget* parent):QOpenGLWidget(parent)
{
}
void QMyOpenglWidget::initializeGL()
{
initializeOpenGLFunctions();
float vertices1[] = {
-1.0, 0, 0,
1.0, 0, 0,
0, 1.0f, 0
};
float vertices2[] = {
-1.0, 0, 0,
1.0, 0, 0,
0, -0.5f, 0
};
QString vShaderFile = ":/shader/vertex.vert";
QString fShaderFile = ":/shader/frag.frag";
cretaeShader(program1, vShaderFile, fShaderFile);
initTrianggleData(vao1, vertices1, ebo1);
QString vShaderFile2 = ":/shader/vertex2.vert";
QString fShaderFile2 = ":/shader/frag2.frag";
cretaeShader(program2, vShaderFile2, fShaderFile2);
initTrianggleData(vao2, vertices2, ebo2);
}
void QMyOpenglWidget::initTrianggleData(GLuint &vao, float* vertices, GLuint &ebo)
{
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
unsigned int vbo1;
glGenBuffers(1, &vbo1);
glBindBuffer(GL_ARRAY_BUFFER, vbo1);
// Is currently bound to target Create a new data store for the buffer object //
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(GL_FLOAT), vertices, GL_STATIC_DRAW);
// Tell the graphics card how to parse the attribute value in the buffer
glVertexAttribPointer(0, 3, GL_FLOAT, FALSE, 3*sizeof (GL_FLOAT), nullptr);
// Turn on VAO The first attribute of Management
glEnableVertexAttribArray(0);
unsigned int indexs[] = {
0, 1, 2
};
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexs), indexs, GL_STATIC_DRAW);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void QMyOpenglWidget::resizeGL(int w, int h)
{
}
void QMyOpenglWidget::paintGL()
{
glClearColor(0.2f, 0.2f, 0.1f, 0.1f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo1);
program1.bind();
glBindVertexArray(vao1);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr);
program2.bind();
glBindVertexArray(vao2);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr);
update();
}
void QMyOpenglWidget::cretaeShader(QOpenGLShaderProgram& program, QString vShaderFile, QString fShaderFile)
{
program.addShaderFromSourceFile(QOpenGLShader::Vertex, vShaderFile);
program.addShaderFromSourceFile(QOpenGLShader::Fragment, fShaderFile);
program.bind();
program.link();
}
vertex.vert
#version 330 core
in vec3 pos;
void main(void)
{
gl_Position = vec4(pos, 1.0f);
}
frag.frag
#version 330 core
out vec4 FragColor; // Clip shader output
void main(void)
{
FragColor = vec4(0.8, 0.8, 0.1, 1.0);
}
Another set shader With this set shader similar

边栏推荐
- Ego planner code parsing Bspline_ Optimizer section (1)
- SQL injection for Web Security (1)
- If the warehouse management communication is not in place, what problems will occur?
- 【Proteus仿真】用24C04与1602LCD设计的简易加密电子密码锁
- Simulation scheduling problem of SystemVerilog (1)
- How does if ($variable) work? [repeat] - how exactly does if ($variable) work? [duplicate]
- 论文阅读 GloDyNE Global Topology Preserving Dynamic Network Embedding
- Recommend a GIF processing artifact less than 300K - gifsicle (free download)
- Flutter网络和数据存储框架搭建 -b1
- Record: writing MySQL commands
猜你喜欢

The installation path cannot be selected when installing MySQL 8.0.23

Flutter网络和数据存储框架搭建 -b1

Free year-end report summary template Welfare Collection

Record: pymysql is used in pycharm to connect to the database

EGO Planner代码解析bspline_optimizer部分(3)

Smart wax therapy machine based on STM32 and smart cloud

Simulation scheduling problem of SystemVerilog (1)

Summary of composition materials for 2020 high-frequency examination center of educational resources

2020 intermediate financial management (escort class)
![[optics] dielectric constant calculation based on MATLAB [including Matlab source code 1926]](/img/cb/ee696d5a7d6bef96fe0db89e8478ed.jpg)
[optics] dielectric constant calculation based on MATLAB [including Matlab source code 1926]
随机推荐
math_泰勒公式
为什么要做特征的归一化/标准化?
Why should the gradient be manually cleared before back propagation in pytorch?
Help change the socket position of PCB part
Record: solve the problem that MySQL is not an internal or external command environment variable
Record: MySQL changes the time zone
Hard disk monitoring and analysis tool: smartctl
cipher
Luogu-p1107 [bjwc2008] Lei Tao's kitten
QT -- qfileinfo file information reading
Record: install MySQL on ubuntu18.04
Understanding of database architecture
[proteus simulation] a simple encrypted electronic password lock designed with 24C04 and 1602LCD
TFs and SVN [closed] - TFs vs SVN [closed]
[free sharing] kotalog diary2022 plan electronic manual ledger
Free year-end report summary template Welfare Collection
flask 生成swagger文档
Basic principle of LSM tree
Work Measurement - 1
Using the visualization results, click to appear the corresponding sentence