aboutsummaryrefslogtreecommitdiffstats
path: root/demos/MiscDemos/tesswind.java
blob: b25ca42793e098ed95db635fefda6bdaefc9488b (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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
 * 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 adverti(float)Math.sing
 * 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.227f.19(c)(2) or subparagraph
 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
 * clause at DFARS 252.227f-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.
 */

/*
 *  tesswind.c
 *  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.
 */
import gl4java.utils.glut.*;
import gl4java.utils.glut.fonts.*;
import gl4java.utils.textures.*;

import gl4java.awt.*;
import java.applet.*;
import java.awt.*;
import java.awt.Dimension;
import java.awt.event.*;
import java.lang.*;
import java.util.*;
import gl4java.*;

public class tesswind extends Applet {

    MyCanvas canvas = null;
    boolean isAnApplet = true;

    public void init()
    {
        Dimension d = getSize();
        setLayout(new BorderLayout());
	canvas = new MyCanvas (d.width, d.height);
        add("Center", canvas);
    }

	public static void main( String args[] ) 
	{
		Frame mainFrame = new Frame("tessdemo");

		tesswind applet = new tesswind();
    		applet.isAnApplet = false;

		applet.setSize(400, 400);
		applet.init();
		applet.start();

		mainFrame.add(applet);

		mainFrame.pack();
		mainFrame.setVisible(true);
	}

    public void start()
    {
    }


    public void stop()
    {
    }

	   static final String MENU_1 = "Odd Winding";
	   static final String MENU_2 = "Nonzero Winding";
	   static final String MENU_3 = "Positive Winding";
	   static final String MENU_4 = "Negative Winding";
	   static final String MENU_5 = "ABS >= 2 Winding";
           static final String MENU_SNAPSHOT = "Snapshot";

	   public static double rects[/*12*/][/*3*/] = 
	      { { 50.0, 50.0, 0.0}, 
	        { 300.0, 50.0, 0.0}, 
	        { 300.0, 300.0, 0.0}, 
		{ 50.0, 300.0, 0.0},
	        { 100.0, 100.0, 0.0}, 
		{ 250.0, 100.0, 0.0}, 
	        { 250.0, 250.0, 0.0}, 
		{ 100.0, 250.0, 0.0},
	        { 150.0, 150.0, 0.0}, 
		{ 200.0, 150.0, 0.0}, 
	        { 200.0, 200.0, 0.0}, 
		{ 150.0, 200.0, 0.0} };

	   public static double spiral[/*16*/][/*3*/] = 
	      { { 400.0, 250.0, 0.0}, 
	        { 400.0, 50.0, 0.0}, 
	        { 50.0, 50.0, 0.0}, 
		{ 50.0, 400.0, 0.0}, 
	        { 350.0, 400.0, 0.0}, 
		{ 350.0, 100.0, 0.0}, 
	        { 100.0, 100.0, 0.0}, 
		{ 100.0, 350.0, 0.0}, 
	        { 300.0, 350.0, 0.0}, 
		{ 300.0, 150.0, 0.0}, 
	        { 150.0, 150.0, 0.0}, 
		{ 150.0, 300.0, 0.0}, 
	        { 250.0, 300.0, 0.0}, 
		{ 250.0, 200.0, 0.0}, 
	        { 200.0, 200.0, 0.0}, 
		{ 200.0, 250.0, 0.0} };

	   public static double quad1[/*4*/][/*3*/] = 
	      { {50.0, 150.0, 0.0}, 
	        {350.0, 150.0, 0.0}, 
	        {350.0, 200.0, 0.0}, 
		{50.0, 200.0, 0.0} };

	   public static double quad2[/*4*/][/*3*/] =
	      { {100.0, 100.0, 0.0}, 
	        {300.0, 100.0, 0.0}, 
	        {300.0, 350.0, 0.0}, 
		{100.0, 350.0, 0.0} };

	   public static double tri[/*3*/][/*3*/] =
	      { {200.0, 50.0, 0.0}, 
	        {250.0, 300.0, 0.0},
	        {150.0, 300.0, 0.0} };
	 
    protected class MyCanvas extends GLCanvas 
        implements MouseListener, ActionListener
    {
        protected GLUTFunc glut = null;
    
	boolean exit = false;
	boolean mkNewLists = false;

	int startList=-1;

        private PopupMenu menu = null;
        private boolean menu_showing = false;
    
	TGATextureGrabber textgrab = null;

	boolean doSnapshot=false;

	public MyCanvas(int w, int h) {
	    super(w,h);
	    GLContext.gljNativeDebug = false;
	    GLContext.gljClassDebug  = false;
	}

	double currentWinding = GLU_TESS_WINDING_ODD;
	int currentShape = 0;
	int tobj;
	int list;

	public void init() {
	    int i;
	    glut = new GLUTFuncLightImplWithFonts(gl, glu);

	    textgrab = new TGATextureGrabber(gl);

	   gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	   gl.glShadeModel(GL_FLAT);    

	   tobj = glu.gluNewTess();

	   glu.gluTessCallback ( tobj, GLU_TESS_VERTEX, gl,
				"glVertex3dv", "([D)V",
				3, 0, 0, 0, 0);
	   glu.gluTessCallback ( tobj, GLU_TESS_BEGIN, this,
				"beginCallback", "(I)V",
				0, 0, 0, 0, 0);
	   glu.gluTessCallback ( tobj, GLU_TESS_END, this,
				"endCallback", "()V",
				0, 0, 0, 0, 0);
	   glu.gluTessCallback ( tobj, GLU_TESS_ERROR, this,
				"errorCallback", "(I)V",
				0, 0, 0, 0, 0);
	   glu.gluTessCallback ( tobj, GLU_TESS_COMBINE, this,
				"combineCallback", "([D[D[F[D)V",
				3, 0, 0, 3, 0);


	   list = gl.glGenLists(4);
	   makeNewLists();

	    reshape(getSize().width, getSize().height);

		glj.gljCheckGL();

		menu = new PopupMenu("Options");
		menu.add(MENU_1);
		menu.add(MENU_2);
		menu.add(MENU_3);
		menu.add(MENU_4);
		menu.add(MENU_5);
    		if(!isAnApplet)
			menu.add(MENU_SNAPSHOT);
		menu.addActionListener(this);
		add(menu);
                addMouseListener(this);
	}


	/*  Make four display lists, 
	 *  each with a different tessellated object. 
	 */
	void makeNewLists () 
	{
	   int i;
	   glu.gluTessProperty(tobj, GLU_TESS_WINDING_RULE, 
			   currentWinding);

	   gl.glNewList(list, GL_COMPILE);
	      glu.gluTessBeginPolygon(tobj, (double[])null);
		 glu.gluTessBeginContour(tobj);
		 for (i = 0; i < 4; i++)
		    glu.gluTessVertex(tobj, rects[i], rects[i]);
		 glu.gluTessEndContour(tobj);
		 glu.gluTessBeginContour(tobj);
		 for (i = 4; i < 8; i++)
		    glu.gluTessVertex(tobj, rects[i], rects[i]);
		 glu.gluTessEndContour(tobj);
		 glu.gluTessBeginContour(tobj);
		 for (i = 8; i < 12; i++)
		    glu.gluTessVertex(tobj, rects[i], rects[i]);
		 glu.gluTessEndContour(tobj);
	      glu.gluTessEndPolygon(tobj);
	   gl.glEndList();

	   gl.glNewList(list+1, GL_COMPILE);
	      glu.gluTessBeginPolygon(tobj, (double[])null);
		 glu.gluTessBeginContour(tobj);
		 for (i = 0; i < 4; i++)
		    glu.gluTessVertex(tobj, rects[i], rects[i]);
		 glu.gluTessEndContour(tobj);
		 glu.gluTessBeginContour(tobj);
		 for (i = 7; i >= 4; i--)
		    glu.gluTessVertex(tobj, rects[i], rects[i]);
		 glu.gluTessEndContour(tobj);
		 glu.gluTessBeginContour(tobj);
		 for (i = 11; i >= 8; i--)
		    glu.gluTessVertex(tobj, rects[i], rects[i]);
		 glu.gluTessEndContour(tobj);
	      glu.gluTessEndPolygon(tobj);
	   gl.glEndList();

	   gl.glNewList(list+2, GL_COMPILE);
	      glu.gluTessBeginPolygon(tobj, (double[])null);
		 glu.gluTessBeginContour(tobj);
		 for (i = 0; i < 16; i++)
		    glu.gluTessVertex(tobj, spiral[i], spiral[i]);
		 glu.gluTessEndContour(tobj);
	      glu.gluTessEndPolygon(tobj);
	   gl.glEndList();

	   gl.glNewList(list+3, GL_COMPILE);
	      glu.gluTessBeginPolygon(tobj, (double[])null);
		 glu.gluTessBeginContour(tobj);
		 for (i = 0; i < 4; i++)
		    glu.gluTessVertex(tobj, quad1[i], quad1[i]);
		 glu.gluTessEndContour(tobj);
		 glu.gluTessBeginContour(tobj);
		 for (i = 0; i < 4; i++)
		    glu.gluTessVertex(tobj, quad2[i], quad2[i]);
		 glu.gluTessEndContour(tobj);
		 glu.gluTessBeginContour(tobj);
		 for (i = 0; i < 3; i++)
		    glu.gluTessVertex(tobj, tri[i], tri[i]);
		 glu.gluTessEndContour(tobj);
	      glu.gluTessEndPolygon(tobj);
	   gl.glEndList();
	}

        public void display()
	{

	   if(exit) return;

	   /* Standard GL4Java Init */
	   if( glj.gljMakeCurrent() == false ) 
	   {
		System.out.println("problem in use() method");
		return;
	   }

	   if(mkNewLists)
	   {
           	makeNewLists();
		mkNewLists = false;
	   }

	   gl.glClear(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(); 

	   if(!isAnApplet && doSnapshot)
	   {
	   	doSnapshot=false;
		textgrab.grabPixels(GL_BACK, 
				    0, 0, cvsGetWidth(), cvsGetHeight());
		textgrab.write2File("tesswind.tga");
	   }

	   /* For your animation dutys ;-) */
	   glj.gljSwap();
	   glj.gljCheckGL();
	   glj.gljFree();
	}

	public void beginCallback(int which)
	{
	   gl.glBegin(which);
	}

	public void errorCallback(int errorCode)
	{
	   String       str;

	   gl.glColor3f( 0.9f, 0.9f, 0.9f );
	   gl.glRasterPos2i( 5, 5 );

	   str = glu.gluErrorString( errorCode );

	   glut.glutBitmapString(glut.GLUT_BITMAP_9_BY_15, str);
	   exit = true;
	}

	public void endCallback()
	{
	   gl.glEnd();
	}

	/*  combineCallback 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.
	 */
	/* ARGSUSED */
	public void combineCallback(double coords[/*3*/], double data[/*4xn*/],
			     float weight[/*4*/], double[/*3*/] dataOut )
	{
	   dataOut[0] = coords[0];
	   dataOut[1] = coords[1];
	   dataOut[2] = coords[2];
	}

	public void reshape(int w, int h)
	{
	   gl.glViewport( 0, 0, w, h );

	   gl.glMatrixMode( GL_PROJECTION );
	   gl.glLoadIdentity();
	   if (w <= h)
	      glu.gluOrtho2D(0.0f, 1000.0f, 0.0f, 1000.0f * (double)h/(double)w);
	   else
	      glu.gluOrtho2D(0.0f, 1000.0f * (double)w/(double)h, 0.0f, 1000.0f);
	   gl.glMatrixMode(GL_MODELVIEW);
	   gl.glLoadIdentity();
	}

        // Methods required for the implementation of MouseListener
        public void mouseEntered(MouseEvent evt)
        {
        }
    
        public void mouseExited(MouseEvent evt)
        {
        }
    
        public void mousePressed(MouseEvent evt)
        {
            if (!menu_showing)
            {
                if ((evt.getModifiers() & evt.BUTTON3_MASK) != 0)
                {
                    menu_showing = true;
                    menu.show(this,evt.getX(),evt.getY());
                }
            }
            else
            {
                menu_showing = false;
            }
        }
    
        public void mouseClicked(MouseEvent evt)
        {
	}

        public void mouseReleased(MouseEvent evt)
        {
        }
    
        // Method required for the implementation of ActionListener
        public void actionPerformed(ActionEvent evt)
        {
            String c = evt.getActionCommand();
            if (c.equals(MENU_1))
            {
		    currentWinding = GLU_TESS_WINDING_ODD;
            }
            else if (c.equals(MENU_2))
            {
		    currentWinding = GLU_TESS_WINDING_NONZERO;
            }
            else if (c.equals(MENU_3))
            {
		    currentWinding = GLU_TESS_WINDING_POSITIVE;
            }
            else if (c.equals(MENU_4))
            {
		    currentWinding = GLU_TESS_WINDING_NEGATIVE;
            }
            else if (c.equals(MENU_5))
            {
		    currentWinding = GLU_TESS_WINDING_ABS_GEQ_TWO;
            }
            else if (c.equals(MENU_SNAPSHOT))
            {
	   	doSnapshot=true;
            }
            if (menu_showing)
            {
                menu_showing = false;
            }
	    mkNewLists = true;
	    repaint();
        }
    }
}