]> Dogcows Code - chaz/rasterize/blob - tri.h
add README for project one
[chaz/rasterize] / tri.h
1
2 /*
3 * CS5600 University of Utah
4 * Charles McGarvey
5 * mcgarvey@eng.utah.edu
6 */
7
8 #ifndef __TRI_H__
9 #define __TRI_H__
10
11 #include "mat.h"
12 #include "vert.h"
13
14
15 /*
16 * A triangle is a polygon of three vertices.
17 */
18 struct tri
19 {
20 vert_t a;
21 vert_t b;
22 vert_t c;
23 };
24 typedef struct tri tri_t;
25
26 /*
27 * Initialize a triangle.
28 */
29 __fast__
30 void tri_init(tri_t* t, vert_t a, vert_t b, vert_t c)
31 {
32 t->a = a;
33 t->b = b;
34 t->c = c;
35 }
36
37
38 /*
39 * Create a new triangle.
40 */
41 __fast__
42 tri_t tri_new(vert_t a, vert_t b, vert_t c)
43 {
44 tri_t t;
45 tri_init(&t, a, b, c);
46 return t;
47 }
48
49 #define TRI_ZERO tri_new(VERT_ZERO, VERT_ZERO, VERT_ZERO)
50
51
52 /*
53 * Create a new triangle on the heap.
54 */
55 __fast__
56 tri_t* tri_alloc(vert_t a, vert_t b, vert_t c)
57 {
58 tri_t* t = (tri_t*)mem_alloc(sizeof(tri_t));
59 tri_init(t, a, b, c);
60 return t;
61 }
62
63
64 /*
65 * Apply a transformation matrix to alter the triangle geometry.
66 */
67 __fast__
68 tri_t tri_transform(tri_t t, mat_t m)
69 {
70 t.a.v = mat_apply(m, t.a.v);
71 t.b.v = mat_apply(m, t.b.v);
72 t.c.v = mat_apply(m, t.c.v);
73 return t;
74 }
75
76
77 /*
78 * Get the barycentric coordinates of a vector against a triangle. The
79 * returned coordinates will be a linear combination, but they may not
80 * actually be barycentric coordinates. Use the function vec_is_barycentric
81 * to check if they really are barycentric coordinates, meaning the point
82 * vector v is inside the triangle, ignoring the Z components.
83 */
84 vec_t tri_barycentric(const tri_t* t, vec_t v);
85
86
87 #endif // __TRI_H__
88
This page took 0.030701 seconds and 4 git commands to generate.