在Android的OpenGL ES的使用颜色拾取对象不检索正确的颜色颜色、对象、正确、Android

2023-09-07 11:48:38 作者:╭゜我的高傲,尔等岂懂

我很新的OpenGL和我在做一个非常简单的程序来检查,如果我可以选择在屏幕上呈现的对象,绘制每一个具有独特的色彩。到目前为止,我使2台广场和一个三角形。

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).

下面是我的code:

该onDrawFrame方法(我的渲染器内)。这里是我渲染的多边形与当用户触摸屏幕的独特的色彩。

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方法如下(我的渲染中也是如此):

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

在另一方面,我继承GLSurfaceView这样我就可以处理用户触摸。在这个班,我用这个方法:

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.

任何帮助将是AP preciated。谢谢你。

Any help would be appreciated. Thanks.

推荐答案

gl.glColor4f(红,绿,蓝,alpha);四个参数都浮类型和值应该是0到1,匹配普通的RGB范围为0〜255,因此,如果您设置的R值128,129和130,实际上,它们之间没有什么区别。他们都是255共同RGB范围。

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.