2023-11-26 08:53:55 +01:00
|
|
|
#include "CB3DJSONMeshFileLoader.h"
|
|
|
|
#include "coreutil.h"
|
2023-11-26 07:43:26 +01:00
|
|
|
#include "IAnimatedMesh.h"
|
2023-11-26 12:52:22 +01:00
|
|
|
#include "S3DVertex.h"
|
|
|
|
#include "SAnimatedMesh.h"
|
|
|
|
#include "SColor.h"
|
|
|
|
#include "CMeshBuffer.h"
|
|
|
|
#include "SMesh.h"
|
|
|
|
#include "vector3d.h"
|
|
|
|
#include "quaternion.h"
|
2023-11-26 07:43:26 +01:00
|
|
|
#include "IReadFile.h"
|
|
|
|
#include "path.h"
|
2023-11-26 11:10:04 +01:00
|
|
|
#include <iostream>
|
2023-11-26 09:37:35 +01:00
|
|
|
#include "os.h"
|
2023-11-26 15:30:26 +01:00
|
|
|
#include <string>
|
2023-11-26 11:10:04 +01:00
|
|
|
#include "json/json.hpp"
|
|
|
|
#include <array>
|
2023-11-26 09:50:44 +01:00
|
|
|
using json = nlohmann::json;
|
2023-11-26 07:48:13 +01:00
|
|
|
|
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace scene
|
|
|
|
{
|
|
|
|
|
2023-11-26 09:09:50 +01:00
|
|
|
//! Remember to remove this function. It's very dumb.
|
|
|
|
void println(const char* data) {
|
2023-11-26 09:37:35 +01:00
|
|
|
printf(data);
|
|
|
|
printf("\n");
|
2023-11-26 09:09:50 +01:00
|
|
|
}
|
2023-11-26 09:50:44 +01:00
|
|
|
const char * const boolToString(bool b)
|
|
|
|
{
|
|
|
|
return b ? "true" : "false";
|
|
|
|
}
|
2023-11-26 09:09:50 +01:00
|
|
|
|
|
|
|
|
2023-11-26 08:53:55 +01:00
|
|
|
// Class methods.
|
2023-11-26 08:03:32 +01:00
|
|
|
CB3DJSONMeshFileLoader::CB3DJSONMeshFileLoader()
|
|
|
|
{
|
2023-11-26 08:53:55 +01:00
|
|
|
|
2023-11-26 08:03:32 +01:00
|
|
|
}
|
2023-11-26 07:48:13 +01:00
|
|
|
|
2023-11-26 08:03:32 +01:00
|
|
|
bool CB3DJSONMeshFileLoader::isALoadableFileExtension(
|
|
|
|
const io::path& fileName) const
|
|
|
|
{
|
2023-11-26 08:53:55 +01:00
|
|
|
//* we could do multiple accepted extensions, like b3dj jb3d, j3d, etc. Talk with core devs about this.
|
2023-11-26 09:21:28 +01:00
|
|
|
//? For now, just JSON because it has vscode linting.
|
|
|
|
return core::hasFileExtension(fileName, "json");
|
2023-11-26 08:03:32 +01:00
|
|
|
}
|
2023-11-26 07:48:13 +01:00
|
|
|
|
2023-11-26 15:51:43 +01:00
|
|
|
/**
|
|
|
|
* Returns true if failure occurs.
|
|
|
|
*/
|
|
|
|
bool grabVec3f(json data, std::string key, irr::core::vector3df& refVec) {
|
2023-11-26 12:52:22 +01:00
|
|
|
|
2023-11-26 15:51:43 +01:00
|
|
|
if (data.contains(key) && data[key].is_array() && data[key].size() == 3) {
|
|
|
|
auto jsonVec3 = data[key];
|
2023-11-26 12:52:22 +01:00
|
|
|
int i = 0;
|
2023-11-26 15:51:43 +01:00
|
|
|
for (auto reference = jsonVec3.begin(); reference != jsonVec3.end(); ++reference) {
|
2023-11-26 15:46:25 +01:00
|
|
|
auto value = *reference;
|
|
|
|
if (!value.is_number_integer()) {
|
2023-11-26 12:52:22 +01:00
|
|
|
os::Printer::log("Error, position in NODE must be an array of 3 integers!", ELL_WARNING);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
switch (i){
|
|
|
|
case 0:
|
2023-11-26 15:51:43 +01:00
|
|
|
refVec.X = value;
|
2023-11-26 12:52:22 +01:00
|
|
|
break;
|
|
|
|
case 1:
|
2023-11-26 15:51:43 +01:00
|
|
|
refVec.Y = value;
|
2023-11-26 12:52:22 +01:00
|
|
|
break;
|
|
|
|
case 2:
|
2023-11-26 15:51:43 +01:00
|
|
|
refVec.Z = value;
|
2023-11-26 12:52:22 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2023-11-26 15:46:25 +01:00
|
|
|
} else {
|
2023-11-26 15:51:43 +01:00
|
|
|
const auto stringWarning = std::string("Error, ").append(key).append(" in NODE must be an array of 3 integers!");
|
|
|
|
os::Printer::log(stringWarning.c_str(), ELL_WARNING);
|
2023-11-26 15:46:25 +01:00
|
|
|
return true;
|
2023-11-26 12:52:22 +01:00
|
|
|
}
|
2023-11-26 15:51:43 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns if errored.
|
|
|
|
bool parseNode(json data, SMeshBuffer* meshBuffer) {
|
|
|
|
|
|
|
|
auto position = irr::core::vector3df{0,0,0};
|
|
|
|
auto scale = irr::core::vector3df{1,1,1};
|
|
|
|
auto rotation = irr::core::quaternion{0,0,0,1};
|
|
|
|
|
2023-11-26 15:53:47 +01:00
|
|
|
if (grabVec3f(data, "position", position)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (grabVec3f(data, "scale", scale)) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-11-26 15:51:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-11-26 12:52:22 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-11-26 11:25:42 +01:00
|
|
|
IAnimatedMesh* parseModel(json data) {
|
2023-11-26 11:10:04 +01:00
|
|
|
|
2023-11-26 12:52:22 +01:00
|
|
|
//! Remember: This is basically a linked tree.
|
2023-11-26 11:22:57 +01:00
|
|
|
|
2023-11-26 12:52:22 +01:00
|
|
|
SMesh* baseMesh(new SMesh {});
|
|
|
|
|
|
|
|
SMeshBuffer* meshBuffer(new SMeshBuffer {});
|
|
|
|
|
|
|
|
// And now we begin the recursion!
|
|
|
|
|
|
|
|
std::cout << data["NODE"] << "\n";
|
|
|
|
|
|
|
|
if (data.contains("NODE") && data["NODE"].is_object()) {
|
|
|
|
println("Yep, that's a node!");
|
|
|
|
|
2023-11-26 15:46:25 +01:00
|
|
|
// If it fails, give up basically.
|
2023-11-26 12:52:22 +01:00
|
|
|
if (parseNode(data["NODE"], meshBuffer)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SAnimatedMesh* animatedMesh(new SAnimatedMesh {});
|
|
|
|
animatedMesh->addMesh(baseMesh);
|
|
|
|
|
|
|
|
return animatedMesh;
|
2023-11-26 11:10:04 +01:00
|
|
|
}
|
|
|
|
|
2023-11-26 08:53:55 +01:00
|
|
|
IAnimatedMesh* CB3DJSONMeshFileLoader::createMesh(io::IReadFile* file) {
|
2023-11-26 09:09:50 +01:00
|
|
|
|
2023-11-26 15:30:26 +01:00
|
|
|
|
2023-11-26 09:09:50 +01:00
|
|
|
// Less than zero? What is this file a black hole?
|
|
|
|
if (file->getSize() <= 0) {
|
2023-11-26 09:37:35 +01:00
|
|
|
os::Printer::log("B3D JSON severe error! File size is 0!", ELL_WARNING);
|
2023-11-26 09:09:50 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
2023-11-26 09:50:44 +01:00
|
|
|
|
2023-11-26 09:37:35 +01:00
|
|
|
// So here we turn this mangled disaster into a C string.
|
2023-11-26 11:22:57 +01:00
|
|
|
// Please consider this the equivalent of duct taping a chainsaw onto a car to cut your lawn.
|
2023-11-26 15:30:26 +01:00
|
|
|
// char* buffer = new char[file->getSize()];
|
|
|
|
auto buffer = std::make_unique<char[]>(file->getSize());
|
2023-11-26 09:50:44 +01:00
|
|
|
|
|
|
|
// Now we read that dang JSON.
|
2023-11-26 15:30:26 +01:00
|
|
|
file->read(buffer.get(), file->getSize());
|
|
|
|
|
|
|
|
char* output = buffer.get();
|
2023-11-26 09:50:44 +01:00
|
|
|
|
2023-11-26 15:30:26 +01:00
|
|
|
// Irrlicht doesn't null terminate the raw output. So we must doe it ourself.
|
2023-11-26 15:46:25 +01:00
|
|
|
output[file->getSize()] = '\0';
|
2023-11-26 09:50:44 +01:00
|
|
|
|
2023-11-26 15:32:28 +01:00
|
|
|
// We have to catch a JSON parse error or else the game will segfault.
|
2023-11-26 15:30:26 +01:00
|
|
|
json data;
|
|
|
|
|
|
|
|
try {
|
|
|
|
data = json::parse(output);
|
|
|
|
} catch (const json::parse_error& e) {
|
|
|
|
std::cout << "message: " << e.what() << '\n'
|
|
|
|
<< "exception id: " << e.id << '\n'
|
|
|
|
<< "byte position of error: " << e.byte << std::endl;
|
|
|
|
os::Printer::log("JSON: Failed to parse!", ELL_WARNING);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2023-11-26 11:10:04 +01:00
|
|
|
|
2023-11-26 11:14:31 +01:00
|
|
|
// Now check some real basic elements of the JSON file.
|
2023-11-26 11:10:04 +01:00
|
|
|
if (!data.contains("format") || !data["format"].is_string() || data["format"] != "BB3DJSON") {
|
2023-11-26 11:14:31 +01:00
|
|
|
os::Printer::log("No format in B3D JSON! Expected: BB3DJSON", ELL_WARNING);
|
2023-11-26 11:10:04 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!data.contains("version") || !data["version"].is_number_integer() || data["version"] != 1) {
|
2023-11-26 11:14:31 +01:00
|
|
|
os::Printer::log("Wrong version in B3D JSON! Expected: 1", ELL_WARNING);
|
2023-11-26 11:10:04 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
2023-11-26 09:50:44 +01:00
|
|
|
|
2023-11-26 11:14:31 +01:00
|
|
|
// Now we can start doing things with it.
|
|
|
|
IAnimatedMesh* finalizedModel = parseModel(data);
|
2023-11-26 09:37:35 +01:00
|
|
|
|
|
|
|
|
2023-11-26 15:32:28 +01:00
|
|
|
return finalizedModel;
|
2023-11-26 08:53:55 +01:00
|
|
|
}
|
|
|
|
|
2023-11-26 07:48:13 +01:00
|
|
|
} // namespace scene
|
|
|
|
} // namespace irr
|