使用颜色在Android OpenGL ES中拾取对象无法检索正确的颜色

时间:2011-09-20 16:02:41

标签: android opengl-es ontouchevent

我是OpenGL的新手,我正在做一个非常简单的程序来检查我是否可以选择在屏幕上呈现的对象,用独特的颜色绘制每个对象。到目前为止,我渲染了2个平面正方形和一个三角形。

问题在于,当用户触摸多边形时,我得到的颜色与黑色不同,但三个对象的颜色索引相同(当它应该不同时)。

这是我的代码:

onDrawFrame方法(在我的渲染器中)。这是我在用户触摸屏幕时使用唯一颜色渲染多边形的地方。

public void onDrawFrame(GL10 gl) {

    // clear Screen and Depth Buffer
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    // Reset the Modelview Matrix
    gl.glLoadIdentity();

    if (mGLView.isPickingEnabled()) {
         gl.glDisable(GL10.GL_TEXTURE_2D);
         gl.glDisable(GL10.GL_LIGHTING);
         gl.glDisable(GL10.GL_FOG);

         // draw colored objects  and picking pixel color
         pickingDraw(gl);

         mGLView.mPickingY = (int)(mHeight - mGLView.mPickingY);

         gl.glReadPixels((int) mGLView.mPickingX, 
                 (int) mGLView.mPickingY, 1, 1, 
                 GL10.GL_RGBA, 
                 GL10.GL_UNSIGNED_BYTE, 
                 mGLView.m_PickingPixelBuffer);

         Log.i("RA", "RGBA COLORS: " +
                 (int) mGLView.m_PickingPixelBuffer.get(0) + " , " +
                 (int) mGLView.m_PickingPixelBuffer.get(1) + " , " +
                 (int) mGLView.m_PickingPixelBuffer.get(2) + " , " +
                 (int) mGLView.m_PickingPixelBuffer.get(3) );

         mGLView.disablePicking();

         // Clear BACK BUFFER
         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    }

    gl.glLoadIdentity();

    // Drawing
    gl.glTranslatef(0.0f, 0.0f, -25.0f);    // move 5 units INTO the screen
                                            // is the same as moving the camera 5 units away
    square.draw(gl);                        // Draw the triangle

    gl.glTranslatef(0.0f, -5.0f, 0.0f);
    square.draw(gl);

    gl.glTranslatef(4.0f, 10.0f, 0.0f);
    triangle.draw(gl);
}

pickingDraw方法如下(在我的渲染器中):

private void pickingDraw(GL10 gl) {
    // Drawing
    gl.glTranslatef(0.0f, 0.0f, -25.0f);    // move 5 units INTO the screen
    gl.glColor4f(128, 0, 0, 1);
    square.draw(gl);                        // Draw the triangle

    gl.glTranslatef(0.0f, -5.0f, 0.0f);
    gl.glColor4f(129, 0, 0, 1);
    square.draw(gl);

    gl.glTranslatef(4.0f, 10.0f, 0.0f);
    gl.glColor4f(130, 0, 0, 1);
    triangle.draw(gl);
}

另一方面,我继承了GLSurfaceView,因此我可以处理用户触摸。在这个课程中,我使用这个方法:

public boolean onTouchEvent(MotionEvent e) {

    float _iX = e.getX();
    float _iY = e.getY();

    int iIndex = -1;
    int iRed;

    m_PickingPixelBuffer = ByteBuffer.allocateDirect(4);
    m_PickingPixelBuffer.order(ByteOrder.nativeOrder());

    // setting globals for Renderer
    mPickingX = _iX;
    mPickingY = _iY;

    mPickingEnabled = true;
    requestRender();

    // wait for picking process (renderer)
    while(mPickingEnabled);

    iRed = m_PickingPixelBuffer.get(0);
    if(iRed < 0)
    {
       iRed += 256;
    }

    Log.i("RA", "Coordinates: (" + mPickingX + ", " + mPickingY + ")");
    Log.i("RA", "Red Color " + iRed);

    return true;
}

当我触摸时,它似乎检测到正方形和三角形,但我得到了三个相同的颜色索引。另外,这三个对象具有相同的颜色(我甚至分别绘制了红色,绿色和蓝色的值,并且它们具有相同的颜色,即绘制的最后一个颜色)。

最后,这是我触摸正方形,然后是空白空间和另一个正方形时得到的输出:

01-19 01:34:23.345: INFO/RA(11566): RGBA COLORS: -1 , 0 , 0 , -1
01-19 01:34:23.355: INFO/RA(11566): Coordinates: (247.81186, 391.0)
01-19 01:34:23.355: INFO/RA(11566): Red Color 255
01-19 01:34:23.385: INFO/RA(11566): RGBA COLORS: -1 , 0 , 0 , -1
01-19 01:34:23.385: INFO/RA(11566): Coordinates: (248.52428, 390.0)
01-19 01:34:23.385: INFO/RA(11566): Red Color 255
01-19 01:34:23.405: INFO/RA(11566): RGBA COLORS: -1 , 0 , 0 , -1
01-19 01:34:23.405: INFO/RA(11566): Coordinates: (248.52428, 390.0)
01-19 01:34:23.405: INFO/RA(11566): Red Color 255
01-19 01:34:24.305: INFO/RA(11566): RGBA COLORS: 0 , 0 , 0 , -1
01-19 01:34:24.315: INFO/RA(11566): Coordinates: (119.571884, 577.0)
01-19 01:34:24.315: INFO/RA(11566): Red Color 0
01-19 01:34:24.335: INFO/RA(11566): RGBA COLORS: 0 , 0 , 0 , -1
01-19 01:34:24.335: INFO/RA(11566): Coordinates: (119.33441, 573.0)
01-19 01:34:24.335: INFO/RA(11566): Red Color 0
01-19 01:34:24.355: INFO/RA(11566): RGBA COLORS: 0 , 0 , 0 , -1
01-19 01:34:24.355: INFO/RA(11566): Coordinates: (119.33441, 573.0)
01-19 01:34:24.355: INFO/RA(11566): Red Color 0
01-19 01:34:25.195: INFO/RA(11566): RGBA COLORS: -1 , 0 , 0 , -1
01-19 01:34:25.195: INFO/RA(11566): Coordinates: (259.80466, 395.0)
01-19 01:34:25.195: INFO/RA(11566): Red Color 255
01-19 01:34:25.215: INFO/RA(11566): RGBA COLORS: -1 , 0 , 0 , -1
01-19 01:34:25.215: INFO/RA(11566): Coordinates: (259.44843, 398.0)
01-19 01:34:25.215: INFO/RA(11566): Red Color 255
01-19 01:34:25.245: INFO/RA(11566): RGBA COLORS: -1 , 0 , 0 , -1
01-19 01:34:25.245: INFO/RA(11566): Coordinates: (259.80466, 395.0)
01-19 01:34:25.245: INFO/RA(11566): Red Color 255

我不知道为什么每个对象的红色值都相同。

任何帮助将不胜感激。 感谢。

2 个答案:

答案 0 :(得分:1)

gl.glColor4f(红色,绿色,蓝色,alpha); 四个参数的类型是float,值应该是0到1,它匹配常见的RGB范围0到255 因此,如果将R值设置为128,129和130,实际上它们之间没有区别。对于常见的RGB范围,它们都是255。

答案 1 :(得分:0)

很久很久以前,在远处的一个星系......

好的,这个问题是历史问题,但如果有人遇到同样的问题,那么检查DITHERING是否关闭是值得的。

我曾经遇到过类似的问题,这对我来说是个问题。