WebGL:显示深度纹理

时间:2013-05-21 20:23:10

标签: webgl framebuffer depth

我是WebGL的新手,并尝试实现深度纹理,以便以后在Shadow Maps中使用。 下面是我对帧缓冲区初始化的代码。 在函数drawTest中,我首先绑定所有需要的Buffers,然后将Model的顶点绘制到附加到纹理的framebuffer中。 这不应该工作吗?我得到的只是一个白色的屏幕,没有错误。

this.drawTest = function (model1) {

    this.model1 = model1;


        if (model1.Image.ReadyState === true && model1.Ready === false) {
            this.PrepareModel(model1);
        }
        if (model1.Ready) {
            gl.bindBuffer(gl.ARRAY_BUFFER, this.model1.Vertices);
            gl.vertexAttribPointer(this.VertexPosition, 3, gl.FLOAT, false, 0, 0);
            gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, model1.Triangles);
        }

        gl.bindFramebuffer(gl.FRAMEBUFFER, this.depthFramebuffer);

        gl.clear(gl.DEPTH_BUFFER_BIT);

        gl.drawElements(gl.TRIANGLES, model1.numItems, gl.UNSIGNED_SHORT, 0);




}


this.InitDrawDepth = function(size) {

        this.depthTextureExt = gl.getExtension("WEBGL_depth_texture"); // Or browser-appropriate prefix
        if(!this.depthTextureExt) { 
            console.log("WEBGL_depth_texture Extension not available!"); 
            return; 
        }

        // Create a color texture
        this.colorTexture = gl.createTexture();
        gl.bindTexture(gl.TEXTURE_2D, this.colorTexture);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, size, size, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);

        this.depthTexture = gl.createTexture();
        gl.bindTexture(gl.TEXTURE_2D, this.depthTexture);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
        gl.texImage2D(gl.TEXTURE_2D, 0, gl.DEPTH_COMPONENT, size, size, 0, gl.DEPTH_COMPONENT, gl.UNSIGNED_SHORT, null);

       // var framebuffer = gl.createFramebuffer();
        this.depthFramebuffer = gl.createFramebuffer();
        gl.bindFramebuffer(gl.FRAMEBUFFER, this.depthFramebuffer);
        gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.colorTexture, 0);
        gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, this.depthTexture, 0);

        if(!gl.checkFramebufferStatus(gl.FRAMEBUFFER) === gl.FRAMEBUFFER_COMPLETE) {
            console.log("Framebuffer incomplete!");
        }

        //reset Framebuffer
        gl.bindFramebuffer(gl.FRAMEBUFFER, null);

        this.depthTextureSize = size;

    }

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题。我有另一个函数导致原始缓冲区出现问题。 感谢所有试图找出问题的人。