aboutsummaryrefslogtreecommitdiffstats
path: root/src/jogl/classes/jogamp/opengl/glu/tessellator/Normal.java
blob: e61f9dee17dc25c7b64c856be07b1a394097af0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* Portions Copyright (C) 2003-2006 Sun Microsystems, Inc.
* All rights reserved.
*/

/*
** License Applicability. Except to the extent portions of this file are
** made subject to an alternative license as permitted in the SGI Free
** Software License B, Version 2.0 (the "License"), the contents of this
** file are subject only to the provisions of the License. You may not use
** this file except in compliance with the License. You may obtain a copy
** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
**
** http://oss.sgi.com/projects/FreeB
**
** Note that, as provided in the License, the Software is distributed on an
** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
**
** NOTE:  The Original Code (as defined below) has been licensed to Sun
** Microsystems, Inc. ("Sun") under the SGI Free Software License B
** (Version 1.1), shown above ("SGI License").   Pursuant to Section
** 3.2(3) of the SGI License, Sun is distributing the Covered Code to
** you under an alternative license ("Alternative License").  This
** Alternative License includes all of the provisions of the SGI License
** except that Section 2.2 and 11 are omitted.  Any differences between
** the Alternative License and the SGI License are offered solely by Sun
** and not by SGI.
**
** Original Code. The Original Code is: OpenGL Sample Implementation,
** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
** Copyright in any portions created by third parties is as indicated
** elsewhere herein. All Rights Reserved.
**
** Additional Notice Provisions: The application programming interfaces
** established by SGI in conjunction with the Original Code are The
** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
** Window System(R) (Version 1.3), released October 19, 1998. This software
** was created using the OpenGL(R) version 1.2.1 Sample Implementation
** published by SGI, but has not been independently verified as being
** compliant with the OpenGL(R) version 1.2.1 Specification.
**
** Author: Eric Veach, July 1994
** Java Port: Pepijn Van Eeckhoudt, July 2003
** Java Port: Nathan Parker Burg, August 2003
*/
package jogamp.opengl.glu.tessellator;

import com.jogamp.opengl.*;
import com.jogamp.opengl.glu.*;

class Normal {
    private Normal() {
    }

    static boolean SLANTED_SWEEP = false;
    static double S_UNIT_X;    /* Pre-normalized */
    static double S_UNIT_Y;
    private static final boolean TRUE_PROJECT = false;

    static {
        if (SLANTED_SWEEP) {
/* The "feature merging" is not intended to be complete.  There are
 * special cases where edges are nearly parallel to the sweep line
 * which are not implemented.  The algorithm should still behave
 * robustly (ie. produce a reasonable tesselation) in the presence
 * of such edges, however it may miss features which could have been
 * merged.  We could minimize this effect by choosing the sweep line
 * direction to be something unusual (ie. not parallel to one of the
 * coordinate axes).
 */
            S_UNIT_X = 0.50941539564955385;    /* Pre-normalized */
            S_UNIT_Y = 0.86052074622010633;
        } else {
            S_UNIT_X = 1.0;
            S_UNIT_Y = 0.0;
        }
    }

    private static double Dot(final double[] u, final double[] v) {
        return (u[0] * v[0] + u[1] * v[1] + u[2] * v[2]);
    }

    static void Normalize(final double[] v) {
        double len = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];

        assert (len > 0);
        len = Math.sqrt(len);
        v[0] /= len;
        v[1] /= len;
        v[2] /= len;
    }

    static int LongAxis(final double[] v) {
        int i = 0;

        if (Math.abs(v[1]) > Math.abs(v[0])) {
            i = 1;
        }
        if (Math.abs(v[2]) > Math.abs(v[i])) {
            i = 2;
        }
        return i;
    }

    static void ComputeNormal(final GLUtessellatorImpl tess, final double[] norm) {
        jogamp.opengl.glu.tessellator.GLUvertex v, v1, v2;
        double c, tLen2, maxLen2;
        double[] maxVal, minVal, d1, d2, tNorm;
        jogamp.opengl.glu.tessellator.GLUvertex[] maxVert, minVert;
        final jogamp.opengl.glu.tessellator.GLUvertex vHead = tess.mesh.vHead;
        int i;

        maxVal = new double[3];
        minVal = new double[3];
        minVert = new jogamp.opengl.glu.tessellator.GLUvertex[3];
        maxVert = new jogamp.opengl.glu.tessellator.GLUvertex[3];
        d1 = new double[3];
        d2 = new double[3];
        tNorm = new double[3];

        maxVal[0] = maxVal[1] = maxVal[2] = -2 * GLU.GLU_TESS_MAX_COORD;
        minVal[0] = minVal[1] = minVal[2] = 2 * GLU.GLU_TESS_MAX_COORD;

        for (v = vHead.next; v != vHead; v = v.next) {
            for (i = 0; i < 3; ++i) {
                c = v.coords[i];
                if (c < minVal[i]) {
                    minVal[i] = c;
                    minVert[i] = v;
                }
                if (c > maxVal[i]) {
                    maxVal[i] = c;
                    maxVert[i] = v;
                }
            }
        }

/* Find two vertices separated by at least 1/sqrt(3) of the maximum
         * distance between any two vertices
         */
        i = 0;
        if (maxVal[1] - minVal[1] > maxVal[0] - minVal[0]) {
            i = 1;
        }
        if (maxVal[2] - minVal[2] > maxVal[i] - minVal[i]) {
            i = 2;
        }
        if (minVal[i] >= maxVal[i]) {
/* All vertices are the same -- normal doesn't matter */
            norm[0] = 0;
            norm[1] = 0;
            norm[2] = 1;
            return;
        }

/* Look for a third vertex which forms the triangle with maximum area
         * (Length of normal == twice the triangle area)
         */
        maxLen2 = 0;
        v1 = minVert[i];
        v2 = maxVert[i];
        d1[0] = v1.coords[0] - v2.coords[0];
        d1[1] = v1.coords[1] - v2.coords[1];
        d1[2] = v1.coords[2] - v2.coords[2];
        for (v = vHead.next; v != vHead; v = v.next) {
            d2[0] = v.coords[0] - v2.coords[0];
            d2[1] = v.coords[1] - v2.coords[1];
            d2[2] = v.coords[2] - v2.coords[2];
            tNorm[0] = d1[1] * d2[2] - d1[2] * d2[1];
            tNorm[1] = d1[2] * d2[0] - d1[0] * d2[2];
            tNorm[2] = d1[0] * d2[1] - d1[1] * d2[0];
            tLen2 = tNorm[0] * tNorm[0] + tNorm[1] * tNorm[1] + tNorm[2] * tNorm[2];
            if (tLen2 > maxLen2) {
                maxLen2 = tLen2;
                norm[0] = tNorm[0];
                norm[1] = tNorm[1];
                norm[2] = tNorm[2];
            }
        }

        if (maxLen2 <= 0) {
/* All points lie on a single line -- any decent normal will do */
            norm[0] = norm[1] = norm[2] = 0;
            norm[LongAxis(d1)] = 1;
        }
    }

    static void CheckOrientation(final GLUtessellatorImpl tess) {
        double area;
        jogamp.opengl.glu.tessellator.GLUface f;
        final jogamp.opengl.glu.tessellator.GLUface fHead = tess.mesh.fHead;
        jogamp.opengl.glu.tessellator.GLUvertex v;
        final jogamp.opengl.glu.tessellator.GLUvertex vHead = tess.mesh.vHead;
        jogamp.opengl.glu.tessellator.GLUhalfEdge e;

/* When we compute the normal automatically, we choose the orientation
 * so that the the sum of the signed areas of all contours is non-negative.
 */
        area = 0;
        for (f = fHead.next; f != fHead; f = f.next) {
            e = f.anEdge;
            if (e.winding <= 0) continue;
            do {
                area += (e.Org.s - e.Sym.Org.s) * (e.Org.t + e.Sym.Org.t);
                e = e.Lnext;
            } while (e != f.anEdge);
        }
        if (area < 0) {
/* Reverse the orientation by flipping all the t-coordinates */
            for (v = vHead.next; v != vHead; v = v.next) {
                v.t = -v.t;
            }
            tess.tUnit[0] = -tess.tUnit[0];
            tess.tUnit[1] = -tess.tUnit[1];
            tess.tUnit[2] = -tess.tUnit[2];
        }
    }

/* Determine the polygon normal and project vertices onto the plane
 * of the polygon.
 */
    public static void __gl_projectPolygon(final GLUtessellatorImpl tess) {
        jogamp.opengl.glu.tessellator.GLUvertex v;
        final jogamp.opengl.glu.tessellator.GLUvertex vHead = tess.mesh.vHead;
        double w;
        final double[] norm = new double[3];
        double[] sUnit, tUnit;
        int i;
        boolean computedNormal = false;

        norm[0] = tess.normal[0];
        norm[1] = tess.normal[1];
        norm[2] = tess.normal[2];
        if (norm[0] == 0 && norm[1] == 0 && norm[2] == 0) {
            ComputeNormal(tess, norm);
            computedNormal = true;
        }
        sUnit = tess.sUnit;
        tUnit = tess.tUnit;
        i = LongAxis(norm);

        if (TRUE_PROJECT) {
/* Choose the initial sUnit vector to be approximately perpendicular
 * to the normal.
 */
            Normalize(norm);

            sUnit[i] = 0;
            sUnit[(i + 1) % 3] = S_UNIT_X;
            sUnit[(i + 2) % 3] = S_UNIT_Y;

/* Now make it exactly perpendicular */
            w = Dot(sUnit, norm);
            sUnit[0] -= w * norm[0];
            sUnit[1] -= w * norm[1];
            sUnit[2] -= w * norm[2];
            Normalize(sUnit);

/* Choose tUnit so that (sUnit,tUnit,norm) form a right-handed frame */
            tUnit[0] = norm[1] * sUnit[2] - norm[2] * sUnit[1];
            tUnit[1] = norm[2] * sUnit[0] - norm[0] * sUnit[2];
            tUnit[2] = norm[0] * sUnit[1] - norm[1] * sUnit[0];
            Normalize(tUnit);
        } else {
/* Project perpendicular to a coordinate axis -- better numerically */
            sUnit[i] = 0;
            sUnit[(i + 1) % 3] = S_UNIT_X;
            sUnit[(i + 2) % 3] = S_UNIT_Y;

            tUnit[i] = 0;
            tUnit[(i + 1) % 3] = (norm[i] > 0) ? -S_UNIT_Y : S_UNIT_Y;
            tUnit[(i + 2) % 3] = (norm[i] > 0) ? S_UNIT_X : -S_UNIT_X;
        }

/* Project the vertices onto the sweep plane */
        for (v = vHead.next; v != vHead; v = v.next) {
            v.s = Dot(v.coords, sUnit);
            v.t = Dot(v.coords, tUnit);
        }
        if (computedNormal) {
            CheckOrientation(tess);
        }
    }
}