我在将唯一整数(索引号)转换为可由 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/