How to draw basic circle in OpenGL ES 2.0 Android

后端 未结 5 1274
时光取名叫无心
时光取名叫无心 2020-12-05 05:36

I\'m new in OpenGL ES 2, and I have read many topics about how to draw a circle in OpenGL ES 2 on Android. Based on Drawing Shapes and this code found on gamedev.net, I can

5条回答
  •  盖世英雄少女心
    2020-12-05 06:17

    If you want to create geometry for the circle, do something like this:

    int vertexCount = 30;
    float radius = 1.0f;
    float center_x = 0.0f;
    float center_y = 0.0f;
    
    // Create a buffer for vertex data
    float buffer[] = new float[vertexCount*2]; // (x,y) for each vertex
    int idx = 0;
    
    // Center vertex for triangle fan
    buffer[idx++] = center_x;
    buffer[idx++] = center_y;
    
    // Outer vertices of the circle
    int outerVertexCount = vertexCount-1;
    
    for (int i = 0; i < outerVertexCount; ++i){
        float percent = (i / (float) (outerVertexCount-1));
        float rad = percent * 2*Math.PI;
    
        //Vertex position
        float outer_x = center_x + radius * cos(rad);
        float outer_y = center_y + radius * sin(rad);
    
        buffer[idx++] = outer_x;
        buffer[idx++] = outer_y;
    }
    
    //Create VBO from buffer with glBufferData()
    

    Then you can draw using glDrawArrays() either as:

    • GL_LINE_LOOP(contour only) or
    • GL_TRIANGLE_FAN(filled shape)

    .

    // Draw circle contours (skip center vertex at start of the buffer)
    glDrawArrays(GL_LINE_LOOP, 2, outerVertexCount);
    
    // Draw circle as a filled shape
    glDrawArrays(GL_TRIANGLE_FAN, 0, vertexCount);
    

提交回复
热议问题