1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.0 (2010/01/01)
#include "Wm5GraphicsPCH.h"
#include "Wm5SkinController.h"
#include "Wm5Renderer.h"
#include "Wm5VertexBufferAccessor.h"
#include "Wm5Visual.h"
using namespace Wm5;
WM5_IMPLEMENT_RTTI(Wm5, Controller, SkinController);
WM5_IMPLEMENT_STREAM(SkinController);
WM5_IMPLEMENT_FACTORY(SkinController);
WM5_IMPLEMENT_DEFAULT_NAMES(Controller, SkinController);
//----------------------------------------------------------------------------
SkinController::SkinController (int numVertices, int numBones)
:
mNumVertices(numVertices),
mNumBones(numBones)
{
mBones = new1<Node*>(mNumBones);
mWeights = new2<float>(mNumBones, mNumVertices);
mOffsets = new2<APoint>(mNumBones, mNumVertices);
}
//----------------------------------------------------------------------------
SkinController::~SkinController ()
{
delete1(mBones);
delete2(mWeights);
delete2(mOffsets);
}
//----------------------------------------------------------------------------
bool SkinController::Update (double applicationTime)
{
if (!Controller::Update(applicationTime))
{
return false;
}
// Get access to the vertex buffer to store the blended targets.
Visual* visual = StaticCast<Visual>(mObject);
assertion(mNumVertices == visual->GetVertexBuffer()->GetNumElements(),
"Controller must have the same number of vertices as the buffer\n");
VertexBufferAccessor vba(visual);
// The skin vertices are calculated in the bone world coordinate system,
// so the visual's world transform must be the identity.
visual->WorldTransform = Transform::IDENTITY;
visual->WorldTransformIsCurrent = true;
// Compute the skin vertex locations.
for (int vertex = 0; vertex < mNumVertices; ++vertex)
{
APoint position = APoint::ORIGIN;
for (int bone = 0; bone < mNumBones; ++bone)
{
float weight = mWeights[vertex][bone];
if (weight != 0.0f)
{
APoint offset = mOffsets[vertex][bone];
APoint worldOffset = mBones[bone]->WorldTransform*offset;
position += weight*worldOffset;
}
}
vba.Position<Float3>(vertex) = position;
}
visual->UpdateModelSpace(Visual::GU_NORMALS);
Renderer::UpdateAll(visual->GetVertexBuffer());
return true;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Streaming support.
//----------------------------------------------------------------------------
SkinController::SkinController (LoadConstructor value)
:
Controller(value),
mNumVertices(0),
mNumBones(0),
mBones(0),
mWeights(0),
mOffsets(0)
{
}
//----------------------------------------------------------------------------
void SkinController::Load (InStream& source)
{
WM5_BEGIN_DEBUG_STREAM_LOAD(source);
Controller::Load(source);
source.Read(mNumVertices);
source.Read(mNumBones);
int numWeightsOffsets = mNumVertices*mNumBones;
mWeights = new2<float>(mNumBones, mNumVertices);
mOffsets = new2<APoint>(mNumBones, mNumVertices);
source.ReadVV(numWeightsOffsets, mWeights[0]);
source.ReadAggregateVV(numWeightsOffsets, mOffsets[0]);
source.ReadPointerVR(mNumBones, mBones);
WM5_END_DEBUG_STREAM_LOAD(SkinController, source);
}
//----------------------------------------------------------------------------
void SkinController::Link (InStream& source)
{
Controller::Link(source);
source.ResolveLink(mNumBones, mBones);
}
//----------------------------------------------------------------------------
void SkinController::PostLink ()
{
Controller::PostLink();
}
//----------------------------------------------------------------------------
bool SkinController::Register (OutStream& target) const
{
if (Controller::Register(target))
{
target.Register(mNumBones, mBones);
return true;
}
return false;
}
//----------------------------------------------------------------------------
void SkinController::Save (OutStream& target) const
{
WM5_BEGIN_DEBUG_STREAM_SAVE(target);
Controller::Save(target);
target.Write(mNumVertices);
target.Write(mNumBones);
int numWeightsOffsets = mNumVertices*mNumBones;
target.WriteN(numWeightsOffsets, mWeights[0]);
target.WriteAggregateN(numWeightsOffsets, mOffsets[0]);
target.WritePointerN(mNumBones, mBones);
WM5_END_DEBUG_STREAM_SAVE(SkinController, target);
}
//----------------------------------------------------------------------------
int SkinController::GetStreamingSize () const
{
int size = Controller::GetStreamingSize();
size += sizeof(mNumVertices);
size += sizeof(mNumBones);
int numWeightsOffsets = mNumVertices*mNumBones;
size += numWeightsOffsets*sizeof(mWeights[0][0]);
size += numWeightsOffsets*sizeof(mOffsets[0][0]);
size += mNumBones*WM5_POINTERSIZE(mBones[0]);
return size;
}
//----------------------------------------------------------------------------
|