【发布时间】: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