File: TessWind.cs

package info (click to toggle)
taoframework 2.1.svn20090801-15.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,312 kB
  • sloc: cs: 113,093; makefile: 908; sh: 107; ansic: 7
file content (375 lines) | stat: -rw-r--r-- 15,755 bytes parent folder | download | duplicates (6)
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#region License
/*
MIT License
Copyright 2003-2005 Tao Framework Team
http://www.taoframework.com
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#endregion License

#region Original Credits / License
/*
 * Copyright (c) 1993-1997, Silicon Graphics, Inc.
 * ALL RIGHTS RESERVED 
 * Permission to use, copy, modify, and distribute this software for 
 * any purpose and without fee is hereby granted, provided that the above
 * copyright notice appear in all copies and that both the copyright notice
 * and this permission notice appear in supporting documentation, and that 
 * the name of Silicon Graphics, Inc. not be used in advertising
 * or publicity pertaining to distribution of the software without specific,
 * written prior permission. 
 *
 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
 * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
 * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
 * 
 * US Government Users Restricted Rights 
 * Use, duplication, or disclosure by the Government is subject to
 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
 * clause at DFARS 252.227-7013 and/or in similar or successor
 * clauses in the FAR or the DOD or NASA FAR Supplement.
 * Unpublished-- rights reserved under the copyright laws of the
 * United States.  Contractor/manufacturer is Silicon Graphics,
 * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
 *
 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
 */
#endregion Original Credits / License

using System;
using Tao.FreeGlut;
using Tao.OpenGl;

namespace Redbook {
    #region Class Documentation
    /// <summary>
    ///     This program demonstrates the winding rule polygon tessellation property.  Four
    ///     tessellated objects are drawn, each with very different contours.  When the w key
    ///     is pressed, the objects are drawn with a different winding rule.
    /// </summary>
    /// <remarks>
    ///     <para>
    ///         Original Author:    Silicon Graphics, Inc.
    ///         http://www.opengl.org/developers/code/examples/redbook/tesswind.c
    ///     </para>
    ///     <para>
    ///         C# Implementation:  Randy Ridge
    ///         http://www.taoframework.com
    ///     </para>
    /// </remarks>
    #endregion Class Documentation
    public sealed class TessWind {
        // --- Fields ---
        #region Private Fields
        private static double currentWinding = Glu.GLU_TESS_WINDING_ODD;
        private static Glu.GLUtesselator tess;
        private static int list;

        private static double[][] rects = new double[12][] {
            new double[] {50.0, 50.0, 0.0},
            new double[] {300.0, 50.0, 0.0},
            new double[] {300.0, 300.0, 0.0},
            new double[] {50.0, 300.0, 0.0},
            new double[] {100.0, 100.0, 0.0},
            new double[] {250.0, 100.0, 0.0},
            new double[] {250.0, 250.0, 0.0},
            new double[] {100.0, 250.0, 0.0},
            new double[] {150.0, 150.0, 0.0},
            new double[] {200.0, 150.0, 0.0},
            new double[] {200.0, 200.0, 0.0},
            new double[] {150.0, 200.0, 0.0}
        };
        private static double[][] spiral = new double[16][] {
            new double[] {400.0, 250.0, 0.0},
            new double[] {400.0, 50.0, 0.0},
            new double[] {50.0, 50.0, 0.0},
            new double[] {50.0, 400.0, 0.0},
            new double[] {350.0, 400.0, 0.0},
            new double[] {350.0, 100.0, 0.0},
            new double[] {100.0, 100.0, 0.0},
            new double[] {100.0, 350.0, 0.0},
            new double[] {300.0, 350.0, 0.0},
            new double[] {300.0, 150.0, 0.0},
            new double[] {150.0, 150.0, 0.0},
            new double[] {150.0, 300.0, 0.0},
            new double[] {250.0, 300.0, 0.0},
            new double[] {250.0, 200.0, 0.0},
            new double[] {200.0, 200.0, 0.0},
            new double[] {200.0, 250.0, 0.0}
        };
        private static double[][] quad1 = new double[4][] {
            new double[] {50.0, 150.0, 0.0},
            new double[] {350.0, 150.0, 0.0},
            new double[] {350.0, 200.0, 0.0},
            new double[] {50.0, 200.0, 0.0}
        };
        private static double[][] quad2 = new double[4][] {
            new double[] {100.0, 100.0, 0.0},
            new double[] {300.0, 100.0, 0.0},
            new double[] {300.0, 350.0, 0.0},
            new double[] {100.0, 350.0, 0.0}
        };
        private static double[][] tri = new double[3][] {
            new double[] {200.0, 50.0, 0.0},
            new double[] {250.0, 300.0, 0.0},
            new double[] {150.0, 300.0, 0.0}
        };
        #endregion Private Fields

        // --- Entry Point ---
        #region Run()
        [STAThread]
        public static void Run() {
            Glut.glutInit();
            Glut.glutInitDisplayMode(Glut.GLUT_SINGLE | Glut.GLUT_RGB);
            Glut.glutInitWindowSize(500, 500);
            Glut.glutCreateWindow("TessWind");
            Init();
            Glut.glutDisplayFunc(new Glut.DisplayCallback(Display));
            Glut.glutKeyboardFunc(new Glut.KeyboardCallback(Keyboard));
            Glut.glutReshapeFunc(new Glut.ReshapeCallback(Reshape));
            Glut.glutMainLoop();
        }
        #endregion Run()

        // --- Application Methods ---
        #region Init()
        private static void Init() {
            Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            Gl.glShadeModel(Gl.GL_FLAT);
            tess = Glu.gluNewTess();
            Glu.gluTessCallback(tess, Glu.GLU_TESS_VERTEX, new Glu.TessVertexCallback1(Gl.glVertex3dv));
            Glu.gluTessCallback(tess, Glu.GLU_TESS_BEGIN, new Glu.TessBeginCallback(Begin));
            Glu.gluTessCallback(tess, Glu.GLU_TESS_END, new Glu.TessEndCallback(End));
            Glu.gluTessCallback(tess, Glu.GLU_TESS_ERROR, new Glu.TessErrorCallback(Error));
            Glu.gluTessCallback(tess, Glu.GLU_TESS_COMBINE, new Glu.TessCombineCallback1(Combine));

            list = Gl.glGenLists(4);
            MakeNewLists();
        }
        #endregion Init()

        #region MakeNewLists()
        /// <summary>
        ///     <para>
        ///         Make four display lists, each with a different tessellated object.
        ///     </para>
        /// </summary>
        private static void MakeNewLists() {
            int i;

            Glu.gluTessProperty(tess, Glu.GLU_TESS_WINDING_RULE, currentWinding);

            Gl.glNewList(list, Gl.GL_COMPILE);
                Glu.gluTessBeginPolygon(tess, IntPtr.Zero);
                    Glu.gluTessBeginContour(tess);
                        for(i = 0; i < 4; i++) {
                            Glu.gluTessVertex(tess, rects[i], rects[i]);
                        }
                    Glu.gluTessEndContour(tess);
                    Glu.gluTessBeginContour(tess);
                        for(i = 4; i < 8; i++) {
                            Glu.gluTessVertex(tess, rects[i], rects[i]);
                        }
                    Glu.gluTessEndContour(tess);
                    Glu.gluTessBeginContour(tess);
                        for(i = 8; i < 12; i++) {
                            Glu.gluTessVertex(tess, rects[i], rects[i]);
                        }
                    Glu.gluTessEndContour(tess);
                Glu.gluTessEndPolygon(tess);
            Gl.glEndList();

            Gl.glNewList(list + 1, Gl.GL_COMPILE);
                Glu.gluTessBeginPolygon(tess, IntPtr.Zero);
                    Glu.gluTessBeginContour(tess);
                        for(i = 0; i < 4; i++) {
                            Glu.gluTessVertex(tess, rects[i], rects[i]);
                        }
                    Glu.gluTessEndContour(tess);
                    Glu.gluTessBeginContour(tess);
                        for(i = 7; i >= 4; i--) {
                            Glu.gluTessVertex(tess, rects[i], rects[i]);
                        }
                    Glu.gluTessEndContour(tess);
                    Glu.gluTessBeginContour(tess);
                        for(i = 11; i >= 8; i--) {
                            Glu.gluTessVertex(tess, rects[i], rects[i]);
                        }
                    Glu.gluTessEndContour(tess);
                Glu.gluTessEndPolygon(tess);
            Gl.glEndList();

            Gl.glNewList(list + 2, Gl.GL_COMPILE);
                Glu.gluTessBeginPolygon(tess, IntPtr.Zero);
                    Glu.gluTessBeginContour(tess);
                        for(i = 0; i < 16; i++) {
                            Glu.gluTessVertex(tess, spiral[i], spiral[i]);
                        }
                    Glu.gluTessEndContour(tess);
                Glu.gluTessEndPolygon(tess);
            Gl.glEndList();

            Gl.glNewList(list + 3, Gl.GL_COMPILE);
                Glu.gluTessBeginPolygon(tess, IntPtr.Zero);
                    Glu.gluTessBeginContour(tess);
                        for(i = 0; i < 4; i++) {
                            Glu.gluTessVertex(tess, quad1[i], quad1[i]);
                        }
                    Glu.gluTessEndContour(tess);
                    Glu.gluTessBeginContour(tess);
                        for(i = 0; i < 4; i++) {
                            Glu.gluTessVertex(tess, quad2[i], quad2[i]);
                        }
                    Glu.gluTessEndContour(tess);
                    Glu.gluTessBeginContour(tess);
                        for(i = 0; i < 3; i++) {
                            Glu.gluTessVertex(tess, tri[i], tri[i]);
                        }
                    Glu.gluTessEndContour(tess);
                Glu.gluTessEndPolygon(tess);
            Gl.glEndList();
        }
        #endregion MakeNewLists()

        // --- Callbacks ---
        #region Begin(int which)
        private static void Begin(int which) {
            Gl.glBegin(which);
        }
        #endregion Begin(int which)

        #region Combine(double[] coordinates, double[][] vertexData, float[] weight, double[] dataOut)
        /// <summary>
        ///     <para>
        ///         The Combine callback is used to create a new vertex when edges intersect.
        ///         coordinate location is trivial to calculate, but weight[4] may be used to
        ///         average color, normal, or texture coordinate data.
        ///     </para>
        /// </summary>
        private static void Combine(double[] coordinates, double[] vertexData, float[] weight, double[] dataOut) {
            double[] vertex = new double[3];

            vertex[0] = coordinates[0];
            vertex[1] = coordinates[1];
            vertex[2] = coordinates[2];
            dataOut = vertex;
        }
        #endregion Combine(double[] coordinates, double[][] vertexData, float[] weight, double[] dataOut)

        #region Display()
        private static void Display() {
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
            Gl.glColor3f(1.0f, 1.0f, 1.0f);
            Gl.glPushMatrix(); 
                Gl.glCallList(list);
                Gl.glTranslatef(0.0f, 500.0f, 0.0f);
                Gl.glCallList(list + 1);
                Gl.glTranslatef(500.0f, -500.0f, 0.0f);
                Gl.glCallList(list + 2);
                Gl.glTranslatef(0.0f, 500.0f, 0.0f);
                Gl.glCallList(list + 3);
            Gl.glPopMatrix();
            Gl.glFlush();
        }
        #endregion Display()

        #region End()
        private static void End() {
            Gl.glEnd();
        }
        #endregion End()

        #region Error(int errorCode)
        private static void Error(int errorCode) {
            Console.WriteLine("Tessellation Error: {0}", Glu.gluErrorString(errorCode));
            Environment.Exit(1);
        }
        #endregion Error(int errorCode)

        #region Keyboard(byte key, int x, int y)
        private static void Keyboard(byte key, int x, int y) {
            switch(key) {
                case 27:
                    Environment.Exit(0);
                    break;
                case (byte) 'w':
                case (byte) 'W':
                    if(currentWinding == Glu.GLU_TESS_WINDING_ODD) {
                        currentWinding = Glu.GLU_TESS_WINDING_NONZERO;
                    }
                    else if(currentWinding == Glu.GLU_TESS_WINDING_NONZERO) {
                        currentWinding = Glu.GLU_TESS_WINDING_POSITIVE;
                    }
                    else if(currentWinding == Glu.GLU_TESS_WINDING_POSITIVE) {
                        currentWinding = Glu.GLU_TESS_WINDING_NEGATIVE;
                    }
                    else if(currentWinding == Glu.GLU_TESS_WINDING_NEGATIVE) {
                        currentWinding = Glu.GLU_TESS_WINDING_ABS_GEQ_TWO;
                    }
                    else if(currentWinding == Glu.GLU_TESS_WINDING_ABS_GEQ_TWO) {
                        currentWinding = Glu.GLU_TESS_WINDING_ODD;
                    }
                    MakeNewLists();
                    Glut.glutPostRedisplay();
                    break;
            }
        }
        #endregion Keyboard(byte key, int x, int y)

        #region Reshape(int w, int h)
        private static void Reshape(int w, int h) {
            Gl.glViewport(0, 0, w, h);
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            if(w <= h) {
                Glu.gluOrtho2D(0.0, 1000.0, 0.0, 1000.0 * (double) h / (double) w);
            }
            else {
                Glu.gluOrtho2D(0.0, 1000.0 * (double) w / (double) h, 0.0, 1000.0);
            }
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();
        }
        #endregion Reshape(int w, int h)

        #region Vertex(double[] vertex)
        private static void Vertex(double[] vertex) {
            double[] pointer;

            pointer = vertex;
            //Gl.glColor3dv(pointer);
            Gl.glColor3f(1.0f, 1.0f, 1.0f);
            Gl.glVertex3dv(vertex);
        }
        #endregion Vertex(double[] vertex)
    }
}