]>
Dogcows Code - chaz/rasterize/blob - triangle.hh
3 * CS5600 University of Utah
5 * mcgarvey@eng.utah.edu
18 * A class for a triangle object.
20 class triangle
: public element
28 triangle(vec_t a
, vec_t b
, vec_t c
) :
35 virtual bool intersect(ray_t ray
, contact_t
& hit
) const
37 plane p
= plane(a
, b
, c
);
38 if (!p
.intersect(ray
, hit
)) {
43 if (!barycentric(bc
, hit
.p
)) {
48 if (vec_dot(p
.normal(), hit
.n
) < S(0.0)) {
49 hit
.n
= vec_neg(hit
.n
);
57 * Calculate barycentric coordinates for the triangle.
59 bool barycentric(scal_t
* bc
, vec_t v
) const
61 scal_t denom
= (b
.y
- c
.y
) * (a
.x
- c
.x
) + (c
.x
- b
.x
) * (a
.y
- c
.y
);
62 bc
[0] = ((b
.y
- c
.y
) * (v
.x
- c
.x
) + (c
.x
- b
.x
) * (v
.y
- c
.y
)) / denom
;
63 bc
[1] = ((c
.y
- a
.y
) * (v
.x
- c
.x
) + (a
.x
- c
.x
) * (v
.y
- c
.y
)) / denom
;
64 bc
[2] = S(1.0) - bc
[0] - bc
[1];
65 if (S(0.0) <= bc
[0] && bc
[0] <= S(1.0) &&
66 S(0.0) <= bc
[1] && bc
[1] <= S(1.0) &&
67 S(0.0) <= bc
[2] && bc
[2] <= S(1.0)) {
73 virtual vec_t
txcoord(vec_t point
) const
75 vec_t t1
= vec_new(S(0.0), S(0.0), S(0.0));
76 vec_t t2
= vec_new(S(1.0), S(0.0), S(0.0));
77 vec_t t3
= vec_new(S(1.0), S(1.0), S(0.0));
79 barycentric(bc
, point
);
80 return vec_interp(t1
, t2
, t3
, bc
);
86 * Destroy a new'd triangle, releasing its memory.
89 void triangle_destroy(plane
* t
)
This page took 0.038407 seconds and 5 git commands to generate.