如何在ES2.0中使用VBO绘制圆

时间:2014-08-13 05:57:53

标签: opengl-es-2.0 vbo

我正在尝试在Linux环境中开发ES 2.0应用程序。我的目标GPU是富士通ruby MB86298。为了优化性能,我决定使用VBO概念。我是VBO的新手。我使用VBO渲染了三角形和四边形等基本图元,其中没有顶点。为了使用VBO渲染表冠,我计算了所有顶点(超过200个)。现在我发现难以将200个顶点的数据发送到VBO.I无法手动输入所有顶点数据并存储在数组中并将其传递给VBO。有没有办法将每个for循环的顶点数据(用于计算顶点的顶点)发送到VBO?任何人都可以使用VBO共享在ES 2.0中绘制圆弧或圆的代码片段吗?

1 个答案:

答案 0 :(得分:2)

这里有一些用于渲染圆圈的代码片段。我还没有编译或运行此代码,因此可能存在(希望是次要的)错别字。

准备VBO,这将完成一次:

// Number of segments the circle is divided into.
const unsigned DIV_COUNT = 32;

// Will use a triangle fan rooted at the origin to draw the circle. So one additional
// point is needed for the origin, and another one because the first point is repeated
// as the last one to close the circle.
GLfloat* coordA = new GLfloat[(DIV_COUNT + 2) * 2];

// Origin.
unsigned coordIdx = 0;
coordA[coordIdx++] = 0.0f;
coordA[coordIdx++] = 0.0f;

// Calculate angle increment from point to point, and its cos/sin.
float angInc = 2.0f * M_PI / static_cast<float>(DIV_COUNT);
float cosInc = cos(angInc);
float sinInc = sin(angInc);

// Start with vector (1.0f, 0.0f), ...
coordA[coordIdx++] = 1.0f;
coordA[coordIdx++] = 0.0f;

// ... and then rotate it by angInc for each point.
float xc = 1.0f;
float yc = 0.0f;
for (unsigned iDiv = 1; iDiv < DIV_COUNT; ++iDiv) {
    float xcNew = cosInc * xc - sinInc * yc;
    yc = sinInc * xc + cosInc * yc;
    xc = xcNew;

    coordA[coordIdx++] = xc;
    coordA[coordIdx++] = yc;
}

// Repeat first point as last point to close circle.
coordA[coordIdx++] = 1.0f;
coordA[coordIdx++] = 0.0f;

GLuint vboId = 0;
glGenBuffers(1, &circVboId);
glBindBuffer(GL_ARRAY_BUFFER, circVboId);
glBufferData(GL_ARRAY_BUFFER, (DIV_COUNT + 2) * 2 * sizeof(GLfloat), coordA, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

delete[] coordA;

然后绘制,posLoc是位置的顶点属性的位置:

glBindBuffer(GL_ARRAY_BUFFER, circVboId);
glVertexAttribPointer(posLoc, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(posLoc);

glDrawArrays(GL_TRIANGLE_FAN, 0, DIV_COUNT + 2);

glBindBuffer(GL_ARRAY_BUFFER, 0);