Skip to content

Commit b277ad1

Browse files
committed
feat: normal texture mapping for plane
1 parent 574937f commit b277ad1

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/objects/plane.cpp

+17-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "core/material.h"
55

66
#include "./plane.h"
7+
#include "utils/debug.h"
78

89
namespace RT {
910

@@ -19,6 +20,7 @@ Plane::Plane(
1920
) : normal(normal.normalized()),
2021
d(d),
2122
SimpleObject3D(material, texture),
23+
normal_texture(normal_texture),
2224
texture_scale(texture_scale),
2325
texture_translate(texture_translate) {
2426
this->texture_up = (texture_up - this->normal * Vector3f::dot(this->normal, texture_up)).normalized();
@@ -28,18 +30,28 @@ Plane::Plane(
2830
bool Plane::Intersect(const Ray &r, Hit &hit, float tmin) const {
2931
const Vector3f &dir = r.GetDirection();
3032
float t = (d - Vector3f::dot(r.GetOrigin(), normal)) / Vector3f::dot(dir, normal);
33+
3134
if (t > tmin && t < hit.GetT()) {
3235
auto color = material->ambientColor;
33-
if (texture != nullptr) {
34-
const auto &hit_point = hit.GetPos();
36+
auto true_normal = normal;
37+
auto hit_point = r.PointAtParameter(t);
38+
39+
// handling texture
40+
if (texture != nullptr || normal_texture != nullptr) {
3541
auto x = (Vector3f::dot(hit_point, texture_right) + texture_translate.x()) / texture_scale;
3642
auto y = (Vector3f::dot(hit_point, texture_up) + texture_translate.y()) / texture_scale;
3743
auto u = x - std::floor(x);
3844
auto v = y - std::floor(y);
39-
color = texture->At(u, v);
45+
if (texture != nullptr) {
46+
color = texture->At(u, v);
47+
}
48+
if (normal_texture != nullptr) {
49+
auto texture_n = normal_texture->At(u, v);
50+
true_normal = texture_n.x() * texture_up + texture_n.y() * texture_right + texture_n.z() * normal;
51+
// fmt::print("true_normal: {}\n", true_normal);
52+
}
4053
}
41-
// TODO: change texture
42-
hit.Set(t, material, normal, r.PointAtParameter(t), color, this);
54+
hit.Set(t, material, true_normal, hit_point, color, this);
4355
return true;
4456
} else {
4557
return false;

0 commit comments

Comments
 (0)