2011-09-20 48 views
0

我對OpenGL非常陌生,我正在做一個非常簡單的程序來檢查我是否可以選擇在屏幕上呈現的對象,使用獨特的顏色繪製每個對象。到目前爲止,我呈現了2個平方和一個三角形。使用顏色在Android OpenGL ES中採摘對象無法檢索正確的顏色

問題是,當用戶觸摸多邊形時,我得到的顏色與黑色不同,但三個對象的顏色索引相同(當它應該不同時)。

這是我的代碼:

的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 

我不知道爲什麼紅色值對每個對象相同。

任何幫助,將不勝感激。 謝謝。

回答

1

gl.glColor4f(紅色,綠色,藍色,alpha); 四個參數的類型是浮動的,值應該是0到1,它們與常見的RGB範圍0到255相匹配 所以如果你設置了R值128,129和130,實際上沒有差別它們之間。對於常見的RGB範圍,它們都是255。

0

很久很久以前,在遙遠的銀河系......

好吧,這個問題是歷史,但是如果有人曾經應該有同樣的問題,這是值得一試的抖動是否關閉。

我曾經有過類似的問題,這是我的問題。