本文介绍了使OpenGL工作(绘画)更快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
im尝试绘制512 * 512点,每个点使用其相邻点.例如
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();
问题是它的运行速度很慢,我知道我可以使用VBO(也可以使用CUDA),但是我不确定如何定义VBO以与GL_TRIANGLE_STRIP一起使用以及如何设置顶点的绘制顺序.
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.
推荐答案
使用VBO的一种方法(存在变体,使用旧的Vertex Array API,直接使用glBufferData和其他好东西上传数据).
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);
这篇关于使OpenGL工作(绘画)更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!