当前位置:网站首页>03 -- QT OpenGL EBO draw triangle
03 -- QT OpenGL EBO draw triangle
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();
void initTrianggleData();
private:
unsigned int vao1;
unsigned int ebo1;
QOpenGLShaderProgram program;
signals:
public slots:
};
#endif // QMYOPENGLWIDGET_H
qmyopenglwidget.cpp
#include "qmyopenglwidget.h"
QMyOpenglWidget::QMyOpenglWidget(QWidget* parent):QOpenGLWidget(parent)
{
}
void QMyOpenglWidget::initializeGL()
{
initializeOpenGLFunctions();
cretaeShader();
initTrianggleData();
}
void QMyOpenglWidget::initTrianggleData()
{
glGenVertexArrays(1, &vao1);
glBindVertexArray(vao1);
float vertices[] = {
-1.0, 0, 0,
1.0, 0, 0,
0, 1.0f, 0
};
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, sizeof(vertices), 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);
float colors[] = {
1.0, 0, 0,
0.0, 1.0, 0,
0, 0.0f, 1.0
};
unsigned int vbo2;
glGenBuffers(1, &vbo2);
glBindBuffer(GL_ARRAY_BUFFER, vbo2);
// Is currently bound to target Create a new data store for the buffer object //
glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
// Tell the graphics card how to parse the attribute value in the buffer
glVertexAttribPointer(1, 3, GL_FLOAT, FALSE, 3*sizeof (GL_FLOAT), nullptr);
// Turn on VAO The first attribute of Management
glEnableVertexAttribArray(1);
unsigned int indexs[] = {
0, 1, 2
};
glGenBuffers(1, &ebo1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo1);
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);
glBindVertexArray(vao1);
//glDrawArrays(GL_TRIANGLES, 0, 3);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr);
update();
}
void QMyOpenglWidget::cretaeShader()
{
program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shader/vertex.vert");
program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shader/frag.frag");
program.bind();
program.link();
}
vertex.vert
#version 330 core
in vec3 pos;
in vec3 colors;
out vec3 fcolors;
void main(void)
{
fcolors = colors;
gl_Position = vec4(pos, 1.0f);
}
frag.frag
#version 330 core
out vec4 FragColor; // Clip shader output
in vec3 fcolors;
void main(void)
{
//FragColor = vec4(0.5, 0.8, 0.1, 1.0);
FragColor = vec4(fcolors, 1.0f);
}
边栏推荐
- Valentine's Day - make an exclusive digital collection for your lover
- FBI警告:有人利用AI换脸冒充他人身份进行远程面试
- Max of PHP FPM_ Some misunderstandings of children
- 【水质预测】基于matlab模糊神经网络水质预测【含Matlab源码 1923期】
- [disease identification] machine vision lung cancer detection system based on Matlab GUI [including Matlab source code 1922]
- BUUCTF
- Buuctf's different flags and simplerev
- The online customer service system developed by PHP is fully open source without encryption, and supports wechat customer service docking
- Summary of learning materials and notes of Zhang Fei's actual combat electronics 1-31
- Differential constrained SPFA
猜你喜欢
【学术相关】顶级论文创新点怎么找?中国高校首次获CVPR最佳学生论文奖有感...
During MySQL installation, the download interface is empty, and the components to be downloaded are not displayed. MySQL installer 8.0.28.0 download interface is empty solution
[water quality prediction] water quality prediction based on MATLAB Fuzzy Neural Network [including Matlab source code 1923]
为什么要做特征的归一化/标准化?
What is the content of game modeling
01. Preparation for automated office (free guidance, only three steps)
A green plug-in that allows you to stay focused, live and work hard
Why should we do feature normalization / standardization?
记录在模拟器中运行flutter时报的错
Record the errors reported when running fluent in the simulator
随机推荐
TFs and SVN [closed] - TFs vs SVN [closed]
Flutter network and data storage framework construction-b1
Next spread
Pecan — @expose()
Le changement est un thème éternel
Merge K ascending linked lists
P3402 persistent and searchable
【光学】基于matlab涡旋光产生【含Matlab源码 1927期】
Php based campus lost and found platform (automatic matching push)
Scrapy爬虫框架
Zhang Fei hardware 90 day learning notes - personal record on day 5. Please see my personal profile / homepage for the complete record
High concurrency Architecture - distributed search engine (ES)
These problems should be paid attention to in the production of enterprise promotional videos
Zhang Fei hardware 90 day learning notes - personal record of day 3, please see my personal profile / homepage for the complete
为什么要做特征的归一化/标准化?
Buuctf's different flags and simplerev
We have built an intelligent retail settlement platform
A green plug-in that allows you to stay focused, live and work hard
Ego planner code parsing Bspline_ Optimizer section (2)
Using the visualization results, click to appear the corresponding sentence