如何使用 opengl-es 2.0 在 android 中填充多边形?

How to fill polygon in android using opengl-es 2.0?

我想绘制如图所示的多边形并用颜色填充它。

我试过用三角形来做多边形,但没有用me.Anyone知道的请帮帮我。

OpenGLProjectRenderer.java

public class OpenGLProjectRenderer implements Renderer {

List<Float> points = new ArrayList<Float>();

private static final String TAG = "Renderer";
private static final int POSITION_COMPONENT_COUNT = 2;
private static final int BYTES_PER_FLOAT = 4;
private FloatBuffer vertexData = ByteBuffer
        .allocateDirect(20000 * BYTES_PER_FLOAT)
        .order(ByteOrder.nativeOrder()).asFloatBuffer();
private Context context;
private int program;

private static final String A_POSITION = "a_Position";
private int aPositionLocation;

private static final String U_COLOR = "u_Color";
private int uColorLocation;

private HashMap<Integer, ArrayList<Float>> lines = new HashMap<Integer, ArrayList<Float>>();
int position = 0;

public OpenGLProjectRenderer(Context context) {

    this.context = context;
}

@Override
public void onDrawFrame(GL10 gl) {
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glUniform4f(uColorLocation, 1.0f, 0.0f, 0.0f, 1.0f);

    for (int p = 0; p < lines.size(); p++) {

        vertexData.put(toFloatarray(lines.get(p)));
        int vertices = (int) lines.get(p).size() / 2;
        int b = vertices % 4 == 0 ? vertices-1 : vertices - 2;
        Log.d(TAG,""+lines.size());
        glDrawArrays(GLES20.GL_LINE_LOOP, 0, lines.size());
        vertexData.clear();
    }

}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    gl.glViewport(0, 0, width, height);

}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    String vertexShaderSource = TextResourceReader.readTextFileFromResource(
            context, R.raw.simple_vertex_shader);
    String fragmentShaderSource = TextResourceReader.readTextFileFromResource(
            context, R.raw.simple_fragment_shader);

    int vertexShader = ShaderHelper.compileVertexShader(vertexShaderSource);
    int fragmentShader = ShaderHelper
            .compileFragmentShader(fragmentShaderSource);
    program = ShaderHelper.linkProgram(vertexShader, fragmentShader);
    ShaderHelper.validateProgram(program);
    glUseProgram(program);
    uColorLocation = glGetUniformLocation(program, U_COLOR);
    aPositionLocation = glGetAttribLocation(program, A_POSITION);
    vertexData.position(0);
    glVertexAttribPointer(aPositionLocation, POSITION_COMPONENT_COUNT,
            GL_FLOAT, false, 0, vertexData);
    glEnableVertexAttribArray(aPositionLocation);
}

ArrayList<Float> temp = new ArrayList<Float>();

public void handleTouchPress(float normalizedX, float normalizedY) {
    Log.v(TAG + " handleTouchPress", points.size() + "");       

    temp.add(normalizedX);
    temp.add(normalizedY);

    lines.put(position, temp);
}

public void handleTouchDrag(float normalizedX, float normalizedY) {

    Log.v(TAG + " handleTouchDrag", points.size() + "");
}

public float[] toFloatarray(List<Float> floatList) {

    float[] floatArray = new float[floatList.size()];
    int i = 0;

    for (Float f : floatList) {
        floatArray[i++] = (f != null ? f : Float.NaN);
    }

    return floatArray;
}

public void handleTouchUp(float normalizedX, float normalizedY) {
    Log.v(TAG + " handleTouchUp", points.size() + "");

    position++;

}}

使用上面的代码,我可以使用上面的代码使用 GL_LINE_LOOP 绘制多边形,但无法填充创建的多边形。

OpenGL ES 2.0 仅支持绘制三角形作为基本图元。使用三角形绘制多边形有 3 种方法, 1)三角形 2) 三角带 3) 三角扇

在您的情况下,如果您知道平面内的点,则可以尝试使用三角形扇形绘制多边形。

Here是画圆的例子