加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – OpenGL更新顶点数组/缓冲区

发布时间:2020-12-16 07:14:14 所属栏目:百科 来源:网络整理
导读:当我第一次向缓冲区添加一些顶点时,这些是我正在调用的相关函数 // Create and bind the object's Vertex Array Object: glGenVertexArrays(1,_vao); glBindVertexArray(_vao); // Create and load vertex data into a Vertex Buffer Object: glGenBuffers(1
当我第一次向缓冲区添加一些顶点时,这些是我正在调用的相关函数

// Create and bind the object's Vertex Array Object:
    glGenVertexArrays(1,&_vao);
    glBindVertexArray(_vao);

    // Create and load vertex data into a Vertex Buffer Object:
    glGenBuffers(1,&_vbo);
    glBindBuffer(GL_ARRAY_BUFFER,_vbo);
    glBufferData(GL_ARRAY_BUFFER,vertices.size() * sizeof(float),&vertices[0],GL_STATIC_DRAW);

    // Tells OpenGL that there is vertex data in this buffer object and what form that vertex data takes:

    // Obtain attribute handles:
    _posAttrib = glGetAttribLocation(program,"position");
    glEnableVertexAttribArray(_posAttrib);
    glVertexAttribPointer(_posAttrib,// attribute handle
                          4,// number of scalars per vertex
                          GL_FLOAT,// scalar type
                          GL_FALSE,0);

    // Unbind vertex array:
    glBindVertexArray(0);

但是稍后在我的程序中,我想添加一些顶点.

我通过以下方式执行此操作(在单独的函数中:

add_vertices(x,y); //adds the necessary vertices to the vector. 
glGenBuffers(1,&_vbo);
glBindBuffer(GL_ARRAY_BUFFER,_vbo);
glBufferData(GL_ARRAY_BUFFER,(TRIANGLE_AMOUNT+1)*4*_number_of_circles * sizeof(float),GL_STATIC_DRAW);

假设glBufferData的第二个参数中的时髦大小很好,我错过了什么吗?还有其他需要调用的OpenGL函数吗?

我没有得到任何错误,但是当我试图通过使用顶点的不同子集循环glDrawArrays来绘制具有新顶点的额外形状时,没有任何反应.只绘制第一个形状.

我希望这是半连贯的……如果有任何我没有提供的信息,请告诉我.

干杯.

解决方法

在OpenGL中,更改缓冲区和交换缓冲区数据是两个不同的事情,需要在之后采取不同的操作:

交换数据

在这种情况下,需要先前生成的vbo.要上传新数据,只需要绑定缓冲区并缓冲新数据:

glBindBuffer(GL_ARRAY_BUFFER,GL_STATIC_DRAW);

创建一个新缓冲区

在这种情况下,glGenerateBuffers会生成一个新缓冲区,并且(除了上传数据外)还必须更新所有VAO绑定.

旁注:在上面显示的代码中,您创建了一个新缓冲区而不删除以前的缓冲区.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读