Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kimkulling/prepare animations #235

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contrib/assimp
Submodule assimp updated 213 files
2 changes: 1 addition & 1 deletion contrib/cppcore
Submodule cppcore updated 41 files
+3 −4 CMakeLists.txt
+1 −1 code/Memory/MemUtils.cpp
+1 −1 code/Random/RandomGenerator.cpp
+1 −1 code/cppcore.cpp
+0 −33 contrib/googletest-1.10.x/googletest/generated/GTestConfig.cmake
+0 −48 contrib/googletest-1.10.x/googletest/generated/GTestConfigVersion.cmake
+0 −10 contrib/googletest-1.10.x/googletest/generated/gmock.pc
+0 −10 contrib/googletest-1.10.x/googletest/generated/gmock_main.pc
+0 −9 contrib/googletest-1.10.x/googletest/generated/gtest.pc
+0 −10 contrib/googletest-1.10.x/googletest/generated/gtest_main.pc
+1 −0 include/cppcore/CPPCoreCommon.h
+1 −1 include/cppcore/Common/Hash.h
+1 −1 include/cppcore/Common/TBitField.h
+17 −2 include/cppcore/Common/TOptional.h
+1 −1 include/cppcore/Common/TSharedPtr.h
+1 −1 include/cppcore/Common/TStringBase.h
+17 −16 include/cppcore/Common/Variant.h
+1 −1 include/cppcore/Container/TArray.h
+1 −1 include/cppcore/Container/THashMap.h
+1 −1 include/cppcore/Container/TList.h
+1 −1 include/cppcore/Container/TQueue.h
+1 −1 include/cppcore/Container/TStaticArray.h
+21 −5 include/cppcore/Memory/MemUtils.h
+1 −1 include/cppcore/Memory/TDefaultAllocator.h
+1 −1 include/cppcore/Memory/TPoolAllocator.h
+2 −2 include/cppcore/Memory/TStackAllocator.h
+1 −1 include/cppcore/Random/RandomGenerator.h
+1 −1 test/CPPCoreCommonTest.cpp
+1 −1 test/Random/RandomGeneratorTest.cpp
+1 −1 test/common/HashTest.cpp
+1 −1 test/common/TBitFieldTest.cpp
+16 −10 test/common/TOptionalTest.cpp
+1 −1 test/common/TSharedPtrTest.cpp
+1 −1 test/common/VariantTest.cpp
+1 −1 test/container/TArrayTest.cpp
+1 −1 test/container/THashMapTest.cpp
+37 −37 test/container/TListTest.cpp
+1 −1 test/container/TQueueTest.cpp
+1 −1 test/container/TStaticArrayTest.cpp
+1 −1 test/memory/TPoolAllocatorTest.cpp
+1 −1 test/memory/TStackAllocatorTest.cpp
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
22 changes: 8 additions & 14 deletions src/Editor/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ 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", false, true, App::RenderBackendType::OpenGLRenderBackend)) {
if (!osreApp.initWindow(100, 100, 1024, 768, "test", false, true, RenderBackendType::OpenGLRenderBackend)) {
kimkulling marked this conversation as resolved.
Show resolved Hide resolved
return -1;
}

Expand All @@ -66,21 +66,15 @@ int main(int argc, char *argv[]) {
// Main loop
bool done = false;
while (!done) {

// 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
{
static int counter = 0;

App::Stage *stage = osreApp.getStage();
if (stage != nullptr) {
World *world = stage->getActiveWorld(0);
}

static int counter = 0;
osreApp.handleEvents();
App::Stage *stage = osreApp.getStage();
if (stage != nullptr) {
World *world = stage->getActiveWorld(0);
}
osreApp.update();
osreApp.requestNextFrame();
}

osreApp.handleEvents();
osreApp.update();
osreApp.requestNextFrame();
return 0;
}
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