Skip to content

Commit 574937f

Browse files
committed
refac: remove ambient_calculator
1 parent 375985a commit 574937f

15 files changed

+57
-78
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ cornell-depth: $(RELEASE_BUILD)
3333
$^ -i scenes/cornell-depth.yml -o output/final/cornell-depth.bmp -p4 -s1024
3434

3535
motion: $(RELEASE_BUILD)
36-
$^ -i scenes/motion.yml -o output/final/motion.bmp -p4 -s512
36+
$^ -i scenes/motion.yml -o output/final/motion.bmp -p3 -s512
3737

3838
cornell-2ball-ppm: $(SPPM_RELEASE_BUILD)
3939
$^ -i scenes/cornell-2ball.yml -o output/final/cornell-2ball-sppm.bmp -p 10000000 -n 50 -r 0.008

scenes/cornell-2ball.yml

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ world:
6666
Ka: 1 1 1
6767
texture:
6868
file: assets/textures/brick.png
69+
normal_texture:
70+
file: assets/textures/normal.png
6971

7072
- type: plane # bottom
7173
normal: 0 1 0

scenes/motion.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ camera:
44
pos: 0, 1, 3
55
dir: 0, 0, -1
66
up: 0, 1, 0
7-
width: 1920
8-
height: 1080
7+
width: 1280
8+
height: 720
99
angle: 50
1010
shutter_time: 0.1
1111

src/core/hit.cpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,19 @@ Hit::Hit(const Hit &h) {
2323

2424
[[nodiscard]] const Vector3f &Hit::GetNormal() const { return normal; }
2525

26-
void Hit::Set(float _t, const Material *m, const Vector3f &n, const Vector3f &hit_point, const SimpleObject3D *object) {
26+
void Hit::Set(float _t, const Material *m, const Vector3f &n,
27+
const Vector3f &hit_point, const Vector3f &color,
28+
const SimpleObject3D *object) {
2729
t = _t;
2830
material = m;
2931
normal = n;
30-
texture_calculator = object;
31-
this->pos = hit_point;
32+
obj = object;
33+
pos = hit_point;
34+
ambient = color;
3235
}
3336

3437
Vector3f Hit::GetAmbient() const {
35-
if (texture_calculator) {
36-
return texture_calculator->AmbientColorAtHit(*this);
37-
} else {
38-
return material->ambientColor;
39-
}
38+
return ambient;
4039
}
4140

4241
const Vector3f &Hit::GetPos() const {

src/core/hit.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,18 @@ class Hit {
2727

2828
[[nodiscard]] Vector3f GetAmbient() const;
2929

30-
void Set(float _t, const Material *m, const Vector3f &n, const Vector3f &hit_point, const SimpleObject3D *object);
30+
void Set(float _t, const Material *m, const Vector3f &n,
31+
const Vector3f &hit_point, const Vector3f &color,
32+
const SimpleObject3D *object);
3133

3234
private:
3335
float t;
3436
const Material *material;
35-
const SimpleObject3D *texture_calculator = nullptr;
37+
const SimpleObject3D *obj = nullptr;
3638

3739
Vector3f normal;
3840
Vector3f pos;
41+
Vector3f ambient;
3942
};
4043

4144
} // namespace RT

src/objects/object3d.h

-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ class SimpleObject3D : public Object3D {
2828
public:
2929
SimpleObject3D(const Material *material, const Texture *texture) : material(material), texture(texture){};
3030

31-
[[nodiscard]] virtual Vector3f AmbientColorAtHit(const Hit &hit) const = 0;
32-
3331
[[nodiscard]] const Material *GetMaterial() const { return material; }
3432

3533
const Material *material;

src/objects/plane.cpp

+14-16
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Plane::Plane(
1212
float d,
1313
const Material *material,
1414
const Texture *texture,
15+
const Texture *normal_texture,
1516
float texture_scale,
1617
const Vector2f &texture_translate,
1718
const Vector3f &texture_up
@@ -24,28 +25,25 @@ Plane::Plane(
2425
this->texture_right = Vector3f::cross(texture_up, normal);
2526
}
2627

27-
bool Plane::Intersect(const Ray &r, Hit &h, float tmin) const {
28+
bool Plane::Intersect(const Ray &r, Hit &hit, float tmin) const {
2829
const Vector3f &dir = r.GetDirection();
2930
float t = (d - Vector3f::dot(r.GetOrigin(), normal)) / Vector3f::dot(dir, normal);
30-
if (t > tmin && t < h.GetT()) {
31-
h.Set(t, material, normal, r.PointAtParameter(t), this);
31+
if (t > tmin && t < hit.GetT()) {
32+
auto color = material->ambientColor;
33+
if (texture != nullptr) {
34+
const auto &hit_point = hit.GetPos();
35+
auto x = (Vector3f::dot(hit_point, texture_right) + texture_translate.x()) / texture_scale;
36+
auto y = (Vector3f::dot(hit_point, texture_up) + texture_translate.y()) / texture_scale;
37+
auto u = x - std::floor(x);
38+
auto v = y - std::floor(y);
39+
color = texture->At(u, v);
40+
}
41+
// TODO: change texture
42+
hit.Set(t, material, normal, r.PointAtParameter(t), color, this);
3243
return true;
3344
} else {
3445
return false;
3546
}
3647
}
3748

38-
Vector3f Plane::AmbientColorAtHit(const Hit &hit) const {
39-
if (texture != nullptr) {
40-
const auto &hit_point = hit.GetPos();
41-
auto x = (Vector3f::dot(hit_point, texture_right) + texture_translate.x()) / texture_scale;
42-
auto y = (Vector3f::dot(hit_point, texture_up) + texture_translate.y()) / texture_scale;
43-
auto u = x - std::floor(x);
44-
auto v = y - std::floor(y);
45-
return texture->At(u / 2, v / 2);
46-
} else {
47-
return material->ambientColor;
48-
}
49-
}
50-
5149
} // namespace RT

src/objects/plane.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ class Plane : public SimpleObject3D {
1515
float d,
1616
const Material *material,
1717
const Texture *texture,
18+
const Texture *normal_texture,
1819
float texture_scale = 1.f,
1920
const Vector2f &texture_translate = Vector2f(0, 0),
2021
const Vector3f &texture_up = Vector3f(0, 1, 0)
2122
);
2223

2324
bool Intersect(const Ray &r, Hit &h, float tmin) const override;
2425

25-
[[nodiscard]] Vector3f AmbientColorAtHit(const Hit &hit) const override;
26-
2726
protected:
27+
const Texture *normal_texture;
2828
const Vector3f normal;
2929
const float d;
3030

src/objects/rotate_bezier.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "./rotate_bezier.h"
55
#include "utils/debug.h"
6+
#include "core/material.h"
67

78
namespace RT {
89

@@ -74,7 +75,9 @@ bool RotateBezier::Intersect(const Ray &ray, Hit &hit, float tmin) const {
7475
hit_point_to_axis.x() * b_deriv.y(),
7576
-b_deriv.x(),
7677
hit_point_to_axis.y() * b_deriv.y());
77-
hit.Set(ray_t, material, normal.normalized(), hit_point, nullptr);
78+
auto color = material->ambientColor;
79+
// TODO: texture
80+
hit.Set(ray_t, material, normal.normalized(), hit_point, color, nullptr);
7881
// LOG(ERROR) << fmt::format(fg(fmt::color::blue), "hit {} (ray_t = {}, normal = {})", hit_point, ray_t, normal);
7982
} else {
8083
// LOG(ERROR) << fmt::format(fg(fmt::color::red), "not found since ray_t = {}, t = {}", ray_t, t);
@@ -85,10 +88,6 @@ bool RotateBezier::Intersect(const Ray &ray, Hit &hit, float tmin) const {
8588
}
8689
}
8790

88-
[[nodiscard]] Vector3f RotateBezier::AmbientColorAtHit(const Hit &hit) const {
89-
throw std::runtime_error("bezier texture not implemented");
90-
}
91-
9291
std::pair<Vector2f, Vector2f> RotateBezier::bezier_evaluate(float bt, float min_t, float max_t) const {
9392
if (bt < min_t - 0.0001) {
9493
return {Vector2f(controls[0].x(), yfirst + bt * (ylast - yfirst)), Vector2f(0, ylast - yfirst)};

src/objects/rotate_bezier.h

-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ class RotateBezier: public SimpleObject3D {
2222

2323
bool Intersect(const Ray &ray, Hit &hit, float tmin) const override;
2424

25-
[[nodiscard]] Vector3f AmbientColorAtHit(const Hit &hit) const override;
26-
2725
std::unique_ptr<Mesh> MakeMesh(const Material *mat, const Texture *tex, int density_x, int density_y) const;
2826

2927
[[nodiscard]] std::pair<Vector2f, Vector2f> bezier_evaluate(float bt, float min_t = 0.f, float max_t = 1.f) const;

src/objects/sphere.cpp

+9-14
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,20 @@ bool Sphere::Intersect(const Ray &r, Hit &h, float tmin) const {
3434
if (t >= tmin && t < h.GetT()) {
3535
Vector3f intersection = r.PointAtParameter(t);
3636
Vector3f normal_at_intersection = (intersection - cur_center).normalized();
37-
h.Set(t, material, normal_at_intersection, intersection, this);
37+
Vector3f color = material->ambientColor;
38+
if (texture != nullptr) {
39+
const auto &hit_point = intersection;
40+
Vector3f center_to_intersection = (hit_point - center).normalized();
41+
float u = std::atan2(center_to_intersection.z(), center_to_intersection.x()) / (float) M_PI / 2.f + 0.5f;
42+
float v = std::asin(center_to_intersection.y()) / (float) M_PI + 0.5f;
43+
color = texture->At(u, v);
44+
}
45+
h.Set(t, material, normal_at_intersection, intersection, color, this);
3846
return true;
3947
} else {
4048
return false;
4149
}
4250
}
4351
}
4452

45-
Vector3f Sphere::AmbientColorAtHit(const Hit &hit) const {
46-
if (texture != nullptr) {
47-
CHECK(false);
48-
const auto &hit_point = hit.GetPos();
49-
Vector3f center_to_intersection = (hit_point - center).normalized();
50-
float u = std::atan2(center_to_intersection.z(), center_to_intersection.x()) / (float) M_PI / 2.f + 0.5f;
51-
float v = std::asin(center_to_intersection.y()) / (float) M_PI + 0.5f;
52-
return texture->At(u, v);
53-
} else {
54-
return material->ambientColor;
55-
}
56-
}
57-
5853
} // namespace RT

src/objects/sphere.h

-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ class Sphere : public SimpleObject3D {
1717

1818
bool Intersect(const Ray &r, Hit &h, float tmin) const override;
1919

20-
[[nodiscard]] Vector3f AmbientColorAtHit(const Hit &hit) const override;
21-
2220
const Vector3f center;
2321
const float radius;
2422

src/objects/triangle.cpp

+9-19
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,22 @@ bool Triangle::Intersect(const Ray &r, Hit &h, float tmin) const {
2626
float beta = tri_det(rd, s, e2) / det_rd_e1_e2; // weight of b
2727
float gamma = tri_det(rd, e1, s) / det_rd_e1_e2; // weight of c
2828
if (t < h.GetT() && t > tmin && 0 <= beta && 0 <= gamma && beta + gamma <= 1) {
29-
if (has_norm) {
30-
Vector3f n = (1 - beta - gamma) * na + beta * nb + gamma * nc;
31-
h.Set(t, material, n.normalized(), r.PointAtParameter(t), this);
32-
} else {
33-
h.Set(t, material, normal, r.PointAtParameter(t), this);
29+
Vector3f color = material->ambientColor;
30+
if (texture != nullptr) {
31+
CHECK(has_tex_coord);
32+
Vector2f uv = (1 - beta - gamma) * ta + beta * tb + gamma * tc;
33+
color = texture->At(uv.x(), uv.y());
3434
}
35+
Vector3f true_normal = has_norm
36+
? (1 - beta - gamma) * na + beta * nb + gamma * nc
37+
: normal;
38+
h.Set(t, material, true_normal, r.PointAtParameter(t), color, this);
3539
return true;
3640
} else {
3741
return false;
3842
}
3943
}
4044

41-
Vector3f Triangle::AmbientColorAtHit(const Hit &hit) const {
42-
if (texture != nullptr) {
43-
CHECK(has_tex_coord);
44-
const Vector3f e1 = a - b, e2 = a - c, s = a - hit.GetPos();
45-
float det_rd_e1_e2 = tri_det(normal, e1, e2);
46-
float beta = tri_det(normal, s, e2) / det_rd_e1_e2; // weight of b
47-
float gamma = tri_det(normal, e1, s) / det_rd_e1_e2; // weight of c
48-
Vector2f uv = (1 - beta - gamma) * ta + beta * tb + gamma * tc;
49-
return texture->At(uv.x(), uv.y());
50-
} else {
51-
return material->ambientColor;
52-
}
53-
}
54-
5545
void Triangle::SetVertexNormal(const Vector3f &_na, const Vector3f &_nb, const Vector3f &_nc) {
5646
na = _na;
5747
nb = _nb;

src/objects/triangle.h

-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ class Triangle : public SimpleObject3D {
1515

1616
bool Intersect(const Ray &r, Hit &h, float tmin) const override;
1717

18-
[[nodiscard]] Vector3f AmbientColorAtHit(const Hit &hit) const override;
19-
2018
void SetVertexNormal(const Vector3f &_na, const Vector3f &_nb, const Vector3f &_nc);
2119
void SetTextureCoord(const Vector2f &_ta, const Vector2f &_tb, const Vector2f &_tc);
2220

src/utils/scene_parser.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ std::unique_ptr<Object3D> SceneParser::parse_obj(const YAML::Node &node) {
107107
auto d = node["d"].as<float>();
108108
auto material = parse_material(node["mat"]);
109109
auto texture = parse_texture(node["texture"]);
110-
return std::make_unique<Plane>(normal, d, material, texture,
110+
auto normal_texture = parse_texture(node["normal_texture"]);
111+
return std::make_unique<Plane>(normal, d, material, texture, normal_texture,
111112
texture_scale, texture_translate, texture_up);
112113

113114
} else if (node_type == "triangle") {

0 commit comments

Comments
 (0)