-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
bce3afd
commit c298b3e
Showing
7 changed files
with
212 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#include "tri.h" | ||
#include "common.h" | ||
|
||
Tri::Tri() | ||
{ | ||
m_ab = m_b.pos - m_a.pos; | ||
m_bc = m_c.pos - m_b.pos; | ||
m_ca = m_a.pos - m_c.pos; | ||
} | ||
|
||
Tri::Tri(const TriVertex &a, const TriVertex &b, const TriVertex &c, Material *pMtrl) | ||
{ | ||
m_a = a; | ||
m_b = b; | ||
m_c = c; | ||
|
||
m_ab = m_b.pos - m_a.pos; | ||
m_bc = m_c.pos - m_b.pos; | ||
m_ca = m_a.pos - m_c.pos; | ||
|
||
this->m_pMtrl = pMtrl; | ||
|
||
Vector3 pos(0,0,320); | ||
init(Vector3::ZERO, pos); | ||
} | ||
|
||
bool Tri::hit(const Ray &ray, HitRecord &record, Light *pLight) const | ||
{ | ||
record.t = Common::FLOAT_MAX; | ||
|
||
const Ray newRay = ray.genNewRay(m_transform); | ||
|
||
bool reverse = false; | ||
if (newRay.dir.isSameDir(getLocalNormal())) | ||
{ | ||
reverse = true; | ||
} | ||
|
||
const float n = (-newRay.origin) * getLocalNormal(reverse); | ||
const float d = newRay.dir * getLocalNormal(reverse); | ||
|
||
record.t = n / d; | ||
if (record.t < Common::FLOAT_SAMLL_NUMBER) | ||
{ | ||
// std::cout << record.t << "," << n << "," << d << std::endl; | ||
// std::cout<<"false"<<std::endl; | ||
return false; | ||
} | ||
|
||
Vector3 localPoint = newRay.origin + record.t * newRay.dir; | ||
if (!isAllFacePositive(localPoint)) | ||
{ | ||
// std::cout<<"false"<<std::endl; | ||
return false; | ||
} | ||
|
||
record.mtrl = *m_pMtrl; | ||
record.transform = m_transform; | ||
|
||
record.point = m_transform.transformPoint(localPoint); | ||
record.normal = m_transform.transformNormal(getLocalNormal(reverse)); | ||
|
||
// float w_a, w_b, w_c; | ||
// getWeight(ap_ab, ap_bc, ap_ca, w_a, w_b, w_c); | ||
|
||
// record.u = m_a.u * w_a + m_b.u * w_b + m_c.u * w_c; | ||
// record.v = m_a.v * w_a + m_b.v * w_b + m_c.v * w_c; | ||
|
||
if (m_pMtrl) | ||
{ | ||
Vector3 r; | ||
record.f = m_pMtrl->eval(record.u, record.v, -newRay.dir, r, record.reflectPdf); | ||
record.dot = Common::clamp(std::abs(r * Common::LOCAL_NORMAL), Common::FLOAT_SAMLL_NUMBER, 1.0f); | ||
record.reflect = m_transform.transformVector(r); | ||
record.isMirror = m_pMtrl->isMirror(); | ||
|
||
if (record.isMirror) | ||
{ | ||
record.dot = 1; | ||
} | ||
} | ||
|
||
// std::cout<<"true"<<std::endl; | ||
return true; | ||
} | ||
|
||
Vector3 Tri::getLocalNormal(bool reverse) const | ||
{ | ||
Vector3 n = m_ab.cross(-m_ca); | ||
|
||
if(reverse) | ||
{ | ||
n *= -1; | ||
} | ||
|
||
return n; | ||
} | ||
|
||
bool Tri::isAllFacePositive(const Vector3 &p) const | ||
{ | ||
Vector3 ap = p - m_a.pos; | ||
Vector3 bp = p - m_b.pos; | ||
Vector3 cp = p - m_c.pos; | ||
|
||
Vector3 bc_bp = m_bc.cross(bp).dir(); | ||
Vector3 ca_cp = m_ca.cross(cp).dir(); | ||
Vector3 ab_ap = m_ab.cross(ap).dir(); | ||
|
||
if(bc_bp != ca_cp) return false; | ||
if(ca_cp != ab_ap) return false; | ||
if(ab_ap != bc_bp) return false; | ||
|
||
return true; | ||
} | ||
|
||
Vector3 Tri::dpdu(const Vector3 &point) const | ||
{ | ||
return Vector3(); | ||
} | ||
|
||
Vector3 Tri::dpdv(const Vector3 &point) const | ||
{ | ||
return Vector3(); | ||
} | ||
|
||
float Tri::u(const Vector3 &point) const | ||
{ | ||
return 0.0f; | ||
} | ||
|
||
float Tri::v(const Vector3 &point) const | ||
{ | ||
return 0.0f; | ||
} |
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,46 @@ | ||
#ifndef _TRI_H_ | ||
#define _TRI_H_ | ||
|
||
#include "geometry.h" | ||
#include "vector.h" | ||
|
||
class TriVertex | ||
{ | ||
public: | ||
TriVertex() {}; | ||
TriVertex(float x, float y, float z) : pos(x, y, z) {}; | ||
|
||
Vector3 pos; | ||
}; | ||
|
||
class Tri : public Geometry | ||
{ | ||
public: | ||
Tri(); | ||
Tri(const TriVertex &a, const TriVertex &b, const TriVertex &c, Material *pMtrl); | ||
|
||
virtual bool hit(const Ray &ray, HitRecord &record, Light *pLight) const override; | ||
|
||
Vector3 getLocalNormal(bool reverse = false) const; | ||
|
||
private: | ||
bool isAllFacePositive(const Vector3 &p) const; | ||
|
||
private: | ||
virtual Vector3 dpdu(const Vector3 &point) const override; | ||
virtual Vector3 dpdv(const Vector3 &point) const override; | ||
|
||
virtual float u(const Vector3 &point) const override; | ||
virtual float v(const Vector3 &point) const override; | ||
|
||
private: | ||
TriVertex m_a; | ||
TriVertex m_b; | ||
TriVertex m_c; | ||
|
||
Vector3 m_ab; | ||
Vector3 m_bc; | ||
Vector3 m_ca; | ||
}; | ||
|
||
#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 |
---|---|---|
@@ -1,14 +1,15 @@ | ||
#include "scene.h" | ||
#include "mesh.h" | ||
#include "common.h" | ||
#include "vector.h" | ||
|
||
int main() | ||
{ | ||
std::cout << "starting..." << std::endl; | ||
Mesh* mesh = new Mesh(Common::BUNNY); | ||
std::cout << "ending..." << std::endl; | ||
// Scene scene; | ||
// scene.run(); | ||
// std::cout << "starting..." << std::endl; | ||
// Mesh* mesh = new Mesh(Common::BUNNY); | ||
// std::cout << "ending..." << std::endl; | ||
Scene scene; | ||
scene.run(); | ||
|
||
return 0; | ||
} |
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