Debian Codegraph @

cmd/smyrna/polytess.c

raw
1 /*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11 #include "config.h"
12
13 #include "polytess.h"
14 #include <stddef.h>
15 #include <util/alloc.h>
16 #include <xdot/xdot.h>
17 tessPoly TP;
18
19 #ifndef _WIN32
20 #define CALLBACK
21 #endif
22 static void CALLBACK combineCallback(double coords[3],
23 double *vertex_data[4],
24 float weight[4],
25 double **dataOut) {
26 (void)vertex_data;
27 (void)weight;
28
29 int i;
30 double *vertex = gv_calloc(6, sizeof(double));
31 vertex[0] = coords[0];
32 vertex[1] = coords[1];
33 vertex[2] = coords[2];
34 for (i = 3; i < 6; i++)
35 {
36 vertex[i] = 0;
37
38 }
39 *dataOut = vertex;
40 }
41
42 static void CALLBACK vertexCallback(void *vertex) {
43 glVertex3dv(vertex);
44 }
45
46 // OpenGL’s `gluTessCallback` function has a prototype indicating it takes a
47 // `void(*)(void)`. But its documentation describes passing in various function
48 // pointers with differing calling conventions. To use this API while also
49 // pacifying all the various build environments, we need this rather silly
50 // wrapper.
51 #ifdef _MSC_VER
52 // MSVC is of the (correct) opinion that casting between function pointers of
53 // incompatible calling conventions is unacceptable behavior…
54 #define MAKE_GLU_CALLBACK(f) f
55 #else
56 // …nevertheless other compilers insist we cast or they believe we have made a
57 // typo
58 #define MAKE_GLU_CALLBACK(f) ((void (*)(void))(f))
59 #endif
60
61 static GLUtesselator* Init(void)
62 {
63 // Create a new tessellation object
64 GLUtesselator* tobj = gluNewTess();
65 // Set callback functions
66 gluTessCallback(tobj, GLU_TESS_VERTEX, MAKE_GLU_CALLBACK(vertexCallback));
67 gluTessCallback(tobj, GLU_TESS_BEGIN, MAKE_GLU_CALLBACK(glBegin));
68 gluTessCallback(tobj, GLU_TESS_END, MAKE_GLU_CALLBACK(glEnd));
69 gluTessCallback(tobj, GLU_TESS_COMBINE, MAKE_GLU_CALLBACK(combineCallback));
70 return tobj;
71 }
72
73 static void Set_Winding_Rule(GLUtesselator *tobj, GLenum winding_rule)
74 {
75 // Set the winding rule
76 gluTessProperty(tobj, GLU_TESS_WINDING_RULE, winding_rule);
77 }
78
79 static void Render_Contour2(GLUtesselator *tobj, sdot_op* p)
80 {
81 double *d = gv_calloc(p->op.u.polygon.cnt * 3, sizeof(double));
82 for (size_t x = 0; x < p->op.u.polygon.cnt; x++)
83 {
84 d[x * 3] = p->op.u.polygon.pts[x].x;
85 d[x * 3 + 1] = p->op.u.polygon.pts[x].y;
86 d[x * 3 + 2] = p->op.u.polygon.pts[x].z + view->Topview->global_z;
87 }
88 for (size_t x = 0; x < p->op.u.polygon.cnt; x++) //loop through the vertices
89 {
90 gluTessVertex(tobj, &d[x * 3], &d[x * 3]); //store the vertex
91 }
92 }
93
94 static void Begin_Polygon(GLUtesselator *tobj)
95 {
96 gluTessBeginPolygon(tobj, NULL);
97 }
98 static void End_Polygon(GLUtesselator *tobj)
99 {
100 gluTessEndPolygon(tobj);
101 }
102 static void Begin_Contour(GLUtesselator *tobj)
103 {
104 gluTessBeginContour(tobj);
105 }
106 static void End_Contour(GLUtesselator *tobj)
107 {
108 gluTessEndContour(tobj);
109 }
110
111 void drawTessPolygon(sdot_op* p)
112 {
113 if (!TP.tobj)
114 {
115 TP.tobj=Init();
116 TP.windingRule=GLU_TESS_WINDING_ODD;
117 }
118 Set_Winding_Rule(TP.tobj,TP.windingRule);
119 Begin_Polygon(TP.tobj);
120 Begin_Contour(TP.tobj);
121 Render_Contour2(TP.tobj,p);
122 End_Contour(TP.tobj);
123 End_Polygon(TP.tobj);
124 }