android - 不损失精度的 glReadPixels (Android)

标签 android opengl-es glreadpixels

我在将唯一整数(索引号)转换为可由 RGB565 OpenGL 表面解释的唯一浮点颜色时遇到了很多麻烦。当我分配独特的颜色时,由于精度损失,它们通常被绘制为略有不同的值,因此当我使用 glReadPixels 读取颜色并尝试将其转换回 float 以进行比较时,它们并不相等。

我在这里发布了一个类似的问题 OpenGL ES 2.0 solid colour & colour value precision issue但未能实现我得到的答案,任何人都可以为此提供具体信息(代码和解释)吗?

最佳答案

如果您只需要 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;

关于android - 不损失精度的 glReadPixels (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31221909/

相关文章:

android - 通过 list 注册的广播接收器是否在应用程序关闭时自动注销?

OpenGL 错误处理最佳实践

ios - 如何在OpenGL中多次添加光?

Android,各向异性过滤?

opengl - 使用 glReadPixels 进行屏幕捕获会导致某些窗口分辨率中的像素发生偏移

ios - 使用深度缓冲区和抗锯齿技术时通过 glreadpixel() 读取数据时出现问题

java - 在 display() JOGL 中调用 glReadPixels

android - 在焦点上更改 EditText 的图标颜色

java - 无法停止 retrofit 发送

android - 在 Android 按钮中设置可绘制的自定义宽度高度图像