Начинающий
-
Автор темы
- #1
Я умею рисовать прямоугольник, треугольник, квадрат ( 2D ). Как создать 3D объект ?
Вот пример кода:
Вот пример кода:
C++:
void Graphics::DrawSquare(Vec4Float color)
{
struct VertexPosColor
{
DirectX::XMFLOAT3 Position;
DirectX::XMFLOAT4 Color;
};
VertexPosColor g_Vertices[] =
{
{ DirectX::XMFLOAT3(-0.5f, 0.5f, 0.0f), DirectX::XMFLOAT4(color.r,color.g,color.b,color.a)}, // Top-left
{ DirectX::XMFLOAT3(0.5f, 0.5f, 0.0f), DirectX::XMFLOAT4(color.r,color.g,color.b,color.a) }, // Top-right
{ DirectX::XMFLOAT3(-0.5f, -0.5f, 0.0f), DirectX::XMFLOAT4(color.r,color.g,color.b,color.a) }, // Bottom-left
{ DirectX::XMFLOAT3(0.5f, -0.5f, 0.0f),DirectX::XMFLOAT4(color.r,color.g,color.b,color.a) }
};
WORD g_Indicies[] =
{
0, 1, 2, // Triangle 1 (top-left, top-right, bottom-left)
2, 1, 3 // Triangle 2 (bottom-left, top-right, bottom-right)
};
// Create vertex buffer
D3D11_BUFFER_DESC desc = {};
desc.Usage = D3D11_USAGE_DEFAULT;
desc.ByteWidth = sizeof(Vertex3D) * std::size(g_Vertices);
desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
desc.CPUAccessFlags = 0;
desc.StructureByteStride = sizeof(VertexPosColor);
D3D11_SUBRESOURCE_DATA data = {};
data.pSysMem = g_Vertices;
ID3D11Buffer* vertexBuffer = nullptr;
this->Device()->CreateBuffer(&desc, &data, &vertexBuffer);
// Create Index Buffer
D3D11_BUFFER_DESC indexBufferDesc;
ZeroMemory(&indexBufferDesc, sizeof(D3D11_BUFFER_DESC));
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexBufferDesc.ByteWidth = sizeof(WORD) * _countof(g_Indicies);
indexBufferDesc.CPUAccessFlags = 0;
indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
D3D11_SUBRESOURCE_DATA indexData = {};
indexData.pSysMem = g_Indicies;
ID3D11Buffer* pIndexBuffer = nullptr;
if (FAILED(Device()->CreateBuffer(&indexBufferDesc, &indexData, &pIndexBuffer)))
{
Logger.DebugLog("Failed to create Index Buffer\n");
}
// Create shaders
ID3D11VertexShader* vertexShader = nullptr;
ID3D11PixelShader* pixelShader = nullptr;
ID3D10Blob* vertexShaderBlob = nullptr;
D3DReadFileToBlob(L"VertexShader.cso", &vertexShaderBlob);
device->CreateVertexShader(vertexShaderBlob->GetBufferPointer(), vertexShaderBlob->GetBufferSize(), nullptr, &vertexShader);
ID3D10Blob* pixelShaderBlob = nullptr;
D3DReadFileToBlob(L"PixelShader.cso", &pixelShaderBlob);
device->CreatePixelShader(pixelShaderBlob->GetBufferPointer(), pixelShaderBlob->GetBufferSize(), nullptr, &pixelShader);
// Set shaders
this->Context()->VSSetShader(vertexShader, nullptr, 0);
this->Context()->PSSetShader(pixelShader, nullptr, 0);
// Set input layout
ID3D11InputLayout* inputLayout = nullptr;
D3D11_INPUT_ELEMENT_DESC layoutDesc[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }
};
if (FAILED(this->Device()->CreateInputLayout(layoutDesc, ARRAYSIZE(layoutDesc), vertexShaderBlob->GetBufferPointer(), vertexShaderBlob->GetBufferSize(), &inputLayout)))
{
Logger.DebugLog("Failed to create input layout\n");
vertexBuffer->Release();
vertexShaderBlob->Release();
pixelShaderBlob->Release();
return;
}
this->Context()->IASetInputLayout(inputLayout);
// Set vertex buffer
UINT stride = sizeof(VertexPosColor);
UINT offset = 0;
this->Context()->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
// Set primitive topology
this->Context()->IASetIndexBuffer(pIndexBuffer, DXGI_FORMAT_R16_UINT, 0);
this->Context()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
this->Context()->OMSetRenderTargets(1, &renderTargetView, depthStencilView);
this->Context()->DrawIndexed(std::size(g_Indicies), 0, 0);
// Cleanup resources
vertexBuffer->Release();
inputLayout->Release();
vertexShader->Release();
pixelShader->Release();
vertexShaderBlob->Release();
pixelShaderBlob->Release();
}