mirror of
https://github.com/JoeyDeVries/LearnOpenGL.git
synced 2026-01-30 20:13:22 +08:00
Another source code update on skeletal animation article.
This commit is contained in:
@@ -12,6 +12,8 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
#define MAX_BONE_INFLUENCE 4
|
||||||
|
|
||||||
struct Vertex {
|
struct Vertex {
|
||||||
// position
|
// position
|
||||||
glm::vec3 Position;
|
glm::vec3 Position;
|
||||||
@@ -23,6 +25,10 @@ struct Vertex {
|
|||||||
glm::vec3 Tangent;
|
glm::vec3 Tangent;
|
||||||
// bitangent
|
// bitangent
|
||||||
glm::vec3 Bitangent;
|
glm::vec3 Bitangent;
|
||||||
|
//bone indexes which will influence this vertex
|
||||||
|
int m_BoneIDs[MAX_BONE_INFLUENCE];
|
||||||
|
//weights from each bone
|
||||||
|
float m_Weights[MAX_BONE_INFLUENCE];
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Texture {
|
struct Texture {
|
||||||
@@ -127,7 +133,13 @@ private:
|
|||||||
// vertex bitangent
|
// vertex bitangent
|
||||||
glEnableVertexAttribArray(4);
|
glEnableVertexAttribArray(4);
|
||||||
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Bitangent));
|
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Bitangent));
|
||||||
|
// ids
|
||||||
|
glEnableVertexAttribArray(5);
|
||||||
|
glVertexAttribIPointer(5, 4, GL_INT, sizeof(Vertex), (void*)offsetof(Vertex, m_BoneIDs));
|
||||||
|
|
||||||
|
// weights
|
||||||
|
glEnableVertexAttribArray(6);
|
||||||
|
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, m_Weights));
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -48,14 +48,14 @@ public:
|
|||||||
meshes[i].Draw(shader);
|
meshes[i].Draw(shader);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto& GetOffsetMatMap() { return m_OffsetMatMap; }
|
auto& GetBoneInfoMap() { return m_BoneInfoMap; }
|
||||||
int& GetBoneCount() { return m_BoneCount; }
|
int& GetBoneCount() { return m_BoneCounter; }
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
std::map<string, BoneInfo> m_OffsetMatMap;
|
std::map<string, BoneInfo> m_BoneInfoMap;
|
||||||
int m_BoneCount = 0;
|
int m_BoneCounter = 0;
|
||||||
|
|
||||||
// loads a model with supported ASSIMP extensions from file and stores the resulting meshes in the meshes vector.
|
// loads a model with supported ASSIMP extensions from file and stores the resulting meshes in the meshes vector.
|
||||||
void loadModel(string const &path)
|
void loadModel(string const &path)
|
||||||
@@ -97,7 +97,7 @@ private:
|
|||||||
|
|
||||||
void SetVertexBoneDataToDefault(Vertex& vertex)
|
void SetVertexBoneDataToDefault(Vertex& vertex)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < MAX_BONE_WEIGHTS; i++)
|
for (int i = 0; i < MAX_BONE_INFLUENCE; i++)
|
||||||
{
|
{
|
||||||
vertex.m_BoneIDs[i] = -1;
|
vertex.m_BoneIDs[i] = -1;
|
||||||
vertex.m_Weights[i] = 0.0f;
|
vertex.m_Weights[i] = 0.0f;
|
||||||
@@ -154,7 +154,7 @@ private:
|
|||||||
|
|
||||||
void SetVertexBoneData(Vertex& vertex, int boneID, float weight)
|
void SetVertexBoneData(Vertex& vertex, int boneID, float weight)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < MAX_BONE_WEIGHTS; ++i)
|
for (int i = 0; i < MAX_BONE_INFLUENCE; ++i)
|
||||||
{
|
{
|
||||||
if (vertex.m_BoneIDs[i] < 0)
|
if (vertex.m_BoneIDs[i] < 0)
|
||||||
{
|
{
|
||||||
@@ -168,8 +168,8 @@ private:
|
|||||||
|
|
||||||
void ExtractBoneWeightForVertices(std::vector<Vertex>& vertices, aiMesh* mesh, const aiScene* scene)
|
void ExtractBoneWeightForVertices(std::vector<Vertex>& vertices, aiMesh* mesh, const aiScene* scene)
|
||||||
{
|
{
|
||||||
auto& boneInfoMap = m_OffsetMatMap;
|
auto& boneInfoMap = m_BoneInfoMap;
|
||||||
int& boneCount = m_BoneCount;
|
int& boneCount = m_BoneCounter;
|
||||||
|
|
||||||
for (int boneIndex = 0; boneIndex < mesh->mNumBones; ++boneIndex)
|
for (int boneIndex = 0; boneIndex < mesh->mNumBones; ++boneIndex)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user