glBegin(GL_TRIANGLE_STRIP);
glVertex(x,y,z);
glVertex(x+1,y,z);
glVertex(x,y,z+1);
glVertex(x+1,y,z+1);
glEnd();
问题是它工作得很慢,我知道我可以使用VBO(与CUDA一起工作)但我不知道如何定义VBO以使用GL_TRIANGLE_STRIP以及如何设置顶点的绘制顺序。
答案 0 :(得分:2)
使用VBO的一种方法(有变种,使用旧的Vertex Array API,使用glBufferData和其他好东西直接上传数据)。
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);