Skip to content

Commit

Permalink
Added support for checked textures
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Jorquera committed Mar 12, 2024
1 parent 5feaabc commit 8cf20fa
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/intersectable.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Hit {
Vector _normal;
std::shared_ptr<Material> _material;
double _t;
double _u;
double _v;
bool _fronFace;

public:
Expand All @@ -24,12 +26,16 @@ class Hit {
void setNormal(const Vector& normal) { _normal = normal; }
void setMaterial(const std::shared_ptr<Material>& material) { _material = material; }
void setT(double t) { _t = t; }
void setU(double u) { _u = u; }
void setV(double v) { _v = v; }
void setFrontFace(bool frontFace) { _fronFace = frontFace; }

const Point& point() const { return _point; }
const Vector& normal() const { return _normal; }
const std::shared_ptr<Material>& material() const { return _material; }
double t() const { return _t; }
double u() const { return _u; }
double v() const { return _v; }
bool frontFace() const { return _fronFace; }

void setFaceNormal(const Ray& ray, const Vector& outwardNormal) {
Expand Down
26 changes: 24 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
#include "sphere.h"
#include "scene.h"
#include "bvh.h"
#include "texture.h"

using namespace std;

int main() {
void randomSpheres() {
shared_ptr<Scene> scene = make_shared<Scene>();

const auto materialGround = make_shared<Lambertian>(Color(0.5, 0.5, 0.5));
const auto checker = std::make_shared<CheckerTexture>(0.32, Color(.2, .3, .1), Color(.9, .9, .9));
const auto materialGround = make_shared<Lambertian>(checker);
scene->add(make_shared<Sphere>(Point(0.0, -1000.0, 0.0), 1000.0, materialGround));

for (auto a = -11; a < 11; a++) {
Expand Down Expand Up @@ -54,5 +56,25 @@ int main() {
bvh->add(make_shared<Bvh>(intersectables));

Camera().render(bvh, "image.ppm");
}

void twoSpheres() {
shared_ptr<Scene> scene = make_shared<Scene>();

const auto checker = make_shared<CheckerTexture>(0.8, Color(.2, .3, .1), Color(.9, .9, .9));

scene->add(make_shared<Sphere>(Point(0.0, -10.0, 0.0), 10.0, make_shared<Lambertian>(checker)));
scene->add(make_shared<Sphere>(Point(0.0, 10.0, 0.0), 10.0, make_shared<Lambertian>(checker)));

Camera camera(16.0 / 9.0, 50, 50, 400, 20.0, Vector(13.0, 2.0, 3.0), Vector(0.0, 0.0, 0.0), Vector(0.0, 1.0, 0.0), 0.0, 10.0);

camera.render(scene, "image.ppm");
}

int main() {
switch (2) {
case 1: randomSpheres(); break;
case 2: twoSpheres(); break;
}
return 0;
}
9 changes: 6 additions & 3 deletions src/material.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "intersectable.h"
#include "texture.h"

class Material {

Expand All @@ -15,16 +16,18 @@ class Lambertian : public Material {

private:

Color _albedo;
std::shared_ptr<Texture> _albedo;

public:

Lambertian(const Color& color):_albedo(color) {}
Lambertian(const Color& color):Lambertian(std::make_shared<SolidColor>(color)) {}
Lambertian(const std::shared_ptr<Texture>& texture): _albedo(texture) {}

bool scatter(const Ray& ray, const Hit& hit, Color& attenuation, Ray& scattered) const override {
auto direction = hit.normal() + Vector::randomUnitInUnitSphere();
if (direction.isNearZero()) direction = hit.normal();
scattered = Ray(hit.point(), direction, ray.time());
attenuation = _albedo;
attenuation = _albedo->color(hit.u(), hit.v(), hit.point());
return true;
}

Expand Down
11 changes: 11 additions & 0 deletions src/sphere.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "sphere.h"

#include <cmath>

using namespace std;

bool Sphere::intersects(const Ray& ray, const Interval& interval, Hit& hit) const {
Expand All @@ -23,7 +25,16 @@ bool Sphere::intersects(const Ray& ray, const Interval& interval, Hit& hit) cons
hit.setPoint(ray.at(hit.t()));
Vector outwardNormal = (hit.point() - currentCenter) / _radius;
hit.setFaceNormal(ray, outwardNormal);
Sphere::computeUV(outwardNormal, hit);
hit.setMaterial(_material);

return true;
}

void Sphere::computeUV(const Point& point, Hit& hit) {
const auto theta = acos(-point.y());
const auto phi = atan2(-point.z(), point.x()) + M_PI;

hit.setU(phi / (2.0 * M_PI));
hit.setV(theta / M_PI);
}
2 changes: 2 additions & 0 deletions src/sphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ class Sphere : public Intersectable {

Aabb boundingBox() const override { return _boundingBox; }

static void computeUV(const Point& point, Hit& hit);

};
64 changes: 64 additions & 0 deletions src/texture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#pragma once

#include "vector.h"

#include <memory>

class Texture {

public:

virtual ~Texture() = default;
virtual Color color(double u, double v, const Point& point) = 0;

};

class SolidColor : public Texture {

private:

Color _color;

public:

SolidColor(const Color& color):_color(color) {}

Color color(double u, double v, const Point &point) override {
return _color;
}

};

class CheckerTexture : public Texture {

private:

double _invScale;
std::shared_ptr<Texture> _even;
std::shared_ptr<Texture> _odd;

public:

CheckerTexture(double scale,
const std::shared_ptr<Texture>& even,
const std::shared_ptr<Texture> odd):
_invScale(1.0 / scale), _even(even), _odd(odd) {}

CheckerTexture(double scale,
const Color& color1,
const Color& color2):
CheckerTexture(scale,
std::make_shared<SolidColor>(color1),
std::make_shared<SolidColor>(color2)) {}

Color color(double u, double v, const Point &point) override {
const auto xInteger = int(std::floor(_invScale * point.x()));
const auto yInteger = int(std::floor(_invScale * point.y()));
const auto zInteger = int(std::floor(_invScale * point.z()));

const bool isEven = (xInteger + yInteger + zInteger) % 2 == 0;

return isEven ? _even->color(u, v, point) : _odd->color(u, v, point);
}

};

0 comments on commit 8cf20fa

Please sign in to comment.