Skip to content

Commit

Permalink
Merge pull request #235 from kimkulling/kimkulling/prepare_animations
Browse files Browse the repository at this point in the history
Kimkulling/prepare animations
  • Loading branch information
kimkulling authored Sep 19, 2024
2 parents abf57f8 + ab7eedd commit 1faa923
Show file tree
Hide file tree
Showing 14 changed files with 226 additions and 80 deletions.
4 changes: 4 additions & 0 deletions samples/02_Demo2D/Demo2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ class Demo2DApp : public App::AppBase {

mCanvasRenderer->drawRect(100, 600, 110, 124, true);

mCanvasRenderer->drawRect(100, 700, 110, 124, true);

mCanvasRenderer->drawRect(100, 1000, 110, 124, true);

return true;
}

Expand Down
9 changes: 7 additions & 2 deletions src/Editor/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,20 @@ int main(int argc, char *argv[]) {
std::cout << "Editor version 0.1\n";

OsreEdApp osreApp(argc, argv);
if (!osreApp.initWindow(100, 100, 1024, 768, "test", WindowMode::Windowed, WindowType::Root, App::RenderBackendType::OpenGLRenderBackend)) {
if (!osreApp.initWindow(100, 100, 1024, 768, "test", false, true, RenderBackendType::OpenGLRenderBackend)) {
return -1;
}


// Main loop
bool done = false;
while (!done) {
done = !osreApp.handleEvents();
static int counter = 0;
done = osreApp.handleEvents();
App::Stage *stage = osreApp.getStage();
if (stage != nullptr) {
World *world = stage->getActiveWorld(0);
}
osreApp.update();
osreApp.requestNextFrame();
}
Expand Down
36 changes: 21 additions & 15 deletions src/Engine/Animation/AnimatorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,31 +82,38 @@ struct OSRE_EXPORT Skeleton {
};

struct VectorKey {
glm::vec3 mVector;
d32 mTime;
f32 Time;
glm::vec3 Value;

VectorKey() : Time(1.0f), Value(1) {
// empty
}

~VectorKey() = default;
};

using VectorKeyArray = ::cppcore::TArray<VectorKey>;

struct RotationKey {
glm::vec4 mQuad;
d32 mTime;
};

using VectorKeyArray = ::cppcore::TArray<VectorKey>;
using RotationKeyArray = ::cppcore::TArray<RotationKey>;

struct NodeAnimation {
VectorKeyArray mPositions;
VectorKeyArray mScalings;
RotationKeyArray mRotations;
};
struct VectorChannel {
size_t NumVectorKeys;
VectorKeyArray VectorKeys;

struct AnimationTrack {
String mName;
double mDuration;
double mTicksPerSecond;
cppcore::TArray<NodeAnimation *> mNodeAnimations;
VectorChannel() : NumVectorKeys(0), VectorKeys() {
// empty
}

~VectorChannel() = default;
};

using VectorChannelArray = ::cppcore::TArray<VectorChannel>;

template <class T>
struct AnimatorBase {
void operator () ( T &out, const T &a, const T &b, f32 d ) const {
Expand All @@ -128,7 +135,6 @@ enum class TransformCommandType {
Count
};


//-------------------------------------------------------------------------------------------------
/// @ingroup Engine
///
Expand All @@ -139,7 +145,7 @@ class OSRE_EXPORT AnimationControllerBase {
public:
/// @brief The class destructor, virtual.
virtual ~AnimationControllerBase() = default;

/// @brief Will update the mouse input state.
/// @param mis The mouse input.
virtual void getMouseUpdate(const App::MouseInputState &mis) = 0;
Expand Down
82 changes: 82 additions & 0 deletions src/Engine/Animation/AnimatorComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)
Copyright (c) 2015-2024 OSRE ( Open Source Render Engine ) by Kim Kulling
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-----------------------------------------------------------------------------------------------*/
#include "Animation/AnimatorComponent.h"

namespace OSRE{
namespace Animation {

using namespace OSRE::App;

AnimatorComponent::AnimatorComponent(Entity *owner, ComponentType type) :
Component(owner, type),
mAnimationTrackArray(),
mActiveTrack() {
// empty
}

AnimatorComponent::~AnimatorComponent() {
// empty
}

void AnimatorComponent::addTrack(AnimationTrack *track) {
if (track == nullptr) {
return;
}

mAnimationTrackArray.add(track);
}

AnimationTrack *AnimatorComponent::getTrackAt(size_t index) const {
if (index >= mAnimationTrackArray.size()) {
return nullptr;
}

return mAnimationTrackArray[index];
}

bool AnimatorComponent::selectTrack(size_t index) {
if (index >= mAnimationTrackArray.size()) {
return false;
}

mActiveTrack = index;

return true;
}

size_t AnimatorComponent::getActiveTrack() const {
return mActiveTrack;
}

bool AnimatorComponent::onUpdate(Time dt) {
return true;
}

bool AnimatorComponent::onRender(RenderBackend::RenderBackendService *renderBackendSrv) {
osre_assert(renderBackendSrv != nullptr);

return true;
}

} // namespace Animation
} // namespace OSRE
74 changes: 74 additions & 0 deletions src/Engine/Animation/AnimatorComponent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)
Copyright (c) 2015-2024 OSRE ( Open Source Render Engine ) by Kim Kulling
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-----------------------------------------------------------------------------------------------*/
#pragma once

#include "Common/osre_common.h"
#include "Animation/AnimatorBase.h"
#include "App/Component.h"

namespace OSRE {
namespace Animation {

struct AnimationTrack {
f32 Duration;
f32 mTicksPerSecond;
size_t NumVectorChannels;
VectorChannel *VectorChannels;

AnimationTrack() :
Duration(1.0f), mTicksPerSecond(1.0f), NumVectorChannels(0l), VectorChannels(nullptr) {
// empty
}

~AnimationTrack() {
delete [] VectorChannels;
VectorChannels = nullptr;
}
};

//-------------------------------------------------------------------------------------------------
/// @ingroup Engine
///
/// @brief Describes the base class for all components.
//-------------------------------------------------------------------------------------------------
class AnimatorComponent : public App::Component {
public:
AnimatorComponent(App::Entity *owner, App::ComponentType type);
~AnimatorComponent();
void addTrack(AnimationTrack *track);
AnimationTrack *getTrackAt(size_t index) const;
bool selectTrack(size_t index);
size_t getActiveTrack() const;

protected:
bool onUpdate(Time dt) override;
bool onRender(RenderBackend::RenderBackendService *renderBackendSrv) override;

private:
using AnimationTrackArray = cppcore::TArray<AnimationTrack*>;
AnimationTrackArray mAnimationTrackArray;
size_t mActiveTrack;
};

} // namespace Animation
} // namespace OSRE
46 changes: 10 additions & 36 deletions src/Engine/App/AssimpWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,24 +216,14 @@ Entity *AssimpWrapper::convertScene() {
if (nullptr != mAssetContext.mScene->mRootNode) {
importNode(mAssetContext.mScene->mRootNode, nullptr);
}
AnimationMap animLookup;
if (nullptr != mAssetContext.mScene->mAnimations) {
cppcore::TArray<AnimationTrack> animationTracks;
animationTracks.resize(mAssetContext.mScene->mNumAnimations);
for (ui32 i = 0; i < mAssetContext.mScene->mNumAnimations; ++i) {
importAnimation(mAssetContext.mScene->mAnimations[i], animationTracks[i], animLookup);
}
}

importAnimations(mAssetContext.mScene);

if (!mAssetContext.mMeshArray.isEmpty()) {
RenderComponent *rc = (RenderComponent*) mAssetContext.mEntity->getComponent(ComponentType::RenderComponentType);
rc->addStaticMeshArray(mAssetContext.mMeshArray);
}

if (mAssetContext.mScene->hasSkeletons()) {
importSkeletons(*mAssetContext.mScene->mSkeletons, mAssetContext.mScene->mNumSkeletons);
}

return mAssetContext.mEntity;
}

Expand Down Expand Up @@ -535,35 +525,19 @@ void AssimpWrapper::importMaterial(aiMaterial *material) {

using Bone2NodeMap = cppcore::THashMap<int, TransformComponent*>;

void AssimpWrapper::importSkeletons( aiSkeleton *skeletons, size_t numSkeletons) {
if (numSkeletons == 0 || skeletons == nullptr) {
void AssimpWrapper::importAnimations(const aiScene *scene) {
if (scene == nullptr) {
return;
}

Bone2NodeMap bone2NodeMap;
for (size_t skeletonIdx = 0; skeletonIdx < numSkeletons; ++skeletonIdx) {
aiSkeleton &currentSkeleton = skeletons[skeletonIdx];
for (size_t boneIdx = 0; boneIdx < currentSkeleton.mNumBones; ++boneIdx) {
aiSkeletonBone *bone = currentSkeleton.mBones[boneIdx];
if (bone == nullptr) {
continue;
}

aiMesh *mesh = bone->mMeshId;
if (mesh == nullptr) {
continue;
}

for (unsigned int i = 0; i < scene->mNumMeshes; ++i) {
const aiMesh *mesh = scene->mMeshes[i];
for (unsigned int n = 0; n < mesh->mNumBones; ++n) {
const aiBone *bone = mesh->mBones[n];
mAssetContext.mBone2NodeMap[bone->mName.data] = scene->mRootNode->FindNode(bone->mName);
}
}
}

void AssimpWrapper::importAnimation(aiAnimation *animation, AnimationTrack &currentAnimationTrack, AnimationMap &animLookup) {
if (nullptr == animation) {
return;
}

currentAnimationTrack.mName = animation->mName.C_Str();
}

} // namespace App
} // namespace OSRE
7 changes: 1 addition & 6 deletions src/Engine/App/AssimpWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ class OSRE_EXPORT AssimpWrapper {
/// @brief Alias for bone to node relations.
using Bone2NodeMap = std::map<const char *, const aiNode *>;

/// @brief Name to animation track relation alias.
using AnimationMap = std::map<const char*, Animation::AnimationTrack*>;

/// @brief The class constructor.
/// @param ids The id container.
/// @param world The world to put the imported entity in.
Expand Down Expand Up @@ -112,11 +109,9 @@ class OSRE_EXPORT AssimpWrapper {
protected:
Entity *convertScene();
void importMeshes( aiMesh **meshes, ui32 numMeshes );
//void importBones(aiMesh* mesh);
void importNode( aiNode *node, TransformComponent *parent );
void importMaterial( aiMaterial *material );
void importSkeletons(aiSkeleton *skeletons, size_t numSkeletons);
void importAnimation(aiAnimation *animation, Animation::AnimationTrack &currentAnimationTrack, AnimationMap &animLookup);
void importAnimations(const aiScene *scene);
void optimizeVertexBuffer();

private:
Expand Down
1 change: 0 additions & 1 deletion src/Engine/App/Component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ bool LightComponent::onUpdate(Time) {
return true;
}


return true;
}

Expand Down
Loading

0 comments on commit 1faa923

Please sign in to comment.