Making opengl work (paint)faster

China☆狼群 提交于 2019-12-05 21:13:31

One way to use VBOs (there are variants, using the old Vertex Array API, directly uploading the data with glBufferData and other nice things).

GLuint vbo; // this variable becomes a handles, keep them safe
GLuint idx;

glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*512*512*3, NULL, GL_STATIC_DRAW);
GLfloat *vbuf = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE);
fill_with_vertex_data(vbo);
glUnmapBuffer(GL_ARRAY_BUFFER);

glGenBuffers(1, &idx);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*512*512*4, NULL, GL_STATIC_DRAW);
GLfloat *ibuf = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE);
GLuint restart_index = 0xffff;
fill_with_indices_restart(ibuf, restart_index); // 0xffff is the restart index to be used, delimiting each triangle strip.
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);

/* ... */

glEnableVertexAttribArray(vertex_position_location);

glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttrxPointer(vertex_position_location, 3, GL_FLOAT, GL_FALSE, 0, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, idx);
glPrimitiveRestartIndex(restart_index);
glDrawElements(GL_TRIANGLE_STRIP, 0, count, 0);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!