2014-09-23 54 views
0

我在寫一個延期着色代碼。
我有一個FBO有4個顏色附件(紋理)和深度附件(渲染緩衝區)。
我用着色器呈現場景,寫入這4個顏色附件。
問題是,第0個顏色附件的數據也寫入其他。GLSL MRT將相同的數據寫入所有顏色附件

下面是我的代碼:

Renderer_Deferred.java 
    public class Renderer_Deferred 
    { 
     private final int FBO; 
     private final int depthbuffer; 
     public final int positiontexture, diffusetexture, normaltexture, bumpmap; 

    Renderer_Deferred() 
    { 
     System.out.println("Setting up the deferrer.."); 
     positiontexture = GL11.glGenTextures(); 
     diffusetexture = GL11.glGenTextures(); 
     normaltexture = GL11.glGenTextures(); 
     bumpmap = GL11.glGenTextures(); 
     FBO = glGenFramebuffers(); 
     glBindFramebuffer(GL_FRAMEBUFFER, FBO); 

     GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, positiontexture); 
     GL11.glTexParameterf(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); 
     GL11.glTexParameterf(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); 
     GL11.glTexImage2D(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0, GL30.GL_RGB32F, Configuration.displayWidth, Configuration.displayheight, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (java.nio.ByteBuffer) null); 
     GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0); 

     GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, diffusetexture); 
     GL11.glTexParameterf(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); 
     GL11.glTexParameterf(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); 
     GL11.glTexImage2D(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0, GL11.GL_RGBA, Configuration.displayWidth, Configuration.displayheight, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (java.nio.ByteBuffer) null); 
     GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0); 

     GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, normaltexture); 
     GL11.glTexParameterf(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); 
     GL11.glTexParameterf(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); 
     GL11.glTexImage2D(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0, GL30.GL_RGB16F, Configuration.displayWidth, Configuration.displayheight, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (java.nio.ByteBuffer) null); 
     GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0); 

     GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, bumpmap); 
     GL11.glTexParameterf(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); 
     GL11.glTexParameterf(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); 
     GL11.glTexImage2D(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0, GL11.GL_RGBA, Configuration.displayWidth, Configuration.displayheight, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (java.nio.ByteBuffer) null); 
     GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0); 

     GL11.glViewport(0, 0, Configuration.displayWidth, Configuration.displayheight); 

     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, positiontexture, 0); 
     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, diffusetexture, 0); 
     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, normaltexture, 0); 
     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, bumpmap, 0); 

     depthbuffer = glGenRenderbuffers(); 
     glBindRenderbuffer(GL_RENDERBUFFER, depthbuffer); 
     glRenderbufferStorage(GL_RENDERBUFFER, GL14.GL_DEPTH_COMPONENT24, Configuration.displayWidth, Configuration.displayheight); 
     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthbuffer); 
     glBindRenderbuffer(GL_RENDERBUFFER, 0); 

     if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) 
     { 
      System.err.println("Generating the deferred FBO failed!"); 
      System.exit(5321); 
     } 

     glBindFramebuffer(GL_FRAMEBUFFER, 0); //////////////////////////////////////////////////////// 
    } 

    public void startRendering() 
    { 
     // CLEAR AND HOOK UP TEXTURES 
     glBindFramebuffer(GL_FRAMEBUFFER, FBO); 

     IntBuffer buf = BufferUtils.createIntBuffer(4); 
     buf.put(GL_COLOR_ATTACHMENT0); 
     buf.put(GL_COLOR_ATTACHMENT1); 
     buf.put(GL_COLOR_ATTACHMENT2); 
     buf.put(GL_COLOR_ATTACHMENT3); 
     buf.flip(); 

     GL20.glDrawBuffers(buf); 

     if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) 
     { 
      System.err.println("Deferred renderer failed to setup correctly!"); 
     } 

     GL11.glEnable(GL11.GL_DEPTH_TEST); 
     GL11.glDepthFunc(GL11.GL_LEQUAL); 
     GL11.glViewport(0, 0, Configuration.displayWidth, Configuration.displayheight); 
     GL11.glClearColor(SimpleCraft.world.skycolor.x, SimpleCraft.world.skycolor.y, SimpleCraft.world.skycolor.z, 0f); 
     GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); 
    } 

    public void stopRendering() 
    { 
     glBindFramebuffer(GL_FRAMEBUFFER, 0); 
    } 
} 

以上是我的代碼,用於激活和停用延遲渲染器。 這個步驟完成後,後處理激活:

public class Renderer_Postprocessor 
{ 
    PostprocessingObject postprocessor; 

    public static int loc_pos, loc_diffuse, loc_normal, loc_bump, loc_ambient; 
    public static int loc_viewingdistance, loc_modelviewmatrix; 

    Renderer_Postprocessor() 
    { 
     postprocessor = new PostprocessingObject(); 
    } 

    public void postprocess(int pos, int diffuse, int normal, int bump) 
    { 
     GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); 
     GL11.glDisable(GL11.GL_DEPTH_TEST); 
     GL20.glUseProgram(Shaders.PROGRAM_POSTPROCESSOR); 
     GL20.glDrawBuffers(GL11.GL_BACK); 

     GL13.glActiveTexture(GL13.GL_TEXTURE0); 
     GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, pos); 
     GL13.glActiveTexture(GL13.GL_TEXTURE1); 
     GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, diffuse); 
     GL13.glActiveTexture(GL13.GL_TEXTURE2); 
     GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, normal); 
     GL13.glActiveTexture(GL13.GL_TEXTURE3); 
     GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, bump); 

     GLUtility.uploadToShader(Configuration.viewDistanceInBlocks, loc_viewingdistance); 
     GLUtility.uploadToShader(Camera.getModelViewMatrix(new Matrix4f(), Camera.getViewMatrix()), loc_modelviewmatrix); 

     GL30.glBindVertexArray(postprocessor.getVAO()); 
     GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 6); 
     GL30.glBindVertexArray(0); 
     GL20.glUseProgram(0); 

     GL13.glActiveTexture(GL13.GL_TEXTURE0); 
     GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0); 
     GL13.glActiveTexture(GL13.GL_TEXTURE1); 
     GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0); 
     GL13.glActiveTexture(GL13.GL_TEXTURE2); 
     GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0); 
     GL13.glActiveTexture(GL13.GL_TEXTURE3); 
     GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0); 
     GL11.glEnable(GL11.GL_DEPTH_TEST); 
    } 
} 

下面是我對deferrer和後處理片段着色:

Deferred_Fragment.frag 
#version 330 core 
#extension GL_ARB_explicit_uniform_location : require 
layout(location=0) uniform sampler2DArray texture_diffuse; 
layout(location=1) uniform sampler2DArray texture_normal; 

uniform vec3 pass_EyeVector; 

in vec4 pass_Position; 
in vec3 pass_TextureCoord; 
in vec3 pass_Normal; 

layout(location = 0) out vec4 pos; 
layout(location = 1) out vec4 dif; 
layout(location = 2) out vec4 nor; 
layout(location = 3) out vec4 bum; 

void main(void) 
{ 
    pos = texture(texture_diffuse, pass_TextureCoord); // Transform from -1/1 to 0/1 
    dif = pass_Position; 
    nor = vec4(pass_Normal, 1.0); 
    bum = texture(texture_normal, pass_TextureCoord); 
} 

而且postprocessor.frag

#version 330 core 
#extension GL_ARB_explicit_uniform_location : require 

layout(location=0) uniform sampler2DRect positiontexture; 
layout(location=1) uniform sampler2DRect diffusetexture; 
layout(location=2) uniform sampler2DRect normaltexture; 
layout(location=3) uniform sampler2DRect bumpmap; 

in vec2 pass_TextureCoord; 
out vec4 out_Color; 

void main(void) 
{ 
    out_Color = texture(diffusetexture, pass_TextureCoord); 
} 
+0

出於好奇,你爲什麼要使用矩形紋理GL 3.3?你應該可以使用'texelFetch(...)'完成同樣的事情。 – 2014-09-23 15:25:24

+0

自從現在一週以來,我一直在遇到這個問題,而且我越來越沮喪,騎自行車瀏覽了我發現的任何延期着色教程。一個使用矩形紋理,所以我也試過。直到現在還沒有使用texelFetch(),現在就嘗試一下並寫下結果。 – user2979040 2014-09-23 16:15:50

回答

1

你」重新嘗試對統一變量使用location佈局限定符:

layout(location=0) uniform sampler2DArray texture_diffuse; 
layout(location=1) uniform sampler2DArray texture_normal; 

layout(location=0) uniform sampler2DRect positiontexture; 
layout(location=1) uniform sampler2DRect diffusetexture; 
layout(location=2) uniform sampler2DRect normaltexture; 
layout(location=3) uniform sampler2DRect bumpmap; 

我一直無法找到GLSL規範中的任何建議,表明這是支持的。至少在330中,只列出了頂點着色器輸入和片段着色器輸出以支持location。在後來的版本中,它似乎也支持其他着色器階段的變量in/out,但我仍然沒有看到任何關於制服的提及。

您可能打算使用此限定符設置採樣器的紋理單位。有一個binding預選賽中,它看起來像這樣:

layout(binding=0) uniform sampler2DArray texture_diffuse; 
layout(binding=1) uniform sampler2DArray texture_normal; 

這是在OpenGL 4.2(GLSL版本420)及更高版本,不過只支持。如果您想堅持使用GLSL 330,則需要使用glGetUniformLocaction()glUniform1i()以舊式方式設置採樣器制服的紋理單元。

+0

感謝您的回答! 制服的佈局(位置)由分機啓用。 我會嘗試再次以舊式的方式上傳紋理併發布結果。我定義的發現是,我的錯誤很可能在延遲渲染器中,因爲我嘗試使用預加載紋理的後處理器,並且它工作正常。 – user2979040 2014-09-24 07:44:54

+0

啊,好吧,我應該在着色器代碼中看到。顧名思義,它定義了制服的位置*雖然,對吧?所以它使'glGetUniformLocation()'調用不必要,但是你仍然需要調用'glUniform1i()'來設置值? – 2014-09-24 07:59:50

+0

最有可能。我刪除了所有的佈局(位置),並回到了老式的方式。現在一切正常,儘管我並沒有真正理解我的錯誤。謝謝您的幫助! – user2979040 2014-09-25 19:06:39

0

您應該使用佈局綁定而不是佈局位置來綁定採樣器。 您應該重新檢查綁定點,因爲您有多個採樣器綁定到相同的位置。如果將紋理綁定到TEXTURE0,請將其指定爲binding = 0。

嘗試將其更改爲:

layout(binding=0) uniform sampler2DArray texture_diffuse; 
layout(binding=1) uniform sampler2DArray texture_normal; 

layout(binding=0) uniform sampler2DRect positiontexture; 
layout(binding=1) uniform sampler2DRect diffusetexture; 
layout(binding=2) uniform sampler2DRect normaltexture; 
layout(binding=3) uniform sampler2DRect bumpmap;