如何在ES2.0中使用VBO画圆 [英] How to draw a circle using VBO in ES2.0

查看:91
本文介绍了如何在ES2.0中使用VBO画圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Linux环境中开发ES 2.0应用程序.我的目标GPU是Fujitsu ruby​​ MB86298.为了优化性能,我决定使用VBO概念.我是VBO的新手.我使用VBO渲染了基本的图元,例如三角形和四边形,而我的顶点较少.为了使用VBO渲染冠,我计算了所有顶点(超过200个).现在我很难将200个顶点的数据发送到VBO,无法手动输入所有顶点数据并存储在数组中并将其传递给VBO.有什么方法可以将每个for循环的顶点数据(用于计算冠的顶点)发送到VBO?谁可以共享使用VBO在ES 2.0中绘制圆弧或圆的代码段?

I am trying to develop an ES 2.0 application in Linux environment. My target GPU is Fujitsu ruby MB86298 . To optimize the performance I have decided to use the VBO concept. I am very new to VBOs. I rendered basic primitives like triangle and quads using VBO where I have less no vertices . For rendering crown using a VBO, I computed all the vertices(more than 200). Now I am finding difficulty in sending this data of 200 vertices to the VBO.I cannot manually enter the all the vertex data and store in an array and pass it to VBO. Is there any way to send that vertex data of each for loop( used in computation of vertices of crown) to the VBO? Can any one share the code snippet of drawing an arc or circle in ES 2.0 using VBO's?

推荐答案

以下是用于渲染圆的一些代码片段.我尚未编译或运行此代码,因此可能有(希望是次要的)错字.

Here's some code fragments for rendering a circle. I haven't compiled or run this code, so there's a possibility of (hopefully minor) typos.

准备VBO,该操作将执行一次:

To prepare the VBO, which would be done once:

// 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是该位置的顶点属性的位置:

Then to draw, with posLoc being the location of the vertex attribute for the position:

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);

这篇关于如何在ES2.0中使用VBO画圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆