Abaab
Abaab

Reputation: 571

Setting up Cube Map

Here is the code I am trying to use to set up a Cube Map in LWJGL.

public static int setUpCubeMap(String filename, int anisotropyLevel, boolean clamp,   boolean pixelated, boolean mipmapped) {
    IntBuffer tmp = BufferUtils.createIntBuffer(1);
    glGenTextures(tmp);
    tmp.rewind();
    try {
        InputStream in = new FileInputStream(filename);
        PNGDecoder decoder = new PNGDecoder(in);
        glEnable(GL_TEXTURE_CUBE_MAP);
        ByteBuffer buf = ByteBuffer.allocateDirect(4 * decoder.getWidth() * decoder.getHeight());
        decoder.decode(buf, decoder.getWidth() * 4, PNGDecoder.Format.RGBA);
        buf.flip();

        glBindTexture(GL_TEXTURE_CUBE_MAP, tmp.get(0));
        org.lwjgl.opengl.ARBTextureStorage.glTexStorage2D(GL_TEXTURE_CUBE_MAP, (int)(Math.log(Math.max(decoder.getHeight(), decoder.getWidth()))/Math.log(2))+1, GL_RGBA8, decoder.getWidth(), decoder.getHeight());
        glTexSubImage2D(GL_TEXTURE_CUBE_MAP, 0, 0, 0, decoder.getWidth(), decoder.getHeight(), GL_RGBA, GL_UNSIGNED_BYTE, buf);

        if(mipmapped)
            org.lwjgl.opengl.GL30.glGenerateMipmap(GL_TEXTURE_CUBE_MAP);

        if(clamp) {
            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);
        } else {
            glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_TEXTURE_WRAP_S);
            glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_TEXTURE_WRAP_T);
        }

        if(pixelated) {
            glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        } else {
            glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        }

        if(mipmapped)
            glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        else
            glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

        if(anisotropyLevel > 1)
            glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropyLevel);

        glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
        glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
        glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
        glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
        glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
        glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
        System.out.println("Loaded texture successfully from: " + filename + " with dimensions of " + decoder.getWidth() + "x" + decoder.getHeight());

    } catch (java.io.FileNotFoundException ex) {
        System.err.println("Error " + filename + " not found");
    } catch (java.io.IOException e) {
        System.err.println("Error decoding " + filename);
    }
    tmp.rewind();
    return tmp.get(0);
}

I then pass the texture to a samplerCube in a shader and call textureCube() to display it. Sadly, the screen shows up black, and when I call glGetError() I get GL_INVALID_OPERATION and GL_INVALID_ENUM. What am I doing wrong here?

Upvotes: 0

Views: 1033

Answers (2)

kakTuZ
kakTuZ

Reputation: 562

There are several problems, none of these definitely prevent the code from working, I list them anyway.

The glTexSubImage2D call at the beginning is is superfluous.

  • calling it with GL_TEXTURE_CUBE_MAP causes an error glTexSubImage2D.
  • you are filling in the data the correct way down below

Also related to this, the call to glGenerateMipmap should happen after those other six glTexSubImage2D calls.

And in this section, in the else clause, the third parameter to both function calls is wrong.

if(clamp) {
    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);
} else {
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_TEXTURE_WRAP_S);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_TEXTURE_WRAP_T);
}

Upvotes: 1

Jean-Simon Brochu
Jean-Simon Brochu

Reputation: 950

I suggest you use gDEBugger; it gives a very understandable description of the problem and exactly when it occurs.

Upvotes: 1

Related Questions