Search code examples
javaopengllwjgl

Skybox does not show textures


My skybox is displayed, but does not display the textures that I load from the image. Instead, it shows the black color.

I am using a render with MultisampledFbo support.

My texture loading code looks like this:

private int loadSkyboxTextures(){
        int texID = glGenTextures();
        glBindTexture(GL_TEXTURE_CUBE_MAP, texID);
        for(int i = 0; i < TEXTURE_FILES.length; i++){
            InputStream file = getClass().getResourceAsStream(TEXTURE_FILES[i]);
            byte[] pixelData = new byte[0];
            try {
                pixelData = new byte[file.available()];
                file.read(pixelData);
            } catch (IOException e) {
                e.printStackTrace();
            }
            ByteBuffer byteBuffer = ByteBuffer.wrap(pixelData);
            glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, 512, 512, 0,
                    GL_RGB, GL_UNSIGNED_BYTE, byteBuffer);
        }

        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

        return texID;
    }

Cube Render Code:

    private void drawSkybox(){
        glEnable(GL_TEXTURE_CUBE_MAP);
        glDepthMask(false);
        glGenBuffers(vbo);
        glBindBuffer (GL_ARRAY_BUFFER, vbo[0]);
        glBufferData(GL_ARRAY_BUFFER, POINTS, GL_STATIC_DRAW);
        glGenVertexArrays(vao);
        glBindVertexArray(vao[0]);
        glVertexAttribPointer(0, 3, GL_FLOAT, false, 3 * Float.BYTES, NULL);
        glEnableVertexAttribArray(0);
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_CUBE_MAP, texId);
        glDrawArrays(GL_TRIANGLES, 0, 36);
        glDisableVertexAttribArray(0);
        glBindVertexArray(0);
        glDepthMask(true);
        glBindBuffer (GL_ARRAY_BUFFER,0);
        glDisable(GL_TEXTURE_CUBE_MAP);
    }

The cube rendering call in the main render function:

                glMatrixMode(GL_PROJECTION);
                glOrtho(-max, max, -1, 1, 10, -10);
                glRotated(cameraX, 1f, 0f, 0);
                glRotated(cameraY, 0f, 1f, 0);
                glGetFloatv(GL_PROJECTION_MATRIX, pm);

                glMatrixMode(GL_MODELVIEW);
                glLoadIdentity();
                drawSkybox();
                glLoadIdentity();
                ...
                //render other objects

Solution

  • Legacy OpenGL's texturing has to be enabled. To perform cube-mapped texturing, GL_TEXTURE_CUBE_MAP has to be enabled (see glEnable):

    glEnable(GL_TEXTURE_CUBE_MAP);
    

    Note, for cube map textures, the texture coordinates are 3-dimensional and treated as a vector form the center of the cube map.