2016-08-22 103 views
2

我想繪製圖像中所示的多邊形並用顏色填充它。 Fill PolygonSimple polygon如何在android中使用opengl-es 2.0填充多邊形?

我試過使用三角形的多邊形,但它不會幫助我。任何人都知道請幫助我。

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但不能夠填補創建多邊形多邊形。

+0

請提供您嘗試過的代碼。 –

+0

這是你的結果,預期的結果是什麼? –

+0

你的問題是繪製非凸多邊形嗎?如果是這樣的話,請看這裏:http://stackoverflow.com/questions/25422846/how-to-force-opengl-to-draw-a-non-convex-filled-polygon。 –

回答

1

OpenGL ES 2.0支持僅繪製三角形作爲基本圖元。有3種方式使用三角形繪製多邊形的, 1)三角形 2)三角形條 3)三角形扇

在你的情況,你可以嘗試三角形扇繪製多邊形,只要你知道里面的計劃點。

Here是繪製圓的一個例子。