问题
im trying to paint 512*512 points , which each point uses its neighbors. for example
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();
the problem is that it works quite slow , i know i can use VBO (working with CUDA aswell) but im not sure how to define the VBO to work with GL_TRIANGLE_STRIP and how to set the painting order of the vertices.
回答1:
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);
来源:https://stackoverflow.com/questions/8292142/making-opengl-work-paintfaster