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

Nemo #2013

Merged
merged 6 commits into from
Sep 29, 2024
Merged

Nemo #2013

Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions projects/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ set(ZENO_EXTENSIONS
Roads
Geometry
PluginPOC
Nemo
)

foreach (name IN ITEMS ${ZENO_EXTENSIONS})
Expand Down
69 changes: 69 additions & 0 deletions projects/Nemo/AnimSequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* MIT License
*
* Copyright (c) 2024 wuzhen
*
* 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:
*
* 1. The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 2. 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 "AnimSequence.h"
#include <fstream>
#include <tinygltf/json.hpp>

namespace nemo {
void Animation::load(std::string path) {
duration.first = INT_MAX;
duration.second = INT_MIN;
channels.clear();

std::ifstream fin(path);
if (!fin.is_open())
throw std::runtime_error("Could not load animation: " + path);
nlohmann::json root = nlohmann::json::parse(fin);
for (const auto &element : root.items()) {
Channel channel;
channel.name = element.key();
std::string type = element.value()["type"];
for (const auto &frame : element.value()["values"].items()) {
int key = std::stoi(frame.key());
duration.first = std::min(duration.first, key);
duration.second = std::max(duration.second, key);
ChannelValue value;
nlohmann::json j = frame.value();
if (type == "matrix") {
glm::dmat4 matrix;
for (int i = 0; i != 16; ++i)
matrix[i / 4][i % 4] = j[i].get<double>();
value = matrix;
} else if (type == "double3") {
value = glm::dvec3{j[0].get<double>(), j[1].get<double>(), j[2].get<double>()};
} else if (type == "double") {
if (j.is_boolean())
value = static_cast<double>(j.get<bool>());
else
value = j.get<double>();
} else {
throw std::runtime_error("unknown type: " + type);
}
channel.frames.insert(std::make_pair(key, value));
}
channels.push_back(std::move(channel));
}
}
} // namespace nemo
63 changes: 63 additions & 0 deletions projects/Nemo/AnimSequence.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* MIT License
*
* Copyright (c) 2024 wuzhen
*
* 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:
*
* 1. The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 2. 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 <glm/glm.hpp>
#include <map>
#include <string>
#include <variant>
#include <vector>

namespace nemo {
using ChannelValue = std::variant<glm::dvec3, glm::dmat4, double>;
struct Channel {
std::string name;
std::map<int, ChannelValue> frames;
};

struct Animation {
std::vector<Channel> channels;
std::pair<int, int> duration;

void load(std::string path);

template <typename T> T get(unsigned id, float time) {
int framebegin = channels[id].frames.begin()->first;
int frameend = channels[id].frames.rbegin()->first;
time = std::max<float>(time, framebegin);
time = std::min<float>(time, frameend);

if (isIntegral(time))
return std::get<T>(channels[id].frames.at(static_cast<int>(time)));

float beg = std::floor(time);
float end = std::ceil(time);
double alpha = (time - beg) / (end - beg);
return (1.0 - alpha) * std::get<T>(channels[id].frames.at(static_cast<int>(beg))) + alpha * std::get<T>(channels[id].frames.at(static_cast<int>(end)));
}

private:
static bool isIntegral(float x) { return std::abs(x - std::roundf(x)) < 1E-5F; }
};
} // namespace nemo
6 changes: 6 additions & 0 deletions projects/Nemo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
file(GLOB NEMO_SOURCE *.cpp *.h)

target_sources(zeno PRIVATE ${NEMO_SOURCE})
target_include_directories(zeno PRIVATE .)


100 changes: 100 additions & 0 deletions projects/Nemo/Context.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* MIT License
*
* Copyright (c) 2024 wuzhen
*
* 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:
*
* 1. The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 2. 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 "Context.h"

namespace nemo {
void DataStorage::cleanup() {
if (!instance || !fnFree)
return;
fnFree(instance);
}

void DataStorage::init(MOD mod) {
fnNew = get_fn<decltype(fnNew)>(mod, "dataNew");
fnFree = get_fn<decltype(fnFree)>(mod, "dataFree");
instance = fnNew();

fnResize = get_fn<decltype(fnResize)>(mod, "dataResize");
fnGlobalIdOffset = get_fn<decltype(fnGlobalIdOffset)>(mod, "dataGlobalIdOffset");
fnTypeid = get_fn<decltype(fnTypeid)>(mod, "dataTypeid");

fnGetBool = get_fn<decltype(fnGetBool)>(mod, "dataGetBool");
fnGetFloat = get_fn<decltype(fnGetFloat)>(mod, "dataGetFloat");
fnGetDouble = get_fn<decltype(fnGetDouble)>(mod, "dataGetDouble");
fnGetInt = get_fn<decltype(fnGetInt)>(mod, "dataGetInt");
fnGetVec3 = get_fn<decltype(fnGetVec3)>(mod, "dataGetVec3");
fnGetDVec3 = get_fn<decltype(fnGetDVec3)>(mod, "dataGetDVec3");
fnGetMat4 = get_fn<decltype(fnGetMat4)>(mod, "dataGetMat4");
fnGetDMat4 = get_fn<decltype(fnGetDMat4)>(mod, "dataGetDMat4");
fnGetMesh = get_fn<decltype(fnGetMesh)>(mod, "dataGetMesh");
fnGetDMesh = get_fn<decltype(fnGetDMesh)>(mod, "dataGetDMesh");
fnGetCuShape = get_fn<decltype(fnGetCuShape)>(mod, "dataGetCuShape");
fnPullCuShape = get_fn<decltype(fnPullCuShape)>(mod, "dataPullCuShape");
fnGetDCuShape = get_fn<decltype(fnGetDCuShape)>(mod, "dataGetDCuShape");
fnPullDCuShape = get_fn<decltype(fnPullDCuShape)>(mod, "dataPullDCuShape");
fnGetCurve = get_fn<decltype(fnGetCurve)>(mod, "dataGetCurve");
fnGetDCurve = get_fn<decltype(fnGetDCurve)>(mod, "dataGetDCurve");
fnGetSurface = get_fn<decltype(fnGetSurface)>(mod, "dataGetSurface");
fnGetDSurface = get_fn<decltype(fnGetDSurface)>(mod, "dataGetDSurface");

fnSetBool = get_fn<decltype(fnSetBool)>(mod, "dataSetBool");
fnSetFloat = get_fn<decltype(fnSetFloat)>(mod, "dataSetFloat");
fnSetDouble = get_fn<decltype(fnSetDouble)>(mod, "dataSetDouble");
fnSetInt = get_fn<decltype(fnSetInt)>(mod, "dataSetInt");
fnSetVec2 = get_fn<decltype(fnSetVec2)>(mod, "dataSetVec2");
fnSetDVec2 = get_fn<decltype(fnSetDVec2)>(mod, "dataSetDVec2");
fnSetVec3 = get_fn<decltype(fnSetVec3)>(mod, "dataSetVec3");
fnSetDVec3 = get_fn<decltype(fnSetDVec3)>(mod, "dataSetDVec3");
fnSetMat4 = get_fn<decltype(fnSetMat4)>(mod, "dataSetMat4");
fnSetDMat4 = get_fn<decltype(fnSetDMat4)>(mod, "dataSetDMat4");
fnSetMesh = get_fn<decltype(fnSetMesh)>(mod, "dataSetMesh");
fnSetDMesh = get_fn<decltype(fnSetDMesh)>(mod, "dataSetDMesh");
fnSetCurve = get_fn<decltype(fnSetCurve)>(mod, "dataSetCurve");
fnSetDCurve = get_fn<decltype(fnSetDCurve)>(mod, "dataSetDCurve");
fnSetSurface = get_fn<decltype(fnSetSurface)>(mod, "dataSetSurface");
fnSetDSurface = get_fn<decltype(fnSetDSurface)>(mod, "dataSetDSurface");
}

void ResourcePool::cleanup() {
if (!instance || !fnFree)
return;
fnFree(instance);
}

void ResourcePool::init(MOD mod, std::string path) {
fnNew = get_fn<decltype(fnNew)>(mod, "resNew");
fnFree = get_fn<decltype(fnFree)>(mod, "resFree");
instance = fnNew();

fnLoad = get_fn<decltype(fnLoad)>(mod, "resLoad");
fnLoad(instance, path);

fnGetTopo = get_fn<decltype(fnGetTopo)>(mod, "resGetTopo");
fnGetUV = get_fn<decltype(fnGetUV)>(mod, "resGetUV");
fnGetColor = get_fn<decltype(fnGetColor)>(mod, "resGetColor");
fnGetNormal = get_fn<decltype(fnGetNormal)>(mod, "resGetNormal");
fnGetUVector = get_fn<decltype(fnGetUVector)>(mod, "resGetUVector");
}
} // namespace nemo
Loading
Loading