为什么WebGL“清除"吸引前端缓冲区? [英] Why WebGL 'clear' draw to front buffer?

查看:155
本文介绍了为什么WebGL“清除"吸引前端缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不需要交换缓冲区或glFinish?

Why no need for swap-buffers or glFinish?

<!DOCTYPE html>
<html>
    <head>
        <title>Game v.0.0</title>
        <script>
            var gl = null;
            function startGame()
            {   {   var canvas = document.getElementById('gameCanvas');
                    var glNames = ["webgl", "experimental-webgl", "webkit-3d", "moz-webgl"];
                    for (var glNameI = 0; glNameI < glNames.length; ++glNameI)
                        try
                        {   gl = canvas.getContext(glNames[glNameI]);
                            if (gl) break;
                        }
                        catch(e)
                        {}
                    if(!gl)
                    {   canvas.outerHTML = "<a>WebGL NOT SUPPORTED? :(</a>";
                        return;
                    }
                }

                window.onkeydown = function(ev)
                {   switch(ev.keyCode)
                    {
                    case 49:// 1 key
                        gl.clearColor(0.3,0.7,0.2,1.0);
                        gl.clear(gl.COLOR_BUFFER_BIT); 
                        break;
                    case 50:// 2 key
                        gl.clearColor(0.3,0.2,0.7,1.0);
                        gl.clear(gl.COLOR_BUFFER_BIT); 
                        break;
                    }
                };
            }
        </script>
        <style type="text/css">
            canvas {border: 2px dotted blue;}
        </style>
    </head>

    <body onload="startGame()">
        <div><canvas id="gameCanvas" width="640" height="480"></canvas></div>
    </body>

</html>

推荐答案

因为这是WebGL的工作方式.

Because that's the way WebGL works.

WebGL自动交换/复制.每当您执行任何会影响WebGL drawingBuffer(例如"backbuffer")的操作时,都会将其标记为交换/复制.下一次浏览器合成网页时,它将执行交换或复制操作.您可以告诉它始终复制.无法告诉它总是交换

WebGL swaps/copies automatically. Anytime you do anything that effects the WebGL drawingBuffer (think "backbuffer) it gets marked to swap/copy. The next time the browser composites the web page it will do either a swap or a copy. You can tell it to always copy. You can not tell it to always swap

具体来说,使用 {preserveDrawingBuffer:true} 来创建WebGL上下文,如

Specifically, creating the WebGL context with {preserveDrawingBuffer: true} as in

gl = someCanvas.getContext("webgl", {preserveDrawingBuffer: true});

告诉WebGL您始终希望它进行复制.

Tells WebGL you always want it to do a copy.

默认值为WebGL根据各种因素选择交换或复制.例如,如果启用了抗锯齿功能,则它始终始终是一个副本(解决方案),而仿效处理功能似乎已关闭,则可能是交换.同样,在这种默认情况下,当 preserveDrawingBuffer 执行复制或交换后为false时,它将清除后备缓冲区.这是为了使它看起来是一致的,而不管它是选择复制还是交换.

The default is WebGL chooses swap or copy depending on various factors. For example if anti-aliasing is on it's always effectively a copy (a resolve) where as if anti-aliasing is off it might be a swap. Also, in this default case, when preserveDrawingBuffer is false after it does a copy or swap it will clear the backbuffer. This is to try to make it appear consistent regardless of whether it chooses to copy or swap.

如果 preserveDrawingBuffer = true,则它永远不会清除后备缓冲区.

If preserveDrawingBuffer = true then it never clears the backbuffer.

如果要对多个JavaScript事件进行大量工作,并且在完成所有工作之前不让用户看到结果,则需要渲染到带有附加纹理或渲染缓冲区的帧缓冲区,然后在所有完成工作比渲染到画布(后缓冲区)要好.

If you want to do a bunch of work over multiple JavaScript events and not let the user see the results until all your work is done you'll need to render to a framebuffer with an attached texture or renderbuffer and then when all your work is done render than attachment to the canvas (the backbuffer).

就像 gl.finish 一样,这在WebGL中是无操作的.没有意义.

as for gl.finish that's a no-op in WebGL. It has no point.

这篇关于为什么WebGL“清除"吸引前端缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆