What's the equivalent of GL_TRIANGLE_STRIP in Metal for iOS?

Trying to draw a strip of triangles as shown here:

GL_TRIANGLE_STRIP

Completed the objc.io tutorial where they draw a quad using two triangles. The triangles are detached 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 stripe like in OpenGL ES without duplicating vertices?

+3


source to share


1 answer


The short answer is this:

renderEncoder.drawPrimitives(MTLPrimitiveType.TriangleStrip, vertexStart: 0, vertexCount: 6)

      

This is equivalent GL_TRIANGLE_STRIP

.

Also you can use an indexed drawing, then you will only load each vertex once, and then you will need to use an array of vertex indices to specify the drawing order. This way you save the data without specifying duplicate vertices.



The indexed drawing is called here.

renderEncoder.drawIndexedPrimitives(submesh.primitiveType, indexCount: submesh.indexCount, indexType: submesh.indexType, indexBuffer: submesh.indexBuffer.buffer, indexBufferOffset: submesh.indexBuffer.offset)

      

Hooray!

+5


source







All Articles