Skip to content

Commit

Permalink
Merge pull request #121 from gameknife/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
gameknife authored Dec 3, 2024
2 parents d8e5acc + c71afaa commit 392685b
Show file tree
Hide file tree
Showing 32 changed files with 688 additions and 616 deletions.
Binary file added art/rigid.blend
Binary file not shown.
Binary file added assets/models/rigid.glb
Binary file not shown.
56 changes: 54 additions & 2 deletions src/Assets/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ namespace Assets

void Model::LoadGLTFScene(const std::string& filename, Assets::CameraInitialSate& cameraInit, std::vector<Assets::Node>& nodes,
std::vector<Assets::Model>& models,
std::vector<Assets::Material>& materials, std::vector<Assets::LightObject>& lights)
std::vector<Assets::Material>& materials, std::vector<Assets::LightObject>& lights, std::vector<Assets::AnimationTrack>& tracks)
{
int32_t matieralIdx = static_cast<int32_t>(materials.size());
int32_t modelIdx = static_cast<int32_t>(models.size());
Expand All @@ -246,7 +246,7 @@ namespace Assets
{
return;
}

// load all textures
std::vector<uint32_t> textureIdMap;

Expand Down Expand Up @@ -530,6 +530,58 @@ namespace Assets
ParseGltfNode(nodes, cameraInit, lights, glm::mat4(1), model, nodeIdx, modelIdx);
}

// load all animations
for ( auto& animation : model.animations )
{
for ( auto& track : animation.channels )
{
if (track.target_path == "translation")
{
AnimationTrack CreateTrack;

CreateTrack.NodeName_ = model.nodes[track.target_node].name;

tinygltf::Accessor inputAccessor = model.accessors[animation.samplers[track.sampler].input];
tinygltf::Accessor outputAccessor = model.accessors[animation.samplers[track.sampler].output];

tinygltf::BufferView inputView = model.bufferViews[inputAccessor.bufferView];
tinygltf::BufferView outputView = model.bufferViews[outputAccessor.bufferView];

int inputStride = inputAccessor.ByteStride(inputView);
int outputStride = outputAccessor.ByteStride(outputView);

for (size_t i = 0; i < inputAccessor.count; ++i)
{
float time = 0.f;
glm::vec3 translation;
if ( inputAccessor.type == TINYGLTF_TYPE_SCALAR )
{

float* position = reinterpret_cast<float*>(&model.buffers[inputView.buffer].data[inputView.byteOffset + inputAccessor.byteOffset + i *
inputStride]);
time = position[0];
}

if ( outputAccessor.type == TINYGLTF_TYPE_VEC3 )
{
float* position = reinterpret_cast<float*>(&model.buffers[outputView.buffer].data[outputView.byteOffset + outputAccessor.byteOffset + i *
outputStride]);
translation = vec3(
position[0],
position[1],
position[2]
);
}

AnimationKey key{time, translation, vec3(1), quat(1, 0, 0, 0)};
CreateTrack.KeyFrames_.emplace_back(key);
}

tracks.emplace_back(CreateTrack);
}
}
}

// if we got camera in the scene
int i = 0;
for (tinygltf::Camera& cam : model.cameras)
Expand Down
19 changes: 18 additions & 1 deletion src/Assets/Model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <memory>
#include <string>
#include <vector>
#include <glm/detail/type_quat.hpp>

namespace Assets
{
Expand Down Expand Up @@ -90,6 +91,20 @@ namespace Assets
uint32_t instanceId_;
bool visible_;
};

struct AnimationKey
{
float Time;
glm::vec3 Translation;
glm::vec3 Scale;
glm::quat Rotation;
};

struct AnimationTrack
{
std::string NodeName_;
std::vector<AnimationKey> KeyFrames_;
};

class Model final
{
Expand All @@ -112,7 +127,7 @@ namespace Assets
std::vector<Material>& materials,
std::vector<LightObject>& lights);
static void LoadGLTFScene(const std::string& filename, Assets::CameraInitialSate& cameraInit, std::vector<class Node>& nodes,
std::vector<Assets::Model>& models, std::vector<Assets::Material>& materials, std::vector<Assets::LightObject>& lights);
std::vector<Assets::Model>& models, std::vector<Assets::Material>& materials, std::vector<Assets::LightObject>& lights, std::vector<Assets::AnimationTrack>& tracks);

// basic geometry
static Model CreateBox(const glm::vec3& p0, const glm::vec3& p1, uint32_t materialIdx);
Expand Down Expand Up @@ -145,6 +160,8 @@ namespace Assets
std::vector<uint32_t> indices_;
std::vector<uint32_t> materialIdx_;
std::shared_ptr<const class Procedural> procedural_;

std::vector<AnimationTrack> AnimationTracks_;

glm::vec3 local_aabb_min;
glm::vec3 local_aabb_max;
Expand Down
Loading

0 comments on commit 392685b

Please sign in to comment.