From 97a3792bffe8fe7d9fa55b1014a6c1fb4df562d8 Mon Sep 17 00:00:00 2001 From: lanariel Date: Tue, 4 Feb 2014 14:59:51 +0100 Subject: [PATCH] Anim Test Not working Joints --- Code/OysterGraphics/DllInterfaces/GFXAPI.cpp | 2 + Code/OysterGraphics/FileLoader/DanLoader.cpp | 3 +- .../FileLoader/ShaderLoader.cpp | 3 +- Code/OysterGraphics/Model/ModelInfo.h | 3 +- .../Render/Rendering/BasicRender.cpp | 83 +++++++++++++++++-- Code/OysterGraphics/Render/Rendering/Render.h | 3 + .../HLSL/Deffered Shaders/PostPass.hlsl | 2 +- .../Deffered Shaders/VertexGatherData.hlsl | 16 ++-- Code/Tester/MainTest.cpp | 3 +- 9 files changed, 97 insertions(+), 21 deletions(-) diff --git a/Code/OysterGraphics/DllInterfaces/GFXAPI.cpp b/Code/OysterGraphics/DllInterfaces/GFXAPI.cpp index 2d9c3dc6..ad010c8b 100644 --- a/Code/OysterGraphics/DllInterfaces/GFXAPI.cpp +++ b/Code/OysterGraphics/DllInterfaces/GFXAPI.cpp @@ -32,6 +32,8 @@ namespace Oyster Render::Resources::Deffered::Init(); Render::Preparations::Basic::SetViewPort(); + Render::Rendering::Basic::cube = API::CreateModel(L"box.dan"); + Render::Rendering::Basic::cube2 = API::CreateModel(L"box2.dan"); return API::Sucsess; } diff --git a/Code/OysterGraphics/FileLoader/DanLoader.cpp b/Code/OysterGraphics/FileLoader/DanLoader.cpp index 0eff143a..0dd76a06 100644 --- a/Code/OysterGraphics/FileLoader/DanLoader.cpp +++ b/Code/OysterGraphics/FileLoader/DanLoader.cpp @@ -397,8 +397,7 @@ void* Oyster::Graphics::Loading::LoadDAN(const wchar_t filename[]) anims[a].Keyframes[b][f].bone.Parent = boneIndex; //read bone transform - ReadData(&anims[a].Keyframes[b][f].bone.Transform,danFile,sizeof(Oyster::Math::Matrix)); - + ReadData(&anims[a].Keyframes[b][f].bone.Relative, danFile, sizeof(Math::Matrix)); ReadData(&anims[a].Keyframes[b][f].time,danFile,sizeof(double)); } diff --git a/Code/OysterGraphics/FileLoader/ShaderLoader.cpp b/Code/OysterGraphics/FileLoader/ShaderLoader.cpp index 17e9d1fe..5edc86dd 100644 --- a/Code/OysterGraphics/FileLoader/ShaderLoader.cpp +++ b/Code/OysterGraphics/FileLoader/ShaderLoader.cpp @@ -143,8 +143,7 @@ namespace Oyster } else { - memset(&out,0,sizeof(out)); - return; + return NULL; } #endif return Core::PipelineManager::CreateShader(data, Core::PipelineManager::ShaderType(type)); diff --git a/Code/OysterGraphics/Model/ModelInfo.h b/Code/OysterGraphics/Model/ModelInfo.h index 3a184f65..dece6668 100644 --- a/Code/OysterGraphics/Model/ModelInfo.h +++ b/Code/OysterGraphics/Model/ModelInfo.h @@ -13,7 +13,8 @@ namespace Oyster { struct Bone { - Math::Float4x4 Transform; + Math::Matrix Relative; + Math::Matrix Absolute; int Parent; }; struct Frame diff --git a/Code/OysterGraphics/Render/Rendering/BasicRender.cpp b/Code/OysterGraphics/Render/Rendering/BasicRender.cpp index e9f988d2..d5f7a676 100644 --- a/Code/OysterGraphics/Render/Rendering/BasicRender.cpp +++ b/Code/OysterGraphics/Render/Rendering/BasicRender.cpp @@ -2,6 +2,7 @@ #include "../Resources/Deffered.h" #include "../../Definitions/GraphicalDefinition.h" #include "../../Model/ModelInfo.h" +#include "../../DllInterfaces/GFXAPI.h" #include #include @@ -14,6 +15,8 @@ namespace Oyster namespace Rendering { Definitions::Pointlight pl; + Model::Model* Basic::cube = NULL; + Model::Model* Basic::cube2 = NULL; void Basic::NewFrame(Oyster::Math::Float4x4 View, Oyster::Math::Float4x4 Projection, Definitions::Pointlight* Lights, int numLights) { @@ -40,12 +43,21 @@ namespace Oyster Resources::Deffered::PointLightsData.Unmap(); } - Math::Matrix RecursiveBindPos(int index, Model::ModelInfo* mi) + Math::Matrix RecursiveBindPosRotation(int index, Model::ModelInfo* mi) { if(mi->bones[index].Parent == index) - return mi->bones[index].Transform; + return mi->bones[index].Relative; + + return mi->bones[index].Relative*mi->bones[mi->bones->Parent].Relative; + } - return mi->bones[index].Transform.GetInverse() * RecursiveBindPos(mi->bones[index].Parent,mi); + Math::Vector4 RecursiveBindPosPosition(int index, Model::ModelInfo* mi) + { + //return Math::Vector4::standard_unit_w; + if(mi->bones[index].Parent == index) + return mi->bones[index].Relative.v[3]; + + return Math::Vector4(RecursiveBindPosPosition(mi->bones->Parent, mi).xyz + (mi->bones[index].Relative.v[3] * RecursiveBindPosRotation(mi->bones->Parent,mi)).xyz,1); } void Basic::RenderScene(Model::Model* models, int count, Math::Matrix View, Math::Matrix Projection) @@ -70,10 +82,71 @@ namespace Oyster Definitions::AnimationData am; //final if(info->Animated && models[i].AnimationPlaying != -1) { + //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 Scale = Math::Matrix::identity; + Scale.m[1][1] = 0.1f; + Scale.m[2][2] = 0.1f; + Scale.m[3][3] = 2; + for(int b = 0; b BoneCount; ++b) { - am.animatedData[b] = RecursiveBindPos(b,info); + Model::Bone Bone = info->bones[b]; + SkinTransform[b] = Bone.Absolute.GetInverse(); + BoneAnimated[b] = Bone.Relative; + BoneAbsAnimated[b] = Bone.Absolute; } + //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; + } + } + + //calculate Absolute Animation Transform + for(int b = 0; b < info->BoneCount; ++b) + { + BoneAbsAnimated[b] = BoneAbsAnimated[info->bones[b].Parent] * BoneAnimated[b]; + cube->WorldMatrix = BoneAbsAnimated[b] * Scale; + cube->WorldMatrix.v[3] = BoneAbsAnimated[b].v[3]; + //Basic::RenderScene(cube,1,View,Projection); + } + + //write data to am + for(int b = 0; b < info->BoneCount; ++b) + { + am.animatedData[b] = BoneAbsAnimated[b] * SkinTransform[b]; + cube2->WorldMatrix = Scale; + cube2->WorldMatrix.v[3] = info->bones[b].Absolute.v[3]; + Basic::RenderScene(cube2,1,View,Projection); + } + + //retore to draw animated model + Definitions::PerModel pm; + pm.WV = View * models[i].WorldMatrix; + pm.WVP = Projection * pm.WV; + + void* data = Resources::Deffered::ModelData.Map(); + memcpy(data,&(pm),sizeof(pm)); + Resources::Deffered::ModelData.Unmap(); + am.Animated = 1; } else @@ -97,7 +170,7 @@ namespace Oyster } else { - Oyster::Graphics::Core::deviceContext->Draw(info->VertexCount,0); + Oyster::Graphics::Core::deviceContext->Draw(info->VertexCount,0); } } } diff --git a/Code/OysterGraphics/Render/Rendering/Render.h b/Code/OysterGraphics/Render/Rendering/Render.h index 48d153d4..f8ac7aed 100644 --- a/Code/OysterGraphics/Render/Rendering/Render.h +++ b/Code/OysterGraphics/Render/Rendering/Render.h @@ -19,6 +19,9 @@ namespace Oyster static void NewFrame(Oyster::Math::Float4x4 View, Oyster::Math::Float4x4 Projection, Definitions::Pointlight* Lights, int numLights); static void RenderScene(Model::Model* models, int count, Math::Matrix View, Math::Matrix Projection); static void EndFrame(); + + static Model::Model* cube; + static Model::Model* cube2; }; } } diff --git a/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/PostPass.hlsl b/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/PostPass.hlsl index 1b00d607..f984ff9f 100644 --- a/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/PostPass.hlsl +++ b/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/PostPass.hlsl @@ -8,6 +8,6 @@ RWTexture2D Output; 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); + Output[DTid.xy] = float4(Ambient[DTid.xy/2].xyz /* * Ambient[DTid.xy/2].w */, 1); //Output[DTid.xy] = Diffuse[DTid.xy] + Specular[DTid.xy]; } \ 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 7395e08d..cbe657f7 100644 --- a/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/VertexGatherData.hlsl +++ b/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/VertexGatherData.hlsl @@ -3,16 +3,14 @@ VertexOut main( VertexIn input ) { VertexOut output; - /*input.pos = ( - (mul(BoneAnimation[input.boneIndex.x], input.pos) * input.boneWeight.x) + - (mul(BoneAnimation[input.boneIndex.y], input.pos) * input.boneWeight.y) + - (mul(BoneAnimation[input.boneIndex.z], input.pos) * input.boneWeight.z) + - (mul(BoneAnimation[input.boneIndex.w], input.pos) * input.boneWeight.w) - * Animated) + input.pos * 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) + + (mul(BoneAnimation[input.boneIndex.z], input.pos) * input.boneWeight.z * Animated) + + (mul(BoneAnimation[input.boneIndex.w], input.pos) * input.boneWeight.w * Animated) + + input.pos * int(1-Animated);*/ - input.pos = ( - (mul(BoneAnimation[input.boneIndex.x], input.pos)/* * input.boneWeight.x*/) - * Animated) + input.pos * int(1-Animated); + /*input.pos = mul(BoneAnimation[input.boneIndex.x], input.pos) * Animated + input.pos * int(1-Animated);*/ //float4x4 m = matrix(float4(1,0,0,0),float4(0,1,0,0), float4(0,0,1,0), float4(0,0,0,1)); //input.pos = mul(BoneAnimation[0], float4(input.pos,1)); diff --git a/Code/Tester/MainTest.cpp b/Code/Tester/MainTest.cpp index d3f63c48..13bd1b94 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.dan"); + m2 = Oyster::Graphics::API::CreateModel(L"still_root_origo.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; @@ -169,6 +169,7 @@ HRESULT InitDirect3D() P = Oyster::Math3D::ProjectionMatrix_Perspective(Oyster::Math::pi/2,1280.0f/720.0f,.1f,10000); + //P = Oyster::Math3D::ProjectionMatrix_Orthographic(10,10,.1f,10000); Oyster::Graphics::API::SetProjection(P); V = Oyster::Math3D::OrientationMatrix_LookAtDirection(Oyster::Math::Float3(0,0,-1),Oyster::Math::Float3(0,1,0),Oyster::Math::Float3(0,0,5.4f));