GL_PRIMITIVE_RESTART Alternative

593 views
Skip to first unread message

John Bell

unread,
Nov 7, 2014, 5:59:05 PM11/7/14
to webgl-d...@googlegroups.com
Hello,

I am drawing a grid of vertices with TRIANGLE_STRIP. At the end of each row, i would call the assigned restart primitive.

WebGl does not support this. What alternatives are there to drawing a simple grid efficiently?


Old School
glGenBuffers(1, &uiVBOIndices);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, uiVBOIndices);
int iIndices[] =
{
0, 4, 1, 5, 2, 6, 3, 7, 16, // First row, then restart
4, 8, 5, 9, 6, 10, 7, 11, 16, // Second row, then restart
8, 12, 9, 13, 10, 14, 11, 15 // Third row, no restart
};
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(iIndices), iIndices, GL_STATIC_DRAW);
glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(HM_SIZE_X*HM_SIZE_Y);

New School
gridIndicesBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, gridIndicesBuffer);
var indices = [];
//Do stuff to indices
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
gridIndicesBuffer.itemSize = 1;
gridIndicesBuffer.numItems = indices.length;
gl.enable(gl.GL_PRIMITIVE_RESTART); 
gl.primitiveRestartIndex(totalIndices);
Any pointers here?

Regards,

Daniel

Kenneth Russell

unread,
Nov 7, 2014, 8:50:50 PM11/7/14
to webgl-d...@googlegroups.com
Have you tried creating a degenerate (zero-area) triangle to move to
the appropriate vertex of the first triangle in the next row?
> --
> You received this message because you are subscribed to the Google Groups
> "WebGL Dev List" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to webgl-dev-lis...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

John Bell

unread,
Nov 7, 2014, 9:50:16 PM11/7/14
to webgl-d...@googlegroups.com
Yeah,

I tried going back to the origin each time.

        var indices = [];
var totalIndices = (imgWidth*imgHeight);
for(var index=0;index<(totalIndices-imgWidth);index++)
{
indices.push(index);
indices.push(index+imgWidth);
if((((index)%(imgWidth))==0) &&(index>0))
{
indices.push(index+imgWidth);
}
}

Any tips?

Alecazam

unread,
Nov 8, 2014, 2:19:58 AM11/8/14
to webgl-d...@googlegroups.com
As Ken stated, just repeat the last index (or vertex), and repeat the first index (or vertex) of your next primitive.   That introduces a few degenerate tris, but those are quickly culled by the GPU.   I've never found primitive restart all that useful, but if you have indexed data then it's an easier way to restart the stream especially if you have numerous restarts.
Reply all
Reply to author
Forward
0 new messages