【发布时间】:2019-01-28 21:43:36
【问题描述】:
我正在加载一个立方体贴图天空盒纹理(3D 坐标而不是 2D),结果天空盒是黑色的。我没有收到任何错误,天空盒在正确的位置渲染,但还是有问题。
我有一个自定义类 CubeMap,它只接受 String[] 作为纹理文件名并使用以下代码加载它们:
public class CubeMap {
private int cubeMapID;
public CubeMap (String[] textureFiles) {
this.cubeMapID = GL11.glGenTextures();
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL13.GL_TEXTURE_CUBE_MAP, cubeMapID);
for (int i = 0; i < textureFiles.length; i++) {
Image data = Loader.decodeTextureFile("res/textures/" + textureFiles[i]);
GL11.glTexImage2D(GL13.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL11.GL_RGBA, data.getWidth(), data.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, data.getByteBuffer());
}
GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
}
public int getCubeMapID() {
return cubeMapID;
}
public void destroy () {
GL11.glDeleteTextures(cubeMapID);
}
}
Loader 中的decodeTextureFile() 方法:
public static Image decodeTextureFile (String fileName) {
int width = 0, height = 0;
ByteBuffer buffer = null;
try {
FileInputStream in = new FileInputStream(fileName);
PNGDecoder decoder = new PNGDecoder(in);
width = decoder.getWidth();
height = decoder.getHeight();
buffer = ByteBuffer.allocateDirect(4 * width * height);
decoder.decode(buffer, width * 4, Format.RGBA);
buffer.flip();
in.close();
} catch (IOException e) {
System.out.println("Error : Decoding in loader failed; IO - Error");
System.exit(-1);
}
return new Image (width, height, buffer);
}
此外,我有一个 SkyBox 类,它创建一个 CubeMap 并将预设数组的顶点存储到一个 vertexArray 和一个 vertexBuffer 中(这部分代码工作正常,所以我看不到需要添加Model代码)
public class SkyBox extends Model {
private static final float SIZE = 500f;
private static final float[] VERTICES = {
-SIZE, SIZE, -SIZE,
-SIZE, -SIZE, -SIZE,
SIZE, -SIZE, -SIZE,
SIZE, -SIZE, -SIZE,
SIZE, SIZE, -SIZE,
-SIZE, SIZE, -SIZE,
-SIZE, -SIZE, SIZE,
-SIZE, -SIZE, -SIZE,
-SIZE, SIZE, -SIZE,
-SIZE, SIZE, -SIZE,
-SIZE, SIZE, SIZE,
-SIZE, -SIZE, SIZE,
SIZE, -SIZE, -SIZE,
SIZE, -SIZE, SIZE,
SIZE, SIZE, SIZE,
SIZE, SIZE, SIZE,
SIZE, SIZE, -SIZE,
SIZE, -SIZE, -SIZE,
-SIZE, -SIZE, SIZE,
-SIZE, SIZE, SIZE,
SIZE, SIZE, SIZE,
SIZE, SIZE, SIZE,
SIZE, -SIZE, SIZE,
-SIZE, -SIZE, SIZE,
-SIZE, SIZE, -SIZE,
SIZE, SIZE, -SIZE,
SIZE, SIZE, SIZE,
SIZE, SIZE, SIZE,
-SIZE, SIZE, SIZE,
-SIZE, SIZE, -SIZE,
-SIZE, -SIZE, -SIZE,
-SIZE, -SIZE, SIZE,
SIZE, -SIZE, -SIZE,
SIZE, -SIZE, -SIZE,
-SIZE, -SIZE, SIZE,
SIZE, -SIZE, SIZE
};
private CubeMap cubeMap;
private int vertexArrayID;
private int vertexCount;
public SkyBox (String[] textures) {
cubeMap = new CubeMap(textures);
vertexArrayID = super.createVertexArray();
super.storeData(0, 3, VERTICES);
vertexCount = VERTICES.length / 3;
GL30.glBindVertexArray(0);
}
public CubeMap getCubeMap() {
return cubeMap;
}
public int getVertexArrayID() {
return vertexArrayID;
}
public int getVertexCount() {
return vertexCount;
}
public void destroy () {
cubeMap.destroy();
}
}
我有一个SkyboxShader 类,我相当确定它可以正常工作。 SkyBox 的实际渲染发生在 SkyboxRenderer 类中:
public class SkyboxRenderer {
private SkyboxShader shader;
public SkyboxRenderer (SkyboxShader shader) {
this.shader = shader;
}
public void render (SkyBox skyBox) {
shader.bind();
shader.useMatrices();
GL30.glBindVertexArray(skyBox.getVertexArrayID());
GL20.glEnableVertexAttribArray(0);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL13.GL_TEXTURE_CUBE_MAP, skyBox.getCubeMap().getCubeMapID());
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, skyBox.getVertexCount());
GL20.glDisableVertexAttribArray(0);
GL30.glBindVertexArray(0);
shader.unbind();
}
}
如果有人知道如何解决此问题,将不胜感激。如果需要,将发布任何其他代码。提前致谢!
编辑
编辑:Image 类是自定义的,只存储 ByteBuffer 和宽度和高度整数。提供了Getters,但没有其他功能。
编辑:我什至尝试重构我的所有代码,但没有成功。
编辑:我认为错误在于立方体贴图,而不是纹理或其他代码,因为当我只使用 6 个不同的四边形来渲染盒子时,我可以让它工作。
编辑:我解决了这个问题。我忘了在渲染器的类型着色器上调用方法.create()。
【问题讨论】: