From 51711b86e3038f187697c698f11e2156adca1d0c Mon Sep 17 00:00:00 2001 From: lanariel Date: Wed, 5 Feb 2014 16:00:28 +0100 Subject: [PATCH] Fixed Animate Skeleton, TODO Fix Skinning --- .../Definitions/GraphicalDefinition.h | 4 +- .../Render/Rendering/BasicRender.cpp | 68 +++-- .../HLSL/Deffered Shaders/GBufferHeader.hlsli | 6 +- .../HLSL/Deffered Shaders/PostPass.hlsl | 8 +- .../Deffered Shaders/VertexGatherData.hlsl | 18 +- .../~AutoRecover.OysterGraphics.vcxproj | 287 ++++++++++++++++++ Code/Tester/MainTest.cpp | 17 +- 7 files changed, 368 insertions(+), 40 deletions(-) create mode 100644 Code/OysterGraphics/~AutoRecover.OysterGraphics.vcxproj diff --git a/Code/OysterGraphics/Definitions/GraphicalDefinition.h b/Code/OysterGraphics/Definitions/GraphicalDefinition.h index d84f7506..93273b30 100644 --- a/Code/OysterGraphics/Definitions/GraphicalDefinition.h +++ b/Code/OysterGraphics/Definitions/GraphicalDefinition.h @@ -51,7 +51,9 @@ namespace Oyster struct AnimationData { - Math::Float4x4 animatedData[100]; + Math::Float4x4 AnimatedData[100]; + Math::Float4x4 BindPoseData[100]; + Math::Float4x4 RotationData[100]; int Animated; Math::Float3 Pad; }; diff --git a/Code/OysterGraphics/Render/Rendering/BasicRender.cpp b/Code/OysterGraphics/Render/Rendering/BasicRender.cpp index 8c3e1565..015a1fdb 100644 --- a/Code/OysterGraphics/Render/Rendering/BasicRender.cpp +++ b/Code/OysterGraphics/Render/Rendering/BasicRender.cpp @@ -84,9 +84,9 @@ namespace Oyster { cube->WorldMatrix == Math::Matrix::identity; ////store inverse absolut transform - Math::Matrix* SkinTransform = new Math::Matrix[info->BoneCount]; - Math::Matrix* BoneAnimated = new Math::Matrix[info->BoneCount]; - Math::Matrix* BoneAbsAnimated = new Math::Matrix[info->BoneCount]; + Math::Matrix SkinTransform[100]; + Math::Matrix BoneAnimated[100]; + Math::Matrix BoneAbsAnimated[100]; Math::Matrix Scale = Math::Matrix::identity; Scale.m[0][0] = 1; @@ -107,28 +107,41 @@ namespace Oyster cube2->WorldMatrix.v[3] = info->bones[b].Absolute.v[3]; //Basic::RenderScene(cube2,1, View, Projection); } - BoneAnimated[8] = Math3D::RotationMatrix(3.14/4, Math::Float3(0, 0, 1)) * info->bones[8].Relative; - BoneAnimated[31] = Math3D::RotationMatrix(3.14/4, Math::Float3(0, 0, 1)) * info->bones[31].Relative; + //BoneAnimated[8] = Math3D::RotationMatrix(3.14/4, Math::Float3(0, 0, 1)) * info->bones[8].Relative; + //BoneAnimated[31] = Math3D::RotationMatrix(3.14/4, Math::Float3(0, 0, 1)) * info->bones[31].Relative; ////for each bone in animation ////HACK use first bone - //int b = 0; - //Model::Animation A = info->Animations[models[i].AnimationPlaying]; - //for(int b = 0; b < A.Bones;++b) - //{ - // //for each frame on bone Write current relative data - // //HACK use first frame - // int f = 0; - // //for(int f = 0; f < A.Frames[b]; ++b) - // { - // //find right frame - // //HACK accept first - // Model::Frame Current = A.Keyframes[b][f]; - // - // //calculate new matrix - // Model::Bone CBone = Current.bone; - // BoneAnimated[CBone.Parent] = CBone.Relative; - // } - //} + int b = 0; + Model::Animation A = info->Animations[models[i].AnimationPlaying]; + while(models[i].AnimationTime>A.duration) + models[i].AnimationTime -= A.duration; + + float position = models[i].AnimationTime; + for(int b = 0; b < A.Bones;++b) + { + //find current frame + int nrOfFrames = A.Frames[b]; + Model::Frame PFrame = A.Keyframes[b][nrOfFrames-1]; + Model::Frame NFrame = A.Keyframes[b][nrOfFrames-1]; + bool FrameFound = false; + for (int i = 0; i < nrOfFrames; i++) + { + if(position < A.Keyframes[b][i].time) + { + PFrame = A.Keyframes[b][i-1]; + NFrame = A.Keyframes[b][i]; + break; + } + } + float denominator = (NFrame.time - PFrame.time); + if(denominator == 0) + { + BoneAnimated[PFrame.bone.Parent] = PFrame.bone.Relative; + continue; + } + float inter = (float)((position - PFrame.time) / denominator); + Math3D::InterpolateOrientation_UsingNonRigidNlerp(PFrame.bone.Relative,NFrame.bone.Relative,inter, BoneAnimated[PFrame.bone.Parent]); + } ////calculate Absolute Animation Transform for(int b = 0; b < info->BoneCount; ++b) @@ -143,7 +156,8 @@ namespace Oyster //write data to am for(int b = 0; b < info->BoneCount; ++b) { - am.animatedData[b] = SkinTransform[b] * BoneAbsAnimated[b]; + am.AnimatedData[b] = (BoneAbsAnimated[b] * SkinTransform[b]); + am.BindPoseData[b] = info->bones[b].Absolute;//Math3D::ExtractRotationMatrix(am.animatedData[b]); } //retore to draw animated model @@ -155,9 +169,9 @@ namespace Oyster memcpy(data,&(pm),sizeof(pm)); Resources::Deffered::ModelData.Unmap(); - delete[]SkinTransform; - delete[]BoneAbsAnimated; - delete[]BoneAnimated; + //delete[]SkinTransform; + //delete[]BoneAbsAnimated; + //delete[]BoneAnimated; am.Animated = 1; } diff --git a/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/GBufferHeader.hlsli b/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/GBufferHeader.hlsli index f28c1b0b..bca4ca25 100644 --- a/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/GBufferHeader.hlsli +++ b/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/GBufferHeader.hlsli @@ -5,7 +5,7 @@ struct VertexIn float3 normal : NORMAL; float3 tangent : TANGENT; float3 biTangent : BITANGENT; - float4 boneIndex : BONEINDEX; + int4 boneIndex : BONEINDEX; float4 boneWeight : BONEWEIGHT; }; @@ -32,7 +32,9 @@ SamplerState S1 : register(s0); cbuffer Animation : register(b0) { - float4x4 BoneAnimation[100]; + float4x4 AnimatedData[100]; + float4x4 BindPoseData[100]; + float4x4 RotationData[100]; int Animated; float3 Pad; } diff --git a/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/PostPass.hlsl b/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/PostPass.hlsl index f984ff9f..36e25514 100644 --- a/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/PostPass.hlsl +++ b/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/PostPass.hlsl @@ -4,10 +4,14 @@ Texture2D Ambient : register(t2); RWTexture2D Output; +#define AmbFactor 0.3f; + [numthreads(16, 16, 1)] void main( uint3 DTid : SV_DispatchThreadID ) { - //Output[DTid.xy] = Diffuse[DTid.xy] + Specular[DTid.xy] + Diffuse[DTid.xy] * Ambient[DTid.xy/2].w;// + float4(Ambient[DTid.xy/4].xyz,1); GLOW - Output[DTid.xy] = float4(Ambient[DTid.xy/2].xyz /* * Ambient[DTid.xy/2].w */, 1); + float4 Light = Diffuse[DTid.xy] + Specular[DTid.xy]; + float4 Amb = float4(Ambient[DTid.xy/2].xyz * Ambient[DTid.xy/2].w,1); + //Output[DTid.xy] = float4(Ambient[DTid.xy/2].xyz /* * Ambient[DTid.xy/2].w */, 1); //Output[DTid.xy] = Diffuse[DTid.xy] + Specular[DTid.xy]; + Output[DTid.xy] = Light + Amb * AmbFactor; } \ No newline at end of file diff --git a/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/VertexGatherData.hlsl b/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/VertexGatherData.hlsl index afb84931..ca0f31fb 100644 --- a/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/VertexGatherData.hlsl +++ b/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/VertexGatherData.hlsl @@ -3,12 +3,20 @@ VertexOut main( VertexIn input ) { VertexOut output; - Matrix boneTrans = BoneAnimation[input.boneIndex.x]*input.boneWeight.x + - BoneAnimation[input.boneIndex.y]*input.boneWeight.y + - BoneAnimation[input.boneIndex.z]*input.boneWeight.z + - BoneAnimation[input.boneIndex.w]*input.boneWeight.w; - input.pos = mul(boneTrans, input.pos) + input.pos * int(1-Animated);; + float3 offsetX = input.pos - BindPoseData[input.boneIndex.x][3].xyz; + float3 offsetY = input.pos - BindPoseData[input.boneIndex.y][3].xyz; + float3 offsetZ = input.pos - BindPoseData[input.boneIndex.z][3].xyz; + float3 offsetW = input.pos - BindPoseData[input.boneIndex.w][3].xyz; + + Matrix boneTrans = AnimatedData[input.boneIndex.x]*input.boneWeight.x + + AnimatedData[input.boneIndex.y]*input.boneWeight.y + + AnimatedData[input.boneIndex.z]*input.boneWeight.z + + AnimatedData[input.boneIndex.w]*input.boneWeight.w; + + input.pos = mul(boneTrans,input.pos) * Animated + input.pos * int(1-Animated); + + input.normal = mul(boneTrans,input.normal) * Animated + input.normal * int(1-Animated); /*input.pos = (mul(BoneAnimation[input.boneIndex.x], input.pos) * input.boneWeight.x * Animated) + (mul(BoneAnimation[input.boneIndex.y], input.pos) * input.boneWeight.y * Animated) + diff --git a/Code/OysterGraphics/~AutoRecover.OysterGraphics.vcxproj b/Code/OysterGraphics/~AutoRecover.OysterGraphics.vcxproj new file mode 100644 index 00000000..53ffc005 --- /dev/null +++ b/Code/OysterGraphics/~AutoRecover.OysterGraphics.vcxproj @@ -0,0 +1,287 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {0EC83E64-230E-48EF-B08C-6AC9651B4F82} + OysterGraphics + + + + DynamicLibrary + true + v110 + MultiByte + + + DynamicLibrary + true + v110 + MultiByte + + + DynamicLibrary + false + v110 + true + MultiByte + + + DynamicLibrary + false + v110 + true + MultiByte + + + + + + + + + + + + + + + + + + + $(SolutionDir)..\Bin\DLL\ + $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ + $(ProjectName)_$(PlatformShortName)D + C:\Program Files (x86)\Visual Leak Detector\include;$(IncludePath) + C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(LibraryPath) + + + $(SolutionDir)..\Bin\DLL\ + $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ + $(ProjectName)_$(PlatformShortName) + C:\Program Files (x86)\Visual Leak Detector\include;$(IncludePath) + C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(LibraryPath) + + + $(SolutionDir)..\Bin\DLL\ + $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ + $(ProjectName)_$(PlatformShortName)D + C:\Program Files (x86)\Visual Leak Detector\include;$(IncludePath) + C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath) + + + $(SolutionDir)..\Bin\DLL\ + $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ + $(ProjectName)_$(PlatformShortName) + C:\Program Files (x86)\Visual Leak Detector\include;$(IncludePath) + C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath) + + + + Level3 + Disabled + true + $(SolutionDir)OysterMath;$(SolutionDir)Misc;%(AdditionalIncludeDirectories) + GFX_DLL_EXPORT;%(PreprocessorDefinitions) + + + true + + + true + + + $(SolutionDir)..\Bin\Content\Shaders\%(Filename).cso + 5.0 + + + + + Level3 + Disabled + true + ..\OysterPhysic3D\Collision;..\OysterPhysics3D;..\OysterMath;..\Misc;%(AdditionalIncludeDirectories) + GFX_DLL_EXPORT;%(PreprocessorDefinitions) + + + true + + + $(SolutionDir)..\Bin\Content\Shaders\%(Filename).cso + 5.0 + + + + + Level3 + Disabled + true + true + true + ..\OysterPhysics3D;..\OysterMath;..\Misc;%(AdditionalIncludeDirectories) + GFX_DLL_EXPORT;%(PreprocessorDefinitions) + + + true + true + true + + + $(SolutionDir)..\Bin\Content\Shaders\%(Filename).cso + true + 5.0 + + + + + Level3 + Disabled + true + true + true + ..\OysterPhysics3D;..\OysterMath;..\Misc;%(AdditionalIncludeDirectories) + GFX_DLL_EXPORT;%(PreprocessorDefinitions) + + + true + true + true + + + $(SolutionDir)..\Bin\Content\Shaders\%(Filename).cso + true + 5.0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {2ec4dded-8f75-4c86-a10b-e1e8eb29f3ee} + + + {f10cbc03-9809-4cba-95d8-327c287b18ee} + + + + + Compute + Compute + Compute + Compute + + + Pixel + Pixel + Pixel + Pixel + + + Compute + 5.0 + Compute + 5.0 + Compute + 5.0 + Compute + 5.0 + + + Vertex + Vertex + Vertex + Vertex + + + Vertex + Vertex + Vertex + Vertex + + + + + 5.0 + + + Pixel + Pixel + Pixel + Pixel + true + 5.0 + main + + + Vertex + Vertex + Vertex + Vertex + true + 5.0 + main + + + + + Pixel + Pixel + Pixel + Pixel + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Code/Tester/MainTest.cpp b/Code/Tester/MainTest.cpp index 13bd1b94..279f89f2 100644 --- a/Code/Tester/MainTest.cpp +++ b/Code/Tester/MainTest.cpp @@ -160,7 +160,7 @@ HRESULT InitDirect3D() } m = Oyster::Graphics::API::CreateModel(L"untitled.dan"); - m2 = Oyster::Graphics::API::CreateModel(L"still_root_origo.dan"); + m2 = Oyster::Graphics::API::CreateModel(L"rigidbind_animtest_arms_bindpose (1).dan"); m2->WorldMatrix = Oyster::Math3D::OrientationMatrix(Oyster::Math::Float3::null,Oyster::Math::Float3(0,0,0),Oyster::Math::Float3::null); m2->AnimationPlaying = 0; m2->AnimationTime = 0.0f; @@ -179,7 +179,7 @@ HRESULT InitDirect3D() Oyster::Graphics::Definitions::Pointlight pl; pl.Color = Oyster::Math::Float3(1,1,1); pl.Bright = 1; - pl.Pos = Oyster::Math::Float3(0,-20.0f,30.4f); + pl.Pos = Oyster::Math::Float3(0,-20.0f,0.4f); pl.Radius = 90; Oyster::Graphics::API::AddLight(pl); @@ -198,7 +198,7 @@ HRESULT Update(float deltaTime) //ma *= 50; //ma.m44 = 1; //m2->WorldMatrix = m2->WorldMatrix * ma; - m2->AnimationTime += deltaTime * 0.5f; + //m2->AnimationTime += deltaTime * 0.5f; //m3->WorldMatrix = Oyster::Math3D::OrientationMatrix(Oyster::Math::Float3(1,0,0)*-0,Oyster::Math::Float3(3,4,-1*angle),Oyster::Math::Float3::null); return S_OK; } @@ -249,6 +249,17 @@ LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam Oyster::Graphics::API::ReloadShaders(); #endif break; + //Z - + case 0x5A: + m2->AnimationTime -= 0.1f; + if(m2->AnimationTime < 0) + m2->AnimationTime = 0; + break; + //X + + case 0x58: + m2->AnimationTime += 0.1f; + break; + } break;