Draw a circle with a sector cut out in OpenGL ES 1.1

I am trying to make the following form using OpenGL ES 1.1. And well, I'm stuck, I really don't know how to do this.

My game currently uses the Android Canvas API, which is not hardware accelerated, so I rewrite it using OpenGL ES. The Canvas class has a drawArc method that draws this shape very easily; Canvas.drawArc

Any tips / tricks on how to do this with OpenGL ES?

enter image description here

Thanks for reading.

+3
source share
3 answers
void gltDrawArc(unsigned int const segments, float angle_start, float angle_stop)
{
    int i;
    float const angle_step = (angle_stop - angle_start)/segments;

    GLfloat *arc_vertices;
    arc_vertices = malloc(2*sizeof(GLfloat) * (segments+2));

    arc_vertices[0] = arc_vertices[1] = 0.

    for(i=0; i<segments+1; i++) {
        arc_vertices[2 + 2*i    ] = cos(angle_start + i*angle_step);
        arc_vertices[2 + 2*i + 1] = sin(angle_start + i*angle_step);
    }
    glVertexPointer(2, GL_FLOAT, 0, arc_vertices);
    glEnableClientState(GL_VERTEX_ARRAY);
    glDrawArrays(GL_TRIANGLE_FAN, 0, segments+2);
    free(arc_vertices);
}
+5
source

How about just fetching a circle at discrete angles and drawing a GL_TRIANGLE_FAN?

: - 2D:

glBegin(GL_TRIANGLE_FAN);
    glVertex2f(0.0f, 0.0f);
    for(angle=startAngle; angle<=endAngle; ++angle)
        glVertex2f(cos(angle), sin(angle));
glEnd();

, , sin cos , , .

+1

android, , , , . OpenGL ES 1.0 Android http://developer.android.com/resources/tutorials/opengl/opengl-es10.html, . , , . . , , , datenwolf. :

public class HelloOpenGLES10Renderer implements GLSurfaceView.Renderer {

    // the number small triangles used to make a circle
    public int segments = 100;
    public float mAngle;
    private FloatBuffer triangleVB;
            // array to hold the FloatBuffer for the small triangles
    private FloatBuffer [] segmentsArray = new FloatBuffer[segments];


    private void initShapes(){

                   .
                   .
                   .

        // stuff to draw holes in the board      
        int i = 0;
        float angle_start = 0.0f;
        float angle_stop = 2.0f * (float) java.lang.Math.PI;
        float angle_step = (angle_stop - angle_start)/segments;

        for(i=0; i<segments; i++) {
            float[] holeCoords;
            FloatBuffer holeVB;
            holeCoords = new float [ 9 ];
            // initialize vertex Buffer for triangle  
            // (# of coordinate values * 4 bytes per float)
            ByteBuffer vbb2 = ByteBuffer.allocateDirect(holeCoords.length * 4); 
            vbb2.order(ByteOrder.nativeOrder());// use the device hardware native byte order
            holeVB = vbb2.asFloatBuffer();  // create a floating point buffer from the ByteBuffer

            float x1 = 0.05f * (float) java.lang.Math.cos(angle_start + i*angle_step);
            float y1 = 0.05f * (float) java.lang.Math.sin(angle_start + i*angle_step);
            float z1 = 0.1f;
            float x2 = 0.05f * (float) java.lang.Math.cos(angle_start + i+1*angle_step);
            float y2 = 0.05f * (float) java.lang.Math.sin(angle_start + i+1*angle_step);
            float z2 = 0.1f;
            holeCoords[0] = 0.0f;
            holeCoords[1] = 0.0f;
            holeCoords[2] = 0.1f;
            holeCoords[3] = x1;
            holeCoords[4] = y1;
            holeCoords[5] = z1;
            holeCoords[6] = x2;
            holeCoords[7] = y2;
            holeCoords[8] = z2;
            holeVB.put(holeCoords);    // add the coordinates to the FloatBuffer
            holeVB.position(0);            // set the buffer to read the first coordinate
            segmentsArray[i] = holeVB;
        }            
    }

    .
    .
    .


public void onDrawFrame(GL10 gl) {

        .
        .
        .
        // Draw hole
        gl.glColor4f( 1.0f - 0.63671875f, 1.0f - 0.76953125f, 1.0f - 0.22265625f, 0.0f);
        for ( int i=0; i<segments; i++ ) {
            gl.glVertexPointer(3, GL10.GL_FLOAT, 0, segmentsArray[i]);
            gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
        }

}

Here's a screen shot in the emulator

0

All Articles