[英]Skybox rendering black while loading textures with LWJGL and PNGDecoder
我正在加载一个立方体贴图天空盒纹理(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);
}
}
加载程序中的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
以及width和height整数。 提供了Getters
剂,但没有其他功能。
编辑:我什至试图重构我所有的代码,以不占优势。
编辑:我认为错误在于立方体贴图,而不是纹理或其他代码,因为当我仅使用6个不同的四边形来渲染该框时,我就可以进行某种工作。
编辑:我解决了问题。 我忘了在渲染器中的类型着色器上调用方法.create()
。
CLOSED:我忘记添加方法调用shader.create(); 在SkyboxRenderer.init()函数中,因此没有链接任何着色器程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.