Java 天空盒是全黑的

Java 天空盒是全黑的,java,opengl,lwjgl,skybox,Java,Opengl,Lwjgl,Skybox,我是openGL的新手 我在LWJGL上创建了这个skybox,但它全是黑色的 SkyboxRenderer类: private static String[] TEXTURE_FILES = {"right","left","bottom","back","front"}; private RawModel cube; private int texture; private SkyboxShader shader; public SkyboxRenderer(Loader loader,

我是openGL的新手

我在LWJGL上创建了这个skybox,但它全是黑色的

SkyboxRenderer类:

private static String[] TEXTURE_FILES = {"right","left","bottom","back","front"};
private RawModel cube;
private int texture;
private SkyboxShader shader;

public SkyboxRenderer(Loader loader, Matrix4f projectionMatirx) {
    cube = loader.loadToVAO(VERTICES, 3);
    texture = loader.loadCubeMap(TEXTURE_FILES);
    shader = new SkyboxShader();
    shader.start();
    shader.loadProjectionMatrix(projectionMatirx);
    shader.stop();
}

public void render(Camera camera){
    shader.start();
    shader.loadViewMatrix(camera);
    GL30.glBindVertexArray(cube.getVaoID());
    GL20.glEnableVertexAttribArray(0);
    GL13.glActiveTexture(GL13.GL_TEXTURE0);
    GL11.glBindTexture(GL13.GL_TEXTURE_CUBE_MAP, texture);
    GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, cube.getVertexCount());
    GL20.glDisableVertexAttribArray(0);
    GL30.glBindVertexArray(0);
    shader.stop();
}
Loader loadCubeMap函数:

public int loadCubeMap(String[] textureFiles){
    int texID = GL11.glGenTextures();
    GL13.glActiveTexture(GL13.GL_TEXTURE0);
    GL11.glBindTexture(GL13.GL_TEXTURE_CUBE_MAP, texID);

    for(int i = 0; i < textureFiles.length;i++){
        TextureData data = decodeTextureFile("res/" + textureFiles[i] + ".png");
        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.getBuffer());

    }
    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);
    GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
    GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
    textures.add(texID);
    return texID;
}

private TextureData decodeTextureFile(String fileName) {
    int width = 0;
    int 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 (Exception e) {
        e.printStackTrace();
        System.err.println("Tried to load texture " + fileName + ", didn't work");
        System.exit(-1);
    }
    return new TextureData(buffer, width, height);
}
}

公共抽象类着色器程序{

private int programID;
private int vertexShaderID;
private int fragmentShaderID;

private static FloatBuffer matrixBuffer = BufferUtils.createFloatBuffer(16);
public ShaderProgram(String vertexFile, String fragmentFile) {
    vertexShaderID = loadShader(vertexFile, GL20.GL_VERTEX_SHADER);
    fragmentShaderID = loadShader(fragmentFile, GL20.GL_FRAGMENT_SHADER);
    programID = GL20.glCreateProgram();
    GL20.glAttachShader(programID, vertexShaderID);
    GL20.glAttachShader(programID, fragmentShaderID);
    bindAttributes();
    GL20.glLinkProgram(programID);
    GL20.glValidateProgram(programID);
    getAllUniformLocations();
}

protected abstract void getAllUniformLocations();

protected int getUniformLocation(String uniformName){
    return GL20.glGetUniformLocation(programID, uniformName); 
}

public void start(){
    GL20.glUseProgram(programID);
}

public void stop(){
    GL20.glUseProgram(0);
}

public void cleanUp(){
    stop();
    GL20.glDetachShader(programID, vertexShaderID);
    GL20.glDetachShader(programID, fragmentShaderID);
    GL20.glDeleteShader(vertexShaderID);
    GL20.glDeleteShader(fragmentShaderID);
    GL20.glDeleteProgram(programID);
}

protected abstract void bindAttributes();

protected void bindAttribute(int attribute, String variableName){
    GL20.glBindAttribLocation(programID, attribute, variableName);
}

protected void loadInt(int location, int value){
    GL20.glUniform1i(location, value);
}


protected void loadFloat(int location, float value){
    GL20.glUniform1f(location, value);
}

protected void loadVector(int location, Vector3f value){
    GL20.glUniform3f(location, value.x, value.y, value.z);
}

protected void load2DVector(int location, Vector2f value){
    GL20.glUniform2f(location, value.x, value.y);
}

protected void loadBoolean(int location, boolean value){
    float toLoad = 0;
    if(value)toLoad = 1;else toLoad = 0;
    GL20.glUniform1f(location, toLoad);
}

protected void loadMatrix(int location, Matrix4f matrix){
    matrix.store(matrixBuffer);
    matrixBuffer.flip();
    GL20.glUniformMatrix4(location, false, matrixBuffer);
}

private static int loadShader(String file, int type){
    StringBuilder shaderSource = new StringBuilder();
    try{
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line;
        while((line = reader.readLine()) != null){
            shaderSource.append(line).append("\n");
        }
        reader.close();
    }catch(IOException e){
        System.err.println("Could not read shader file!");
        e.printStackTrace();
        System.exit(-1);
    }
    int shaderID = GL20.glCreateShader(type);
    GL20.glShaderSource(shaderID, shaderSource);
    GL20.glCompileShader(shaderID);
    if(GL20.glGetShaderi(shaderID, GL20.GL_COMPILE_STATUS)==GL11.GL_FALSE){
        System.out.println(GL20.glGetShaderInfoLog(shaderID, 500));
        System.out.println("Could not compile shader.");
        System.exit(-1);    
    }
    return shaderID;
}
}

skyboxFragmentShader:

#version 400

in vec3 textureCoords;
out vec4 out_Color;

uniform samplerCube cubeMap;

void main(void){
    out_Color = texture(cubeMap, textureCoords);
}
skyboxVertexShader

#version 400

in vec3 position;
out vec3 textureCoords;

uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;

void main(void){

    gl_Position = projectionMatrix * viewMatrix * vec4(position, 1.0); 
    textureCoords = position;

}`

您的立方体贴图纹理未完成立方体:

加载程序代码在调用它的数组中的所有文件上进行迭代:

您只为立方体提供了5个面,而忘记了“顶部”面

根据德国劳埃德船级社规范(引用自本规范第8.17节)

如果六个纹理中的每个纹理 单独考虑的图像是mipmap完整的另外,一个 如果以下条件均成立,则立方体贴图纹理为立方体完成 正确:

  • 六个立方体贴图面中的每个面的level_基本纹理图像都具有相同、正和正方形尺寸。

  • 水平基准 每个图像都使用相同的内部格式指定

它进一步定义纹理完整性:

使用上述定义,纹理是完整的,除非满足以下任何条件: 条件成立:

  • [……]
  • 该纹理是立方体贴图纹理,并且不是立方体完整
  • [……]
因此,立方体贴图纹理不完整

第11.1.3.5节规定:

如果在着色器中使用了采样器,但采样器的关联纹理不可用 按照第8.17节的定义,对于非阴影采样器,将返回完整的
(0;0;0;1)
,对于阴影采样器,将返回完整的
0


因此,确实,立方体贴图应该显示为完全黑色。

你的skybox着色器代码是什么样子的?@derhass我将其添加到其他着色器的问题帖子中。其他着色器工作是否正常?你正在为立方体贴图方形加载的纹理是否正确?@RetoKoradi是纹理1024x1024@KodeKishin:您仍然没有包含着色器文件的实际源代码。其他着色器工作的事实对这个问题并没有真正的帮助。你的skybox着色器需要对立方体贴图纹理进行正确采样,现在还不清楚它是否这样做。非常感谢:D@derhass
#version 400

in vec3 position;
out vec3 textureCoords;

uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;

void main(void){

    gl_Position = projectionMatrix * viewMatrix * vec4(position, 1.0); 
    textureCoords = position;

}`
for(int i = 0; i < textureFiles.length;i++){
    // [...]
    GL11.glTexImage2D(GL13.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, [...])
}
String[] TEXTURE_FILES = {"right","left","bottom","back","front"};