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

Commit

Permalink
I forgot what I've done
Browse files Browse the repository at this point in the history
  • Loading branch information
椎名深雪 committed Nov 3, 2019
1 parent 41cb2dd commit 3f718ad
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 27 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ file(GLOB libcoreSource src/core/*.cpp
src/core/shapes/*.*
src/core/cameras/*.*
src/core/bsdfs/*.*
src/core/lights/*.*
src/core/integrators/*.*
src/core/samplers/*.*
external/tinyobjloader/tiny_obj_loader.cc
Expand Down
8 changes: 5 additions & 3 deletions src/api/light.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ namespace miyuki::core {
virtual void sampleLe(const Point2f &u1, const Point2f &u2, LightRaySample &sample) = 0;

};
class Scene;
struct VisibilityTester {
Ray shadowRay;

class AreaLight : public Light {
public:
virtual void setShape(Shape *shape) = 0;
bool visible(Scene &scene) ;
};

}
#endif //MIYUKIRENDERER_LIGHT_H
43 changes: 33 additions & 10 deletions src/api/mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
#include <api/serialize.hpp>
#include <api/accelerator.h>
#include <api/material.h>
#include <cereal/types/unordered_map.hpp>

namespace miyuki::core {
class Mesh;

class AreaLight;

/// SOA
Expand All @@ -42,18 +44,25 @@ namespace miyuki::core {


struct MeshTriangle {
AreaLight * light = nullptr;
AreaLight *light = nullptr;
Mesh *mesh = nullptr;
uint16_t name_id = -1;
uint32_t primID = -1;

MeshTriangle() = default;

const Point3f &vertex(size_t) const;
[[nodiscard]] const Point3f &vertex(size_t) const;

[[nodiscard]] const Normal3f &normal(size_t) const;

[[nodiscard]] const Point2f &texCoord(size_t) const;

const Normal3f &normal(size_t) const;

const Point2f &texCoord(size_t) const;
[[nodiscard]] const Vec3f Ng() const {
Vec3f e1 = (vertex(1) - vertex(0));
Vec3f e2 = (vertex(2) - vertex(0));
return e1.cross(e2).normalized();
}

bool intersect(const Ray &ray, Intersection &isct) const {
float u, v;
Expand Down Expand Up @@ -101,22 +110,35 @@ namespace miyuki::core {
uv.x = 1.0f - uv.x;
uv.y = 1.0f - uv.y;
}
sample.uv = uv;
sample.pdf = 1 / area();
sample.p = (1 - uv.x - uv.y) * vertex(0) + uv.x * vertex(1) + uv.y * vertex(1);
Vec3f e1 = (vertex(1) - vertex(0));
Vec3f e2 = (vertex(2) - vertex(0));
sample.normal = e1.cross(e2).normalized();
}

Float area() const {
[[nodiscard]] Float area() const {
return Vec3f(vertex(1) - vertex(0)).cross(vertex(2) - vertex(0)).length();
}

BSDF *getBSDF() const {
[[nodiscard]] BSDF *getBSDF() const {
return nullptr;
}

Material * getMaterial()const;
[[nodiscard]] Material *getMaterial() const;

[[nodiscard]] Vec3f positionAt(const Point2f &uv) const {
return lerp3(vertex(0), vertex(1), vertex(2), uv[0], uv[1]);
}

[[nodiscard]] Normal3f normalAt(const Point2f &uv) const {
return lerp3(normal(0), normal(1), normal(2), uv[0], uv[1]);
}

[[nodiscard]] Point2f texCoordAt(const Point2f &uv) const {
return lerp3(texCoord(0), texCoord(1), texCoord(2), uv[0], uv[1]);
}
};


Expand All @@ -128,14 +150,15 @@ namespace miyuki::core {
VertexData _vertex_data;
std::vector<Point3i> _indices;
std::vector<std::string> _names;
std::vector<std::shared_ptr<Material>> materials;
std::vector<std::shared_ptr<Material>> _materials;
std::unordered_map<std::string, std::shared_ptr<Material>> materials;
std::string filename;

MYK_DECL_CLASS(Mesh, "Mesh", interface = "Shape")

MYK_AUTO_SER(filename)
MYK_AUTO_SER(filename, materials)

MYK_AUTO_INIT(filename)
MYK_AUTO_INIT(filename, materials)

bool intersect(const Ray &ray, Intersection &isct) const {
return accelerator->intersect(ray, isct);
Expand Down
2 changes: 1 addition & 1 deletion src/api/shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

namespace miyuki::core {
struct ShadingPoint {
Point2f uv;
Point2f texCoord;
Normal3f Ns;
Normal3f Ng;
};
Expand Down
1 change: 1 addition & 0 deletions src/api/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace miyuki::core {
Point3f p;
Float pdf;
Normal3f normal;
Point2f uv;
};


Expand Down
3 changes: 0 additions & 3 deletions src/core/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ namespace miyuki::core {
camera->preprocess();
integrator->preprocess();
sampler->preprocess();
for (auto &i:shapes) {
i->preprocess();
}
Film film(filmDimension[0], filmDimension[1]);
auto scene = std::make_shared<Scene>();
for (const auto& i: shapes) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/integrators/rtao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ namespace miyuki::core {
for (int s = 0; s < spp; s++) {
CameraSample sample;
sampler->startNextSample();
camera->generateRay(sampler->next2D(), sampler->next2D(), Point2i{i, j},
Point2i{film.width, film.height},
camera->generateRay(sampler->next2D(), sampler->next2D(), Point2i(i, j),
Point2i(film.width, film.height),
sample);

Intersection isct;
Expand Down
65 changes: 65 additions & 0 deletions src/core/lights/arealight.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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 "arealight.h"
#include <api/mesh.h>


namespace miyuki::core {
void AreaLight::setTriangle(MeshTriangle *shape) {
this->triangle = shape;
emission = triangle->getMaterial()->emission;
}

Spectrum AreaLight::Li(ShadingPoint &sp) const {
return emission->evaluate(sp);
}

void
AreaLight::sampleLi(const Point2f &u, Intersection &isct, LightSample &sample, VisibilityTester &tester) const {
SurfaceSample surfaceSample;
triangle->sample(u, surfaceSample);
auto wi = surfaceSample.p - isct.p;
tester.shadowRay = Ray(isct.p, wi, RayBias, 1);
ShadingPoint sp;
sp.Ng = triangle->Ng();
sp.Ns = triangle->normalAt(surfaceSample.uv);
sp.texCoord = triangle->texCoordAt(surfaceSample.uv);
sample.Li = Li(sp);
sample.wi = wi.normalized();
sample.pdf = wi.lengthSquared() / sample.wi.absDot(surfaceSample.normal) * surfaceSample.pdf;
}

Float AreaLight::pdfLi(const Intersection &intersection, const Vec3f &wi) const {
Intersection _isct;
Ray ray(intersection.p, wi, RayBias);
if (!triangle->intersect(ray, _isct)) {
return 0.0f;
}
Float SA = triangle->area() * wi.absDot(_isct.Ng) / (_isct.distance * _isct.distance);
return 1.0f / SA;
}

void AreaLight::sampleLe(const Point2f &u1, const Point2f &u2, LightRaySample &sample) {
MIYUKI_NOT_IMPLEMENTED();
}
}
49 changes: 49 additions & 0 deletions src/core/lights/arealight.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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.

#ifndef MIYUKIRENDERER_AREALIGHT_H
#define MIYUKIRENDERER_AREALIGHT_H

#include <api/light.h>
#include <api/shader.h>
#include <api/serialize.hpp>

namespace miyuki::core {
class AreaLight final: public Light {
MeshTriangle *triangle = nullptr;
std::shared_ptr<Shader> emission;
public:
MYK_DECL_CLASS(AreaLight, "AreaLight", interface = "Light")

Spectrum Li(ShadingPoint &sp) const override;

void
sampleLi(const Point2f &u, Intersection &isct, LightSample &sample, VisibilityTester &tester) const override;

Float pdfLi(const Intersection &intersection, const Vec3f &wi) const override;

void sampleLe(const Point2f &u1, const Point2f &u2, LightRaySample &sample) override;

void setTriangle(MeshTriangle *shape);
};
}
#endif //MIYUKIRENDERER_AREALIGHT_H
13 changes: 10 additions & 3 deletions src/core/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,25 @@

#include <api/scene.h>
#include <api/detail/entity-funcs.h>
#include <core/lights/arealight.h>


namespace miyuki::core {
void Scene::preprocess() {
accelerator = std::make_shared<TopLevelBVHAccelerator>();
auto setLight = [](MeshTriangle *triangle) {

auto setLight = [=](MeshTriangle *triangle) {
auto mat = triangle->getMaterial();
if (mat->emission != nullptr) {
auto light = std::make_shared<AreaLight>();
light->setTriangle(triangle);
lights.emplace_back(light);
}
};
std::vector<Shape *> v;
for (auto &i:shapes) {
i->preprocess();
v.push_back(i.get());

i->foreach(setLight);
}
accelerator->build(v);
}
Expand Down
7 changes: 6 additions & 1 deletion src/core/shapes/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ namespace miyuki::core {
for (int i = 0; i < triangles.size(); i++) {
triangles[i].primID = i;
}
_materials.clear();
for (const auto &name : _names) {
auto mat = materials.at(name);
_materials.emplace_back(mat);
}
accelerator = std::dynamic_pointer_cast<Accelerator>(CreateEntity("BVHAccelerator"));
accelerator->build(this);
}
Expand Down Expand Up @@ -339,7 +344,7 @@ namespace miyuki::core {
}

Material *MeshTriangle::getMaterial() const {
return mesh->materials[name_id].get();
return mesh->_materials[name_id].get();
}
}

34 changes: 34 additions & 0 deletions src/core/visibility.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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 <api/light.h>
#include <api/scene.h>

namespace miyuki::core{
bool VisibilityTester::visible(miyuki::core::Scene &scene) {
Intersection isct;
if (!scene.intersect(shadowRay, isct) || isct.distance >= shadowRay.tMax - RayBias) {
return true;
}
return false;
}
}
9 changes: 5 additions & 4 deletions src/mesh-importer/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@
// 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>
#include <api/mesh.h>


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

0 comments on commit 3f718ad

Please sign in to comment.