【问题标题】:OpenGL ES 2.0 Android - Color PickingOpenGL ES 2.0 Android - 颜色选择
【发布时间】:2014-07-27 20:24:25
【问题描述】:

我正在尝试在 android OpenGL ES 中使用 GLES20.glReadPixels 函数实现颜色选择。问题是这个函数总是返回 0,0,0,0 作为颜色而不是正确的颜色值。知道为什么吗?我的代码如下所示:

public boolean onTouchEvent(MotionEvent event)
    {
        if (event != null)
        {
            float x = event.getX();
            float y = event.getY();

            if (event.getAction() == MotionEvent.ACTION_UP)
            {
                int newX = (int)x;
                int newY = (int)y;


                ByteBuffer pixel = ByteBuffer.allocate(4);
                pixel.order(ByteOrder.nativeOrder());
                pixel.position(0);

                GLES20.glReadPixels(newX, (int)mRenderer.viewport[3] - newY, 1, 1,
                        GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, pixel);


             }

             return true;
        }
        else
        {
                return super.onTouchEvent(event);
        }

所以正如我之前所说...像素数组的结果总是0,0,0,0。不知道为什么:/我做错了什么?我使用灯塔教程作为参考: http://www.lighthouse3d.com/opengl/picking/index.php3?color2 在这一点上我真的看不到错误:/ 哦,我忘了告诉我我的场景包含一个完全是蓝色的 3D 立方体,所以当我点击它时结果应该是 0,0,1,0 但它不是:(

编辑: 绘制 Cube 的渲染器中的代码(它围绕其 y 轴旋转)

    public void onDrawFrame(GL10 unused) {
       float[] scratch = new float[16];
       GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);

       GLES20.glEnable(GLES20.GL_DEPTH_TEST);

       Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3.0f, 0f, -0.3f, 0.0f, 0.0f, 1.0f, 0.0f);


       Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);


       float[] mModelMatrix = new float[16];

       Matrix.setIdentityM(mModelMatrix, 0);
       Matrix.translateM(mModelMatrix, 0, 0, 0, -0.5f);

       Matrix.setIdentityM(mRotationMatrix, 0);
       Matrix.rotateM(mRotationMatrix, 0, mDeltaX, 0, 1.0f, 0);
       Matrix.rotateM(mRotationMatrix, 0, -mDeltaY, 1.0f, 0, 0);
       mDeltaX = 0.2f;
       mDeltaY = 0.2f;

       float[] mTempMatrix = new float[16];

       Matrix.multiplyMM(mTempMatrix, 0, mRotationMatrix, 0, mAccumulatedRotation, 0);
       System.arraycopy(mTempMatrix, 0, mAccumulatedRotation, 0, 16);

       float[] temp = new float[16];

       Matrix.multiplyMM(temp, 0, mModelMatrix, 0 , mAccumulatedRotation, 0);

       Matrix.multiplyMM(scratch, 0, mMVPMatrix, 0, temp, 0);

       Matrix.setIdentityM(mModelMatrix, 0);
       Matrix.translateM(mModelMatrix, 0, 0, 0, 0.5f);

       float[] temp2 = new float[16];
       Matrix.multiplyMM(temp2, 0, scratch, 0, mModelMatrix, 0);

       mCube.drawCube(temp2);      


}

【问题讨论】:

  • 您能否编辑您的问题以包括在选取前后发生的渲染循环的相关部分。
  • 我编辑了我的问题,但老实说,我认为问题出在触摸处理函数中......而其他地方都没有:/
  • 对,我觉得渲染代码不重要。

标签: android opengl-es color-picker glreadpixels


【解决方案1】:

与所有其他 OpenGL 调用一样,glReadPixels() 仅在存在当前 OpenGL 上下文时才有效。

在 Android 中,OpenGL 渲染主要使用 GLSurfaceView 完成,它负责生成用于渲染的辅助线程,创建 OpenGL 上下文,并在调用 @ 中的方法时使该上下文在辅助渲染线程中处于当前状态987654323@实现。

onTouchEvent() 在 UI 线程中调用,因此您不会在此处拥有当前的 OpenGL 上下文。要使用glReadPixels(),您可以使用GLSurfaceView.queueEvent() 方法将请求转发到您的渲染线程,然后在下次调用Renderer.onDraw() 方法时对其进行异步处理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多