|
| 1 | +/** |
| 2 | + * Polygon tessellation library, ported from SGI's GLU implementation |
| 3 | + * written by Eric Veach in 1994 |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * GLU tessellation property and callback type enums |
| 8 | + */ |
| 9 | +export enum gluEnum { |
| 10 | + /** Winding rule property */ |
| 11 | + GLU_TESS_WINDING_RULE = 100140, |
| 12 | + /** Boundary only property - if true, only outputs boundary contours instead of triangulated polygons */ |
| 13 | + GLU_TESS_BOUNDARY_ONLY = 100141, |
| 14 | + /** Tolerance property for vertex merging */ |
| 15 | + GLU_TESS_TOLERANCE = 100142, |
| 16 | + |
| 17 | + /** Begin callback - called when beginning a new primitive */ |
| 18 | + GLU_TESS_BEGIN = 100100, |
| 19 | + /** Vertex callback - called for each vertex */ |
| 20 | + GLU_TESS_VERTEX = 100101, |
| 21 | + /** End callback - called when ending a primitive */ |
| 22 | + GLU_TESS_END = 100102, |
| 23 | + /** Error callback - called when an error occurs */ |
| 24 | + GLU_TESS_ERROR = 100103, |
| 25 | + /** Edge flag callback - called to indicate whether edges are on the polygon boundary */ |
| 26 | + GLU_TESS_EDGE_FLAG = 100104, |
| 27 | + /** Combine callback - called when vertices need to be merged or interpolated */ |
| 28 | + GLU_TESS_COMBINE = 100105, |
| 29 | + |
| 30 | + /** Begin callback with client data */ |
| 31 | + GLU_TESS_BEGIN_DATA = 100106, |
| 32 | + /** Vertex callback with client data */ |
| 33 | + GLU_TESS_VERTEX_DATA = 100107, |
| 34 | + /** End callback with client data */ |
| 35 | + GLU_TESS_END_DATA = 100108, |
| 36 | + /** Error callback with client data */ |
| 37 | + GLU_TESS_ERROR_DATA = 100109, |
| 38 | + /** Edge flag callback with client data */ |
| 39 | + GLU_TESS_EDGE_FLAG_DATA = 100110, |
| 40 | + /** Combine callback with client data */ |
| 41 | + GLU_TESS_COMBINE_DATA = 100111, |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Winding rules determine which regions are considered "inside" the polygon |
| 46 | + */ |
| 47 | +export enum windingRule { |
| 48 | + /** Odd-even winding rule - alternating regions are inside/outside */ |
| 49 | + GLU_TESS_WINDING_ODD = 100130, |
| 50 | + /** Non-zero winding rule - regions with non-zero winding number are inside */ |
| 51 | + GLU_TESS_WINDING_NONZERO = 100131, |
| 52 | + /** Positive winding rule - regions with positive winding number are inside */ |
| 53 | + GLU_TESS_WINDING_POSITIVE = 100132, |
| 54 | + /** Negative winding rule - regions with negative winding number are inside */ |
| 55 | + GLU_TESS_WINDING_NEGATIVE = 100133, |
| 56 | + /** Absolute value >= 2 winding rule - regions where |winding| >= 2 are inside */ |
| 57 | + GLU_TESS_WINDING_ABS_GEQ_TWO = 100134, |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Primitive types that can be output by the tessellator |
| 62 | + */ |
| 63 | +export enum primitiveType { |
| 64 | + /** Line loop primitive */ |
| 65 | + GL_LINE_LOOP = 2, |
| 66 | + /** Triangle list primitive */ |
| 67 | + GL_TRIANGLES = 4, |
| 68 | + /** Triangle strip primitive */ |
| 69 | + GL_TRIANGLE_STRIP = 5, |
| 70 | + /** Triangle fan primitive */ |
| 71 | + GL_TRIANGLE_FAN = 6, |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Error codes that can be returned by the tessellator |
| 76 | + */ |
| 77 | +export enum errorType { |
| 78 | + GLU_TESS_MISSING_BEGIN_POLYGON = 100151, |
| 79 | + GLU_TESS_MISSING_END_POLYGON = 100153, |
| 80 | + GLU_TESS_MISSING_BEGIN_CONTOUR = 100152, |
| 81 | + GLU_TESS_MISSING_END_CONTOUR = 100154, |
| 82 | + GLU_TESS_COORD_TOO_LARGE = 100155, |
| 83 | + GLU_TESS_NEED_COMBINE_CALLBACK = 100156, |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * The main GLU tessellator class. Used to tessellate complex polygons with holes into simpler primitives |
| 88 | + */ |
| 89 | +export class GluTesselator { |
| 90 | + /** |
| 91 | + * Create a new tessellator object |
| 92 | + */ |
| 93 | + constructor(); |
| 94 | + |
| 95 | + /** |
| 96 | + * Delete the tessellator object. Note: In JavaScript this is largely a no-op as garbage collection handles cleanup |
| 97 | + */ |
| 98 | + gluDeleteTess(): void; |
| 99 | + |
| 100 | + /** |
| 101 | + * Set a tessellation property |
| 102 | + * @param which - The property to set (from gluEnum) |
| 103 | + * @param value - The value to set |
| 104 | + */ |
| 105 | + gluTessProperty(which: gluEnum, value: number | boolean): void; |
| 106 | + |
| 107 | + /** |
| 108 | + * Get a tessellation property |
| 109 | + * @param which - The property to get (from gluEnum) |
| 110 | + * @returns The current value of the property |
| 111 | + */ |
| 112 | + gluGetTessProperty(which: gluEnum): number | boolean; |
| 113 | + |
| 114 | + /** |
| 115 | + * Specify the normal vector for the polygon. This helps the tessellator determine the polygon orientation. For 2D tessellation, typically use (0, 0, 1) |
| 116 | + * @param x - X component of normal |
| 117 | + * @param y - Y component of normal |
| 118 | + * @param z - Z component of normal |
| 119 | + */ |
| 120 | + gluTessNormal(x: number, y: number, z: number): void; |
| 121 | + |
| 122 | + /** |
| 123 | + * Register a callback function for tessellation events. Different callback types have different signatures |
| 124 | + */ |
| 125 | + gluTessCallback( |
| 126 | + which: gluEnum.GLU_TESS_BEGIN | gluEnum.GLU_TESS_BEGIN_DATA, |
| 127 | + callback: ((type: primitiveType, polygonData?: any) => void) | null, |
| 128 | + ): void; |
| 129 | + gluTessCallback( |
| 130 | + which: gluEnum.GLU_TESS_EDGE_FLAG | gluEnum.GLU_TESS_EDGE_FLAG_DATA, |
| 131 | + callback: ((flag: boolean, polygonData?: any) => void) | null, |
| 132 | + ): void; |
| 133 | + gluTessCallback( |
| 134 | + which: gluEnum.GLU_TESS_VERTEX | gluEnum.GLU_TESS_VERTEX_DATA, |
| 135 | + callback: ((vertexData: any, polygonData?: any) => void) | null, |
| 136 | + ): void; |
| 137 | + gluTessCallback( |
| 138 | + which: gluEnum.GLU_TESS_END | gluEnum.GLU_TESS_END_DATA, |
| 139 | + callback: ((polygonData?: any) => void) | null, |
| 140 | + ): void; |
| 141 | + gluTessCallback( |
| 142 | + which: gluEnum.GLU_TESS_ERROR | gluEnum.GLU_TESS_ERROR_DATA, |
| 143 | + callback: ((errno: errorType | gluEnum, polygonData?: any) => void) | null, |
| 144 | + ): void; |
| 145 | + gluTessCallback( |
| 146 | + which: gluEnum.GLU_TESS_COMBINE | gluEnum.GLU_TESS_COMBINE_DATA, |
| 147 | + callback: |
| 148 | + | (( |
| 149 | + coords: number[], |
| 150 | + vertexData: any[], |
| 151 | + weight: number[], |
| 152 | + polygonData?: any, |
| 153 | + ) => any) |
| 154 | + | null, |
| 155 | + ): void; |
| 156 | + |
| 157 | + /** |
| 158 | + * Add a vertex to the current contour. Must be called between gluTessBeginContour and gluTessEndContour |
| 159 | + * @param coords - Vertex coordinates [x, y, z] |
| 160 | + * @param data - Client data associated with this vertex (will be passed to callbacks) |
| 161 | + */ |
| 162 | + gluTessVertex(coords: number[], data: any): void; |
| 163 | + |
| 164 | + /** |
| 165 | + * Begin a new polygon. Must be called before adding any contours |
| 166 | + * @param data - Client data for this polygon (will be passed to callbacks) |
| 167 | + */ |
| 168 | + gluTessBeginPolygon(data: any): void; |
| 169 | + |
| 170 | + /** |
| 171 | + * Begin a new contour within the current polygon. Must be called after gluTessBeginPolygon and before adding vertices |
| 172 | + */ |
| 173 | + gluTessBeginContour(): void; |
| 174 | + |
| 175 | + /** |
| 176 | + * End the current contour. Must be called after adding vertices to a contour |
| 177 | + */ |
| 178 | + gluTessEndContour(): void; |
| 179 | + |
| 180 | + /** |
| 181 | + * End the current polygon and perform tessellation. This triggers the tessellation and fires the registered callbacks |
| 182 | + */ |
| 183 | + gluTessEndPolygon(): void; |
| 184 | +} |
0 commit comments