Skip to content

Commit

Permalink
Fixed scene::simpleCast
Browse files Browse the repository at this point in the history
objects further away from the point than the light is will no longer cast a shadow on the point
  • Loading branch information
rbxb committed Feb 14, 2019
1 parent a0fed78 commit 65a0f58
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 40 deletions.
15 changes: 15 additions & 0 deletions src/canvas.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "canvas.h"
#include <fstream>
#include <algorithm>

v3 color::toV3(color c)
Expand Down Expand Up @@ -61,3 +62,17 @@ void canvas::set(size_t x, size_t y, color c)
data[p + 1] = c.g;
data[p + 2] = c.b;
}

void canvas::savePPM(char * path)
{
std::ofstream ofs;
ofs.open(path, std::ios::binary);
ofs << "P6\n" << width << " " << height << "\n255\n";
for (size_t y = 0; y < height; y++) {
for (size_t x = 0; x < width; x++) {
color c = get(x, y);
ofs << c.r << c.g << c.b;
}
}
ofs.close();
}
1 change: 1 addition & 0 deletions src/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class canvas {
size_t getHeight();
color get(size_t x, size_t y);
void set(size_t x, size_t y, color c);
void savePPM(char * path);
private:
size_t width;
size_t height;
Expand Down
10 changes: 10 additions & 0 deletions src/light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ v3 pointLight::dir(v3 p)
return normalize(orig - p);
}

float pointLight::d(v3 p)
{
return mag(orig - p);
}

v3 pointLight::getColor()
{
return color;
Expand All @@ -39,6 +44,11 @@ v3 directionalLight::dir(v3 p)
return dir_;
}

float directionalLight::d(v3 p)
{
return -1;
}

v3 directionalLight::getColor()
{
return color;
Expand Down
3 changes: 3 additions & 0 deletions src/light.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class light {
public:
virtual v3 dir(v3 p) = 0;
virtual float d(v3 p) = 0;
virtual v3 getColor() = 0;
};

Expand All @@ -13,6 +14,7 @@ class pointLight : public light {
pointLight();
pointLight(v3 orig_, v3 color_);
v3 dir(v3 p) override;
float d(v3 p) override;
v3 getColor() override;
private:
v3 orig;
Expand All @@ -24,6 +26,7 @@ class directionalLight : public light {
directionalLight();
directionalLight(v3 dir__, v3 color_);
v3 dir(v3 p) override;
float d(v3 p) override;
v3 getColor() override;
private:
v3 dir_;
Expand Down
12 changes: 1 addition & 11 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "render.h"
#include "view.h"
#include <vector>
#include <fstream>
#include "sphere.h"
#include "plane.h"
#include "light.h"
Expand Down Expand Up @@ -34,14 +33,5 @@ int main() {

render(sn, cvs, 10);

std::ofstream ofs;
ofs.open("./ppm/out.ppm", std::ios::binary);
ofs << "P6\n" << cvs->getWidth() << " " << cvs->getHeight() << "\n255\n";
for (size_t y = 0; y < cvs->getHeight(); y++) {
for (size_t x = 0; x < cvs->getWidth(); x++) {
color c = cvs->get(x, y);
ofs << c.r << c.g << c.b;
}
}
ofs.close();
cvs->savePPM("./ppm/out.ppm");
}
51 changes: 23 additions & 28 deletions src/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,37 +47,30 @@ v3 scene::cast(v3 orig, v3 dir, char count, float retained)
if (closestObject != nullptr) {
orig = dir * closestDistance + orig;
hit h = closestObject->at(orig);

//reflection
v3 hover = h.surfacen * 0.001f + orig;
v3 refld = reflect(dir, h.mappedn);
//fresnel
float albedo = std::min(h.albedo + h.albedo * fresnel(dir, refld), 1.0f);
//float albedo = h.albedo;
v3 reflColor = cast(hover, refld, count, retained * albedo);

//lighting
v3 lightColor = lighting(hover, h.mappedn);

//combine lighting and reflection
reflColor = min(reflColor * albedo + lightColor * (1 - albedo), 1);

//transparency
/*v3 sink;
if (dir * h.surfacen < 0) {
sink = -h.surfacen * 0.001f + orig;
if (h.albedo > 0) {
v3 refld = reflect(dir, h.mappedn);
float albedo = std::min(h.albedo + h.albedo * fresnel(dir, refld), 1.0f);
v3 reflColor = cast(hover, refld, count, retained * albedo);
lightColor = min(reflColor * albedo + lightColor * (1 - albedo), 1);
}
else {
sink = h.surfacen * 0.001f + orig;
}
v3 refrd = refract(dir, h.mappedn, h.refraction);
v3 refrColor = cast(sink, refrd, count, retained * (1 - h.opacity));

//combine reflect and transparency
reflColor = min(reflColor * h.opacity + refrColor * (1 - h.opacity), 1);*/
if (h.opacity < 1) {
v3 sink;
if (dir * h.surfacen < 0) {
sink = -h.surfacen * 0.001f + orig;
}
else {
sink = h.surfacen * 0.001f + orig;
}
v3 refrd = refract(dir, h.mappedn, h.refraction);
v3 refrColor = cast(sink, refrd, count, retained * (1 - h.opacity));
lightColor = min(lightColor * h.opacity + refrColor * (1 - h.opacity), 1);
}

//combine all with diffuse
c = c + multiply(h.c, reflColor);
c = c + multiply(h.c, lightColor);

//fog
float fog = std::min(closestDistance / fogDistance, 1.0f);
Expand All @@ -93,18 +86,20 @@ v3 scene::lighting(v3 orig, v3 dir)
v3 c = ambient;
for (light * l : lights) {
v3 ldir = l->dir(orig);
if (!simpleCast(orig, ldir)) {
float ld = l->d(orig);
if (!simpleCast(orig, ldir, ld)) {
float m = std::max(dir * ldir, 0.0f);
c = c + l->getColor() * m;
}
}
return min(c, 1);
}

bool scene::simpleCast(v3 orig, v3 dir)
bool scene::simpleCast(v3 orig, v3 dir, float ld)
{
for (object * o : objects) {
if (o->intersect(orig, dir) > 0) {
float d = o->intersect(orig, dir);
if (d > 0 && (ld < 0 || d < ld)) {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class scene {
view getView();
v3 cast(v3 orig, v3 dir, char count, float retained);
v3 lighting(v3 orig, v3 dir);
bool simpleCast(v3 orig, v3 dir);
bool simpleCast(v3 orig, v3 dir, float ld);
private:
view v;
std::vector<object*> objects;
Expand Down

0 comments on commit 65a0f58

Please sign in to comment.