数据格式如下:
final int width = 256;
final int height = 256;
final float[][][] data = new float[width][height][4];
FloatBuffer dataBuf;
int textureHandle;
FrameBuffer testFrame;
@Override
public void create () {
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
data[i][j][0] = 0.4f; /* r */
data[i][j][1] = 0.38f; /* g */
data[i][j][2] = 0.2f; /* b */
data[i][j][3] = 0.9f; /* a */
}
}
dataBuf = ByteBuffer.allocateDirect( Float.BYTES * 4 * width * height ).asFloatBuffer();
for (float[][] dat : data) { /* Float Byte size * RGBA * width * height */
for (float[] floats : dat) {
dataBuf.put(floats, 0, 4);
}
}
dataBuf.position(0); /* reset the caret position to the beginning of the array */
textureHandle = Gdx.gl.glGenTexture();
Gdx.gl.glActiveTexture(GL20.GL_TEXTURE1);
Gdx.gl.glBindTexture(GL30.GL_TEXTURE_2D, textureHandle);
Gdx.gl.glTexParameteri(GL30.GL_TEXTURE_2D, GL30.GL_TEXTURE_MIN_FILTER, GL30.GL_NEAREST);
Gdx.gl.glTexParameteri(GL30.GL_TEXTURE_2D, GL30.GL_TEXTURE_MAG_FILTER, GL30.GL_LINEAR);
Gdx.gl.glTexImage2D(
GL30.GL_TEXTURE_2D, 0, GL30.GL_RGBA32F,
width, height, 0, GL30.GL_RGBA, GL30.GL_FLOAT, dataBuf
);
}
着色器的行为正确,因为它们是使用帧缓冲区对象进行测试的,并且它们显示帧缓冲区的正确内容。
然而,当渲染生成的纹理时,它似乎偏离了原始的颜色/值。
在大多数情况下,由FloatBuffers
导致黑色纹理,有时会出现意想不到的颜色(例如柠檬绿而不是米色)。
不幸的是我不能玩glPixelStorei
很多,因为该接口缺少大部分参数。但无论如何glGetError
总是返回 0,所以我怀疑数据在某种程度上被错误地编译到了dataBuf
字节流。
这里可能有什么问题?
Edit:一些调试细节:
-
glGetError()
始终为零
- the individual components seem to have a rough idea of the data, but most of the values produce a black texture:
- r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f --> 黑屏
- r: 0.9f, g: 0.9f, b: 0.9f, a: 0.9f --> 白屏
- r: 0.9f, g: 0.0f, b: 0.0f, a: 0.9f --> 红屏
- r: 0.0f, g: 0.9f, b: 0.0f, a: 0.9f --> 绿屏
- r:0.0f,g:0.0f,b:0.9f,a:0.9f --> 蓝屏
- r: 0.5f, g: 0.5f, b: 0.5f, a: 0.5f --> 黑屏
- r: 0.4f, g: 0.32f, b: 0.2f, a: 0.9f --> 绿屏
- 上面我怀疑是因为浮点值之间存在转换错误
dataBuf
和 openGL-s GL_FLOAT 上传纹理时
- 否则,着色器和设置可以正常工作,因为它是使用帧缓冲区的颜色附件进行测试的,并且使用上述所有值都可以按预期工作。不同之处在于颜色纹理不是手动生成的,而是通过渲染到帧缓冲区中
glClear(GL_COLOR_BUFFER_BIT);
- 使用整数缓冲区也可以按预期工作(整数数组,值 0-255):
dataBuf.position(0);
Gdx.gl.glTexImage2D(
GL30.GL_TEXTURE_2D, 0, GL30.GL_RGBA,
width, height, 0, GL30.GL_RGBA, GL30.GL_UNSIGNED_INT, dataBuf
);