-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
254 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (C) Kevin Suffern 2000-2007. | ||
// This C++ code is for non-commercial purposes only. | ||
// This C++ code is licensed under the GNU General Public License Version 2. | ||
// See the file COPYING.txt for the full license. | ||
|
||
// Copyright notice for changes since the originally published version: | ||
// Copyright (C) Eduárd Mándy 2019-2021 | ||
// Though this C++ code was change in a large measure it still has the original copyright notice. | ||
// This C++ code is for non-commercial purposes only. | ||
// This C++ code is licensed under the GNU General Public License Version 2. | ||
// See the file COPYING.txt for the full license. | ||
|
||
#include "FlatUVMeshTriangle.h" | ||
|
||
#include <limits> | ||
|
||
FlatUVMeshTriangle::FlatUVMeshTriangle(Mesh* _mesh_ptr, const int i0, const int i1, const int i2) : MeshTriangle(_mesh_ptr, i0, i1, i2) {} | ||
|
||
FlatUVMeshTriangle* FlatUVMeshTriangle::clone() const { return new FlatUVMeshTriangle(*this); } | ||
|
||
bool FlatUVMeshTriangle::hit(const Ray& ray, float& tmin, ShadeRec& sr) const { | ||
Point3D v0(mesh_ptr->vertices[index0]); | ||
Point3D v1(mesh_ptr->vertices[index1]); | ||
Point3D v2(mesh_ptr->vertices[index2]); | ||
|
||
float a = v0.x - v1.x, b = v0.x - v2.x, c = ray.d.x, d = v0.x - ray.o.x; | ||
float e = v0.y - v1.y, f = v0.y - v2.y, g = ray.d.y, h = v0.y - ray.o.y; | ||
float i = v0.z - v1.z, j = v0.z - v2.z, k = ray.d.z, l = v0.z - ray.o.z; | ||
|
||
float m = f * k - g * j, n = h * k - g * l, p = f * l - h * j; | ||
float q = g * i - e * k, s = e * j - f * i; | ||
|
||
float inv_denom = 1.0f / (a * m + b * q + c * s); | ||
|
||
float e1 = d * m - b * n - c * p; | ||
float beta = e1 * inv_denom; | ||
|
||
if (beta < 0.0f) { | ||
return false; | ||
} | ||
|
||
float r = e * l - h * i; | ||
float e2 = a * n + d * q + c * r; | ||
float gamma = e2 * inv_denom; | ||
|
||
if (gamma < 0.0f) { | ||
return false; | ||
} | ||
|
||
if (beta + gamma > 1.0f) { | ||
return false; | ||
} | ||
|
||
float e3 = a * p - b * r + d * s; | ||
float t = e3 * inv_denom; | ||
|
||
if (t < std::numeric_limits<float>::epsilon()) { | ||
return false; | ||
} | ||
|
||
tmin = t; | ||
sr.u = interpolate_u(beta, gamma); | ||
sr.v = interpolate_v(beta, gamma); | ||
|
||
// From the book, chapter 29.6 Triangle Meshes, page 663 | ||
// Storing local hit point in ShadeRec object is not required here | ||
// because the function ImageTexture::get_color in Listing 29.7 | ||
// doesn't use the local hit point when it uses u and v. | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (C) Kevin Suffern 2000-2007. | ||
// This C++ code is for non-commercial purposes only. | ||
// This C++ code is licensed under the GNU General Public License Version 2. | ||
// See the file COPYING.txt for the full license. | ||
|
||
// Copyright notice for changes since the originally published version: | ||
// Copyright (C) Eduárd Mándy 2019-2021 | ||
// Though this C++ code was change in a large measure it still has the original copyright notice. | ||
// This C++ code is for non-commercial purposes only. | ||
// This C++ code is licensed under the GNU General Public License Version 2. | ||
// See the file COPYING.txt for the full license. | ||
|
||
#ifndef __FLAT_UV_MESH_TRIANGLE__ | ||
#define __FLAT_UV_MESH_TRIANGLE__ | ||
|
||
#include "MeshTriangle.h" | ||
|
||
class FlatUVMeshTriangle : public MeshTriangle { | ||
public: | ||
|
||
FlatUVMeshTriangle() = default; | ||
|
||
explicit FlatUVMeshTriangle(Mesh* _meshPtr, const int i0, const int i1, const int i2); | ||
|
||
~FlatUVMeshTriangle() = default; | ||
|
||
FlatUVMeshTriangle(const FlatUVMeshTriangle& fmt) = default; | ||
FlatUVMeshTriangle(FlatUVMeshTriangle&& fmt) = default; | ||
FlatUVMeshTriangle& operator=(const FlatUVMeshTriangle& rhs) = default; | ||
FlatUVMeshTriangle& operator=(FlatUVMeshTriangle&& rhs) = default; | ||
|
||
FlatUVMeshTriangle* clone() const override; | ||
|
||
bool hit(const Ray& ray, float& tmin, ShadeRec& sr) const override; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (C) Kevin Suffern 2000-2007. | ||
// This C++ code is for non-commercial purposes only. | ||
// This C++ code is licensed under the GNU General Public License Version 2. | ||
// See the file COPYING.txt for the full license. | ||
|
||
// Copyright notice for changes since the originally published version: | ||
// Copyright (C) Eduárd Mándy 2019-2021 | ||
// Though this C++ code was change in a large measure it still has the original copyright notice. | ||
// This C++ code is for non-commercial purposes only. | ||
// This C++ code is licensed under the GNU General Public License Version 2. | ||
// See the file COPYING.txt for the full license. | ||
|
||
#include "SmoothUVMeshTriangle.h" | ||
|
||
#include <limits> | ||
|
||
SmoothUVMeshTriangle::SmoothUVMeshTriangle(Mesh* _mesh_ptr, const int i0, const int i1, const int i2) : MeshTriangle(_mesh_ptr, i0, i1, i2) {} | ||
|
||
SmoothUVMeshTriangle* SmoothUVMeshTriangle::clone() const { return new SmoothUVMeshTriangle(*this); } | ||
|
||
bool SmoothUVMeshTriangle::hit(const Ray& ray, float& tmin, ShadeRec& sr) const { | ||
Point3D v0(mesh_ptr->vertices[index0]); | ||
Point3D v1(mesh_ptr->vertices[index1]); | ||
Point3D v2(mesh_ptr->vertices[index2]); | ||
|
||
float a = v0.x - v1.x, b = v0.x - v2.x, c = ray.d.x, d = v0.x - ray.o.x; | ||
float e = v0.y - v1.y, f = v0.y - v2.y, g = ray.d.y, h = v0.y - ray.o.y; | ||
float i = v0.z - v1.z, j = v0.z - v2.z, k = ray.d.z, l = v0.z - ray.o.z; | ||
|
||
float m = f * k - g * j, n = h * k - g * l, p = f * l - h * j; | ||
float q = g * i - e * k, s = e * j - f * i; | ||
|
||
float inv_denom = 1.0f / (a * m + b * q + c * s); | ||
|
||
float e1 = d * m - b * n - c * p; | ||
float beta = e1 * inv_denom; | ||
|
||
if (beta < 0.0f) { | ||
return false; | ||
} | ||
|
||
float r = e * l - h * i; | ||
float e2 = a * n + d * q + c * r; | ||
float gamma = e2 * inv_denom; | ||
|
||
if (gamma < 0.0f) { | ||
return false; | ||
} | ||
|
||
if (beta + gamma > 1.0f) { | ||
return false; | ||
} | ||
|
||
float e3 = a * p - b * r + d * s; | ||
float t = e3 * inv_denom; | ||
|
||
if (t < std::numeric_limits<float>::epsilon()) { | ||
return false; | ||
} | ||
|
||
tmin = t; | ||
sr.normal = interpolate_normal(beta, gamma); // for smooth shading | ||
sr.u = interpolate_u(beta, gamma); | ||
sr.v = interpolate_v(beta, gamma); | ||
|
||
// From the book, chapter 29.6 Triangle Meshes, page 663 | ||
// Storing local hit point in ShadeRec object is not required here | ||
// because the function ImageTexture::get_color in Listing 29.7 | ||
// doesn't use the local hit point when it uses u and v. | ||
|
||
return true; | ||
} | ||
|
||
Normal SmoothUVMeshTriangle::interpolate_normal(const float beta, const float gamma) const { | ||
Normal normal((1 - beta - gamma) * mesh_ptr->normals[index0] + beta * mesh_ptr->normals[index1] + gamma * mesh_ptr->normals[index2]); | ||
normal.normalize(); | ||
|
||
return normal; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (C) Kevin Suffern 2000-2007. | ||
// This C++ code is for non-commercial purposes only. | ||
// This C++ code is licensed under the GNU General Public License Version 2. | ||
// See the file COPYING.txt for the full license. | ||
|
||
// Copyright notice for changes since the originally published version: | ||
// Copyright (C) Eduárd Mándy 2019-2021 | ||
// Though this C++ code was change in a large measure it still has the original copyright notice. | ||
// This C++ code is for non-commercial purposes only. | ||
// This C++ code is licensed under the GNU General Public License Version 2. | ||
// See the file COPYING.txt for the full license. | ||
|
||
#ifndef __SMOOTH_UV_MESH_TRIANGLE__ | ||
#define __SMOOTH_UV_MESH_TRIANGLE__ | ||
|
||
#include "MeshTriangle.h" | ||
|
||
class SmoothUVMeshTriangle : public MeshTriangle { | ||
public: | ||
|
||
SmoothUVMeshTriangle() = default; | ||
|
||
explicit SmoothUVMeshTriangle(Mesh* _meshPtr, const int i0, const int i1, const int i2); | ||
|
||
~SmoothUVMeshTriangle() = default; | ||
SmoothUVMeshTriangle(const SmoothUVMeshTriangle& fmt) = default; | ||
SmoothUVMeshTriangle(SmoothUVMeshTriangle&& fmt) = default; | ||
SmoothUVMeshTriangle& operator=(const SmoothUVMeshTriangle& rhs) = default; | ||
SmoothUVMeshTriangle& operator=(SmoothUVMeshTriangle&& rhs) = default; | ||
|
||
virtual SmoothUVMeshTriangle* clone() const override; | ||
|
||
virtual bool hit(const Ray& ray, float& tmin, ShadeRec& sr) const override; | ||
|
||
protected: | ||
|
||
Normal interpolate_normal(const float beta, const float gamma) const; | ||
}; | ||
|
||
#endif |