简体   繁体   English

OpenGL ES glReadPixels返回错误的值

[英]OpenGL ES glReadPixels returns wrong values

First of all first time here so hello everyone. 首先第一次来这里,大家好。 After searching the net for days including this site I failed to overcome this problem: 在网上搜寻了包括该站点在内的几天后,我未能克服此问题:

public void onDrawFrame(GL10 gl) {

gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity(); //load identity
        GLU.gluLookAt(gl, 0, -5, -25, 0, 0, 0, 0, 2, 0); //set camera

        if (fingerInput.isClicking()){
            /* Color Picking 4 START */
            gl.glDisable(GL10.GL_TEXTURE_2D); //turn off texturing, lighting and fog
            gl.glDisable(GL10.GL_FOG);
            gl.glDisable(GL10.GL_LIGHTING);

            while (i<squares.size()){ //draw picking colors
                squares.get(i).pickingDraw(gl); //note: picking is the same as draw() only with id colors and not textures
                i++;
            }
            i=0;

            gl.glReadPixels(fingerInput.getStart().x, screen_height-fingerInput.getStart().y, 1, 1, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, pixels); //read what was the color id pressed, store it in 'pixels' (a 4 slots array buffer)

            Log.d("tlog","at coords: ("+(screen_height-fingerInput.getStart().x)+", "+(screen_height-fingerInput.getStart().y)+")");

            for (j=0; j<4; j++){
                RGBA[j] = (int)(pixels.get(j) & 0xff);
                if (RGBA[j] < 0) RGBA[j]+=256; //correcting error caused by java using unsigned bytes and opengl singed bytes 
            }

anyway, for picking purposes, the squares are drawn each with a unique color,(currently drawing 3 squares with colors 99,96 and 93 red, and 0s at blue green alpha) glReadPixels returns on clicking either (99,0,0) or (91,0,0). 无论如何,出于拾取目的,每个正方形都以唯一的颜色绘制(当前绘制3个正方形,其颜色分别为99,96和93红色,蓝绿色alpha为0)glReadPixels单击(99,0,0)或返回(91,0,0)。

if the box is colored (x,0,0,255) it returns a value as if it had a list of possible values with spaces of 8 between them. 如果该框是彩色的(x,0,0,255),它将返回一个值,就好像它具有一个可能的值列表,它们之间有8个空格。 (91,99,107..) sort of "rounding" each read color value to nearest "possible" value. (91,99,107 ..)将每种读取的颜色值“舍入”到最接近的“可能”值。

I came across a similar problem a couple of months ago, but I was using the Bitmap class with the getPixel method to read the colors of the individual pixels. 几个月前,我遇到了一个类似的问题,但是我将Bitmap类与getPixel方法一起使用来读取各个像素的颜色。 As a test I used a black image, completely black so all the pixels should have the value 0 , but the getPixel method was returning the values 0,6,0,6,0,6...etc. 作为测试,我使用了全黑的黑色图像,因此所有像素都应具有值0 ,但是getPixel方法返回的值为0,6,0,6,0,6 ... etc。

If you take a look at this link on the google code page of the Android SDK, someone else has got the same issue as me: Bitmap.getPixel error Unfortunately not fixed yet. 如果您在Android SDK的Google代码页上查看此链接,则其他人会遇到与我相同的问题: Bitmap.getPixel错误不幸的是尚未修复。

I am not sure if it has anything to do with OpenGL on the Android, but based on my experience, later SDK versions sometimes tend to mess up the color values somehow. 我不确定它是否与Android上的OpenGL有关,但是根据我的经验,以后的SDK版本有时会以某种方式弄乱颜色值。 The only thing that worked that day was using a lower SDK version. 那天唯一有效的方法是使用较低的SDK版本。

Try your code on 1.4 or 1.5, if the problem disappears then wait for the fix on the later SDK. 在1.4或1.5上尝试您的代码,如果问题消失了,请等待更高版本的SDK上的修复。 If it persists then it is another issue. 如果问题仍然存在,那就是另一个问题。

PS: If you find a solution on later SDKs for this, please let me know. PS:如果您在以后的SDK中找到解决方案,请告诉我。

As i stated in here: 正如我在这里所说的:

Java has no signed bytes and this implementation calls for a "GL10.GL_UNSIGNED_BYTE" answer from glReadPixels. Java没有带符号的字节,此实现要求glReadPixels提供“ GL10.GL_UNSIGNED_BYTE”答案。 the conversion hurts the data returned. 转换会损害返回的数据。 (eg a 95 red would be returned as 91 or 99). (例如95红色将返回为91或99)。

Since Java is incapable of dealing with this conversion issue there are two solutions i see for the problem: 1) Making a conversion map between each two possible values (for one color) and a new absolute value. 由于Java无法处理此转换问题,因此我为该问题提供了两种解决方案:1)在每两个可能值(对于一种颜色)和新的绝对值之间建立转换映射。

2) Using NDK would probably give the precise color value immediately. 2)使用NDK可能会立即给出精确的颜色值。 Bear in mind i haven't tested it and deciding on using NDK with OpenGL doesn't always give better performance than Java. 请记住,我还没有对其进行测试,因此决定将NDK与OpenGL配合使用时,并不总是比Java提供更好的性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM