c++ - DirectX 11 制作一个正方形。

标签 c++ geometry directx

所以我从这里开始学习教程 http://www.rastertek.com/dx11tut04.html在第 4 部分。我试图在继续下一个教程之前完成所有练习。

除了将三角形更改为正方形外,我已经设法完成了所有这些操作。

起初我尝试添加第四个顶点用于位置和颜色并更改第四个顶点以绘制最后一个角和中间尖端(如教程中创建的那样)位于右上角,遵循顺时针绘制它们的规则.那没有用,所以我想起必须使用三角形绘制形状,所以我选择尝试使用两个三角形。第一次按预期绘制,第二次按预期绘制,每个都完成了半个正方形,但我无法让它们同时绘制。

我确保顺时针绘制它们并添加足够的索引以容纳所有顶点,但它不会将它们都绘制成一个正方形。我错过了什么吗?这是我正在使用的函数。

      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;
    }

最佳答案

关闭,但您的索引缓冲区不正确。由于您已将第二个三角形定义为顶点 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.

关于c++ - DirectX 11 制作一个正方形。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20412807/

相关文章:

基于到达特定点所需的最小线段数量评估像素的算法,同时仅穿过有效区域?

math - 查找三角形三点的外接圆心[不使用指南针]

c# - 快速多窗口渲染

WPF渲染卡住

windows-8 - Win8 应用程序认证失败 : 3. 10 - 如果您的应用程序包含 ARM 或 Neutral 程序包,则它必须支持 Microsoft Direct3D 功能级别 9_1

c++ - MSVCP140.dll 尽管/MT 运行时库设置

c++ - 这是 C++ 中的一个缺陷,即 std::get<T> ( const std::pair<const T, U>& ) 由于 const T 而无法编译吗?

C++,如何用另一个类的线程更改另一个类的变量

c++ - 从字符串序列中提取最后 2 个单词,以空格分隔

algorithm - 定位边界2D实体