DirectX 11制作正方形。

时间:2013-12-05 23:16:43

标签: c++ geometry directx

所以我在第4部分关注了这里的教程http://www.rastertek.com/dx11tut04.html。我正在尝试完成所有练习,然后再继续学习下一个教程。

除了将三角形改成正方形外,我已成功完成所有这些操作。

首先,我尝试为位置和颜色添加第四个顶点,然后更改第四个顶点以绘制最后一个角和中间尖端(因为它在教程中创建)为右上角,遵循顺时针绘制它们的规则。这没用,所以我记得必须使用三角形绘制形状,所以我选择尝试使用两个三角形。第一次按预期绘制,第二次按预期绘制,每次完成一个正方形的一半,但我无法让它们同时绘制。

我确保顺时针绘制它们并添加足够的索引以容纳所有顶点,它只是不会将它们绘制成方形。我错过了什么吗?这是我正在工作的功能。

      bool ModelClass::InitializeBuffers(ID3D11Device* device)
    {
VertexType* vertices;
unsigned long* indices;
D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
    D3D11_SUBRESOURCE_DATA vertexData, indexData;
HRESULT result;


// Set the number of vertices in the vertex array.
m_vertexCount = 6;

// Set the number of indices in the index array.
m_indexCount = 6;

// Create the vertex array.
vertices = new VertexType[m_vertexCount];
if(!vertices)
{
    return false;
}

// Create the index array.
indices = new unsigned long[m_indexCount];
if(!indices)
{
    return false;
}

// Load the vertex array with data.

vertices[0].position = D3DXVECTOR3(-1.0f, -1.0f, 0.0f);  // Bottom left.
vertices[0].color = D3DXVECTOR4(1.0f, 0.0f, 0.0f, 1.0f);

vertices[1].position = D3DXVECTOR3(-1.0f, 1.0f, 0.0f);  // Top left.
vertices[1].color = D3DXVECTOR4(1.0f, 0.0f, 0.0f, 1.0f);

vertices[2].position = D3DXVECTOR3(1.0f, 1.0f, 0.0f);  // top right.
vertices[2].color = D3DXVECTOR4(1.0f, 0.0f, 0.0f, 1.0f);

vertices[3].position = D3DXVECTOR3(-1.0f, -1.0f, 0.0f);  // Bottom left.
vertices[3].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);

vertices[4].position = D3DXVECTOR3(1.0f, 1.0f, 0.0f);  // top right.
vertices[4].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);

vertices[5].position = D3DXVECTOR3(1.0f, -1.0f, 0.0f);  // Bottom right.
vertices[5].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);

// Load the index array with data.
indices[0] = 0;  // Bottom left.
indices[1] = 1;  // Top left.
indices[2] = 2;  // top right.

indices[3] = 0;  // Bottom left.
indices[4] = 1;  // Top left.
indices[5] = 2;  // top right.
// Set up the description of the static vertex buffer.
    vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
    vertexBufferDesc.ByteWidth = sizeof(VertexType) * m_vertexCount;
    vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vertexBufferDesc.CPUAccessFlags = 0;
    vertexBufferDesc.MiscFlags = 0;
vertexBufferDesc.StructureByteStride = 0;

// Give the subresource structure a pointer to the vertex data.
    vertexData.pSysMem = vertices;
vertexData.SysMemPitch = 0;
vertexData.SysMemSlicePitch = 0;

// Now create the vertex buffer.
    result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);
if(FAILED(result))
{
    return false;
}

// Set up the description of the static index buffer.
    indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
    indexBufferDesc.ByteWidth = sizeof(unsigned long) * m_indexCount;
    indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
    indexBufferDesc.CPUAccessFlags = 0;
    indexBufferDesc.MiscFlags = 0;
indexBufferDesc.StructureByteStride = 0;

// Give the subresource structure a pointer to the index data.
    indexData.pSysMem = indices;
indexData.SysMemPitch = 0;
indexData.SysMemSlicePitch = 0;

// Create the index buffer.
result = device->CreateBuffer(&indexBufferDesc, &indexData, &m_indexBuffer);
if(FAILED(result))
{
    return false;
}

// Release the arrays now that the vertex and index buffers have been created and loaded.
delete [] vertices;
vertices = 0;

delete [] indices;
indices = 0;

return true;
    }

1 个答案:

答案 0 :(得分:3)

关闭,但您的索引缓冲区不正确。由于您已将第二个三角形定义为顶点3,4和5,因此您的索引需要匹配:

indices[0] = 0;  // Bottom left.
indices[1] = 1;  // Top left.
indices[2] = 2;  // top right.

indices[3] = 3;  // Bottom left.
indices[4] = 4;  // Top left.
indices[5] = 5;  // top right.

或者,由于您已经在使用DrawIndexed,因此可以消除重复的顶点:

vertices[0].position = D3DXVECTOR3(-1.0f, -1.0f, 0.0f);  // Bottom left.
vertices[0].color = D3DXVECTOR4(1.0f, 0.0f, 0.0f, 1.0f);

vertices[1].position = D3DXVECTOR3(-1.0f, 1.0f, 0.0f);  // Top left.
vertices[1].color = D3DXVECTOR4(1.0f, 0.0f, 0.0f, 1.0f);

vertices[2].position = D3DXVECTOR3(1.0f, 1.0f, 0.0f);  // top right.
vertices[2].color = D3DXVECTOR4(1.0f, 0.0f, 0.0f, 1.0f);

vertices[3].position = D3DXVECTOR3(1.0f, -1.0f, 0.0f);  // Bottom right.
vertices[3].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f);

// Load the index array with data.
indices[0] = 0;  // Bottom left.
indices[1] = 1;  // Top left.
indices[2] = 2;  // top right.

indices[3] = 0;  // Bottom left.
indices[4] = 2;  // Top right.
indices[5] = 3;  // Bottom right.