-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTriangle.h
More file actions
57 lines (45 loc) · 1.21 KB
/
Triangle.h
File metadata and controls
57 lines (45 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef TRIANGLE_H
#define TRIANGLE_H
#include "Object3D.h"
#include <vecmath.h>
#include <cmath>
#include <iostream>
using namespace std;
///TODO: implement this class.
///Add more fields as necessary,
///but do not remove hasTex, normals or texCoords
///they are filled in by other components
class Triangle: public Object3D
{
public:
Triangle();
///@param a b c are three vertex positions of the triangle
Triangle( const Vector3f& a, const Vector3f& b, const Vector3f& c, Material* m):Object3D(m){
hasTex = false;
this->a = a;
this->b = b;
this->c = c;
}
virtual bool intersect( const Ray& ray, Hit& hit , float tmin){
//Initializing Matrix ColumnWise
Matrix3f M(a-b, a-c, ray.getDirection(), true);
Vector3f B = a-ray.getOrigin();
Vector3f X = M.inverse() * B;
float alpha, beta, gamma, t;
beta = X[0]; gamma = X[1]; t = X[2];
alpha = 1-beta-gamma;
//Intersection
if(beta+gamma <= 1 && beta >= 0 && gamma >= 0 && t > tmin && t < hit.getT())
{
hit.set(t, material, (alpha*normals[0] + beta*normals[1]+ gamma*normals[2]).normalized());
return true;
}
return false;
}
bool hasTex;
Vector3f normals[3];
Vector2f texCoords[3];
protected:
Vector3f a, b, c;
};
#endif //TRIANGLE_H