I seem to have an issue with my shader code. I an trying to work with animated skeleton meshes, and ran into trouble, so I changed my shader vertex function to work like my normal shader just with the original inputs. My simple cube is displaying completely wrong.
Here is my vertex shader:
cbuffer MatrixBuffer
{
matrix world;
matrix view;
matrix projection;
};
cbuffer SkinnedBuffer
{
float4x4 BoneTransforms[96];
};
struct VertexInput
{
float3 position : POSITION;
float2 tex : TEXCOORD0;
float3 normal : NORMAL;
float3 tangent : TANGENT;
float3 binormal : BINORMAL;
float3 weights : BLENDWEIGHT;
uint4 boneIndices : BLENDINDICES;
};
struct VertexOutput
{
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
float3 normal : NORMAL;
float3 tangent : TANGENT;
float3 binormal : BINORMAL;
};
VertexOutput main( VertexInput input )
{
VertexOutput output;
float4 pos = float4(input.position.xyz,1.0f);
output.position = mul(pos,world);
output.position = mul(output.position,view);
output.position = mul(output.position,projection);
// store the tex coords
output.tex = input.tex;
// calculate the normal vector against the world matrix only and then normalize the final value
output.normal = mul(input.normal,(float3x3)world);
output.normal = normalize(output.normal);
// calculate the tangents
output.tangent = mul(input.tangent,(float3x3)world);
output.tangent = normalize(output.tangent);
// calculate the binormals
output.binormal = mul(input.binormal,(float3x3)world);
output.binormal = normalize(output.binormal);
return output;
}
And here is the shader Initialization funciton:
struct ShaderBuffer
{
unsigned int Size;
void* Buffer;
ShaderBuffer() { Size = 0; Buffer = 0; }
};
ShaderBuffer vertexShaderBuffer, pixelShaderBuffer;
//
D3D11_INPUT_ELEMENT_DESC polygonLayout[7] = {
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 16, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TANGENT", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 36, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"BINORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 48, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"BLENDWEIGHT", 0, DXGI_FORMAT_R32G32B32_FLOAT,0, 60, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"BLENDINDICES", 0, DXGI_FORMAT_R8G8B8A8_UINT, 0, 72, D3D11_INPUT_PER_VERTEX_DATA, 0}
};
unsigned int numElements;
D3D11_SAMPLER_DESC samplerDesc;
ID3D11Device* device = graphics->GetDevice();
vertexShaderBuffer.Buffer = LoadCompiledShader(vsfilename,vertexShaderBuffer.Size);
pixelShaderBuffer.Buffer = LoadCompiledShader(psfilename,pixelShaderBuffer.Size);
if(FAILED(device->CreateVertexShader(vertexShaderBuffer.Buffer,vertexShaderBuffer.Size,0,&m_vertexShader)))
return false;
if(FAILED(device->CreatePixelShader(pixelShaderBuffer.Buffer,pixelShaderBuffer.Size,0,&m_pixelShader)))
return false;
//
numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
// create the vertex input layout
if(FAILED(device->CreateInputLayout(polygonLayout,numElements, vertexShaderBuffer.Buffer, vertexShaderBuffer.Size, &m_layout)))
return false;
delete [] vertexShaderBuffer.Buffer;
delete [] pixelShaderBuffer.Buffer;
m_matrixBuffer.Initialize(device);
m_lightBuffer.Initialize(device);
m_skinnedBuffer.Initialize(device);
// Create a texture sampler state description.
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.MipLODBias = 0.0f;
samplerDesc.MaxAnisotropy = 1;
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
samplerDesc.BorderColor[0] = 0;
samplerDesc.BorderColor[1] = 0;
samplerDesc.BorderColor[2] = 0;
samplerDesc.BorderColor[3] = 0;
samplerDesc.MinLOD = 0;
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
if(FAILED(device->CreateSamplerState(&samplerDesc,&m_samplerState)))
return false;
return true;
I think it is an alignment problem. When I debug it in visual studio 2012 and check the vertex shader input, the values are waaaay off.
Let me know if there is something I am missing here...
Thanks
EDIT: Also, my buffer initialization code looks like this:
// Make the constant buffer
D3D11_BUFFER_DESC desc;
desc.Usage = D3D11_USAGE_DYNAMIC;
desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
desc.MiscFlags = 0;
desc.ByteWidth = sizeof(T);
desc.StructureByteStride = 0;
device->CreateBuffer(&desc,0,&m_Buffer);
Edit2: My Vertex Structure
struct SkinnedVertex
{
DirectX::XMFLOAT3 position;
DirectX::XMFLOAT2 texture;
DirectX::XMFLOAT3 normal;
DirectX::XMFLOAT3 tangent;
DirectX::XMFLOAT3 binormal;
DirectX::XMFLOAT3 weights;
unsigned char boneIndices[4];
};