我正在使用 PNGDecoder 解码高度图 png。然而,当我打印控制台中返回的值时,我发现一些 rgb 值返回为负数
-128, -128, -128, -1
-128, -128, -128, -1
-128, -128, -128, -1
-124, -124, -124, -1
-119, -119, -119, -1
-118, -118, -118, -1
我用来解码然后读取的代码如下
public static ByteBuffer decodeImage(String path) throws IOException {
InputStream stream = new FileInputStream(path);
PNGDecoder decoder = new PNGDecoder(stream);
ByteBuffer decodedImageData = ByteBuffer.allocateDirect(4 * decoder.getWidth() * decoder.getHeight());
decoder.decodeFlipped(decodedImageData, decoder.getWidth() * 4, PNGDecoder.Format.RGBA);
decodedImageData.flip();
return decodedImageData;
}
private void applyHeightMap (String heightMapPath) throws IOException {
float vertices[] = plane.getVertices();
ByteBuffer buffer = Texture.decodeImage(heightMapPath);
for (int i = 0; i < 2624 * 1756; i++) {
byte r = buffer.get();
byte g = buffer.get();
byte b = buffer.get();
byte a = buffer.get();
if(r < 0 || b < 0 || g < 0) {
System.out.println(r + ", " + g + ", " + b+", "+a);
}
}
}
不确定为什么负值很少,以及为什么 alpha 通道读为 -1
我使用的图像是https://learnopengl.com/img/guest/2021/tessellation/height_map/iceland_heightmap.png