1

我是openGL的新手

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

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();
}

加载程序 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);
}

纹理存在,但天空盒是全黑的,有人可以帮帮我吗!我该如何解决

由于代码太多,我需要添加更多详细信息以进行发布...

天空盒着色器:

公共类 SkyboxShader 扩展 ShaderProgram{

private static final String VERTEX_FILE = "src/com/redcatengine/skybox/skyboxVertexShader.txt";
private static final String FRAGMENT_FILE = "src/com/redcatengine/skybox/skyboxFragmentShader.txt";

private int location_projectionMatrix;
private int location_viewMatrix;

public SkyboxShader() {
    super(VERTEX_FILE, FRAGMENT_FILE);
}

public void loadProjectionMatrix(Matrix4f matrix){
    super.loadMatrix(location_projectionMatrix, matrix);
}

public void loadViewMatrix(Camera camera){
    Matrix4f matrix = Maths.createViewMatrix(camera);
    matrix.m30 = 0;
    matrix.m31 = 0;
    matrix.m32 = 0;
    super.loadMatrix(location_viewMatrix, matrix);
}

@Override
protected void getAllUniformLocations() {
    location_projectionMatrix = super.getUniformLocation("projectionMatrix");
    location_viewMatrix = super.getUniformLocation("viewMatrix");
}

@Override
protected void bindAttributes() {
    super.bindAttribute(0, "position");
}

}

公共抽象类 ShaderProgram {

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;
}

}

天空盒片段着色器:

#version 400

in vec3 textureCoords;
out vec4 out_Color;

uniform samplerCube cubeMap;

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

天空盒顶点着色器

#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;

}`
4

1 回答 1

1

您的立方体贴图纹理不是立方体完整的:

您的加载程序代码会遍历调用它的数组中的所有文件:

for(int i = 0; i < textureFiles.length;i++){
    // [...]
    GL11.glTexImage2D(GL13.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, [...])
}

但是,您的输入数组仅包含5 个整数:

String[] TEXTURE_FILES = {"right","left","bottom","back","front"};

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

根据 GL 规范(引用来自OpenGL 4.5 核心配置文件规范的第 8.17 节),

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

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

  • 每个级别基础图像都使用相同的内部格式指定。

它进一步定义纹理完整性

使用前面的定义,除非以下任何条件成立,否则纹理是完整的:

  • [...]
  • 纹理是立方体贴图纹理,不是立方体完整的。
  • [...]

所以你的立方体贴图纹理是不完整的。

第 11.1.3.5 节规定:

如果在着色器中使用采样器并且采样器的关联纹理不完整,如第 8.17 节中定义的那样,(0; 0; 0; 1)将为非阴影采样器和0阴影采样器返回。

所以确实,你的立方体贴图应该是全黑的。

于 2015-12-13T18:25:51.490 回答