This repository has been archived by the owner on Dec 31, 2020. It is now read-only.
/
ModelTargetSkinnedVboMesh.cpp
130 lines (105 loc) · 4.2 KB
/
ModelTargetSkinnedVboMesh.cpp
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
#include "ModelTargetSkinnedVboMesh.h"
#include "Skeleton.h"
#include "SkinnedVboMesh.h"
namespace model {
ModelTargetSkinnedVboMesh::ModelTargetSkinnedVboMesh( SkinnedVboMesh * mesh )
: mSkinnedVboMesh( mesh )
, mSubDataOffset(0)
, mAttribLocation(0)
{
}
void ModelTargetSkinnedVboMesh::incrementOffsets( size_t dataSize )
{
mSubDataOffset += dataSize;
mAttribLocation++;
}
template<class T>
void ModelTargetSkinnedVboMesh::bufferSubData( const T& buffer, size_t dataSize )
{
ci::gl::VboMesh& vboMesh = mSkinnedVboMesh->getActiveSection()->getVboMesh();
vboMesh.getStaticVbo().bufferSubData( mSubDataOffset, dataSize, buffer.data() );
vboMesh.getStaticVbo().unbind();
}
void ModelTargetSkinnedVboMesh::setCustomAttribute( const std::string& name, GLuint location )
{
MeshVboSectionRef sect = mSkinnedVboMesh->getActiveSection();
sect->getShader().bind();
sect->getVboMesh().setCustomStaticLocation( location, sect->getShader().getAttribLocation( name ) );
sect->getShader().unbind();
}
void ModelTargetSkinnedVboMesh::setActiveSection( int index )
{
mSkinnedVboMesh->setActiveSection( index );
mSubDataOffset = 0;
mAttribLocation = 0;
}
std::shared_ptr<Skeleton> ModelTargetSkinnedVboMesh::getSkeleton() const
{
return mSkinnedVboMesh->getSkeleton();
}
void ModelTargetSkinnedVboMesh::loadVertexPositions( const std::vector<ci::Vec3f>& positions )
{
size_t dataSize = sizeof(GLfloat) * ci::Vec3f::DIM * positions.size();
bufferSubData< std::vector<ci::Vec3f> >( positions, dataSize );
setCustomAttribute( "position", mAttribLocation );
incrementOffsets( dataSize );
}
void ModelTargetSkinnedVboMesh::loadVertexNormals( const std::vector<ci::Vec3f>& normals )
{
mSkinnedVboMesh->getActiveSection()->setHasNormals( true ); //FIXME: remove this
size_t dataSize = sizeof(GLfloat) * ci::Vec3f::DIM * normals.size();
bufferSubData< std::vector<ci::Vec3f> >( normals, dataSize );
setCustomAttribute( "normal", mAttribLocation );
incrementOffsets( dataSize );
}
void ModelTargetSkinnedVboMesh::loadIndices( const std::vector<uint32_t>& indices )
{
ci::gl::VboMesh& vboMesh = mSkinnedVboMesh->getActiveSection()->getVboMesh();
vboMesh.bufferIndices( indices );
vboMesh.unbindBuffers();
}
void ModelTargetSkinnedVboMesh::loadTex( const std::vector<ci::Vec2f>& texCoords, const MaterialInfo& matInfo )
{
mSkinnedVboMesh->getActiveSection()->setMatInfo( matInfo );
size_t dataSize = sizeof(GLfloat) * ci::Vec2f::DIM * texCoords.size();
bufferSubData< std::vector<ci::Vec2f> >( texCoords, dataSize);
setCustomAttribute( "texcoord", mAttribLocation );
incrementOffsets( dataSize );
}
void ModelTargetSkinnedVboMesh::loadSkeleton( const SkeletonRef& skeleton )
{
if( skeleton->getNumBones() > SkinnedVboMesh::MAXBONES )
throw "Skeleton has more bones than maximal the number allowed.";
mSkinnedVboMesh->setSkeleton( skeleton );
}
void ModelTargetSkinnedVboMesh::loadBoneWeights( const std::vector<BoneWeights>& boneWeights )
{
std::vector<ci::Vec4f> boneWeightsBuffer;
std::vector<ci::Vec4f> boneIndicesBuffer;
for( const auto& boneWeight : boneWeights ) {
ci::Vec4f vWeights = ci::Vec4f::zero();
ci::Vec4f vIndices = ci::Vec4i::zero();
for( unsigned int b =0; b < boneWeight.mActiveNbWeights; ++b ) {
NodeRef bone = boneWeight.getBone( b );
vWeights[b] = boneWeight.getWeight(b);
//FIXME: Maybe use ints on the desktop?
vIndices[b] = bone->getBoneIndex();
}
boneWeightsBuffer.push_back( vWeights );
boneIndicesBuffer.push_back( vIndices );
}
size_t dataSize = sizeof(GLfloat) * ci::Vec4f::DIM * boneWeightsBuffer.size();
bufferSubData< std::vector<ci::Vec4f> >( boneWeightsBuffer, dataSize );
setCustomAttribute( "boneWeights", mAttribLocation );
incrementOffsets( dataSize );
bufferSubData< std::vector<ci::Vec4f> >( boneIndicesBuffer, dataSize);
setCustomAttribute( "boneIndices", mAttribLocation );
incrementOffsets( dataSize );
mSkinnedVboMesh->getActiveSection()->boneMatrices = &mSkinnedVboMesh->mBoneMatrices;
mSkinnedVboMesh->getActiveSection()->invTransposeMatrices = &mSkinnedVboMesh->mInvTransposeMatrices;
}
void ModelTargetSkinnedVboMesh::loadDefaultTransformation( const ci::Matrix44f& transformation )
{
mSkinnedVboMesh->setDefaultTransformation( transformation );
}
} //end namespace model