2013-05-16 33 views
0

當我觸摸屏幕glReadpixels返回像素的RGB。觸摸後,我調用具有相同RGB值的隱藏元素的方法。之後,如果我觸摸屏幕glReadpixels只返回0值。glReadPixels在隱藏元素時失敗

public void onDrawFrame(GL10 gl) { 
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 
    gl.glLoadIdentity();     //Reset The Current Modelview Matrix 

    gl.glPushMatrix(); 
    gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f); 
    gl.glTranslatef(0.0f, 1.2f, -6.0f); //Move down 1.0 Unit And Into The Screen 6.0 
    squareOne.draw(gl);      //Draw the square 
    gl.glPopMatrix(); 

    gl.glPushMatrix(); 
    gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f); 
    gl.glTranslatef(0.0f, -1.2f, -6.0f); //Move down 1.0 Unit And Into The Screen 6.0 
    squareTwo.draw(gl); 
    gl.glPopMatrix(); 

    //Picking colors Objects Code 
    ByteBuffer PixelBuffer = ByteBuffer.allocateDirect(4); 
    PixelBuffer.order(ByteOrder.nativeOrder()); 
    gl.glReadPixels((int) this.touchPointx, (int) this.touchPointy, 1, 1, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, PixelBuffer); 
    byte b[] = new byte[4]; 
    PixelBuffer.get(b); 

    String key = "" + b[0] + b[1] + b[2]; 
    Log.i(key,key); 

    if(b[0]==0 && b[1]==-1 && b[2]==0){ 
     this.squareOne.setHidden(true); 
    } 
    if(b[0]==0 && b[1]==0 && b[2]==-1){ 
     this.squareTwo.setHidden(true); 
    } 

} 

----------------------------- Square.java ----------- ----------------------------------

public void draw(GL10 gl) { 
    if(!isHidden()){ 
     //Set the face rotation 
     gl.glFrontFace(GL10.GL_CW); 

     //Point to our vertex buffer 
     gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer); 

     //Enable vertex buffer 
     gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 

     //Draw the vertices as triangle strip 
     gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length/3); 

     //Disable the client state before leaving 
     gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 
    } 
} 
public boolean isHidden() { 
    return hidden; 
} 

回答

0

我想你應該比較兩個數組。
因此,建立一個數組的顏色索引,以便與要檢查的像素進行比較。
然後將這些分配給廣場,我強烈建議寫一個方形課。
但它應該以任何方式工作。

我在想是這樣的:

public boolean compareColor(int[] color){ 
    int bpp = 4; 
    glReadBuffer(GL_FRONT); 
    ByteBuffer buffer = BufferUtils.createByteBuffer(WIDTH * HEIGHT * bpp); 

    glReadPixels(pixelX, pixelY, WIDTH, HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, buffer); 


    for(int x = 0; x < WIDTH; x++){ 
     for(int y = 0; y < HEIGHT; y++){ 
      int i = (x + (WIDTH * y)) * bpp; 
      int r = buffer.get(i) & 0xFF; 
      int g = buffer.get(i + 1) & 0xFF; 
      int b = buffer.get(i + 2) & 0xFF; 

      if(r == color[0]) 
       &&(g == color[1]) 
       &&(b == color[2]){ 

       return true; 
      } 
     } 
    } 
    return false; 
} 

可能有語法錯誤,我寫在答題框(;

我希望它能幫助,如果你需要clarrification只是問。