iamjustaprogrammer
iamjustaprogrammer

Reputation: 1664

What is the equivalent of GL_TRIANGLE_STRIP in Metal for iOS?

Trying to draw a strip of triangles as illustrated here:

GL_TRIANGLE_STRIP

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?

Upvotes: 8

Views: 2793

Answers (1)

haawa
haawa

Reputation: 3104

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!

Upvotes: 8

Related Questions