Picking objects in Android OpenGL ES using colors does not retrieve correct colors

允我心安 提交于 2019-12-23 04:49:24

问题


I am very new to OpenGL and I'm doing a very simple program to check if I can pick objects rendered on the screen, drawing each one with a unique color. So far, I render 2 flat squares and one triangle.

The problem is that when the user touches the polygons, I get a color different from black, but the color index is the same for the three objects (when it should be different).

Here is my code:

The onDrawFrame method (within my Renderer). Here is where I render the polygons with an unique color when the user is touching the screen.

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 method is the following (within my Renderer too):

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

On the other hand, I inherited GLSurfaceView so I can handle user touches. Within this class, I use this method:

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

It seems that it detects the squares and the triangle when I touch them, but I get the same color index for the three of them. Plus, the three objects have the same exact color (I even drew each one with a value of red, green and blue respectively and they had the same color, the color of the last one that was drawn).

Finally, this is the output I get when I touch an square, then an empty space, and another square:

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

I don't know why the red values are the same for each object.

Any help would be appreciated. Thanks.


回答1:


gl.glColor4f(red, green, blue, alpha); the type of the four parameters are float and the value are supposed to be 0 to 1, which match the common RGB range 0 to 255 so if you set the R value 128, 129 and 130, in fact , there is no difference between them. They are all 255 for common RGB range.




回答2:


Long long ago, in a galaxy far away...

Ok, this question is history, but if someone ever should have the same problem, it's worth checking whether DITHERING is turned off.

I had a similar issue once, and that was the issue for me.



来源:https://stackoverflow.com/questions/7488197/picking-objects-in-android-opengl-es-using-colors-does-not-retrieve-correct-colo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!