ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
Triangle3D.cpp
Go to the documentation of this file.
1#include "Triangle3D.h"
2
4
6 this->p1 = p1;
7 this->p2 = p2;
8 this->p3 = p3;
9
10 this->Normal();
11}
12
14 edge1 = *p2 - *p1;
15 edge2 = *p3 - *p1;
17
18 return &normal;
19}
20
21bool Triangle3D::DidIntersect(Vector3D ray, Vector3D direction, Vector3D* intersect, Vector3D* color) {
22 Vector3D pvec = direction.CrossProduct(edge2);
23 float det = edge1.DotProduct(pvec);
24 float invDet = 1.0f / det;
25
26 if (fabs(det) < 0.000001f) return false;
27
28 Vector3D tvec = ray - *p1;
29 float u = tvec.DotProduct(pvec) * invDet;
30 if (u < 0.0f || u > 1.0f) return false;
31
32 Vector3D qvec = tvec.CrossProduct(edge1);
33 float v = direction.DotProduct(qvec) * invDet;
34 if (v < 0.0f || u + v > 1.0f) return false;
35
36 float t = edge2.DotProduct(qvec) * invDet;
37
38 if (t > 0.000001f) {
39 Vector3D tempInt = *p1 + (edge2 * u) + (edge1 * v);
40
41 tempInt = ray + direction.Multiply(t);
42
43 intersect->X = tempInt.X;
44 intersect->Y = tempInt.Y;
45 intersect->Z = tempInt.Z;
46
47 color->X = u;
48 color->Y = v;
49 color->Z = (1.0f - u - v);
50
51 return true;
52 }
53 else {
54 return false;
55 }
56}
57
59 return p1->ToString() + " " + p2->ToString() + " " + p3->ToString();
60}
Defines the Triangle3D class for representing and manipulating 3D triangles.
String ToString()
Converts the triangle's data to a string representation.
Triangle3D()
Default constructor.
Definition Triangle3D.cpp:3
Vector3D * p1
Pointer to the first vertex of the triangle.
Definition Triangle3D.h:24
Vector3D * Normal()
Calculates and returns the normal of the triangle.
Vector3D * p3
Pointer to the third vertex of the triangle.
Definition Triangle3D.h:26
Vector3D normal
Normal vector of the triangle.
Definition Triangle3D.h:34
bool DidIntersect(Vector3D ray, Vector3D direction, Vector3D *intersect, Vector3D *color)
Tests whether a ray intersects with the triangle.
Vector3D * p2
Pointer to the second vertex of the triangle.
Definition Triangle3D.h:25
Vector3D edge2
Edge vector from the first to the third vertex.
Definition Triangle3D.h:33
Vector3D edge1
Edge vector from the first to the second vertex.
Definition Triangle3D.h:32
Represents a 3D vector (X, Y, Z) and provides methods for vector arithmetic.
Definition Vector3D.h:26
Vector3D CrossProduct(const Vector3D &vector) const
Computes the cross product of this vector with another Vector3D.
Definition Vector3D.cpp:98
String ToString() const
Converts the vector to a string representation.
Definition Vector3D.cpp:189
float Z
The Z-component of the 3D vector.
Definition Vector3D.h:30
Vector3D UnitSphere() const
Normalizes this vector such that its magnitude is 1 (if non-zero).
Definition Vector3D.cpp:106
Vector3D Multiply(const Vector3D &vector) const
Multiplies this vector by another Vector3D component-wise.
Definition Vector3D.cpp:66
float X
The X-component of the 3D vector.
Definition Vector3D.h:28
float Y
The Y-component of the 3D vector.
Definition Vector3D.h:29
float DotProduct(const Vector3D &vector) const
Computes the dot product of this vector with another Vector3D.
Definition Vector3D.cpp:155