5

我正在从这里阅读教程。

<script class = "WebGL">
var gl;
function initGL() {
  // Get A WebGL context
  var canvas = document.getElementById("canvas");
  gl = getWebGLContext(canvas);
  if (!gl) {
    return;
  }
}
var positionLocation;
var resolutionLocation;
var colorLocation;
var translationLocation;
var rotationLocation;
var translation = [50,50];
var rotation = [0, 1];
var angle = 0;
function initShaders() {
  // setup GLSL program
  vertexShader = createShaderFromScriptElement(gl, "2d-vertex-shader");
  fragmentShader = createShaderFromScriptElement(gl, "2d-fragment-shader");
  program = createProgram(gl, [vertexShader, fragmentShader]);
  gl.useProgram(program);

  // look up where the vertex data needs to go.
  positionLocation = gl.getAttribLocation(program, "a_position");

  // lookup uniforms
  resolutionLocation = gl.getUniformLocation(program, "u_resolution");
  colorLocation = gl.getUniformLocation(program, "u_color");
  translationLocation = gl.getUniformLocation(program, "u_translation");
    rotationLocation = gl.getUniformLocation(program, "u_rotation");

  // set the resolution
  gl.uniform2f(resolutionLocation, canvas.width, canvas.height);
}
function initBuffers() {
  // Create a buffer.
  var buffer = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
  gl.enableVertexAttribArray(positionLocation);
  gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);

  // Set Geometry.
  setGeometry(gl);
}

function setColor(red, green, blue) {
    gl.uniform4f(colorLocation, red, green, blue, 1);
}
// Draw the scene.
function drawScene() {
    // Clear the canvas.
    gl.clear(gl.COLOR_BUFFER_BIT);

    // Set the translation.
    gl.uniform2fv(translationLocation, translation);
    // Set the rotation.
    gl.uniform2fv(rotationLocation, rotation);

    // Draw the geometry.
    gl.drawArrays(gl.TRIANGLES, 0, 6);
}


// Fill the buffer with the values that define a letter 'F'.
function setGeometry(gl) {
/*Assume size1 is declared*/
    var vertices = [
         -size1/2, -size1/2,
         -size1/2, size1/2,
         size1/2, size1/2,
         size1/2, size1/2,
         size1/2, -size1/2,
         -size1/2, -size1/2 ];
      gl.bufferData(
         gl.ARRAY_BUFFER,
         new Float32Array(vertices),
         gl.STATIC_DRAW);
}
function animate() {
    translation[0] += 0.01;
    translation[1] += 0.01;
    angle += 0.01;
    rotation[0] = Math.cos(angle);
    rotation[1] = Math.sin(angle);
}
function tick() {
    requestAnimFrame(tick);
    drawScene();
    animate();
}
function start() {

    initGL();
    initShaders();
    initBuffers();
    setColor(0.2, 0.5, 0.5);
    tick();
}

</script>

<!-- vertex shader -->
<script id="2d-vertex-shader" type="x-shader/x-vertex">
    attribute vec2 a_position;

    uniform vec2 u_resolution;
    uniform vec2 u_translation;
    uniform vec2 u_rotation;
    void main() {
        vec2 rotatedPosition = vec2(
        a_position.x * u_rotation.y + a_position.y * u_rotation.x,
        a_position.y * u_rotation.y - a_position.x * u_rotation.x);

       // Add in the translation.
       vec2 position = rotatedPosition + u_translation;

       // convert the position from pixels to 0.0 to 1.0
       vec2 zeroToOne = position / u_resolution;

       // convert from 0->1 to 0->2
       vec2 zeroToTwo = zeroToOne * 2.0;

       // convert from 0->2 to -1->+1 (clipspace)
       vec2 clipSpace = zeroToTwo - 1.0;

       gl_Position = vec4(clipSpace, 0, 1);
    }
</script>
<!-- fragment shader -->
<script id="2d-fragment-shader" type="x-shader/x-fragment">
    precision mediump float;

    uniform vec4 u_color;

    void main() {
       gl_FragColor = u_color;
    }
</script>

我的 1 个形状的 WebGL 程序的工作原理是这样的:

  1. 从画布元素中获取上下文 (gl)。
  2. 用我的对象的形状初始化缓冲区
  3. drawScene(): 调用gl.drawArrays()
  4. 如果有动画,一个更新函数,它会更新我的形状的角度、位置,然后drawScene()都在 中tick(),以便它被重复调用。

现在,当我需要超过 1 个形状时,我是否应该一次用许多对象填充单个缓冲区,然后使用它稍后调用drawScene()一次绘制所有对象 [OR] 如果我重复调用initBufferand drawScene()from requestAnimFrame().

4

3 回答 3

12

在伪代码中

在初始化时

  • 从画布元素中获取上下文 (gl)。
  • 对于每个着色器
    • 创建着色器
    • 查找属性和统一位置
  • 对于每个形状
    • 用形状初始化缓冲区
  • 对于每个纹理
    • 创建纹理和/或用数据填充它们。

抽奖时

  • 对于每个形状
    • 如果最后使用的着色器与此形状调用所需的着色器不同gl.useProgram
    • 对于着色器所需的每个属性
      • 调用gl.enableVertexAttribArraygl.bindBuffergl.vertexAttribPointer为形状所需的每个属性以及当前着色器的属性位置。
    • 对于着色器所需的每个制服
      • 使用当前着色器的位置调用gl.uniformXXX所需的值
    • 调用gl.drawArrays或者如果数据被索引调用gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, bufferOfIndicesForCurrentShape)后跟gl.drawElements

常见优化

1) 通常你不需要设置每一个制服。例如,如果您使用相同的着色器绘制 10 个形状,并且该着色器采用 viewMatrix 或 cameraMatrix,则 viewMatrix uniform 或 cameraMatrix uniform 对于每个形状都可能相同,因此只需设置一次。

2)您通常可以将调用移至gl.enableVertexAttribArray初始化时间。

于 2013-12-31T02:23:47.710 回答
3

在一个缓冲区中拥有多个网格(并按照您的建议使用单个渲染它们gl.drawArrays())在复杂场景中产生更好的性能,但显然此时您无法更改每个网格的着色器制服(例如变换)。

如果您想让网格独立运行,则必须分别渲染每个网格。您仍然可以将所有网格保存在一个缓冲区中以避免gl.bindBuffer()调用产生的一些开销,但恕我直言,这不会有太大帮助,至少在简单的场景中没有。

于 2012-10-22T20:04:39.557 回答
1

为场景中所需的每个对象分别创建缓冲区,否则它们将无法独立移动和使用着色器效果。

但那是在你的对象不同的情况下。从我在这里得到的信息来看,我认为您只想在不同的位置多次绘制相同的形状,对吗?

translationLocation这样做的方法是在第一次绘制形状后,使用不同的平移矩阵设置该制服。这样,当您再次绘制形状时,它将位于其他地方,而不是在另一个之上,因此您可以看到它。您可以不同地设置所有这些转换矩阵,然后gl.drawElements再次调用,因为您将绘制已在使用的相同缓冲区。

于 2012-10-23T02:55:08.303 回答