在android中用opengl-es画一个圆会得到一个正方形

发布于 2024-10-14 11:28:11 字数 959 浏览 1 评论 0原文

我正在尝试使用以下代码在 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();

但是,我从未在屏幕上绘制过圆形,而只绘制过正方形。我很困惑,感谢任何帮助:)

I am trying to draw a circle in opengl-es using the following code. It creates a number of points around a circle. If I print out the values in the vertex array it can be seen that they form points around a circle.

// 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

And the calls to opengl in my drawing function:

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

However I never get a circle drawn to the screen, only ever a square. I am very confused, any help is appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£烟消云散 2024-10-21 11:28:11

看起来错误的一件事是 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 调用中的一半点。

one thing that looks wrong is the calculation of rad. Try modifying it to something like:

double rad = deg2rad(i*360/(points*3));

You need to divide it by points*3 because the i counter is getting incremented by 3 on each iteration. You should print out the value of rad on each iteration and verify that you're getting values incrementing between 0 and 2 * PI

EDIT: you may also need to change the direction of the circle (i'm not sure at the top of my head). but if the above doesn't render anything, try reversing it:

double rad = deg2rad(360.0 - i*360/(points*3));

Also, it looks like you're only rendering half the points in the glDrawArrays call.

会傲 2024-10-21 11:28:11

//...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);
    }
}

轻松潇洒。


//...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);
    }
}

Easy breezy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文