Hey Guys!
So i keep getting this flickering effect whit my animated models! but the flickering is random and i doubt it has anything to do whit the actual animation. So through some research i came to the conclusion that it might be because of the constant buffer data that i am sending over to the GPU where the animations are actually happening. Since i am using D3D11_MAP_WRITE_DISCARD when calling the map/unmap functions i think it might not filling in all of the data. I do know that the flickering does stop when i do not update the constant buffer. which proves that the bug most likely is in the updating of the constant buffer. Only problem is i do not know what data i am not filling in or how i might be updating the buffer wrong.
Here is the code where i feel in the structure( BufferAnimStruct ) that is going to be used for the constant buffer
Animation* Current_Animation = _Model->GetAnimation(_Model->GetCurrentAnimation());
Mesh* TempMesh = _Model->GetMesh();
vector<FLOAT4X4> CurrentPoses = ProcessChannels(Current_Animation, _DT);
BufferAnimStruct ConstStruct = _Model->GetAnimInfo();
for (unsigned int i = 0; i < TempMesh->The_Joints.size(); i++)
{
FLOAT4X4 Temp;
FLOAT4X4 Bind_Pose = Current_Animation->The_Joints[i].bind_pose_transform;
Temp = Math::GetInstance()->FLOAT4X4Inverse(Bind_Pose);
ConstStruct.InverseBindPose[i] = Math::GetInstance()->FLOAT4X4ToXMFLOAT4X4(Temp);
Temp = CurrentPoses[i];
ConstStruct.Pose[i] = Math::GetInstance()->FLOAT4X4ToXMFLOAT4X4(Temp);
}
_Model->SetAnimInfo(ConstStruct);
and here is the code when i update the buffer
D3D11_MAPPED_SUBRESOURCE m_SR1 = {};
Renderer::GetInstance()->GetContext()->Map(m_bAnimConstBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &m_SR1);
BufferAnimStruct* BufferAnim = (BufferAnimStruct*)m_SR1.pData;
memcpy(m_SR1.pData, &m_bAnimInfo, sizeof(BufferAnimStruct));
Renderer::GetInstance()->GetContext()->Unmap(m_bAnimConstBuffer, 0);
and last but not least here is the code in the vertex shader where the vertexs are moved according to the poses sent through the constant buffer.
cbuffer AnimationInfo : register(b0)
{
float4x4 InverseBind[30];
float4x4 Pose[30];
}
OUTPUT_VERTEX main(INPUT_VERTEX fromVertexBuffer)
{
OUTPUT_VERTEX sendToRasterizer = (OUTPUT_VERTEX)0;
float4 localH = float4(fromVertexBuffer.coordinate, 1);
float4 vSum = float4(0, 0, 0, 0);
int Num = fromVertexBuffer.Number_Influences.x;
if (Num > 0)
{
for (int i = 0; i < Num; i++)
{
int JointIndex = fromVertexBuffer.JointIndex[i];
float4 offset = mul(localH, InverseBind[JointIndex]);
float4 Result = mul(offset, Pose[JointIndex]);
Result *= fromVertexBuffer.Weight.x;
vSum += Result;
}
localH = vSum;
}
localH = mul(localH, worldMatrix);
localH = mul(localH, viewMatrix);
localH = mul(localH, projectionMatrix);
sendToRasterizer.projectedCoordinate = localH;
return sendToRasterizer;
}
Thanks for the help.