OpenGL ES 2.0的Andr​​oid的 - 颜色选择 [英] OpenGL ES 2.0 Android - Color Picking

查看:275
本文介绍了OpenGL ES 2.0的Andr​​oid的 - 颜色选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现的颜色使用GLES20.glReadPixels功能的Andr​​oid OpenGL ES的采摘。问题是,该功能总是返回0,0,0,0颜色和不正确的颜色的值。知道为什么吗?我的code是这样的:

 公共布尔onTouchEvent(MotionEvent事件)
    {
        如果(事件!= NULL)
        {
            浮X = event.getX();
            浮Y = event.getY();            如果(event.getAction()== MotionEvent.ACTION_UP)
            {
                INT下一页末=(int)的X;
                INT newy指定=(INT)Y;
                ByteBuffer的像素= ByteBuffer.allocate(4);
                pixel.order(ByteOrder.nativeOrder());
                pixel.position(0);                GLES20.glReadPixels(下一页末,(int)的mRenderer.viewport [3] - newy指定,1,1,
                        GLES20.GL_RGBA,GLES20.GL_UNSIGNED_BYTE,像素);
             }             返回true;
        }
        其他
        {
                返回super.onTouchEvent(事件);
        }

因此​​,正如我之前说的...像素阵列的结果始终是0,0,0,0。不知道为什么:/我在做什么错了?我用的灯塔教程作为参考:
http://www.lighthouse3d.com/opengl/picking/index.php3?color2
我实在看不出在这一点上的错误:/
哦,我忘了告诉我的场景中包含一个3D立方体,这是完全BLUE所以结果应该像0,0,1,0当我点击它,但它不是:(

编辑:
从哪里立方绘制渲染器的code(它旋转角落找寻其y轴)

 公共无效onDrawFrame(GL10未使用){
       浮动[] =从无到有新的浮动[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.0,0.0,1.0F,0.0);
       Matrix.multiplyMM(mMVPMatrix,0,mProjectionMatrix,0,mViewMatrix,0);
       浮动[] = mModelMatrix新的浮动[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;       浮动[] = mTempMatrix新的浮动[16];       Matrix.multiplyMM(mTempMatrix,0,mRotationMatrix,0,mAccumulatedRotation,0);
       System.arraycopy(mTempMatrix,0,mAccumulatedRotation,0,16);       浮动[] TEMP =新的浮动[16];       Matrix.multiplyMM(温度,0,mModelMatrix,0,mAccumulatedRotation,0);       Matrix.multiplyMM(划痕,0,mMVPMatrix,0,温度,0);       Matrix.setIdentityM(mModelMatrix,0);
       Matrix.translateM(mModelMatrix,0,0,0,0.5F);       浮动[] = TEMP2新的浮动[16];
       Matrix.multiplyMM(TEMP2,0,划伤,0,mModelMatrix,0);       mCube.drawCube(TEMP2);
}


解决方案

像所有其他的OpenGL调用, glReadPixels()只能当有电流通过OpenGL上下文。

在Android中,OpenGL渲染是使用大多做了 GLSurfaceView ,这需要产卵渲染辅助线程,创建一个OpenGL上下文,并使得在这方面目前的护理二级渲染线程调用时在你的 GLSurfaceView.Renderer 实施方法。

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

I'm trying to implement color picking using GLES20.glReadPixels function in android OpenGL ES. The problem is that this function is always returning 0,0,0,0 as color and not the correct color values. Any idea why? My code looks like this:

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

So as I said before... the result of the pixel array is always 0,0,0,0. Dont know why :/ What am I doing wrong? I was using the lighthouse tutorial as a reference: http://www.lighthouse3d.com/opengl/picking/index.php3?color2 And I really can't see the mistake at this point :/ Oh I forgot to tell that my scene contains a 3D cube which is fully BLUE so the result should be something like 0,0,1,0 when I click on it but it isn't :(

EDIT: The code from the Renderer where the Cube is drawn (it rotates arround its y-axis)

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


}

解决方案

Like all other OpenGL calls, glReadPixels() only works if there is a current OpenGL context.

In Android, OpenGL rendering is mostly done using a GLSurfaceView, which takes care of spawning a secondary thread for rendering, creating an OpenGL context, and making that context current in the secondary rendering thread while invoking the methods in your GLSurfaceView.Renderer implementation.

onTouchEvent() is invoked in the UI thread, so you won't have a current OpenGL context here. To use glReadPixels(), you can forward the request to your rendering thread using the GLSurfaceView.queueEvent() method, and then process it asynchronously the next time your Renderer.onDraw() method is invoked.

这篇关于OpenGL ES 2.0的Andr​​oid的 - 颜色选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆