4
4
#include " core/material.h"
5
5
6
6
#include " ./plane.h"
7
+ #include " utils/debug.h"
7
8
8
9
namespace RT {
9
10
@@ -19,6 +20,7 @@ Plane::Plane(
19
20
) : normal (normal .normalized()),
20
21
d (d),
21
22
SimpleObject3D(material, texture),
23
+ normal_texture(normal_texture),
22
24
texture_scale(texture_scale),
23
25
texture_translate(texture_translate) {
24
26
this ->texture_up = (texture_up - this ->normal * Vector3f::dot (this ->normal , texture_up)).normalized ();
@@ -28,18 +30,28 @@ Plane::Plane(
28
30
bool Plane::Intersect (const Ray &r, Hit &hit, float tmin) const {
29
31
const Vector3f &dir = r.GetDirection ();
30
32
float t = (d - Vector3f::dot (r.GetOrigin (), normal )) / Vector3f::dot (dir, normal );
33
+
31
34
if (t > tmin && t < hit.GetT ()) {
32
35
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 ) {
35
41
auto x = (Vector3f::dot (hit_point, texture_right) + texture_translate.x ()) / texture_scale;
36
42
auto y = (Vector3f::dot (hit_point, texture_up) + texture_translate.y ()) / texture_scale;
37
43
auto u = x - std::floor (x);
38
44
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
+ }
40
53
}
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 );
43
55
return true ;
44
56
} else {
45
57
return false ;
0 commit comments