Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am new to OpenGL ES and facing a hard time drawing a circle on my GLSurfaceView. Here's what I have so far.

The circle class

public class MyGLBall {

private int points=40;
private float vertices[]={0.0f,0.0f,0.0f};
private FloatBuffer vertBuff;


//centre of circle

public MyGLBall(){

    vertices=new float[(points+1)*3];
    for(int i=3;i<(points+1)*3;i+=3){
      double rad=(i*360/points*3)*(3.14/180);
      vertices[i]=(float)Math.cos(rad);
      vertices[i+1]=(float) Math.sin(rad);
      vertices[i+2]=0;
    }     
      ByteBuffer bBuff=ByteBuffer.allocateDirect(vertices.length*4);    
      bBuff.order(ByteOrder.nativeOrder());
      vertBuff=bBuff.asFloatBuffer();
      vertBuff.put(vertices);
      vertBuff.position(0);


}

public void draw(GL10 gl){
    gl.glPushMatrix();
    gl.glTranslatef(0, 0, 0);
//  gl.glScalef(size, size, 1.0f);
    gl.glColor4f(1.0f,1.0f,1.0f, 1.0f); 
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, points/2);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glPopMatrix();
 }  

 }

I couldn't retrieve the screenshot of my image but here's what it looks like enter image description here

As you can see the border has crests and troughs thereby rendering it squiggly, which I do not want. All I want is a simple curve.

share|improve this question
    
what you probably want to do is use a texture, that way you only have to use a couple vertices. –  Ryan Mar 27 '12 at 6:35
    
thanks. Can you elaborate the solution as an answer? I could really use some guidance here. –  ladiesMan217 Mar 27 '12 at 6:37
    
"fix my code" problems are generally not liked around here. i'd suggest you search google for some tutorials on OpenGL or OpenGL ES. At the very least, tell us what you know to be wrong with your code. –  stephelton Mar 27 '12 at 6:47
    
@stephelton Updated my answer. I couldn't retrieve the image but the border of the circle is squiggly just like that of the above. –  ladiesMan217 Mar 27 '12 at 7:07
    
You should clarify that otherwise people are likely to think that is somehow rendered and/or be confused. –  stephelton Mar 27 '12 at 7:25
show 2 more comments

2 Answers

up vote 3 down vote accepted

I can't imagine how your code would produce the image you linked. I could however, imagine how it might produce an image like this:

enter image description here

With flat sides. So really what you want to do is increase the number of sides. Try setting:

private int points=40;

to something larger like

private int points=360;

If you wanted a loop like your image instead of a circle you can do something like this:

public float[] DrawLoop(float centerX, float centerY, float sides, float innerRadius, float outerRadius) {
    float[] vertices = new float[(sides+1)*4];
    for (int i = 0; i <= sides; i+=4) {
        verticies[i+0] = centerX + (sin(toRadians(360f * (i / sides))) * innerRadius);
        verticies[i+1] = centerY - (cos(toRadians(360f * (i / sides))) * innerRadius);
        verticies[i+2] = centerX + (sin(toRadians(360f * (i / sides))) * outerRadius);
        verticies[i+3] = centerY - (cos(toRadians(360f * (i / sides))) * outerRadius);
    }
    return vertices;
}
share|improve this answer
add comment

I believe there's a bug in your code in this line:

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

... which would cause the kind of error you described.

I think the line should look like this instead:

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

Note the parentheses () around points*3

If you fix your code, I believe your output will no longer be 'squiggly'.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.