2015-12-13 28 views
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); 
} 

紋理存在,但skybox是fullyblack有人可以幫助我! 我該如何解決

我需要添加更多的細節張貼,因爲有太多的代碼......

SkyboxShader:

公共類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; 
} 

}

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; 

}` 
+1

你的天空箱着色器代碼是什麼樣子的? – derhass

+0

@derhass我把它添加到問題發佈其他着色器作品pefectly – Alice

+0

您正在加載的立方體貼圖廣場的紋理? –

回答

1

你的立方體貼圖質地不立方體完整

你裝載程序代碼在所有文件迭代在數組中它被稱爲有:

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

但是,您的輸入數組只包含 entires:

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

你只提供5個面的立方體,並忘了「頂」的臉。

按照GL規範(行情是從OpenGL 4.5 core profile specification第8.17),

立方體貼圖紋理映射是完成如果每六個紋理 圖像,單獨考慮,是紋理貼圖完成。 另外, 立方圖紋理是立方體完全如果滿足以下條件的所有保持 真:的每六個立方圖面的

  • 的level_base紋理圖像具有相同的,正的,正方形的尺寸。

  • 水平基準 圖像每個指定具有相同的內部格式。

它還繼續定義質地完整性

使用前面的定義,紋理完成,除非下列任何 條件成立:

  • [...]
  • 該紋理是立方體貼圖紋理,並且不是多維數據集完整的。
  • [...]

所以你的立方體貼圖質地不完成

第11.1.3.5狀態:

如果採樣器在使用着色器和取樣的相關質地不 完成,如第8.17定義,(0; 0; 0; 1)將用於非陰影採樣返回和0爲陰影採樣器。

確實,您的立方體貼圖應該顯示爲全黑。

+0

非常感謝你:D @derhass – Alice