当前位置:网站首页>OpenGL: dynamically modify vbo/ebo
OpenGL: dynamically modify vbo/ebo
2022-07-22 09:13:00 【Wings of June】
A recent one Demo, It needs to be in operation , Support switching models , Just need to change the data
own OpenGL I haven't learned systematically , A batch of water , After a long time, I just can't
class GlWidget : public QOpenGLWidget, protected QOpenGLFunctions_3_3_Core
{
Q_OBJECT
public:
GlWidget(QWidget *parent);
~GlWidget() Q_DECL_OVERRIDE;
void LoadModel();
protected:
// On the first call resizeGL() or paintGL() Before calling once .
void initializeGL() Q_DECL_OVERRIDE;
// Whenever adjusted Widget The size of ( Show the window for the first time Widget It will be called when , Because all new creation Widget Will automatically get resizing events ).
void resizeGL(int width, int height) Q_DECL_OVERRIDE;
void paintGL() Q_DECL_OVERRIDE;// Rendering OpenGL scene , You need to update Widget Is called .
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void wheelEvent(QWheelEvent *event) Q_DECL_OVERRIDE;
Top loaded initializeGL Function is mainly initialization , Bind some model data that does not need to be changed VAO
overloaded function paintGL It mainly refreshes constantly , You can also modify the data here , But the loading model is not suitable here , Because this is a place that keeps refreshing , And the frequency of model loading will be very small , And there must be a key trigger
So the function to load the model is LoadModel, It is necessary to modify the data here , The modified data will be synchronized to paintGL,paintGL Real time refresh
I started LoadModel The code is like this :
//------------- Read the model data from the file and store it in the dynamic array -----------
float* screen = new float[N];
unsigned int* screen_idx = new unsigned int[M];
//---------------------- screen VAO ----------------------
screenProgram.bind();// Bind shader
glGenVertexArrays(1, &screenVao);// Generate VAO
glBindVertexArray(screenVao);// binding VAO
glGenBuffers(1, &screenVbo);// Generate VBO
glBindBuffer(GL_ARRAY_BUFFER, screenVbo);/// binding VBO
glBufferData(GL_ARRAY_BUFFER, N * sizeof(float), screen, GL_STATIC_DRAW);// Go to VBO Fill in the data
glGenBuffers(1, &screenEbo);// Generate EBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, screenEbo);// binding EBO
glBufferData(GL_ELEMENT_ARRAY_BUFFER, M * sizeof(unsigned int), screen_idx, GL_STATIC_DRAW);// Go to EBO Fill in the data
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), reinterpret_cast<void*>(0));
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glBindVertexArray(screenVao);
glDrawElements(GL_TRIANGLES, vbo_num, GL_UNSIGNED_INT, 0);
screenProgram.release();
delete[]screen;
delete[]screen_idx;
It just doesn't work , You can't even try , It was once suspected that there was something wrong with dynamic arrays , Later, it was replaced by a vector
//...
QVector<float> screen;
QVector<unsigned int> screen_idx;
//...
glBufferData(GL_ARRAY_BUFFER, N * sizeof(float), &screen[0], GL_STATIC_DRAW);
//...
glBufferData(GL_ELEMENT_ARRAY_BUFFER, M * sizeof(unsigned int), &screen_idx[0], GL_STATIC_DRAW);
no way .
Later, a big man's words inspired me opengl VAO VBO Correct misunderstandings _ The squalid -CSDN Blog
Mistake 1 :
to VBO When giving data , namely call glBindBuffer(GL_ARRAY_BUFFER,vbo)
glBufferData(GL_ARRAY_BUFFER,sizeof(data),GL_STATIC_DRAW)
These two functions , It needs to be done in advance adopt glBindVertexArray() Bind to a VAO On .Corrigendum :
actually ,glBufferData(), hold Array data write in One vbo when , There is no need to bind glBindVertexArray() One VAO.
It's also very simple : I want to give one vbo Write data , I don't really care about vao Which is , It has nothing to do with the fact that I want to write data !
Only in hold vbo The data of , Really pass to shader When , That is to say call glVertexAttribPointer() Such as for shader Operation of ( such as glUseProgram,glEnableVertexAttribArray ) when , You must clearly specify to which VAO On .
stay glBufferData() when , Just give a brief answer vbo The data transfer , Follow shader irrelevant , No need at all bind whatever VAO.
Then modify the main code to the following :
glBindBuffer(GL_ARRAY_BUFFER, screenVbo);
glBufferData(GL_ARRAY_BUFFER, N * sizeof(float), screen, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, screenEbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, M * sizeof(unsigned int), screen_idx, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), reinterpret_cast<void*>(0));
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
Is to remove the rebinding VAO, Because of this VAO In fact, it has been bound in initialization
There is no need to regenerate VAOVBOEBO And so on. , Because there are also , Just bind VBO/EBO, Then fill in the data .
边栏推荐
- Grep, egrep, fgrep character matching
- 使用style解决异形屏横屏时的白色边框
- Single cell paper record (Part5) -- a joint model of unpaired data from scrna SEQ and st for impating missing gene
- 登录式shell和非登录式shell
- 腾讯游戏 :我们如何基于 StarRocks 构建云原生数仓
- Single cell paper record (Part18) -- spectral clustering based on learning similarity matrix
- Is it safe for beginners to open accounts? Can I apply online?
- 好轮子收藏:一个支持几乎所有流行格式的图像加载库stb_image.h
- 文献记录(part108)--Distributionally Robust and Multi-Objective Nonnegative Matrix Factorization
- 前辈的前后台分离介绍
猜你喜欢
【幹貨】知識共享的障礙及解决方法
Hisilicon [hi3531]gpio lighting application and register operation
Vscode configuration code automatic formatting and repair
position: -webkit-sticky; /* for Safari */ position: sticky;
导入头文件到pch 出错
Token
创建线程池的七种方式
关于 vditor 发布 plaintext 语法问题
重写dispatchTouchEvent实现播放画面锁功能
解决按钮UIButton重复点击问题
随机推荐
Experimental support for decorators is a feature that is subject to change in a future release. Set
Literature learning (part98) -- pseudosupervised deep subspace clustering
A method of keeping the original scale of OpenGL map
好轮子收藏:一个支持几乎所有流行格式的图像加载库stb_image.h
分库分表的数据存储方案思路
Samsung 6818 water lamp program based on uboot
Scheme design of meal return process of performance system
解决按钮UIButton重复点击问题
JDBC事务操作
SQL子查询
2021-09-23
ArrayList源码解析
【OAuth2】二、OAuth2.1的已知变动
文献学习(part98)--Pseudo-supervised Deep Subspace Clustering
【Oauth2】四、OAuth2AuthorizationRequestRedirectFilter
A custom control for picture selection
华泰证券在哪开户安全,手机可以吗
监督学习(回归、分类问题)与无监督学习(聚类问题)
前辈的前后台分离介绍
Token