glReadPixels不会丢失精度(Android)

时间:2015-07-04 14:46:40

标签: android opengl-es glreadpixels

我很难将独特的整数(索引号)转换为可由RGB565 OpenGL曲面解释的独特浮点颜色。当我指定唯一的颜色时,由于精度的损失,它们被绘制为稍微不同的值,所以当我用glReadPixels读取颜色并尝试将其转换回浮动进行比较时,它们并不相等。

我在这里发布了一个类似的问题OpenGL ES 2.0 solid colour & colour value precision issue但未能实现我给出的答案,有人可以给我详细说明(代码和解释)吗?

1 个答案:

答案 0 :(得分:1)

如果您只需要605个唯一值,那么10位精度(最多1024个值)就足够了。

RGB565具有16位精度,因此您可以使用6位额外的精度作为纠错形式,将值间隔开来,这样如果通过舍入或抖动或其他方式对值进行小幅调整,您可以将其设置为最接近的有效值。

因此,将10位中的3位分配给R,将4位分配给G,将3分配给B.

例如,红色和蓝色的范围为0-31,但您只需要8个可能的值(3位),因此您只存储值2,6,10,14,18,22,26,30。当扩展到8位时,这些值将是16,48,80,112,144,176,208,240。然后,当您重建索引时,0-31范围内的任何值都被解释为0,32 -63是1,64-95是2,依此类推(这可以通过简单的位移来完成)。这样,+ / - 少量的小错误就无所谓了。

void assignID(int regionnumber)
{
    int UBR=31; //Upper boundary for blue and red
    int UG=63; //Upper boundary for green

    // split regionnumber into 3/4/3 bits:
    int R = (regionnumber >> 7) & 7;
    int G = (regionnumber >> 3) & 15;
    int B = regionnumber & 7;

    // space out the values by multiplying by 4 and adding 2:
    R = R * 4 + 2;
    G = G * 4 + 2;
    B = B * 4 + 2;

    // combine into an RGB565 value if you need it:
    int RGB565 = (R << 11) | (G << 5) | B;

    // assign the colors
    regions[regionnumber].mColorID[0] = ((float)R)/UBR;
    regions[regionnumber].mColorID[1] = ((float)G)/UG; // careful, there was a bug here
    regions[regionnumber].mColorID[2] = ((float)B)/UBR;
}

然后在另一端,当您从屏幕读取值时,将RGB值转换回每个3,4和3位的整数并重建该区域:

int R = (b[0] & 0xFF) >> 5;
int G = (b[1] & 0xFF) >> 4;
int B = (b[2] & 0xFF) >> 5;

int regionnumber = (R << 7) | (G << 3) | B;
相关问题