简体   繁体   English

Skybox在使用LWJGL和PNGDecoder加载纹理时呈现黑色

[英]Skybox rendering black while loading textures with LWJGL and PNGDecoder

I'm loading a cube map skybox texture (3D coordinates rather than 2D) and the skybox turns out to be black. 我正在加载一个立方体贴图天空盒纹理(3D坐标而不是2D坐标),而天空盒却变成了黑色。 I don't receive any errors and the skybox is rendering in at the right locations and all, but something is still not right. 我没有收到任何错误,并且天空盒正在所有正确的位置渲染,但是仍然有些不正确。

I have a custom class CubeMap which just takes in a String[] for the file names of the textures and loads them with the following code: 我有一个自定义类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);
    }
}

The decodeTextureFile() method in 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);
}

Additionally, I have a SkyBox class which creates a CubeMap and stores the vertices of a preset array into a vertexArray and a vertexBuffer (this part of the code works fine so I don't see the need to add the Model code) 此外,我还有一个SkyBox类,该类创建一个CubeMap并将预设数组的顶点存储到vertexArrayvertexBuffer (这部分代码可以正常工作,因此我不需要添加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();
    }
}

I have a SkyboxShader class of which I'm fairly certain that it functions properly. 我有一个SkyboxShader类,我可以肯定它能正常运行。 The actual rendering of the SkyBox happens in the SkyboxRenderer class: 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();
    }
}

If anyone knows how to fix this problem it would be greatly appreciated. 如果有人知道如何解决此问题,将不胜感激。 Any additional code will be posted if requested. 如果需要,将发布任何其他代码。 Thanks in advance! 提前致谢!


Edits 编辑

Edit: The Image class is custom and just stores a ByteBuffer and width and height ints. 编辑: Image类是自定义的,仅存储ByteBuffer以及width和height整数。 Getters are provided but no additional functionality. 提供了Getters剂,但没有其他功能。

Edit: I've even tried refactoring all of my code to no prevail. 编辑:我什至试图重构我所有的代码,以不占优势。

Edit: I think the error lies in the cube map, not the textures or other code, as I can get it to kind-of work when I just use 6 different quads to render the box. 编辑:我认为错误在于立方体贴图,而不是纹理或其他代码,因为当我仅使用6个不同的四边形来渲染该框时,我就可以进行某种工作。

Edit: I solved the problem. 编辑:我解决了问题。 I forgot to call the method .create() on type shader in the renderer. 我忘了在渲染器中的类型着色器上调用方法.create()

CLOSED: I forgot to add the method call shader.create(); CLOSED:我忘记添加方法调用shader.create(); in the SkyboxRenderer.init() function so no shader programs were linked. 在SkyboxRenderer.init()函数中,因此没有链接任何着色器程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM