How to draw basic circle in OpenGL ES 2.0 Android

后端 未结 5 1278
时光取名叫无心
时光取名叫无心 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:29

    This is a modified version of the above answer. It also includes the code to color the circle as well. Most of the functions are used as OpenGL ES1 though. Mind the naming conventions of class toilet, LOL. If you need the code of other classes where I render OpenGL as well, let me know.

    import java.nio.ByteBuffer;
    import java.nio.ByteOrder;
    import java.nio.FloatBuffer;
    import javax.microedition.khronos.opengles.GL10;
    
    public class Toilet {
    
        // Circle variables
        int circlePoints = 30;
        float radius = 1.0f;
        float center_x = 0.0f;
        float center_y = 0.0f;
    
        // Outer vertices of the circle i.e. excluding the center_x, center_y
        int circumferencePoints = circlePoints-1;
    
        // Circle vertices and buffer variables
        int vertices = 0;
        float circleVertices[] = new float[circlePoints*2];
        private FloatBuffer toiletBuff; // 4 bytes per float
    
        // Color values
        private float rgbaValues[] = {
                   1,     1,  0,     .5f,
                   .25f,  0,  .85f,  1,
                   0,     1,  1,     1
        };
    
        private FloatBuffer colorBuff;
    
        public Toilet()
        {
            // The initial buffer values
            circleVertices[vertices++] = center_x;
            circleVertices[vertices++] = center_y;
    
            // Set circle vertices values
            for (int i = 0; i < circumferencePoints; i++)
            {
                float percent = (i / (float) (circumferencePoints - 1));
                float radians = (float) (percent * 2 * Math.PI);
    
                // Vertex position
                float outer_x = (float) (center_x + radius * Math.cos(radians));
                float outer_y = (float) (center_y + radius * Math.sin(radians));
    
                circleVertices[vertices++] = outer_x;
                circleVertices[vertices++] = outer_y;
            }
    
            // Float buffer short has four bytes
            ByteBuffer toiletByteBuff = ByteBuffer
                    .allocateDirect(circleVertices.length * 4);
    
            // Garbage collector won't throw this away
            toiletByteBuff.order(ByteOrder.nativeOrder());
            toiletBuff = toiletByteBuff.asFloatBuffer();
            toiletBuff.put(circleVertices);
            toiletBuff.position(0);
    
            // Float buffer short has four bytes
            ByteBuffer clrBuff = ByteBuffer.allocateDirect(rgbaValues.length * 4);
            // garbage collector wont throw this away
            clrBuff.order(ByteOrder.nativeOrder());
            colorBuff = clrBuff.asFloatBuffer();
            colorBuff.put(rgbaValues);
            colorBuff.position(0);
        }
    
        // Draw methods
        public void draw(GL10 gl) {
    
            // Get the front face
            gl.glFrontFace(GL10.GL_CW); // Front facing is clockwise
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    
            // Enable color array
            gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
    
            // Pointer to the buffer
            gl.glVertexPointer(2, GL10.GL_FLOAT, 0, toiletBuff);
    
            // Pointer to color
            gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuff);
    
            // Draw hollow circle
            //gl.glDrawArrays(GL10.GL_LINE_LOOP, 1, circumferencePoints);
    
            // Draw circle as filled shape
            gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, circlePoints);
    
            gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
            gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        }
    }
    

提交回复
热议问题