Browse Source

Add support for .gltf file reading with 1 buffer binary

master
Riyyi 3 years ago
parent
commit
68b2e93969
  1. 14
      inferno/src/inferno/io/file.cpp
  2. 2
      inferno/src/inferno/io/file.h
  3. 23
      inferno/src/inferno/io/gltffile.cpp
  4. 2
      inferno/src/inferno/io/gltffile.h

14
inferno/src/inferno/io/file.cpp

@ -6,7 +6,7 @@
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
std::ifstream ifstream(path);
@ -26,14 +26,22 @@ namespace Inferno {
}
// 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
ifstream.read(buffer.get(), length);
ifstream.close();
// Null termination
buffer[length] = '\0';
return buffer;
}
std::string File::read(const std::string &path)
{
// 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)

2
inferno/src/inferno/io/file.h

@ -3,6 +3,7 @@
#include <fstream> // std::ifstream, std::ofstream
#include <iomanip> // std::setfill, std::setw
#include <memory> // std::shared_ptr
#include <string> // std::string
#include "inferno/assert.h"
@ -13,6 +14,7 @@ namespace Inferno {
class File {
public:
static std::shared_ptr<char[]> raw(const std::string& path);
static std::string read(const std::string& path);
static int32_t length(const std::string& path, std::ifstream& ifstream);

23
inferno/src/inferno/io/gltffile.cpp

@ -7,6 +7,7 @@
#include "inferno/io/gltffile.h"
#include "inferno/io/log.h"
#include "inferno/util/string.h"
#include "inferno/util/json.h"
namespace Inferno {
@ -18,9 +19,10 @@ namespace Inferno {
return glb(path);
}
else if (extension.compare(".gltf") == 0) {
ASSERT_NOT_REACHED();
return gltf(path);
}
ASSERT(false, "GltfFile unknown file extension!");
return {};
}
@ -98,4 +100,23 @@ namespace Inferno {
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

2
inferno/src/inferno/io/gltffile.h

@ -15,6 +15,8 @@ namespace Inferno {
private:
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[]>, std::shared_ptr<char[]>> gltf(const std::string& path);
};
} // namespace Inferno

Loading…
Cancel
Save