So I got this code to compute a normal. Here it is.
void ComputeNormal(const D3DXVECTOR3& p0,
const D3DXVECTOR3& p1,
const D3DXVECTOR3& p2,
D3DXVECTOR3& out)
{
D3DXVECTOR3 u = p1 - p0;
D3DXVECTOR3 v = p2 - p0;
D3DXVec3Cross(&out, &u, &v);
D3DXVec3Normalize(&out, &out);
}
And I got my drawing part of my directx right here. draws a pyramid
void LightingApp::DRAWMYPYRMAID()
{
D3DXVECTOR3 test;
ComputeNormal(D3DXVECTOR3(-2.0f, 0.0f, -2.0f),DD3DXVECTOR3(0.0f, 2.0f, 0.0f),3DXVECTOR3(2.0f, 0.0f, -2.0f),test);
Vertex vertices[] =
{
{ XMFLOAT3(-2.0f, 0.0f, 2.0f) },
{ XMFLOAT3(2.0f, 0.0f, 2.0f) },
{ XMFLOAT3(0.0f, 2.0f, 0.0f) },
{ XMFLOAT3(-2.0f, 0.0f, -2.0f) },
{ XMFLOAT3(2.0f, 0.0f, -2.0f), }
};
D3D11_BUFFER_DESC vbd;
vbd.Usage = D3D11_USAGE_IMMUTABLE;
vbd.ByteWidth = sizeof(Vertex) * 5;
vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vbd.CPUAccessFlags = 0;
vbd.MiscFlags = 0;
vbd.StructureByteStride = 0;
D3D11_SUBRESOURCE_DATA vinitData;
vinitData.pSysMem = vertices;
HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mBoxVB));
Vertex normal[] =
{
{ XMFLOAT3(test.x, test.y, test.z) }
};
D3D11_BUFFER_DESC vbds;
vbds.Usage = D3D11_USAGE_IMMUTABLE;
vbds.ByteWidth = sizeof(Vertex) * 1;
vbds.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vbds.CPUAccessFlags = 0;
vbds.MiscFlags = 0;
vbds.StructureByteStride = 0;
D3D11_SUBRESOURCE_DATA vinitDatas;
vinitDatas.pSysMem = normal;
HR(md3dDevice->CreateBuffer(&vbds, &vinitDatas, &mBoxVBs));
// Create the index buffer
UINT indices[] = {
0,1,4,
4,3,0,
0,2,1,
1,2,4,
4,2,3,
3,2,0
};
D3D11_BUFFER_DESC ibd;
ibd.Usage = D3D11_USAGE_IMMUTABLE;
ibd.ByteWidth = sizeof(UINT) * 18;
ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
ibd.CPUAccessFlags = 0;
ibd.MiscFlags = 0;
ibd.StructureByteStride = 0;
D3D11_SUBRESOURCE_DATA iinitData;
iinitData.pSysMem = indices;
HR(md3dDevice->CreateBuffer(&ibd, &iinitData, &mBoxIB));
}
Im getting two sides colored and the whole bottom colored. Im only calculating one normal for one side, so I only want one triangle to be colored. But im seeing two sides colored and the bottom colored. What am I messing up on in the code? how can I resolve this?