Skip to content
This repository has been archived by the owner on Mar 19, 2020. It is now read-only.

Commit

Permalink
strangle error in sahbvh
Browse files Browse the repository at this point in the history
  • Loading branch information
椎名深雪 committed Oct 31, 2019
1 parent 60f841d commit feccfef
Show file tree
Hide file tree
Showing 18 changed files with 217 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ build/
generated.*
cmake-*/
include/

# Imported mesh files
*.mesh
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ file(GLOB libcoreSource src/core/*.cpp
add_library(core ${MiyukiAPI} ${libcoreSource})
target_link_libraries(core fmt)
add_executable(myk-cli src/standalone-renderer/main.cpp ${MiyukiAPI})
add_executable(mesh-importer src/mesh-importer/importer.cpp ${MiyukiAPI})
target_link_libraries(mesh-importer core)
target_link_libraries(myk-cli core)
Binary file added data/breakfast_room/breakfast_room.mesh
Binary file not shown.
32 changes: 31 additions & 1 deletion data/breakfast_room/scene.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
{

"camera": {
"type": "PerspectiveCamera",
"fov": 80,
"translate": [
0,
0,
0
],
"rotate": [
0,
0,
0
]
},
"integrator": {
"type": "RTAO",
"spp": 1
},
"sampler": {
"type": "RandomSampler"
},
"shapes": [
{
"type": "Mesh",
"filename": "./breakfast_room.mesh"
}
],
"filmDimension": [
1080,
720
]
}
Empty file added doc/doc.md
Empty file.
4 changes: 4 additions & 0 deletions src/api/detail/entity-funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ namespace miyuki {

MYK_PUBLIC_API std::shared_ptr<Entity> CreateEntityParams(const nlohmann::json &);

MYK_PUBLIC_API void BindEntity(const std::shared_ptr<Entity> &entity, const std::string &name);

MYK_PUBLIC_API std::shared_ptr<Entity> GetEntity(const std::string &name);

template<class T>
void Register() {
T::_register();
Expand Down
1 change: 1 addition & 0 deletions src/api/entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <mutex>

#include <nlohmann/json_fwd.hpp>
#include <api/detail/entity-funcs.h>

namespace miyuki {
using json = nlohmann::json;
Expand Down
2 changes: 1 addition & 1 deletion src/api/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <api/scene.h>
#include <api/camera.h>
#include <api/sampler.h>

#include <api/shape.h>
#include <cereal/types/vector.hpp>


Expand Down
24 changes: 11 additions & 13 deletions src/api/serialize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ namespace miyuki {

inline void from_json(const json &j, Matrix4 &m) {
for (int i = 0; i < 4; i++) {
m[i] = j[i].get < Vec < Float, 4 >> ();
m[i] = j[i].get<Vec<Float, 4 >>();
}
}

Expand All @@ -101,19 +101,19 @@ namespace miyuki {
transform = Transform(j.get<Matrix4>());
}

template<class T>
void from_json(const json &j, std::shared_ptr<T>& p) {
p = std::dynamic_pointer_cast<T>(CreateEntityParams(j));
}
}
namespace nlohmann {
template<typename T>
struct adl_serializer<std::shared_ptr<T>> {
static void to_json(json &j, const std::shared_ptr<T> &opt) {
MIYUKI_NOT_IMPLEMENTED();
}

template<class T>
void from_json(const json &j, std::vector<T>& vec){
for(auto& i: j){
vec.emplace_back(i.get<T>());
static void from_json(const json &j, std::shared_ptr<T> &p) {
p = std::dynamic_pointer_cast<T>(miyuki::CreateEntityParams(j));
}
}
};
}

namespace miyuki::serialize {
using nlohmann::json;

Expand Down Expand Up @@ -333,7 +333,6 @@ namespace miyuki::serialize {
};



struct InitializeVisitor {
const json &params;

Expand Down Expand Up @@ -498,5 +497,4 @@ namespace miyuki::serialize {
}



#endif //MIYUKIRENDERER_SERIALIZE_HPP
16 changes: 14 additions & 2 deletions src/core/accelerators/sahbvh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

namespace miyuki::core {
int BVHAccelerator::recursiveBuild(int begin, int end, int depth) {
// log::log("depth: {}, primitives:{} \n", depth, end - begin);
log::log("depth: {}, primitives:{} \n", depth, end - begin);
Bounds3f box{{MaxFloat, MaxFloat, MaxFloat},
{MinFloat, MinFloat, MinFloat}};
Bounds3f centroidBound{{MaxFloat, MaxFloat, MaxFloat},
Expand Down Expand Up @@ -80,6 +80,18 @@ namespace miyuki::core {
for (int i = begin; i < end; i++) {
auto offset = centroidBound.offset(primitive[i]->getBoundingBox().centroid())[axis];
int b = std::min<int>(nBuckets - 1, std::floor(offset * nBuckets));
if (b < 0) {
for (int i = begin; i < end; i++) {
fmt::print("{} {} {}\n",
primitive[i]->getBoundingBox().pMin[0],
primitive[i]->getBoundingBox().pMin[1],
primitive[i]->getBoundingBox().pMin[2]);
fmt::print("{} {} {}\n",
primitive[i]->getBoundingBox().pMax[0],
primitive[i]->getBoundingBox().pMax[1],
primitive[i]->getBoundingBox().pMax[2]);
}
}
buckets[b].count++;
buckets[b].bound = buckets[b].bound.unionOf(primitive[i]->getBoundingBox());
}
Expand Down Expand Up @@ -118,7 +130,6 @@ namespace miyuki::core {
BVHNode &node = nodes.back();
node.box = box;
node.count = -1;
nodes.push_back(node);
nodes[ret].left = recursiveBuild(begin, mid - &primitive[0], depth + 1);
nodes[ret].right = recursiveBuild(mid - &primitive[0], end, depth + 1);

Expand All @@ -128,6 +139,7 @@ namespace miyuki::core {

void BVHAccelerator::build(const std::vector<Primitive *> &primitives) {
nodes.clear();
log::log("Building BVH\n");
primitive = primitives;
recursiveBuild(0, primitive.size(), 0);
log::log("BVH nodes:{}\n", nodes.size());
Expand Down
5 changes: 5 additions & 0 deletions src/core/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <api/shape.h>
#include <api/sampler.h>
#include <api/film.h>
#include <api/log.hpp>

namespace miyuki::core {
void SceneGraph::render(const std::string &outImageFile) {
Expand All @@ -21,6 +22,10 @@ namespace miyuki::core {
scene->primitives.emplace_back(i);
}
scene->preprocess();
log::log("Start Rendering...\n");
integrator->render(scene, camera, sampler, film);
film.writeImage(outImageFile);
log::log("Written to {}\n", outImageFile);

}
}
2 changes: 2 additions & 0 deletions src/core/integrators/rtao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
#include <api/parallel.h>
#include <api/film.h>
#include <api/sampling.h>
#include <api/log.hpp>

namespace miyuki::core {

void RTAO::render(const std::shared_ptr<Scene> &scene, const std::shared_ptr<Camera> &camera,
const std::shared_ptr<Sampler> &sampler, Film &film) {
for (int i = 0; i < film.width; i++) {
log::log("{}/{}\n", i + 1, film.width);
for (int j = 0; j < film.height; j++) {
for (int s = 0; s < spp; s++) {
CameraSample sample;
Expand Down
38 changes: 33 additions & 5 deletions src/core/serialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace miyuki {
return _instance;
}

std::unordered_map<std::string, std::weak_ptr<Entity>> bindings;
std::unordered_map<std::string, Type *> types;
std::unordered_map<std::string, std::set<std::string>> impls;
};
Expand Down Expand Up @@ -69,13 +70,40 @@ namespace miyuki {
}

std::shared_ptr<Entity> CreateEntityParams(const nlohmann::json &params) {
auto entity = CreateEntity(params.at("type").get<std::string>());
if (!entity) {
log::log("failed to create entity with type {}\n", params.at("type").get<std::string>());
if (params.is_string()) {
auto name = params.get<std::string>();
return GetEntity(name);
} else {
auto entity = CreateEntity(params.at("type").get<std::string>());
if (!entity) {
log::log("failed to create entity with type {}\n", params.at("type").get<std::string>());
return nullptr;
}
if (params.contains("@ref")) {
BindEntity(entity, params.at("@ref").get<std::string>());
}
entity->initialize(params);
return entity;
}
}

void BindEntity(const std::shared_ptr<Entity> &entity, const std::string &name) {
auto &bindings = EntityManager::instance()->bindings;
bindings[name] = entity;
}

std::shared_ptr<Entity> GetEntity(const std::string &name) {
auto &bindings = EntityManager::instance()->bindings;
auto iter = bindings.find(name);
if (iter == bindings.end()) {
return nullptr;
}
auto &wp = iter->second;
if (wp.expired()) {
bindings.erase(iter);
return nullptr;
}
entity->initialize(params);
return entity;
return wp.lock();
}

namespace serialize {
Expand Down
4 changes: 2 additions & 2 deletions src/core/shaders/common-shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace miyuki::core {

MYK_AUTO_INIT(value)

MYK_DECL_CLASS(FloatShader, "Shader.Float", interface = "Shader")
MYK_DECL_CLASS(FloatShader, "FloatShader", interface = "Shader")

[[nodiscard]] Spectrum evaluate(const ShadingPoint &point) const override {
return miyuki::core::Spectrum(value);
Expand All @@ -48,7 +48,7 @@ namespace miyuki::core {

MYK_AUTO_INIT(value)

MYK_DECL_CLASS(RGBShader, "Shader.RGB", interface = "Shader")
MYK_DECL_CLASS(RGBShader, "RGBShader", interface = "Shader")

[[nodiscard]] Spectrum evaluate(const ShadingPoint &point) const override {
return miyuki::core::Spectrum(value);
Expand Down
17 changes: 16 additions & 1 deletion src/core/shapes/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <api/log.hpp>
#include <fstream>


namespace miyuki::core {
template<class T>
static void write(std::vector<char> &buffer, const T &v) {
Expand Down Expand Up @@ -70,7 +71,12 @@ namespace miyuki::core {

void Mesh::preprocess() {
if (!_loaded) {
loadFromFile(filename);
auto ext = fs::path(filename).extension().string();
if (ext == ".mesh")
loadFromFile(filename);
else if (ext == ".obj") {
importFromFile(filename);
}
}
accelerator = std::dynamic_pointer_cast<Accelerator>(CreateEntity("BVHAccelerator"));
std::vector<Primitive *> primitives;
Expand Down Expand Up @@ -211,6 +217,15 @@ namespace miyuki::core {
return true;
}

void Mesh::writeToFile(const std::string &filename) {
std::vector<char> buffer;
toBinary(buffer);
this->filename = filename;
std::ofstream out(filename, std::ios::out | std::ios::binary);
out.write(buffer.data(), buffer.size());
}


void Mesh::toBinary(std::vector<char> &buffer) const {
writeString(buffer, "BINARY_MESH");
write(buffer, _names.size());
Expand Down
6 changes: 5 additions & 1 deletion src/core/shapes/mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ namespace miyuki::core {
return false;
}

Bounds3f getBoundingBox() const override {
[[nodiscard]] Bounds3f getBoundingBox() const override {
return Bounds3f{
min(vertex(0), min(vertex(1), vertex(2))),
max(vertex(0), max(vertex(1), vertex(2)))
Expand Down Expand Up @@ -156,6 +156,7 @@ namespace miyuki::core {
// TODO: set import directory
bool importFromFile(const std::string &filename);


void toBinary(std::vector<char> &buffer) const;

void fromBinary(const std::vector<char> &buffer);
Expand All @@ -164,6 +165,9 @@ namespace miyuki::core {

void preprocess() override;

// Alert! This changes Mesh::filename
void writeToFile(const std::string&filename);

};


Expand Down
35 changes: 35 additions & 0 deletions src/mesh-importer/importer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// MIT License
//
// Copyright (c) 2019 椎名深雪
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <core/shapes/mesh.h>


int main(int argc, char **argv) {
if (argc != 3) {
printf("Usage: mesh-importer src dst\n");
return 0;
}
miyuki::core::Mesh mesh;
mesh.importFromFile(argv[1]);
mesh.writeToFile(argv[2]);
return 0;
}

Loading

0 comments on commit feccfef

Please sign in to comment.