2013-11-20 16:51:53 +01:00
|
|
|
#include "Render.h"
|
2013-12-18 20:28:06 +01:00
|
|
|
#include "../Resources/Deffered.h"
|
2013-11-21 18:31:16 +01:00
|
|
|
#include "../../Definitions/GraphicalDefinition.h"
|
2013-11-26 09:09:35 +01:00
|
|
|
#include "../../Model/ModelInfo.h"
|
2014-02-04 14:59:51 +01:00
|
|
|
#include "../../DllInterfaces/GFXAPI.h"
|
2013-12-05 14:56:34 +01:00
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
2013-11-20 16:51:53 +01:00
|
|
|
|
|
|
|
namespace Oyster
|
|
|
|
{
|
|
|
|
namespace Graphics
|
|
|
|
{
|
|
|
|
namespace Render
|
|
|
|
{
|
|
|
|
namespace Rendering
|
|
|
|
{
|
2013-12-18 20:28:06 +01:00
|
|
|
Definitions::Pointlight pl;
|
2014-02-04 14:59:51 +01:00
|
|
|
Model::Model* Basic::cube = NULL;
|
|
|
|
Model::Model* Basic::cube2 = NULL;
|
2013-11-20 16:51:53 +01:00
|
|
|
|
2013-12-18 20:28:06 +01:00
|
|
|
void Basic::NewFrame(Oyster::Math::Float4x4 View, Oyster::Math::Float4x4 Projection, Definitions::Pointlight* Lights, int numLights)
|
2013-11-20 16:51:53 +01:00
|
|
|
{
|
2013-11-28 14:34:52 +01:00
|
|
|
Preparations::Basic::ClearBackBuffer(Oyster::Math::Float4(1,0,0,1));
|
2014-01-22 16:31:33 +01:00
|
|
|
Preparations::Basic::ClearRTV(Resources::Deffered::GBufferRTV,Resources::Deffered::GBufferSize,Math::Float4(0,0,0,1));
|
2013-12-18 20:28:06 +01:00
|
|
|
Core::PipelineManager::SetRenderPass(Graphics::Render::Resources::Deffered::GeometryPass);
|
2013-11-21 18:31:16 +01:00
|
|
|
|
2014-01-27 14:24:13 +01:00
|
|
|
void* data;
|
2013-11-21 18:31:16 +01:00
|
|
|
|
2013-12-18 20:28:06 +01:00
|
|
|
Definitions::LightConstants lc;
|
|
|
|
lc.InvProj = Projection.GetInverse();
|
|
|
|
lc.Pixels = Core::resolution;
|
|
|
|
lc.Lights = numLights;
|
2013-12-18 20:36:41 +01:00
|
|
|
lc.View = View;
|
2014-01-08 07:01:59 +01:00
|
|
|
lc.Proj = Projection;
|
|
|
|
lc.SSAORadius = 3;
|
2013-12-18 20:28:06 +01:00
|
|
|
|
|
|
|
data = Resources::Deffered::LightConstantsData.Map();
|
|
|
|
memcpy(data, &lc, sizeof(Definitions::LightConstants));
|
|
|
|
Resources::Deffered::LightConstantsData.Unmap();
|
|
|
|
|
|
|
|
data = Resources::Deffered::PointLightsData.Map();
|
|
|
|
memcpy(data, Lights, sizeof(Definitions::Pointlight) * numLights);
|
|
|
|
Resources::Deffered::PointLightsData.Unmap();
|
2013-11-20 16:51:53 +01:00
|
|
|
}
|
2013-11-21 18:31:16 +01:00
|
|
|
|
2014-02-04 14:59:51 +01:00
|
|
|
Math::Matrix RecursiveBindPosRotation(int index, Model::ModelInfo* mi)
|
2014-02-03 12:09:11 +01:00
|
|
|
{
|
|
|
|
if(mi->bones[index].Parent == index)
|
2014-02-04 14:59:51 +01:00
|
|
|
return mi->bones[index].Relative;
|
|
|
|
|
|
|
|
return mi->bones[index].Relative*mi->bones[mi->bones->Parent].Relative;
|
|
|
|
}
|
2014-02-03 12:09:11 +01:00
|
|
|
|
2014-02-04 14:59:51 +01:00
|
|
|
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);
|
2014-02-03 12:09:11 +01:00
|
|
|
}
|
|
|
|
|
2014-01-08 07:01:59 +01:00
|
|
|
void Basic::RenderScene(Model::Model* models, int count, Math::Matrix View, Math::Matrix Projection)
|
2013-11-20 16:51:53 +01:00
|
|
|
{
|
2013-11-21 13:50:43 +01:00
|
|
|
for(int i = 0; i < count; ++i)
|
|
|
|
{
|
2014-01-16 09:30:01 +01:00
|
|
|
if(&models[i] == NULL)
|
|
|
|
continue;
|
2013-11-21 13:50:43 +01:00
|
|
|
if(models[i].Visible)
|
|
|
|
{
|
2014-01-08 07:01:59 +01:00
|
|
|
Definitions::PerModel pm;
|
|
|
|
pm.WV = View * models[i].WorldMatrix;
|
|
|
|
pm.WVP = Projection * pm.WV;
|
|
|
|
|
2013-12-18 20:28:06 +01:00
|
|
|
void* data = Resources::Deffered::ModelData.Map();
|
2014-01-08 07:01:59 +01:00
|
|
|
memcpy(data,&(pm),sizeof(pm));
|
2013-12-18 20:28:06 +01:00
|
|
|
Resources::Deffered::ModelData.Unmap();
|
2013-11-28 14:34:52 +01:00
|
|
|
|
|
|
|
Model::ModelInfo* info = (Model::ModelInfo*)models[i].info;
|
2014-01-27 14:24:13 +01:00
|
|
|
|
2014-02-03 12:09:11 +01:00
|
|
|
|
|
|
|
Definitions::AnimationData am; //final
|
2014-01-27 14:24:13 +01:00
|
|
|
if(info->Animated && models[i].AnimationPlaying != -1)
|
|
|
|
{
|
2014-02-07 08:34:01 +01:00
|
|
|
cube->WorldMatrix = Math::Matrix::identity;
|
2014-02-04 17:59:04 +01:00
|
|
|
////store inverse absolut transform
|
2014-02-05 16:00:28 +01:00
|
|
|
Math::Matrix SkinTransform[100];
|
|
|
|
Math::Matrix BoneAnimated[100];
|
|
|
|
Math::Matrix BoneAbsAnimated[100];
|
2014-02-04 14:59:51 +01:00
|
|
|
|
|
|
|
Math::Matrix Scale = Math::Matrix::identity;
|
2014-02-04 17:59:04 +01:00
|
|
|
Scale.m[0][0] = 1;
|
|
|
|
Scale.m[1][1] = 1;
|
|
|
|
Scale.m[2][2] = 2;
|
|
|
|
|
|
|
|
|
2014-02-04 14:59:51 +01:00
|
|
|
|
2014-02-03 12:09:11 +01:00
|
|
|
for(int b = 0; b <info->BoneCount; ++b)
|
2014-01-27 14:24:13 +01:00
|
|
|
{
|
2014-02-04 14:59:51 +01:00
|
|
|
Model::Bone Bone = info->bones[b];
|
|
|
|
SkinTransform[b] = Bone.Absolute.GetInverse();
|
|
|
|
BoneAnimated[b] = Bone.Relative;
|
|
|
|
BoneAbsAnimated[b] = Bone.Absolute;
|
2014-02-04 15:06:50 +01:00
|
|
|
|
|
|
|
|
2014-02-04 17:59:04 +01:00
|
|
|
cube2->WorldMatrix = Scale;
|
2014-02-04 15:06:50 +01:00
|
|
|
cube2->WorldMatrix.v[3] = info->bones[b].Absolute.v[3];
|
2014-02-04 17:59:04 +01:00
|
|
|
//Basic::RenderScene(cube2,1, View, Projection);
|
2014-02-04 14:59:51 +01:00
|
|
|
}
|
2014-02-05 16:00:28 +01:00
|
|
|
//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;
|
2014-02-04 17:59:04 +01:00
|
|
|
////for each bone in animation
|
|
|
|
////HACK use first bone
|
2014-02-05 16:00:28 +01:00
|
|
|
int b = 0;
|
|
|
|
Model::Animation A = info->Animations[models[i].AnimationPlaying];
|
|
|
|
while(models[i].AnimationTime>A.duration)
|
2014-02-07 08:34:01 +01:00
|
|
|
models[i].AnimationTime -= (float)A.duration;
|
2014-02-05 16:00:28 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2014-02-07 08:34:01 +01:00
|
|
|
float denominator = (float)(NFrame.time - PFrame.time);
|
2014-02-05 16:00:28 +01:00
|
|
|
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]);
|
|
|
|
}
|
2014-02-04 17:59:04 +01:00
|
|
|
|
|
|
|
////calculate Absolute Animation Transform
|
2014-02-04 14:59:51 +01:00
|
|
|
for(int b = 0; b < info->BoneCount; ++b)
|
|
|
|
{
|
|
|
|
BoneAbsAnimated[b] = BoneAbsAnimated[info->bones[b].Parent] * BoneAnimated[b];
|
2014-02-04 17:59:04 +01:00
|
|
|
//SkinTransform[b] = BoneAbsAnimated[b];
|
|
|
|
cube->WorldMatrix = Scale;
|
2014-02-04 14:59:51 +01:00
|
|
|
cube->WorldMatrix.v[3] = BoneAbsAnimated[b].v[3];
|
2014-02-07 08:34:01 +01:00
|
|
|
cube->WorldMatrix = models[i].WorldMatrix * cube->WorldMatrix;
|
2014-02-04 15:06:50 +01:00
|
|
|
Basic::RenderScene(cube,1,View,Projection);
|
2014-02-04 14:59:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//write data to am
|
|
|
|
for(int b = 0; b < info->BoneCount; ++b)
|
|
|
|
{
|
2014-02-05 16:00:28 +01:00
|
|
|
am.AnimatedData[b] = (BoneAbsAnimated[b] * SkinTransform[b]);
|
2014-01-27 14:24:13 +01:00
|
|
|
}
|
2014-02-04 14:59:51 +01:00
|
|
|
|
|
|
|
//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();
|
|
|
|
|
2014-02-05 16:00:28 +01:00
|
|
|
//delete[]SkinTransform;
|
|
|
|
//delete[]BoneAbsAnimated;
|
|
|
|
//delete[]BoneAnimated;
|
2014-02-04 17:59:04 +01:00
|
|
|
|
2014-01-27 14:24:13 +01:00
|
|
|
am.Animated = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
am.Animated = 0;
|
|
|
|
|
|
|
|
data = Resources::Deffered::AnimationData.Map();
|
|
|
|
memcpy(data,&am,sizeof(Definitions::AnimationData));
|
|
|
|
Resources::Deffered::AnimationData.Unmap();
|
|
|
|
|
2013-12-18 20:28:06 +01:00
|
|
|
if(info->Material.size())
|
|
|
|
{
|
2014-01-16 09:30:01 +01:00
|
|
|
Core::deviceContext->PSSetShaderResources(0,(UINT)info->Material.size(),&(info->Material[0]));
|
2013-12-18 20:28:06 +01:00
|
|
|
}
|
2013-11-21 13:50:43 +01:00
|
|
|
|
2013-11-26 09:09:35 +01:00
|
|
|
|
|
|
|
info->Vertices->Apply();
|
|
|
|
if(info->Indexed)
|
2013-11-21 13:50:43 +01:00
|
|
|
{
|
2013-11-26 09:09:35 +01:00
|
|
|
info->Indecies->Apply();
|
2014-01-17 08:51:12 +01:00
|
|
|
Oyster::Graphics::Core::deviceContext->DrawIndexed(info->IndexCount,0,0);
|
2013-11-21 13:50:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-04 14:59:51 +01:00
|
|
|
Oyster::Graphics::Core::deviceContext->Draw(info->VertexCount,0);
|
2013-11-21 13:50:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-20 16:51:53 +01:00
|
|
|
}
|
2014-01-27 14:24:13 +01:00
|
|
|
|
|
|
|
|
2013-11-20 16:51:53 +01:00
|
|
|
void Basic::EndFrame()
|
|
|
|
{
|
2013-12-18 20:28:06 +01:00
|
|
|
Core::PipelineManager::SetRenderPass(Resources::Deffered::LightPass);
|
|
|
|
|
2014-01-16 09:30:01 +01:00
|
|
|
Core::deviceContext->Dispatch((UINT)((Core::resolution.x + 15U) / 16U), (UINT)((Core::resolution.y + 15U) / 16U), 1);
|
2013-12-18 20:28:06 +01:00
|
|
|
|
2014-01-08 07:01:59 +01:00
|
|
|
Core::PipelineManager::SetRenderPass(Resources::Deffered::PostPass);
|
|
|
|
|
2014-01-16 09:30:01 +01:00
|
|
|
Core::deviceContext->Dispatch((UINT)((Core::resolution.x + 15U) / 16U), (UINT)((Core::resolution.y + 15U) / 16U), 1);
|
2014-01-08 07:01:59 +01:00
|
|
|
|
2013-12-18 20:28:06 +01:00
|
|
|
Core::swapChain->Present(0,0);
|
2013-11-20 16:51:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|