gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:50:32 25 4
gpt4 key购买 nike

所以我从这里开始学习教程 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com