Add support for .gltf file reading with 1 buffer binary
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
namespace Inferno {
|
namespace Inferno {
|
||||||
|
|
||||||
std::string File::read(const std::string &path)
|
std::shared_ptr<char[]> File::raw(const std::string& path)
|
||||||
{
|
{
|
||||||
// Create input stream object and open file
|
// Create input stream object and open file
|
||||||
std::ifstream ifstream(path);
|
std::ifstream ifstream(path);
|
||||||
@@ -26,14 +26,22 @@ namespace Inferno {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Allocate memory filled with zeros
|
// Allocate memory filled with zeros
|
||||||
auto buffer = std::make_unique<char[]>(length);
|
auto buffer = std::shared_ptr<char[]>(new char[length + 1]);
|
||||||
|
|
||||||
// Fill buffer with file contents
|
// Fill buffer with file contents
|
||||||
ifstream.read(buffer.get(), length);
|
ifstream.read(buffer.get(), length);
|
||||||
ifstream.close();
|
ifstream.close();
|
||||||
|
|
||||||
|
// Null termination
|
||||||
|
buffer[length] = '\0';
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string File::read(const std::string &path)
|
||||||
|
{
|
||||||
// Create string from the buffer and return
|
// Create string from the buffer and return
|
||||||
return std::string(buffer.get(), length);
|
return std::string(raw(path).get());
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t File::length(const std::string& path, std::ifstream& ifstream)
|
int32_t File::length(const std::string& path, std::ifstream& ifstream)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <fstream> // std::ifstream, std::ofstream
|
#include <fstream> // std::ifstream, std::ofstream
|
||||||
#include <iomanip> // std::setfill, std::setw
|
#include <iomanip> // std::setfill, std::setw
|
||||||
|
#include <memory> // std::shared_ptr
|
||||||
#include <string> // std::string
|
#include <string> // std::string
|
||||||
|
|
||||||
#include "inferno/assert.h"
|
#include "inferno/assert.h"
|
||||||
@@ -13,6 +14,7 @@ namespace Inferno {
|
|||||||
|
|
||||||
class File {
|
class File {
|
||||||
public:
|
public:
|
||||||
|
static std::shared_ptr<char[]> raw(const std::string& path);
|
||||||
static std::string read(const std::string& path);
|
static std::string read(const std::string& path);
|
||||||
static int32_t length(const std::string& path, std::ifstream& ifstream);
|
static int32_t length(const std::string& path, std::ifstream& ifstream);
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
#include "inferno/io/gltffile.h"
|
#include "inferno/io/gltffile.h"
|
||||||
#include "inferno/io/log.h"
|
#include "inferno/io/log.h"
|
||||||
#include "inferno/util/string.h"
|
#include "inferno/util/string.h"
|
||||||
|
#include "inferno/util/json.h"
|
||||||
|
|
||||||
namespace Inferno {
|
namespace Inferno {
|
||||||
|
|
||||||
@@ -18,9 +19,10 @@ namespace Inferno {
|
|||||||
return glb(path);
|
return glb(path);
|
||||||
}
|
}
|
||||||
else if (extension.compare(".gltf") == 0) {
|
else if (extension.compare(".gltf") == 0) {
|
||||||
ASSERT_NOT_REACHED();
|
return gltf(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ASSERT(false, "GltfFile unknown file extension!");
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,4 +100,23 @@ namespace Inferno {
|
|||||||
return { chunkData, chunkLengthInt };
|
return { chunkData, chunkLengthInt };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::pair<std::shared_ptr<char[]>, std::shared_ptr<char[]>> GltfFile::gltf(const std::string& path)
|
||||||
|
{
|
||||||
|
auto jsonContent = File::raw(path);
|
||||||
|
|
||||||
|
json json = json::parse(jsonContent.get());
|
||||||
|
ASSERT(Json::hasProperty(json, "buffers"), "GltfFile missing required property 'buffers'");
|
||||||
|
|
||||||
|
std::shared_ptr<char[]> binaryContent { nullptr };
|
||||||
|
for (auto& [key, buffer] : json["buffers"].items()) {
|
||||||
|
auto uri = Json::parseStringProperty(buffer, "uri", true);
|
||||||
|
ASSERT(uri, "GltfFile missing required property 'uri'");
|
||||||
|
ASSERT(uri.value().find("data:", 0) == std::string::npos, "GltfFile embedded binary data is unsupported");
|
||||||
|
binaryContent = File::raw("assets/gltf/" + uri.value());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { jsonContent, binaryContent };
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Inferno
|
} // namespace Inferno
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ namespace Inferno {
|
|||||||
private:
|
private:
|
||||||
static std::pair<std::shared_ptr<char[]>, std::shared_ptr<char[]>> glb(const std::string& path);
|
static std::pair<std::shared_ptr<char[]>, std::shared_ptr<char[]>> glb(const std::string& path);
|
||||||
static std::pair<std::shared_ptr<char[]>, uint32_t> readChunk(std::ifstream& ifstream, uint32_t offset, uint32_t type);
|
static std::pair<std::shared_ptr<char[]>, uint32_t> readChunk(std::ifstream& ifstream, uint32_t offset, uint32_t type);
|
||||||
|
|
||||||
|
static std::pair<std::shared_ptr<char[]>, std::shared_ptr<char[]>> gltf(const std::string& path);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Inferno
|
} // namespace Inferno
|
||||||
|
|||||||
Reference in New Issue
Block a user