2016-07-31 42 views
0

我無法將我的shadow mapping實現從正向呈現轉換爲延遲呈現。我找到了這個問題,並且它沒有被髮送到着色器。我懷疑這是因爲我的「GBuffer」FBO綁定而不是立方體貼圖FBO。OpenGL - 在綁定不同的FBO時訪問立方體貼圖

如果GLSL可以在一次繪製調用中訪問來自兩個不同FBO的彩色附件,我想要走這條路線,這可能嗎?

下面,工作轉發渲染代碼。

public void NormalPass(Shader shader) 
{ 
    // Reset state 
    GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); 
    GL.BindTexture(TextureTarget.Texture2D, 0); 

    GL.UseProgram(shader.ID); 
    GL.Viewport(0, 0, Width, Height); 
    GL.CullFace(CullFaceMode.Back); 
    GL.ClearColor(0.5f, 0.5f, 0.5f, 1f); 

    // Uniforms 
    Matrix4 viewMatrix = player.GetViewMatrix(); 
    Matrix4 projectionMatrix; 
    Matrix4.CreatePerspectiveFieldOfView((float)Math.PI/4, (float)Width/(float)Height, 0.1f, 100.0f, out projectionMatrix); 
    GL.UniformMatrix4(shader.viewMatrixLocation, false, ref viewMatrix); 
    GL.UniformMatrix4(shader.projectionMatrixLocation, false, ref projectionMatrix); 
    GL.Uniform3(shader.lightPositionLocation, lightPos); 

    GL.BindTexture(TextureTarget.TextureCubeMap, cubeTex); 
    DrawScene(shader, false);        // False = Not shadowpass 
    GL.BindTexture(TextureTarget.TextureCubeMap, 0); 
} 

這裏,失敗的延遲渲染修改

public void Composite(Shader shader) 
{ 
    //Set up FBO reading/writing 
    GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, 0); 
    GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, gBuffer.FBO); 

    // Bind textures 
    GL.ActiveTexture(TextureUnit.Texture2); 
    GL.BindTexture(TextureTarget.Texture2D, gBuffer.positionTexture); 
    GL.ActiveTexture(TextureUnit.Texture1); 
    GL.BindTexture(TextureTarget.Texture2D, gBuffer.normalTexture); 

    GL.UseProgram(shader.ID); 
    GL.Disable(EnableCap.DepthTest); 
    GL.Viewport(0, 0, Width, Height); 
    GL.CullFace(CullFaceMode.Back); 
    GL.ClearColor(0.5f, 0.5f, 0.5f, 1f); 
    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); 

    // Uniforms 
    Matrix4 viewMatrix = player.GetViewMatrix(); 
    GL.UniformMatrix4(shader.viewMatrixLocation, false, ref viewMatrix); 
    GL.Uniform3(shader.lightPositionLocation, lightPos); 

    GL.BindTexture(TextureTarget.TextureCubeMap, cubeTex); 
    Quad2D.drawScreenSizedQuad(); // Draw the combined lighting and diffuse to screen 
    GL.BindTexture(TextureTarget.TextureCubeMap, 0); 
} 

快樂按要求提供任何信息。

回答

2

如果GLSL可以在一次繪製調用中訪問來自兩個不同FBO的顏色附件,我想要走這條路線,這可能嗎?

這個問題表明對發生的事情缺乏瞭解。 GLSL訪問紋理,而不是FBO的顏色附件。

顯然,紋理可以用作顏色附件。但是區別很重要,因爲無論如何,GLSL正在訪問綁定的紋理,而不是FBO中的任何內容。

OpenGL關於從作爲FBO的顏色附件附加的紋理讀取的唯一規則是:只要這些紋理實際上並未附加到呈現期間活動繪製幀緩衝區的特定幀緩衝區命令。即使如此,OpenGL will only care if you break the feedback loop rules

因此,您對讀取幀緩衝區的綁定是毫無意義的。這不是讓紋理可供閱讀或某些東西;這不是讀取幀緩衝區綁定點的用途。它僅用於framebuffer reading commandsframebuffer blitting operations

因此,簡單地將默認幀緩衝區綁定到FramebufferTarget.Framebuffer,就像您在前進的情況下所做的那樣,本來就沒有問題。

你的原則問題是這樣的:

GL.BindTexture(TextureTarget.TextureCubeMap, cubeTex); 

您沒有使用glActiveTexture指定紋理單元到立方體貼圖進行綁定。這意味着它將使用您設置的最後一個紋理單元:TextureUnit.Texture1,並且您已經在那裏綁定了2D紋理。 GLSL從同一紋理單元中讀取兩種不同類型的紋理是非法的。

所以賠率是好的,這不是你的意思。 在撥打任何glBindTexture之前,始終使用使用glActiveTexture。除非你使用glBindTextures from GL 4.4/ARB_multibindglBindTextureUnit from GL 4.5/ARB_DSA

+0

這清除了我的許多誤解,肯定會讓我在賽道上節省很多時間,非常感謝Jason。順便說一下,它現在也在工作。 –

相關問題