Trying to draw a strip of triangles as illustrated here:
Completed the objc.io tutorial, where they draw a quad using two triangles. The triangles are disconnected and drawn individually, meaning I need to specify 6 vertices instead of 4.
// Interleaved vertex data X,Y,Z,W, R,G,B,A
static float vertexData[] = {
// First triangle: From bottom right, clockwise
0.5, -0.5, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, // bottom right
-0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, // bottom left
-0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, // top left
// Second triangle: From top right, clockwise
0.5, 0.5, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, // top right
0.5, -0.5, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, // bottom right
-0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, // top left
};
Is there a way to draw a strip as in OpenGL ES without duplicating vertices?
The short answer is this:
renderEncoder.drawPrimitives(MTLPrimitiveType.TriangleStrip, vertexStart: 0, vertexCount: 6)
It's an equivalent to GL_TRIANGLE_STRIP
.
Also you may want to use indexed drawing, then you will only load each vertex once, and after that you will need to use array of vertex indices to specify draw order. That way you will save data by not specifying duplicated vertices.
Here's a call for indexed drawing.
renderEncoder.drawIndexedPrimitives(submesh.primitiveType, indexCount: submesh.indexCount, indexType: submesh.indexType, indexBuffer: submesh.indexBuffer.buffer, indexBufferOffset: submesh.indexBuffer.offset)
Cheers!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With