隐藏元素时glReadPixels失败

时间:2013-05-16 13:50:46

标签: java android opengl-es glreadpixels

当我触摸屏幕时,glReadpixels返回像素的RGB。触摸后我调用一个方法来隐藏具有相同RGB值的元素。在此之后,如果我触摸屏幕,glReadpixels只返回0值。

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;
}

1 个答案:

答案 0 :(得分: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;
}

可能有语法错误,我在答案框中写了(;

我希望它有所帮助,如果你需要澄清只是问。