java - Skybox 是全黑的

标签 java opengl lwjgl skybox

我是 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);
}

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

我需要添加更多详细信息以便发布,因为代码太多......

天空盒着色器:

公共(public)类 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");
}

}

公共(public)抽象类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;

}`

最佳答案

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

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

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 core profile specification 第 8.17 节),

A cube map texture is mipmap complete if each of the six texture images, considered individually, is mipmap complete. Additionally, a cube map texture is cube complete if the following conditions all hold true:

  • The level_base texture images of each of the six cube map faces have identical, positive, and square dimensions.

  • The levelbase images were each specified with the same internal format.

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

Using the preceding definitions, a texture is complete unless any of the following conditions hold true:

  • [...]
  • The texture is a cube map texture, and is not cube complete.
  • [...]

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

第 11.1.3.5 节规定:

If a sampler is used in a shader and the sampler’s associated texture is not complete, as defined in section 8.17, (0; 0; 0; 1) will be returned for a non-shadow sampler and 0 for a shadow sampler.

事实上,您的立方体贴图应该显示为全黑。

关于java - Skybox 是全黑的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34253032/

相关文章:

java - 游戏崩溃并回调失败,因为闭包实例已被垃圾收集

java - Hello world 可以运行,但随后出现没有主线的错误?

java - Mesos 任务状态时间戳

java - 您会使用发布/订阅或队列来进行长时间运行的后台进程吗?

c++ - 如何让 OpenGL 在 OSX 上运行

opengl - (OpenGL 3.1 - 4.2) 动态统一数组?

java - 渲染不同的VAO

java - 事务隔离 READ UNCOMMITTED 不起作用

c++ - 在 OpenGL 3+ 中纹理球体

java - Slick2D:在游戏循环之前初始化图形对象