在android中用opengl-es画一个圆会得到一个正方形
我正在尝试使用以下代码在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来错误的一件事是 rad 的计算。尝试将其修改为以下内容:
您需要将其除以
points*3
,因为i
计数器在每次迭代时都会增加 3。您应该在每次迭代中打印出 rad 的值,并验证您获得的值是否在 0 到 2 之间递增 * PI编辑:您可能还需要更改方向圆的(我不确定在我的头顶)。但如果上面没有渲染任何内容,请尝试反转它:
此外,看起来您只渲染了
glDrawArrays
调用中的一半点。one thing that looks wrong is the calculation of
rad
. Try modifying it to something like:You need to divide it by
points*3
because thei
counter is getting incremented by 3 on each iteration. You should print out the value ofrad
on each iteration and verify that you're getting values incrementing between 0 and 2 * PIEDIT: 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:
Also, it looks like you're only rendering half the points in the
glDrawArrays
call.轻松潇洒。
Easy breezy.