Skip to content

Commit

Permalink
I don't want to write scene parse!
Browse files Browse the repository at this point in the history
  • Loading branch information
GraphicsEnthusiast committed Jan 14, 2024
1 parent d6d48e9 commit 0d936bc
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
2 changes: 2 additions & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ add_library(core STATIC
Texture.h
Transform.cpp
Transform.h
SceneParser.cpp
SceneParser.h
Utils.h
)

Expand Down
14 changes: 14 additions & 0 deletions core/SceneParser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "SceneParser.h"

void SceneParser::LoadFromJson(const std::string& fn, bool& is_succeed) {
json data = CreateJsonFromFile(fn, is_succeed);
if (!is_succeed) {
scene = NULL;

return;
}

RTCDevice rtc_device = rtcNewDevice(NULL);
scene = std::make_shared<Scene>(rtc_device);
Parse(data);
}
81 changes: 81 additions & 0 deletions core/SceneParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#pragma once

#include "Utils.h"
#include "Scene.h"

typedef nlohmann::json json;

inline bool HasExtension(const std::string& value, const std::string& ending) {
if (ending.size() > value.size()) {
return false;
}

return std::equal(
ending.rbegin(), ending.rend(), value.rbegin(),
[](char a, char b) { return std::tolower(a) == std::tolower(b); });
}

inline json CreateJsonFromFile(const std::string& fn, bool& is_succeed) {
std::ifstream fst;
fst.open(fn.c_str());
is_succeed = false;
fst.exceptions(std::ifstream::failbit || std::ifstream::badbit);
if (!fst.is_open()) {
printf("Open scene json failed! \n");

return json();
}
else {
is_succeed = true;
std::stringstream buffer;
buffer << fst.rdbuf();
std::string str = buffer.str();
if (HasExtension(fn, "bson")) {
return json::from_bson(str);
}
else {
return json::parse(str);
}
}
}

class SceneParser {
public:
SceneParser() = default;

void LoadFromJson(const std::string& fn, bool& is_succeed);

std::shared_ptr<Renderer> CreateRenderer();

private:
void Parse(const json& data);

void ParseCamera(const json& data);

void ParseLights(const json& data);

void ParseShapes(const json& data);

void ParseMaterials(const json& data);

void ParseMediums(const json& data);

void ParseSampler(const json& data);

void ParseFilter(const json& data);

void ParseIntegrator(const json& data);

private:
float width, height;
std::vector<std::shared_ptr<Light>> lights;
std::unordered_map<std::string, std::shared_ptr<Material>> materials;
std::unordered_map<std::string, std::shared_ptr<Medium>> mediums;
std::vector<Shape*> shapes;
std::shared_ptr<Camera> camera;
std::shared_ptr<Scene> scene;
std::shared_ptr<Sampler> sampler;
std::shared_ptr<Filter> filter;
std::shared_ptr<Integrator> integrator;
std::shared_ptr<PostProcessing> post;
};
6 changes: 5 additions & 1 deletion core/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
#include <filesystem>
#include <random>
#include <cmath>
#include <iostream>
#include <limits>
#include <memory>
#include <string>
#include <queue>
#include <map>
#include <fstream>
#include <vector>
#include <sstream>
#include <time.h>
#include <glad/glad.h>
#include <glfw/glfw3.h>
Expand All @@ -27,6 +28,7 @@
#include <glm/gtx/string_cast.hpp>
#include <embree3/rtcore.h>
#include <embree3/rtcore_ray.h>
#include <nlohmann/json.hpp>

template <int nSpectrumSamples>
class CoefficientSpectrum;
Expand Down Expand Up @@ -105,6 +107,8 @@ class HenyeyGreenstein;
class Medium;
class Homogeneous;

class SceneParser;

using Vector2u = glm::uvec2;
using Vector2i = glm::ivec2;
using Vector2f = glm::vec2;
Expand Down

0 comments on commit 0d936bc

Please sign in to comment.