我正在尝试使用以下代码在opengl-es中绘制一个圆圈。它围绕圆圈创建了许多点。如果我打印出顶点数组中的值,可以看出它们围绕圆形成了点。
// Create verticies for a circle
points=40;
vertices = new float[(points+1)*3];
//CENTER OF CIRCLE
vertices[0]=0.0f;
vertices[1]=0.0f;
vertices[2]=0.0f;
for (int i = 3; i<(points+1)*3; i+=3){
double rad = deg2rad(i*360/points);
vertices[i] = (float) (Math.cos(rad));
vertices[i+1] = (float) (Math.sin(rad));
vertices[i+2] = 0;
}
// vertexBuffer is filled with verticies
在我的绘图函数中调用opengl:
// IN MY DRAWING FUNCTION:
gl.glPushMatrix();
gl.glTranslatef(x, y, 0);
gl.glScalef(size, size, 1.0f);
gl.glColor4f(1.0f,1.0f,1.0f, 1.0f);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, points/2);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glPopMatrix();
然而,我从未在屏幕上画过一个圆圈,只有一个正方形。我很困惑,任何帮助表示赞赏:)
答案 0 :(得分:2)
一件看起来不对的事是rad
的计算。尝试将其修改为:
double rad = deg2rad(i*360/(points*3));
您需要将其除以points*3
,因为i
计数器在每次迭代时增加3。您应该在每次迭代时打印出rad
的值,并验证您的值是否在0到2之间递增* PI
double rad = deg2rad(360.0 - i*360/(points*3));
此外,您似乎只在glDrawArrays
来电中提取了一半的分数。
答案 1 :(得分:2)
//...vertex and index buffers...
int VERTICES=180; // more than needed // changed variable name
float coords[] = new float[VERTICES * 3];
float theta = 0;
for (int i = 0; i < VERTICES * 3; i += 3) {
coords[i + 0] = (float) Math.cos(theta);
coords[i + 1] = (float) Math.sin(theta);
coords[i + 2] = 0;
vertexBuffer.put(coords[i + 0]);
vertexBuffer.put(coords[i + 1]);
vertexBuffer.put(coords[i + 2]);
theta += Math.PI / 90;
}
// ....set buffer positions to zero...
protected void draw(GL10 gl) {
gl.glColor4f(0, 0, 1, 0.5f);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glDrawElements(GL10.GL_TRIANGLE_FAN, VERTICES,
GL10.GL_UNSIGNED_SHORT, indexBuffer);
}
}
轻松愉快。