2016-10-24 31 views
0

如何在webgl中使用子缓冲区绘制缓冲区? 例如,我有不同的观点来创建一条线,但包含了我不想连接的部分。使用子缓冲区绘制缓冲区webgl

///Example code 
    var lines = [ 
     [0, 0, 0, 1, 1, 1], 
     [2, 2, 2, 3, 3, 3], 
     [5, 5, 5, 7, 7, 7] 
    ]; 
    var colores = [ 
     [10, 43, 100, 1, ], 
     [0, 100, 0, 1], 
     [100, 100, 0, 1] 
    ] 
    for (var i = 0; i < lines.length; i++) { 
     lineVertexPositionBuffer[i] = gl.createBuffer(); 
     gl.bindBuffer(gl.ARRAY_BUFFER, lineVertexPositionBuffer[i]); 
     lineVertexPositionBuffer[i].itemSize = 3; 
     lineVertexPositionBuffer[i].numItems = line[i].length/3; 

     colorVertexBuffer[i] = gl.createBuffer(); 
     gl.bindBuffer(gl.ARRAY_BUFFER, colorVertexBuffer[i]); 
     gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colores[i]), gl.DYNAMIC_DRAW); 
     colorVertexBuffer[i].itemSize = 4; 
     colorVertexBuffer[i].numItems = colores[i].length/4; 

    } 

    function DrawScene() { 
     for (var i = 0; i < lineVertexPositionBuffer.length; i++) { 
      gl.bindBuffer(gl.ARRAY_BUFFER, colorVertexBuffer[i]); 
      gl.vertexAttribPointer(currentProgram.textureCoordAttribute, colorVertexBuffer[i].itemSize, gl.FLOAT, false, 0, 0); 
      gl.bindBuffer(gl.ARRAY_BUFFER, null); 

      gl.bindBuffer(gl.ARRAY_BUFFER, lineVertexPositionBuffer[i]); 
      gl.vertexAttribPointer(currentProgram.vertexPositionAttribute, 3, gl.FLOAT, false, 0, 0); 
      gl.drawArrays(gl.LINE_STRIP, 0, lineVertexPositionBuffer[i].numItems); 
      gl.bindBuffer(gl.ARRAY_BUFFER, null); 
     } 
    } 

回答

0

您可以使用gl.drawElements并提供索引列表,告诉GL实际使用哪些顶点。要允许通过单个调用画出多个段,可以使用gl.LINES。如果你有点p0..p6其中0,1,2和4,5连接你需要索引为0,1,1,2,4,5。基本上来自对的两个索引告诉gl连接哪些顶点线。你甚至可以做指标失灵像0,5,4,3,5,3

为了把顶点和颜色数据,以相同的缓冲您可以交错他们或把他们后对方​​喜欢

/* interleaved. Padding isn't mandator but it keeps math simpler 
    and GPUs often prefer cache aligned data. */ 
[x0, y0, z0, pad0, r0, g0, b0, a0, 
x1, y1, z1, pad1, r1, g1, b1, a1,...] 
gl.vertexAttribPointer(vertexLocation, 3, gl.FLOAT, false, 4*4*2, 0); 
gl.vertexAttribPointer(colorLocation, 4, gl.FLOAT, false, 4*4*2, 4*4); 

/* n positions followed by n colors */ 
[x0, y0, z0, pad0, 
x1, y1, z1, pad1, 
... 
r0, g0, b0, a0, 
r1, g1, b1, a1,...] /* to WebGLBuffer in ARRAY_BUFFER_BINDING */ 
gl.vertexAttribPointer(vertexLocation, 3, gl.FLOAT, false, 4*4, 0); 
gl.vertexAttribPointer(colorLocation, 4, gl.FLOAT, false, 4*4, 4*4*n); 

/* Index buffer to draw lines between vertices 0->1,1->3, 1->4 and 7->9 with gl.LINES */ 
[0, 1, 1, 3, 1, 4, 7, 9] /* to WebGLBuffer in ELEMENT_ARRAY_BUFFER_BINDING */ 

/* gl.drawArrays can't duplicate same drawing 
    because vertices are referenced out of order. drawElements 
    also allows using a vertex for multiple lines. */ 
gl.drawElements(gl.LINES, 4*2, GL_UNSIGNED_BYTE, 0); 

/* Alternative with only inorder but skiped vertices that can be duplicated with DrawArrays. 
    Connections are 0->1, 1->2, 4->5 and 6->7*/ 
[0, 1, 1, 2, 4, 5, 6, 7] /* to WebGLBuffer in ELEMENT_ARRAY_BUFFER_BINDING */ 
gl.drawElements(GL.LINES, 4*2, GL_UNSIGNED_BYTE, 0); 

/* Same rendering with multiple drawArrays calls */ 
gl.DrawArrays(gl.LINE_STRIP, 0, 3) /* Draw strip from 0 to 3 */ 
gl.DrawArrays(gl.LINES, 4, 2) /* Draw line between 4 and 5 */ 
gl.DrawArrays(gl.LINES, 6, 2) /* Draw line between 6 and 7 */ 

这能帮助你找到答案吗?

+0

是好的,但我需要这是动态的。因为这些行可能会更改,添加更多或删除它们。 – Julio

+0

@Julio您可以使用BufferSubData修改索引缓冲区和顶点缓冲区。但是索引缓冲区比顶点缓冲区小,因此分配新缓冲区并填充它比在运行时修改顶点便宜。但是我错误地理解了你的问题吗? –

0

目前尚不清楚你想要做什么。如果您的线条是动态的,只需将它们放入缓冲区并绘制,然后将新线条放入缓冲区并绘制,冲洗并重复。

var gl = document.querySelector("canvas").getContext("webgl", { 
 
    preserveDrawingBuffer: true, 
 
}); 
 

 
var program = twgl.createProgramFromScripts(gl, ["vs", "fs"]); 
 
var positionLoc = gl.getAttribLocation(program, "position"); 
 
var colorLoc = gl.getUniformLocation(program, "u_color"); 
 

 
var buffer = gl.createBuffer(); 
 

 
function putRandomLinesInBuffer(buf, numLines) { 
 
    var points = new Float32Array(numLines * 4); 
 
    for (var i = 0; i < numLines; ++i) { 
 
    points[i * 4 + 0] = Math.random() * 2 - 1; 
 
    points[i * 4 + 1] = Math.random() * 2 - 1; 
 
    points[i * 4 + 2] = Math.random() * 2 - 1; 
 
    points[i * 4 + 3] = Math.random() * 2 - 1; 
 
    } 
 
    gl.bindBuffer(gl.ARRAY_BUFFER, buf); 
 
    gl.bufferData(gl.ARRAY_BUFFER, points, gl.DYNAMIC_DRAW); 
 
} 
 

 
function render() { 
 
    var numLines = 5 + Math.random() * 10 | 0; 
 
    putRandomLinesInBuffer(buffer, numLines); 
 
    
 
    gl.useProgram(program); 
 
    
 
    gl.enableVertexAttribArray(positionLoc); 
 
    gl.bindBuffer(gl._ARRAY_BUFFER, buffer); 
 
    gl.vertexAttribPointer(positionLoc, 2, gl.FLOAT, false, 0, 0); 
 
    
 
    gl.uniform4f(colorLoc, Math.random(), Math.random(), Math.random(), 1); 
 
    
 
    gl.drawArrays(gl.LINES, 0, numLines * 2); 
 
    requestAnimationFrame(render); 
 
} 
 
requestAnimationFrame(render);
<script src="https://twgljs.org/dist/2.x/twgl.min.js"></script> 
 
<canvas></canvas> 
 
<script id="vs" type="foo"> 
 
attribute vec4 position; 
 
void main() { 
 
    gl_Position = position; 
 
} 
 
</script> 
 
<script id="fs" type="foo"> 
 
precision mediump float; 
 
uniform vec4 u_color; 
 
void main() { 
 
    gl_FragColor = u_color; 
 
} 
 
</script>

如果你不希望每次可以分配一个最大尺寸,并使用gl.bufferSubData虽然在我的经验gl.bufferData比在大多数情况下gl.bufferSubData更快地更换整个缓冲区。

此外,如果你还没有退房http://webglfundamentals.org