这是我试图在 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);
}
然后我将纹理传递给samplerCube
着色器中的 a 并调用textureCube()
以显示它。可悲的是,屏幕显示为黑色,当我打电话时,glGetError()
我得到GL_INVALID_OPERATION
and GL_INVALID_ENUM
。我在这里做错了什么?