Skip to content

Commit

Permalink
Enable normal interpolation.
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsy committed Oct 23, 2015
1 parent 882fe78 commit 55c4aa5
Show file tree
Hide file tree
Showing 28 changed files with 156 additions and 111 deletions.
4 changes: 2 additions & 2 deletions example/pathtracing_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ int main(int argc, char **argv) {

Scene scene;
Camera camera;
cornellBox(&scene, &camera, width, height);
// kittenBox(&scene, &camera, width, height);
// cornellBox(&scene, &camera, width, height);
kittenBox(&scene, &camera, width, height);
// kittenEnvmap(&scene, &camera, width, height);

Timer timer;
Expand Down
10 changes: 7 additions & 3 deletions src/bsdf/brdf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,16 @@ namespace spica {
}

void RefractiveBSDF::sample(const Vector3D& in, const Vector3D& normal, double rand1, double rand2, Vector3D* out, double* pdf) const {
const Vector3D orientingNormal = in.dot(normal) < 0.0 ? normal : -normal;
const bool into = normal.dot(orientingNormal) > 0.0;
const Vector3D orientN = in.dot(normal) < 0.0 ? normal : -normal;
const bool into = normal.dot(orientN) > 0.0;

Vector3D reflectdir, transmitdir;
double fresnelRe, fresnelTr;
const bool isTotalReflectance = helper::checkTotalReflection(into, in, normal, orientingNormal, &reflectdir, &transmitdir, &fresnelRe, &fresnelTr);
const bool isTotalReflectance =
helper::checkTotalReflection(into, in,
normal, orientN,
&reflectdir, &transmitdir,
&fresnelRe, &fresnelTr);

if (isTotalReflectance) {
(*out) = reflectdir;
Expand Down
4 changes: 2 additions & 2 deletions src/bsdf/bsdf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ namespace spica {

// Reflactive object should be considered differently
if (_type & BsdfType::Refractive) {
const bool into = in.dot(normal) < 0.0;
const Vector3D orientN = into ? normal : -normal;
const Vector3D orientN = in.dot(normal) < 0.0 ? normal : -normal;
const bool into = normal.dot(orientN) > 0.0;
Vector3D refdir, transdir;
double fresnelRe, fresnelTr;
if (helper::checkTotalReflection(into, in, normal, orientN,
Expand Down
16 changes: 11 additions & 5 deletions src/core/vbo.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,17 @@ namespace spica {

template <class T, typename std::enable_if<std::is_base_of<IShape, T>::value>::type *&>
void VBO::add(const T& shape, const Color& color) {
const std::vector<Triangle> tris = shape.triangulate();
for (int i = 0; i < tris.size(); i++) {
const Vector3D& normal = tris[i].normal();
for (int j = 0; j < 3; j++) {
add(tris[i][j], normal, color);
const Trimesh tris = shape.triangulate();
const int triID = _vertices.size();
for (int i = 0; i < tris.numVerts(); i++) {
const VertexData& v = tris.getVertexData(i);
add(v.pos(), v.normal(), color);
}

const std::vector<Triplet> faces = tris.getIndices();
for (int i = 0; i < faces.size(); i++) {
for(int k = 0; k < 3; k++) {
_indices.push_back(triID + faces[i][k]);
}
}
}
Expand Down
31 changes: 14 additions & 17 deletions src/light/area_light.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,33 @@
namespace spica {

AreaLight::AreaLight()
: _emittance()
, _triangles()
, _samplePdf()
, _totalArea(0.0) {
: _emittance{}
, _triangles{}
, _samplePdf{}
, _totalArea{0.0} {
}

AreaLight::AreaLight(const std::vector<Triangle>& triangles, const Color& emittance)
: _emittance(emittance)
, _triangles(triangles)
, _samplePdf()
, _totalArea() {
AreaLight::AreaLight(const Trimesh& tris, const Color& emittance)
: _emittance{emittance}
, _triangles{}
, _samplePdf{}
, _totalArea{} {
for (int i = 0; i < tris.numFaces(); i++) {
_triangles.push_back(tris.getTriangle(i));
}
calcSamplePdf();
}

AreaLight::~AreaLight() {
}

AreaLight::AreaLight(const AreaLight& l)
: _emittance()
, _triangles()
, _samplePdf()
, _totalArea(0.0) {
: AreaLight{} {
this->operator=(l);
}

AreaLight::AreaLight(AreaLight&& l)
: _emittance()
, _triangles()
, _samplePdf()
, _totalArea(0.0) {
: AreaLight{} {
this->operator=(std::move(l));
}

Expand Down
2 changes: 1 addition & 1 deletion src/light/area_light.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace spica {

public:
AreaLight();
AreaLight(const std::vector<Triangle>& triangles, const Color& emittance);
AreaLight(const Trimesh& triangles, const Color& emittance);
AreaLight(const AreaLight& l);
AreaLight(AreaLight&& l);

Expand Down
2 changes: 1 addition & 1 deletion src/light/lighting.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace spica {
return *this;
}

Lighting Lighting::asAreaLight(const std::vector<Triangle>& triangles,
Lighting Lighting::asAreaLight(const Trimesh& triangles,
const Color& emittance) {
Lighting l;
l._ptr = std::make_unique<AreaLight>(triangles, emittance);
Expand Down
2 changes: 1 addition & 1 deletion src/light/lighting.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace spica {

/** Initialize lighting as area light
*/
static Lighting asAreaLight(const std::vector<Triangle>& triangles, const Color& emittance);
static Lighting asAreaLight(const Trimesh& triangles, const Color& emittance);

LightSample sample(Stack<double>& rstack) const;
Color directLight(const Vector3D& dir) const;
Expand Down
13 changes: 2 additions & 11 deletions src/renderer/pathtrace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ namespace spica {
if (bsdf.type() & BsdfType::Bssrdf) {
Assertion(_integrator != nullptr,
"Subsurface intergrator is NULL !!");

bssrdfRad = bsdf.sampleBssrdf(ray.direction(),
isect.position(),
isect.normal(),
Expand Down Expand Up @@ -187,7 +188,7 @@ namespace spica {
// Acquire random numbers
const double rands[2] = { rstk.pop(), rstk.pop() };

const BSDF& bsdf = scene.getBsdf(triID);
const BSDF& bsdf = scene.getBsdf(triID);

if (bsdf.type() & BsdfType::Scatter) {
// Scattering surface
Expand Down Expand Up @@ -220,19 +221,9 @@ namespace spica {
Vector3D nextdir;
bsdf.sample(in, n, rands[0], rands[1], &nextdir, &pdf);

Vector3D on = in.dot(n) < 0.0 ? n : -n;

Intersection isect;
if (scene.intersect(Ray(v, nextdir), &isect)) {
if (scene.isLightCheck(isect.objectID())) {
if (bsdf.type() == BsdfType::Refractive) {
if(on.dot(nextdir) < 0.0) {
// printf("pdf = %f\n", pdf);
// return {};
} else {
// printf("pdf = %f\n", pdf);
}
}
return (refl * scene.directLight(nextdir)) / pdf;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/scenes/predefined_scenes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ namespace spica {
//scene->add(Quad(l00, l10, l11, l01), LambertianBRDF::factory(Color(0.0, 0.0, 0.0)), Color(32.0, 32.0, 32.0), true);

// Back light
scene->setAreaLight(Sphere(Vector3D(-5.0, 5.0, -5.0), 3.0),
scene->setAreaLight(Sphere(Vector3D(5.0, 5.0, 5.0), 2.0),
Color(32.0, 32.0, 32.0));

// Walls
Expand Down
39 changes: 24 additions & 15 deletions src/scenes/scene.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace spica {
std::vector<unsigned int> _bsdfIds;
std::vector<unsigned int> _lightIds;

std::vector<BSDF> _bsdfs;
std::vector<BSDF> _bsdfs;
std::shared_ptr<IAccel> _accel;

Lighting _lighting;
Expand Down Expand Up @@ -102,8 +102,8 @@ namespace spica {

void setEnvmap(const Image& image, const Camera& camera) {
const Sphere& shape = this->boundingSphere(camera);
std::vector<Triangle> tris = shape.triangulate();
const int newTris = static_cast<int>(tris.size());
Trimesh tris = shape.triangulate();
const int newTris = static_cast<int>(tris.numFaces());
const int nowTris = static_cast<int>(_triangles.size());
_lighting = Lighting::asEnvmap(shape, image);

Expand Down Expand Up @@ -197,9 +197,10 @@ namespace spica {

if (triID != -1) {
Color color;
double u = hitpoint.texcoord().x();
double v = hitpoint.texcoord().y();
if (_triangles[triID].isTextured()) {
double u = hitpoint.texcoord().x();
double v = hitpoint.texcoord().y();
// Compute texture coordinates
const Vector2D t0 = _vertices[_triangles[triID][0]].texcoord();
const Vector2D t1 = _vertices[_triangles[triID][1]].texcoord();
const Vector2D t2 = _vertices[_triangles[triID][2]].texcoord();
Expand All @@ -210,6 +211,13 @@ namespace spica {
} else {
color = getBsdf(triID).reflectance();
}

const Vector3D n0 = _vertices[_triangles[triID][0]].normal();
const Vector3D n1 = _vertices[_triangles[triID][1]].normal();
const Vector3D n2 = _vertices[_triangles[triID][2]].normal();
const Vector3D n = (n0 + u * (n1 - n0) + v * (n2 - n0)).normalized();

hitpoint.setNormal(n);
(*isect) = Intersection(triID, hitpoint, color);
} else {
(*isect) = Intersection();
Expand Down Expand Up @@ -248,14 +256,14 @@ namespace spica {
addBsdf(bsdf, trip.size());
}

void setAreaLight(const std::vector<Triangle>& tris, const Color& emission) {
void setAreaLight(const Trimesh& tris, const Color& emission) {
addTriangles(tris);

// Set as area light
_lighting = Lighting::asAreaLight(tris, emission);

// If new object is a light, store triangle indices
const int newTris = static_cast<int>(tris.size());
const int newTris = static_cast<int>(tris.numFaces());
const int nowTris = static_cast<int>(_triangles.size()) - newTris;
for (int i = 0; i < newTris; i++) {
_lightIds.push_back(nowTris + i);
Expand All @@ -266,14 +274,15 @@ namespace spica {
}

private:
void addTriangles(const std::vector<Triangle>& tris) {
const int newTris = static_cast<int>(tris.size());
for (int i = 0; i < newTris; i++) {
const int vid = static_cast<int>(_vertices.size());
for (int k = 0; k < 3; k++) {
_vertices.emplace_back(tris[i][k]);
}
_triangles.emplace_back(vid, vid + 1, vid + 2);
void addTriangles(const Trimesh& tris) {
const int vid = static_cast<int>(_vertices.size());
for (int i = 0; i < tris.numVerts(); i++) {
_vertices.push_back(tris.getVertexData(i));
}

const std::vector<Triplet> faces = tris.getIndices();
for (int i = 0; i < faces.size(); i++) {
_triangles.emplace_back(vid + faces[i][0], vid + faces[i][1], vid + faces[i][2]);
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/shape/bbox.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "../core/common.h"

#include "trimesh.h"

namespace spica {

BBox::BBox()
Expand Down Expand Up @@ -122,9 +124,9 @@ namespace spica {
return true;
}

std::vector<Triangle> BBox::triangulate() const {
Trimesh BBox::triangulate() const {
// TODO: this is STAB
return std::vector<Triangle>{};
return Trimesh{};
}

} // namespace spica
Expand Down
2 changes: 1 addition & 1 deletion src/shape/bbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace spica {
int maximumExtent() const;

//! Triangulate
std::vector<Triangle> triangulate() const override;
Trimesh triangulate() const override;

//! Total area
double area() const override;
Expand Down
4 changes: 2 additions & 2 deletions src/shape/disk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "../renderer/ray.h"
#include "../renderer/renderer_helper.h"

#include "triangle.h"
#include "trimesh.h"

namespace spica {

Expand Down Expand Up @@ -56,7 +56,7 @@ namespace spica {
return PI * _radius * _radius;
}

std::vector<Triangle> Disk::triangulate() const {
Trimesh Disk::triangulate() const {
Vector3D u, v;
helper::calcLocalCoords(this->_normal, &u, &v);

Expand Down
2 changes: 1 addition & 1 deletion src/shape/disk.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace spica {

double area() const override;

std::vector<Triangle> triangulate() const override;
Trimesh triangulate() const override;

inline Vector3D center() const { return _center; }
inline Vector3D normal() const { return _normal; }
Expand Down
2 changes: 1 addition & 1 deletion src/shape/meshio.cc
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ namespace spica {
ifs.close();

if (!hasTexture) {
(*trimesh) = Trimesh(vertices, texcoords, vertIDs);
(*trimesh) = Trimesh(vertices, vertIDs);
} else {
trimesh->resize(vertIDs.size() * 3, vertIDs.size());
for (int i = 0; i < vertIDs.size(); i++) {
Expand Down
5 changes: 4 additions & 1 deletion src/shape/plane.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "../core/common.h"
#include "../renderer/ray.h"

#include "trimesh.h"

namespace spica {

Plane::Plane()
Expand Down Expand Up @@ -49,9 +51,10 @@ namespace spica {
return INFTY;
}

std::vector<Triangle> Plane::triangulate() const {
Trimesh Plane::triangulate() const {
Assertion(false, "Plane has infinite area, "
"which cannot be triangulated!!");
return Trimesh{};
}

} // namespace spica
2 changes: 1 addition & 1 deletion src/shape/plane.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace spica {

double area() const override;

std::vector<Triangle> triangulate() const override;
Trimesh triangulate() const override;

inline double distance() const { return _distance; }
inline Vector3D normal() const { return _normal; }
Expand Down
12 changes: 6 additions & 6 deletions src/shape/quad.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "../math/vector3d.h"
#include "../renderer/ray.h"

#include "triangle.h"
#include "trimesh.h"

namespace spica {

Expand Down Expand Up @@ -60,11 +60,11 @@ namespace spica {
return _points[id];
}

std::vector<Triangle> Quad::triangulate() const {
std::vector<Triangle> retval(2);
retval[0] = tr(0, 1, 2);
retval[1] = tr(0, 2, 3);
return std::move(retval);
Trimesh Quad::triangulate() const {
std::vector<Triangle> tris(2);
tris[0] = tr(0, 1, 2);
tris[1] = tr(0, 2, 3);
return std::move(Trimesh{tris});
}

Vector3D Quad::normal() const {
Expand Down
Loading

0 comments on commit 55c4aa5

Please sign in to comment.