GteSkinController.cpp
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #include <GTEnginePCH.h>
10 #include <Graphics/GteNode.h>
11 #include <Graphics/GteVisual.h>
12 using namespace gte;
13 
15 {
16 }
17 
18 SkinController::SkinController(int numVertices, int numBones, Updater const& postUpdate)
19  :
20  mNumVertices(numVertices),
21  mNumBones(numBones),
22  mBones(numBones),
23  mWeights(numBones, numVertices),
24  mOffsets(numBones, numVertices),
25  mPostUpdate(postUpdate),
26  mPosition(nullptr),
27  mStride(0),
28  mFirstUpdate(true),
29  mCanUpdate(false)
30 {
31 }
32 
33 bool SkinController::Update(double applicationTime)
34 {
35  if (!Controller::Update(applicationTime))
36  {
37  return false;
38  }
39 
40  if (mFirstUpdate)
41  {
42  mFirstUpdate = false;
43  OnFirstUpdate();
44  }
45 
46  if (mCanUpdate)
47  {
48  // The skin vertices are calculated in the bone world coordinate system,
49  // so the visual's world transform must be the identity.
50  Visual* visual = reinterpret_cast<Visual*>(mObject);
52  visual->worldTransformIsCurrent = true;
53 
54  // Compute the skin vertex locations.
55  char* current = mPosition;
56  for (int vertex = 0; vertex < mNumVertices; ++vertex)
57  {
58  Vector4<float> position{ 0.0f, 0.0f, 0.0f, 0.0f };
59  for (int bone = 0; bone < mNumBones; ++bone)
60  {
61  float weight = mWeights[vertex][bone];
62  if (weight != 0.0f)
63  {
64  Vector4<float> offset = mOffsets[vertex][bone];
65 #if defined (GTE_USE_MAT_VEC)
66  Vector4<float> worldOffset =
67  mBones[bone].lock()->worldTransform * offset;
68 #else
69  Vector4<float> worldOffset =
70  offset * mBones[bone].lock()->worldTransform;
71 #endif
72  position += weight * worldOffset;
73  }
74  }
75 
76  Vector3<float>* target = reinterpret_cast<Vector3<float>*>(current);
77  (*target)[0] = position[0];
78  (*target)[1] = position[1];
79  (*target)[2] = position[2];
80  current += mStride;
81  }
82 
83  visual->UpdateModelBound();
84  visual->UpdateModelNormals();
85  mPostUpdate(visual->GetVertexBuffer());
86  return true;
87  }
88 
89  return false;
90 }
91 
93 {
94  // Get access to the vertex buffer positions to store the blended targets.
95  Visual* visual = reinterpret_cast<Visual*>(mObject);
96  VertexBuffer* vbuffer = visual->GetVertexBuffer().get();
97  if (mNumVertices == static_cast<int>(vbuffer->GetNumElements()))
98  {
99  // Get the position data.
100  VertexFormat vformat = vbuffer->GetFormat();
101  int const numAttributes = vformat.GetNumAttributes();
102  for (int i = 0; i < numAttributes; ++i)
103  {
104  VASemantic semantic;
105  DFType type;
106  unsigned int unit, offset;
107  if (vformat.GetAttribute(i, semantic, type, unit, offset))
108  {
109  if (semantic == VA_POSITION
110  && (type == DF_R32G32B32_FLOAT || type == DF_R32G32B32A32_FLOAT))
111  {
112  mPosition = vbuffer->GetData() + offset;
113  mStride = vformat.GetVertexSize();
114  mCanUpdate = true;
115  break;
116  }
117  }
118  }
119  }
120 
121  mCanUpdate = (mPosition != nullptr);
122 }
ControlledObject * mObject
Definition: GteController.h:71
unsigned int GetNumElements() const
Definition: GteResource.h:106
unsigned int GetVertexSize() const
Array2< float > mWeights
bool GetAttribute(int i, VASemantic &semantic, DFType &type, unsigned int &unit, unsigned int &offset) const
virtual bool Update(double applicationTime)
std::shared_ptr< VertexBuffer > const & GetVertexBuffer() const
Definition: GteVisual.h:77
GLuint GLuint GLfloat weight
Definition: glext.h:9668
std::vector< std::weak_ptr< Node > > mBones
bool UpdateModelBound()
Definition: GteVisual.cpp:27
GLenum target
Definition: glcorearb.h:1662
virtual bool Update(double applicationTime)
bool worldTransformIsCurrent
Definition: GteSpatial.h:51
VA_POSITION
Transform worldTransform
Definition: GteSpatial.h:50
VertexFormat const & GetFormat() const
DF_R32G32B32A32_FLOAT
Definition: GteDataFormat.h:20
Array2< Vector4< float > > mOffsets
char const * GetData() const
Definition: GteResource.h:151
std::function< void(std::shared_ptr< VertexBuffer > const &)> Updater
bool UpdateModelNormals()
Definition: GteVisual.cpp:50
GLfloat f
Definition: glcorearb.h:1921
int GetNumAttributes() const
static Transform const IDENTITY
Definition: GteTransform.h:157
GLintptr offset
Definition: glcorearb.h:660
DF_R32G32B32_FLOAT
Definition: GteDataFormat.h:20
SkinController(int numVertices, int numBones, Updater const &postUpdate)
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:103


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:01