2016-10-24 175 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