Skip to content

Commit

Permalink
Addon - ofxAssimp (#7776)
Browse files Browse the repository at this point in the history
  • Loading branch information
NickHardeman authored Dec 1, 2023
1 parent 3fccc6d commit dd09e97
Show file tree
Hide file tree
Showing 68 changed files with 10,414 additions and 0 deletions.
113 changes: 113 additions & 0 deletions addons/ofxAssimp/addon_config.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# All variables and this file are optional, if they are not present the PG and the
# makefiles will try to parse the correct values from the file system.
#
# Variables that specify exclusions can use % as a wildcard to specify that anything in
# that position will match. A partial path can also be specified to, for example, exclude
# a whole folder from the parsed paths from the file system
#
# Variables can be specified using = or +=
# = will clear the contents of that variable both specified from the file or the ones parsed
# from the file system
# += will add the values to the previous ones in the file or the ones parsed from the file
# system
#
# The PG can be used to detect errors in this file, just create a new project with this addon
# and the PG will write to the console the kind of error and in which line it is

meta:
ADDON_NAME = ofxAssimp
ADDON_DESCRIPTION = Addon for loading several 3d formats through the assimp library
ADDON_AUTHOR = OF Team
ADDON_TAGS = "3D" "assimp"
ADDON_URL = http://github.com/openframeworks/openFrameworks

common:
# dependencies with other addons, a list of them separated by spaces
# or use += in several lines
# ADDON_DEPENDENCIES =

# include search paths, this will be usually parsed from the file system
# but if the addon or addon libraries need special search paths they can be
# specified here separated by spaces or one per line using +=
ADDON_INCLUDES+=../ofxAssimpModelLoader/libs/assimp/include/

# any special flag that should be passed to the compiler when using this
# addon
# ADDON_CFLAGS =

# any special flag that should be passed to the linker when using this
# addon, also used for system libraries with -lname
# ADDON_LDFLAGS =

# linux only, any library that should be included in the project using
# pkg-config
# ADDON_PKG_CONFIG_LIBRARIES =

# osx/iOS only, any framework that should be included in the project
# ADDON_FRAMEWORKS =

# source files, these will be usually parsed from the file system looking
# in the src folders in libs and the root of the addon. if your addon needs
# to include files in different places or a different set of files per platform
# they can be specified here
# ADDON_SOURCES =

# some addons need resources to be copied to the bin/data folder of the project
# specify here any files that need to be copied, you can use wildcards like * and ?
# ADDON_DATA =

# when parsing the file system looking for libraries exclude this for all or
# a specific platform
# ADDON_LIBS_EXCLUDE =

linuxarmv6l:
ADDON_PKG_CONFIG_LIBRARIES = assimp
ADDON_LIBS_EXCLUDE = ../ofxAssimpModelLoader/libs/assimp
ADDON_INCLUDES_EXCLUDE = ../ofxAssimpModelLoader/libs/assimp/%

linuxarmv7l:
ADDON_PKG_CONFIG_LIBRARIES = assimp
ADDON_LIBS_EXCLUDE = ../ofxAssimpModelLoader/libs/assimp
ADDON_INCLUDES_EXCLUDE = ../ofxAssimpModelLoader/libs/assimp/%

linuxaarch64:
ADDON_PKG_CONFIG_LIBRARIES = assimp
ADDON_LIBS_EXCLUDE = ../ofxAssimpModelLoader/libs/assimp
ADDON_INCLUDES_EXCLUDE = ../ofxAssimpModelLoader/libs/assimp/%

linux:
ADDON_PKG_CONFIG_LIBRARIES = assimp
ADDON_LIBS_EXCLUDE = ../ofxAssimpModelLoader/libs/assimp
ADDON_INCLUDES_EXCLUDE = ../ofxAssimpModelLoader/libs/assimp/%

linux64:
ADDON_PKG_CONFIG_LIBRARIES = assimp
ADDON_LIBS_EXCLUDE = ../ofxAssimpModelLoader/libs/assimp
ADDON_INCLUDES_EXCLUDE = ../ofxAssimpModelLoader/libs/assimp/%

msys2:
ADDON_PKG_CONFIG_LIBRARIES = assimp
ADDON_LIBS_EXCLUDE = ../ofxAssimpModelLoader/libs/assimp
ADDON_INCLUDES_EXCLUDE = ../ofxAssimpModelLoader/libs/assimp/%

android/armeabi-v7a:
ADDON_LIBS=
ADDON_LIBS+=../ofxAssimpModelLoader/libs/assimp/lib/android/armeabi-v7a/libassimp.a
ADDON_LIBS+=../ofxAssimpModelLoader/libs/assimp/lib/android/armeabi-v7a/libIrrXML.a

android/x86:
ADDON_LIBS=
ADDON_LIBS+=../ofxAssimpModelLoader/libs/assimp/lib/android/x86/libassimp.a
ADDON_LIBS+=../ofxAssimpModelLoader/libs/assimp/lib/android/x86/libIrrXML.a

ios:
ADDON_LIBS=
ADDON_LIBS=../ofxAssimpModelLoader/libs/assimp/lib/ios/assimp.a

osx:
ADDON_LIBS=
ADDON_LIBS=../ofxAssimpModelLoader/libs/assimp/lib/osx/assimp.a

emscripten:
ADDON_LIBS=
ADDON_LIBS=../ofxAssimpModelLoader/libs/assimp/lib/emscripten/libassimp.a
158 changes: 158 additions & 0 deletions addons/ofxAssimp/src/Source/ofxAssimpSrcAnimKeyCollection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
//
// ofxAssimpSrcAnimKeyCollection.cpp
// Created by Nick Hardeman on 11/1/23.
//

#include "ofxAssimpSrcAnimKeyCollection.h"
#include "ofxAssimpUtils.h"

using namespace ofx::assimp;

//--------------------------------------------------------------
void SrcAnimKeyCollection::setup( aiNodeAnim* aNodeAnim, float aDurationInTicks ) {
mNodeAnim = aNodeAnim;
mDurationInTicks = aDurationInTicks;
}

//--------------------------------------------------------------
bool SrcAnimKeyCollection::hasKeys() {
return mNodeAnim != nullptr;
}


//--------------------------------------------------------------
glm::vec3 SrcAnimKeyCollection::getVec3ForTime( const float& atime, const std::vector<ofx::assimp::AnimVectorKey>& akeys ) {
size_t numKeys = akeys.size();
for( size_t i = 0; i < numKeys; i++ ) {
if( akeys[i].time == atime ) {
return akeys[i].value;
} else if( akeys[i].time > atime ) {
if( i > 0 ) {
float keyDiff = akeys[i].time - akeys[i-1].time;
return glm::mix( akeys[i-1].value, akeys[i].value, (atime-akeys[i-1].time) / keyDiff );
} else {
return akeys[i].value;
}
}
}
if( numKeys > 1 ) {
return akeys.back().value;
}
return glm::vec3(0.f, 0.f, 0.f);
}

//--------------------------------------------------------------
glm::vec3 SrcAnimKeyCollection::getPosition( const float& atime ) {
auto rpos = glm::vec3(0.f, 0.f, 0.f);
if( positionKeys.size() < 1 ) {

} else if( positionKeys.size() == 1 ) {
rpos = positionKeys[0].value;
} else {
rpos = getVec3ForTime( atime, positionKeys );
}
return rpos;
}

//--------------------------------------------------------------
glm::vec3 SrcAnimKeyCollection::getScale( const float& atime ) {
auto rscale = glm::vec3(1.f, 1.f, 1.f);
if( scaleKeys.size() < 1 ) {

} else if(scaleKeys.size() == 1 ) {
rscale = scaleKeys[0].value;
} else {
rscale = getVec3ForTime( atime, scaleKeys );
}
return rscale;
}

//--------------------------------------------------------------
glm::quat SrcAnimKeyCollection::getRotation( const float& atime ) {
size_t numKeys = rotationKeys.size();
auto rq = glm::quat(1.f, 0.f, 0.f, 0.f);
if(numKeys < 2) {
if( numKeys == 1 ) {
return rotationKeys[0].value;
} else {
return rq;
}
} else {
for( size_t i = 0; i < numKeys; i++ ) {
if( rotationKeys[i].time == atime ) {
return rotationKeys[i].value;
} else if( rotationKeys[i].time > atime ) {
if( i > 0 ) {
float keyDiff = rotationKeys[i].time - rotationKeys[i-1].time;
return glm::slerp( rotationKeys[i-1].value, rotationKeys[i].value, ((atime-rotationKeys[i-1].time) / keyDiff) );
} else {
return rotationKeys[i].value;
}
}
}
}
if( numKeys > 1 ) {
return rotationKeys.back().value;
}
return rq;
}

//--------------------------------------------------------------
std::vector<AnimVectorKey> SrcAnimKeyCollection::getAnimVectorKeysForTime(const float& aStartTime, const float& aEndTime, unsigned int aNumKeys, aiVectorKey* aAiKeys) {

std::vector<AnimVectorKey> rkeys;
if( aNumKeys < 1 ) {
return rkeys;
}

if(aNumKeys == 1) {
AnimVectorKey vkey;
vkey.time = aStartTime;
vkey.value = aiVecToOfVec(aAiKeys[0].mValue);
vkey.valueAi = aAiKeys[0].mValue;
rkeys.push_back( vkey );
return rkeys;
}

double currTime = aStartTime;
for( unsigned int i = 0; i < aNumKeys; i++ ) {
auto& key1 = aAiKeys[i];
auto v1 = aiVecToOfVec(key1.mValue);
AnimVectorKey vkey;
vkey.time = key1.mTime;
vkey.value = v1;
vkey.valueAi = key1.mValue;
rkeys.push_back( vkey );
}

return rkeys;
}

//--------------------------------------------------------------
std::vector<AnimRotationKey> SrcAnimKeyCollection::getAnimRotationKeysForTime(const float& aStartTime, const float& aEndTime, unsigned int aNumKeys, aiQuatKey* aAiKeys) {

std::vector<AnimRotationKey> rkeys;
if( aNumKeys < 1 ) {
return rkeys;
}
if(aNumKeys == 1) {
AnimRotationKey vkey;
vkey.time = aStartTime;
vkey.value = aiQuatToOfQuat(aAiKeys[0].mValue);
vkey.valueAi = aAiKeys[0].mValue;
rkeys.push_back( vkey );
return rkeys;
}
double currTime = aStartTime;
for( unsigned int i = 0; i < aNumKeys; i++ ) {
auto& key1 = aAiKeys[i];
auto v1 = aiQuatToOfQuat(key1.mValue);
AnimRotationKey vkey;
vkey.time = key1.mTime;
vkey.value = v1;
vkey.valueAi = key1.mValue;
rkeys.push_back( vkey );
}

return rkeys;
}
56 changes: 56 additions & 0 deletions addons/ofxAssimp/src/Source/ofxAssimpSrcAnimKeyCollection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// ofxAssimpSrcAnimKeyCollection.h
// Created by Nick Hardeman on 11/1/23.
//

#pragma once
#include <assimp/scene.h>
#include "ofNode.h"


namespace ofx::assimp {

struct AnimVectorKey {
float time = 0.0;
glm::vec3 value;
aiVector3D valueAi;
};

struct AnimRotationKey {
float time = 0.0;
glm::quat value;
aiQuaternion valueAi;
};

class SrcAnimKeyCollection {
public:
unsigned int uId = 0;
std::string name = "";
std::vector<ofx::assimp::AnimVectorKey> positionKeys;
std::vector<ofx::assimp::AnimRotationKey> rotationKeys;
std::vector<ofx::assimp::AnimVectorKey> scaleKeys;

void clear() {
positionKeys.clear();
rotationKeys.clear();
scaleKeys.clear();
}

void setup( aiNodeAnim* aNodeAnim, float aDurationInTicks );
bool hasKeys();

glm::vec3 getVec3ForTime( const float& atime, const std::vector<ofx::assimp::AnimVectorKey>& akeys );

glm::vec3 getPosition( const float& atime );
glm::vec3 getScale( const float& atime );
glm::quat getRotation( const float& atime );

std::vector<AnimVectorKey> getAnimVectorKeysForTime(const float& aStartTime, const float& aEndTime, unsigned int aNumKeys, aiVectorKey* aAiKeys);
std::vector<AnimRotationKey> getAnimRotationKeysForTime(const float& aStartTime, const float& aEndTime, unsigned int aNumKeys, aiQuatKey* aAiKeys);


aiNodeAnim* mNodeAnim = nullptr;
float mDurationInTicks = 1;

};
}
Loading

0 comments on commit dd09e97

Please sign in to comment.