我正在嘗試direct3D,所以我畫了一些三角形,這工作得很好,它畫了3個黃色三角形。頂點顏色無法正常工作
但是現在我正在嘗試製作三角形,其中每個頂點具有不同的顏色並在三角形內插入顏色。屏幕上顯示的是三角形各邊的三個三角形有不同的顏色,但在三角形內部,一切都是白色的。
我留下了一些代碼,我認爲這與問題無關,如果有一個功能,你認爲問題可能會發生,只需詢問我將編輯帖子。
這是我得到的,你可以看到,只有三角形的邊得到一個顏色: http://puu.sh/kzuSn/4c8d0b01f0.png
這是我用它來創建三角形的代碼。
CPP文件:
#include "TrianglePosCol2D.h"
struct VertexPosCol
{
XMFLOAT3 Position;
XMFLOAT4 Color;
};
TrianglePosCol2D::TrianglePosCol2D(ID3D11Device* pD3DDevice)
:m_pD3DDevice(pD3DDevice)
,m_pVertexLayout(nullptr)
,m_pVertexBuffer(nullptr)
,m_pEffect(nullptr)
,m_pTechnique(nullptr)
,m_NumVertices(0)
{
}
TrianglePosCol2D::~TrianglePosCol2D(void)
{
if(m_pVertexBuffer) m_pVertexBuffer->Release();
if(m_pVertexLayout) m_pVertexLayout->Release();
if(m_pEffect) m_pEffect->Release();
}
HRESULT TrianglePosCol2D::Create(XMFLOAT3 p1, XMFLOAT3 p2, XMFLOAT3 p3, XMFLOAT4 c1, XMFLOAT4 c2, XMFLOAT4 c3)
{
HRESULT hr = S_OK;
// Compile the vertex shader
BuildEffect();
// Define the input layout
D3D11_INPUT_ELEMENT_DESC inputDesc[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT,0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0}
};
UINT numElements = sizeof(inputDesc)/sizeof(inputDesc[0]);
// Create the input layout
D3DX11_PASS_DESC passDesc = { 0 };
m_pTechnique->GetPassByIndex(0)->GetDesc(&passDesc);
hr = m_pD3DDevice->CreateInputLayout(
inputDesc,
numElements,
passDesc.pIAInputSignature,
passDesc.IAInputSignatureSize,
&m_pVertexLayout);
if (FAILED(hr))
{
return hr;
}
// Create vertex buffer
m_NumVertices = 3;
//VertexPosCol vertices[] = { (p1,p2,p3),(c1,c2,c3)};
VertexPosCol vertices[] = { { p1,c1 }, { p2,c2 }, { p3,c3 } };
//vertices.Position = (p1, p2, p3);
//vertices.Color = (c1, c2, c3);
D3D11_BUFFER_DESC bufferDesc = { 0 };
bufferDesc.Usage = D3D11_USAGE_IMMUTABLE;
bufferDesc.ByteWidth = sizeof(vertices)*m_NumVertices;
bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA initData = { 0 };
initData.pSysMem = &vertices[0];
hr = m_pD3DDevice->CreateBuffer(&bufferDesc, &initData, &m_pVertexBuffer);
return hr;
}
void TrianglePosCol2D::Paint(ID3D11DeviceContext* pDeviceContext)
{
// Set the input layout
pDeviceContext->IASetInputLayout(m_pVertexLayout);
// Set vertex buffer
UINT stride = sizeof(VertexPosCol);
UINT offset = 0;
pDeviceContext->IASetVertexBuffers(0, 1, &m_pVertexBuffer, &stride, &offset);
// Set primitive topology
pDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// Render a triangle
D3DX11_TECHNIQUE_DESC techDesc;
m_pTechnique->GetDesc(&techDesc);
for (UINT p = 0; p <techDesc.Passes; ++p)
{
m_pTechnique->GetPassByIndex(p)->Apply(0, pDeviceContext);
pDeviceContext->Draw(m_NumVertices, 0);
}
}
這是用於創建三角形的代碼。
HRESULT GP2_RenderingPipeline::GameInitialize()
{
HRESULT hr;
m_pColoredTriangle1 = new TrianglePosCol2D(m_pDevice);
hr = m_pColoredTriangle1->Create(XMFLOAT3(-1.f, -1.f, 0.99f), XMFLOAT3(-1.f, 1.f, 0.99f), XMFLOAT3(0.f, 1.f, 0.99f), XMFLOAT4(255.f, 0.f, 0.f, 0.f), XMFLOAT4(0.f, 255.f, 0.f, 0.f), XMFLOAT4(0.f, 0.f, 255.f, 0.f));
if (FAILED(hr))
{
MessageBox(nullptr, L"Create TrianglePos2D failed!", L"DirectX says NO", MB_ABORTRETRYIGNORE | MB_ICONERROR);
exit(-1);
}
m_pColoredTriangle2 = new TrianglePosCol2D(m_pDevice);
hr = m_pColoredTriangle2->Create(XMFLOAT3(-0.5f, 0.f, 0.99f), XMFLOAT3(0.5f, 0.f, .99f), XMFLOAT3(0.f, -1.f, 0.99f), XMFLOAT4(255.f, 0.f, 0.f, 0.f), XMFLOAT4(0.f, 255.f, 0.f, 0.f), XMFLOAT4(0.f, 0.f, 255.f, 0.f));
if (FAILED(hr))
{
MessageBox(nullptr, L"Create TrianglePos2D failed!", L"DirectX says NO", MB_ABORTRETRYIGNORE | MB_ICONERROR);
exit(-1);
}
m_pColoredTriangle3 = new TrianglePosCol2D(m_pDevice);
hr = m_pColoredTriangle3->Create(XMFLOAT3(0.f, 1.f, 0.99f), XMFLOAT3(1.f, 1.f, 0.99f), XMFLOAT3(1.f, -1.f, 0.99f), XMFLOAT4(255.f, 0.f, 0.f, 0.f), XMFLOAT4(0.f, 255.f, 0.f, 0.f), XMFLOAT4(0.f, 0.f, 255.f, 0.f));
if (FAILED(hr))
{
MessageBox(nullptr, L"Create TrianglePos2D failed!", L"DirectX says NO", MB_ABORTRETRYIGNORE | MB_ICONERROR);
exit(-1);
}
return S_OK;
}
我會嘗試一下,非常感謝你:D這總是很簡單的錯誤,讓我瀏覽幾個小時的答案。 – user3678781
我該如何接受?我似乎無法找到它的選項:s – user3678781
@ user3678781 - 檢查複選標記。並選擇向上箭頭,如果你發現答案是特別有用的(或一般的好答案)。 – Jeff