2013-04-13 116 views
1

我使用的代碼創建一個幀緩衝中的WebGL:WebGL的幀緩衝區是空白

this.buffer = ctx.createFramebuffer(); 
ctx.bindFramebuffer(ctx.FRAMEBUFFER, this.buffer); 

this.depthTexture = this.createTexture(ctx, ctx.DEPTH_COMPONENT, 
    ctx.DEPTH_COMPONENT, ctx.UNSIGNED_SHORT);  
ctx.framebufferTexture2D(ctx.FRAMEBUFFER, ctx.DEPTH_ATTACHMENT, 
    ctx.TEXTURE_2D, this.depthTexture, 0); 


this.colourTexture = this.createTexture(ctx, ctx.RGBA, ctx.RGBA, ctx.FLOAT);   
ctx.framebufferTexture2D(ctx.FRAMEBUFFER, ctx.COLOR_ATTACHMENT0, 
    ctx.TEXTURE_2D, this.colourTexture, 0); 

抽獎,它使用

this.ctx.useProgram(this.forwardShader.program); 
this.ctx.bindFramebuffer(this.ctx.FRAMEBUFFER, this.framebuffer.buffer); 
this.ctx.viewport(0, 0, this.framebuffer.size, this.framebuffer.size); 
this.ctx.clear(this.ctx.COLOR_BUFFER_BIT | this.ctx.DEPTH_BUFFER_BIT); 

mat4.identity(this.viewMatrix); 
mat4.identity(this.modelMatrix); 

this.camera.transform(this.viewMatrix); 

mat4.translate(this.modelMatrix, this.modelMatrix, [0, 0, -2]); 

this.mStack.pushMatrix(this.modelMatrix); 

mat4.translate(this.modelMatrix, this.modelMatrix, [0, -1, 0]); 
mat4.rotateX(this.modelMatrix, this.modelMatrix, -Math.PI/2.0); 

this.setShaderUniforms(); 
this.plane.draw(this.forwardShader); 

this.mStack.popMatrix(this.modelMatrix); 

this.setShaderUniforms(); 
this.cube.draw(this.forwardShader); 

this.ctx.bindFramebuffer(this.ctx.FRAMEBUFFER, null); 

然後渲染使用

this.ctx.useProgram(this.renderTextureShader.program); 
this.ctx.viewport(0, 0, this.viewportWidth, this.viewportHeight);  
this.ctx.clear(this.ctx.COLOR_BUFFER_BIT | this.ctx.DEPTH_BUFFER_BIT); 

this.renderTexture.draw(this.framebuffer.colourTexture, this.renderTextureShader); 

結果它完美的工作,但是當我改變它來渲染深度紋理時,我得到一個白色的屏幕。我已經檢查了相關的擴展名被啓用和(儲存數組把它們顯然,如果他們得到gc'ed它可能會導致錯誤。在什麼即時做錯將是巨大的

的完整代碼可以在我找到的任何反饋bitbucket

+0

深度線性化可以幫助https://stackoverflow.com/a/44357374/3012928 –

回答

2

它可能是你的深度數據範圍內的問題,請嘗試:

this.depthTexture = this.createTexture(ctx, ctx.DEPTH_COMPONENT, ctx.DEPTH_COMPONENT, ctx.FLOAT);

+0

這樣做會導致錯誤「INVALID_OPERATION:texImage2D:DEPTH_COMPONENT格式的無效類型」。我懷疑例如,雖然啓用了深度紋理和浮點紋理的擴展,但兩者不能合併 –