Skip to content

Commit

Permalink
Add required vec3/vec4/quat json serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Nov 4, 2023
1 parent 42a4b4f commit 4ce3c0e
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions shared/sdk/Math.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <algorithm>
#include <nlohmann/json.hpp>

#define GLM_ENABLE_EXPERIMENTAL

Expand Down Expand Up @@ -195,4 +196,78 @@ static glm::vec3 ue_euler_from_rotation_matrix(const glm::mat4& m) {

return rotator;
}

static nlohmann::json to_json(const glm::vec3& v) {
return nlohmann::json{ { "x", v.x }, { "y", v.y }, { "z", v.z } };
}

static nlohmann::json to_json(const glm::vec4& v) {
return nlohmann::json{ { "x", v.x }, { "y", v.y }, { "z", v.z }, { "w", v.w } };
}

static nlohmann::json to_json(const glm::quat& q) {
return nlohmann::json{ { "x", q.x }, { "y", q.y }, { "z", q.z }, { "w", q.w } };
}

static glm::vec3 from_json_vec3(const nlohmann::json& j) {
glm::vec3 result{};

if (j.contains("x")) {
result.x = j["x"].get<float>();
}

if (j.contains("y")) {
result.y = j["y"].get<float>();
}

if (j.contains("z")) {
result.z = j["z"].get<float>();
}

return result;
}

static glm::vec4 from_json_vec4(const nlohmann::json& j) {
glm::vec4 result{};

if (j.contains("x")) {
result.x = j["x"].get<float>();
}

if (j.contains("y")) {
result.y = j["y"].get<float>();
}

if (j.contains("z")) {
result.z = j["z"].get<float>();
}

if (j.contains("w")) {
result.w = j["w"].get<float>();
}

return result;
}

static glm::quat from_json_quat(const nlohmann::json& j) {
glm::quat result{};

if (j.contains("x")) {
result.x = j["x"].get<float>();
}

if (j.contains("y")) {
result.y = j["y"].get<float>();
}

if (j.contains("z")) {
result.z = j["z"].get<float>();
}

if (j.contains("w")) {
result.w = j["w"].get<float>();
}

return result;
}
}

0 comments on commit 4ce3c0e

Please sign in to comment.