2014-10-28 29 views
0

我一直試圖理解/調試模板緩衝區的問題。我可能不知道它是如何工作的,而我認爲它的工作原理。基本上,我有一個場景,我渲染所有的固體對象,併爲它們指定模版值1.然後,我再次穿過相同的場景,但是第二次渲染透明對象並給出它們的模板值爲2。最後一步我有一個屏幕四邊形,將場景繪製成紋理。我想用模板緩存只繪製在模板位被設置爲1或2,所以在代碼的最後四:模板緩衝區清除值不是0?

// First enable the stencil buffer 
gl.enable(gl.STENCIL_TEST); 
gl.clearStencil(0); 
gl.clear(gl.STENCIL_BUFFER_BIT); // presumably this clears the buffer to 0? 

// ...for the solid meshes 
gl.stencilFunc(gl.ALWAYS, 1, 0xffffffff); // Always pass the stencil 
gl.stencilOp(gl.REPLACE, gl.REPLACE, gl.REPLACE); // Replace the stencil value with ref=1 


// ...for the transparent meshes 
gl.stencilFunc(gl.ALWAYS, 2, 0xffffffff); // Always pass the stencil 
gl.stencilOp(gl.REPLACE, gl.REPLACE, gl.REPLACE); // Replace the stencil value with ref=2 

// ...for the final screen quad 
gl.stencilFunc(gl.GEQUAL, 1, 0xffffffff); // Only draw the bits higher than 1 (so 1 and 2 should be included) 
gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP); // Dont touch the stencil values in the buffer 

然而,這似乎永遠不會工作。從我的測試中看來,模板值設置爲0xffffffff,然後再爲它們賦值。這使得GEQUAL測試毫無意義,因爲緩衝區值大於0.

我創建了一個JS小提琴來演示我的意思。我繪製一個小方塊並將其值設爲4.然後繪製一個更大的方塊並測試gequal爲5.我會認爲更大的方塊總是會失敗。但它總是通過。當我測試使用gl.stencilFunc(gl.GEQUAL,4,0xffffffff);我只看到正確的小廣場。但是當我測試gl.stencilFunc(gl.GEQUAL,0xffffff,0xffffffff);那麼它也看起來是正確的。這對我說,模板緩衝區沒有設置爲0?

http://jsfiddle.net/90af0rov/

我真的感到困惑,並希望在此< :(

回答

1

你身邊得到GEQUAL操作stencilFunc走錯了路的指針 在這裏看到的文檔:https://www.opengl.org/sdk/docs/man2/xhtml/glStencilFunc.xml

GL_GEQUAL 通過if(ref & mask)> =(模板&掩碼)​​

因此,在您的代碼示例中,它只會在ref(它是1)等於當前模板緩衝區中的值時繪製。如果您將其更改爲:

gl.stencilFunc(gl.LESS, 0, 0xffffffff); 

那麼就應該修復它,因爲它現在會通過,如果零裁判小於模板緩存中值。在這種情況下,您也可以使用NOTEQUAL而不是LESS。