forked from RavEngine/RavEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeshAssetSkinned.cpp
123 lines (100 loc) · 3.77 KB
/
MeshAssetSkinned.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
#include "MeshAssetSkinned.hpp"
#include <fmt/format.h>
#include "App.hpp"
#include <assimp/cimport.h>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <assimp/material.h>
#include <assimp/mesh.h>
#include <filesystem>
using namespace RavEngine;
using namespace std;
//TODO: avoid opening the file twice -- this is a double copy and repeats work, therefore slow
MeshAssetSkinned::MeshAssetSkinned(const std::string& path, Ref<SkeletonAsset> skeleton, float scale) : MeshAsset(path,scale){
auto fullpath = StrFormat("objects/{}",path);
if (!App::Resources->Exists(fullpath.c_str())){
Debug::Fatal("No asset at {}",fullpath);
}
auto str = App::Resources->FileContentsAt(fullpath.c_str());
//pull from cmrc
auto file_ext = filesystem::path(fullpath).extension();
//uses a meta-flag to auto-triangulate the input file
const aiScene* scene = aiImportFileFromMemory(str.data(), str.size(),
aiProcess_CalcTangentSpace |
aiProcess_GenSmoothNormals |
aiProcess_JoinIdenticalVertices |
aiProcess_ImproveCacheLocality |
aiProcess_LimitBoneWeights |
aiProcess_RemoveRedundantMaterials |
aiProcess_SplitLargeMeshes |
aiProcess_Triangulate |
aiProcess_GenUVCoords |
aiProcess_SortByPType |
aiProcess_FindInstances |
aiProcess_ValidateDataStructure |
aiProcess_OptimizeMeshes |
aiProcess_FindInvalidData ,
file_ext.string().c_str());
if (!scene){
Debug::Fatal("Cannot load: {}", aiGetErrorString());
}
std::vector<vweights> allweights;
{
uint32_t numverts = 0;
for(int i = 0; i < scene->mNumMeshes; i++){
numverts += scene->mMeshes[i]->mNumVertices;
}
allweights.resize(numverts);
}
uint16_t current_offset = 0;
auto sk = skeleton->GetSkeleton().get();
auto calcMesh = [&](const aiMesh* mesh){
for(int i = 0; i < mesh->mNumBones; i++){
auto bone = mesh->mBones[i];
vweights weights;
//find this bone in the skeleton to determine joint index
auto it = std::find(sk->joint_names().begin(),sk->joint_names().end(),string(bone->mName.C_Str()));
if (it == sk->joint_names().end()){
continue;
}
auto idx = std::distance(sk->joint_names().begin(), it);
//copy (index + current_offset) and influence
for(int j = 0; j < bone->mNumWeights; j++){
auto weightval = bone->mWeights[j];
allweights[weightval.mVertexId + current_offset].weights.push_back({vweights::vw{static_cast<float>(idx),weightval.mWeight}});
}
}
};
//go through mesh and pull out weights
for(int i = 0; i < scene->mNumMeshes; i++){
auto mesh = scene->mMeshes[i];
calcMesh(mesh);
current_offset += mesh->mNumVertices;
}
struct wrapper{
vweights::vw w[4];
};
//make gpu version
std::vector<wrapper> weightsgpu;
weightsgpu.reserve(allweights.size());
std::memset(weightsgpu.data(), 0, weightsgpu.size() * sizeof(weightsgpu[0]));
for(const auto& weights : allweights){
wrapper w;
uint8_t i = 0;
for(const auto& weight : weights.weights){
w.w[i].influence = weight.influence;
w.w[i].joint_idx = weight.joint_idx;
i++;
}
weightsgpu.push_back(w);
}
//map to GPU
bgfx::VertexLayout layout;
layout.begin()
.add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
.add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
.add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
.add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
.end();
weightsHandle = bgfx::createVertexBuffer(bgfx::copy(weightsgpu.data(), weightsgpu.size() * sizeof(weightsgpu[0])), layout);
}