How to change the triangle in to a square

Viewed 10
1 Answers

Instead of drawing a single triangle, you draw TWO triangles that share two vertices. The key challenge is making sure you specify them with the correct winding order for your rendering setup.

    // Single multi-colored triangle
    static const Vertex s_vertexData[3] =
    {
        { { 0.0f,   0.5f,  0.5f, 1.0f },{ 1.0f, 0.0f, 0.0f, 1.0f } },  // Top / Red
        { { 0.5f,  -0.5f,  0.5f, 1.0f },{ 0.0f, 1.0f, 0.0f, 1.0f } },  // Right / Green
        { { -0.5f, -0.5f,  0.5f, 1.0f },{ 0.0f, 0.0f, 1.0f, 1.0f } }   // Left / Blue
    };

...
    context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
    context->Draw(3, 0);

SimpleTriangle sample on GitHub

    // Two triangles forming a quad with the same color at all corners
    static const Vertex s_vertexData[4] =
    {
        { { -0.5f, -0.5f, 0.5f, 1.0f }, { 0.f, 1.f } },
        { {  0.5f, -0.5f, 0.5f, 1.0f }, { 1.f, 1.f } },
        { {  0.5f,  0.5f, 0.5f, 1.0f }, { 1.f, 0.f } },
        { { -0.5f,  0.5f, 0.5f, 1.0f }, { 0.f, 0.f } },
    };

...

    context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
    context->Draw(4, 0);

SimpleTexture sample on GitHub

As you are new to DirectX, you may want to take a look at DirectX Tool Kit.

Related