当前位置:网站首页>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

边栏推荐
- 第一章:递归求n的阶乘n!
- Webrtc[41] - Analysis of the establishment process of webrtc transmission channel
- How can I avoid "div/0!" Errors in Google Docs spreadsheet- How do I avoid the '#DIV/0!' error in Google docs spreadsheet?
- Dynamic planning -- expansion topics
- 【光学】基于matlab涡旋光产生【含Matlab源码 1927期】
- DriveSeg:动态驾驶场景分割数据集
- What is the content of game modeling
- flask 生成swagger文档
- 第二十章:y= sin(x)/x,漫步坐标系计算,y= sin(x)/x 带廓幅图形,奥运五环,小球滚动与弹跳,流水显示,矩形优化裁剪,r个皇后全控nxn棋盘
- If the warehouse management communication is not in place, what problems will occur?
猜你喜欢

Simulation scheduling problem of SystemVerilog (1)

Ego planner code parsing Bspline_ Optimizer section (3)

FBI warning: some people use AI to disguise themselves as others for remote interview

第一章:递归求n的阶乘n!

If the warehouse management communication is not in place, what problems will occur?

KINGS

Counting from the East and counting from the West will stimulate 100 billion industries. Only storage manufacturers who dare to bite the "hard bone" will have more opportunities

SQL injection for Web Security (1)

Sentinel source code analysis part I sentinel overview

Verilog HDL continuous assignment statement, process assignment statement, process continuous assignment statement
随机推荐
EGO Planner代码解析bspline_optimizer部分(1)
Zhang Fei hardware 90 day learning notes - personal records on day 2, please see my personal profile / homepage for the complete
High concurrency Architecture - read write separation
C enum contains value - C enum contains value
P1891 crazy LCM (Euler function)
FBI警告:有人利用AI换脸冒充他人身份进行远程面试
EGO Planner代碼解析bspline_optimizer部分(1)
Read the paper glodyne global topology preserving dynamic network embedding
我们做了一个智能零售结算平台
Go home early today
What is the content of game modeling
论文阅读 GloDyNE Global Topology Preserving Dynamic Network Embedding
If the warehouse management communication is not in place, what problems will occur?
Sentinel source code analysis part I sentinel overview
Thinking about festivals
Valentine's Day - make an exclusive digital collection for your lover
Nous avons fait une plateforme intelligente de règlement de détail
Dart JSON编码器和解码器剖析
第二章:基于分解的求水仙花数,基于组合的求水仙花数, 兰德尔数,求[x,y]内的守形数,探求n位守形数,递推探索n位逐位整除数
第一章:求所有阶乘和数,大奖赛现场统分程序设计,三位阶乘和数,图形点扫描,递归求n的阶乘n!,求n的阶乘n!,舍罕王失算