aboutsummaryrefslogtreecommitdiffstats
path: root/demos/MiscDemos/stencil.java
blob: b5f7fe7dafc7f6441ffff85d7e50cb21a6564115 (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
/**
 * @(#) stencil.java
 * @(#) author: Silicon Graphics (converted to Java by Ron Cemer)
 */

/*
 * 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.
 */

/*
 *  This program demonstrates use of the stencil buffer for
 *  masking nonrectangular regions.  
 *  Whenever the window is redrawn, a value of 1 is drawn 
 *  into a diamond-shaped region in the stencil buffer.  
 *  Elsewhere in the stencil buffer, the value is 0.
 *  Then a blue sphere is drawn where the stencil value is 1,
 *  and yellow torii are drawn where the stencil value is not 1.
 */

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.util.*;
import java.io.*;
import java.util.*;
import gl4java.GLContext;
import gl4java.awt.GLCanvas;

public class stencil extends Applet 
{
    stencilCanvas canvas1 = null;
    stencilCanvas canvas2 = null;

	Panel cvs = null;

        /* Initialize the applet */


	public void init()
	{
		Dimension d = getSize();
		setLayout(new BorderLayout());

		canvas1 = new stencilCanvas(d.width, d.height, 0, false);
		System.out.println("the left canvas has 0 stencil-bits, self-window");
		canvas2 = new stencilCanvas(d.width, d.height, 8, true);
		System.out.println("the right canvas should have 8 stencil-bits, ownWindow");

		cvs = new Panel();
		cvs.setLayout(new GridLayout(1,2));
		cvs.add(canvas1);
		if(canvas2!=null)
			cvs.add(canvas2);
		add("Center", cvs);
	}


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

	        GLContext.gljNativeDebug = true;
	        GLContext.gljClassDebug = true;

		stencil applet = new stencil();

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

		mainFrame.add(applet);

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

        /* Start the applet */


    public void start()
    {
    }


        /* Stop the applet */


    public void stop()
    {
    }


        /* Destroy the applet */



        /* Local GLCanvas extension class */


    private class stencilCanvas extends GLCanvas
    {
        private static final float M_PI = 3.14159265359f;
        private static final int YELLOWMAT = 1, BLUEMAT = 2;
        private boolean initdone = false;

        public stencilCanvas(int w, int h, 
	                     int _stencilBits,
			     boolean _createOwnWindow)
        {
            super(w, h);
	    stencilBits = _stencilBits;
	    createOwnWindow = _createOwnWindow;
        }
    
        public void preInit()
        {
            doubleBuffer = true;
            stereoView = false;
        }
    
        public void init()
        {
	    // Examine some OpenGL properties
	    int [] res=new int[6];

	    gl.glGetIntegerv(GL_STENCIL_BITS,res);

            System.out.println("init(): " + this + "\n\t" +
	                       "Stencil bits are "+res[0] +"\n\t" +
			       "IsOwnCreatedWindow: "+createOwnWindow);

            float yellow_diffuse[] = { 0.7f, 0.7f, 0.0f, 1.0f };
            float yellow_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
            
            float blue_diffuse[] = { 0.1f, 0.1f, 0.7f, 1.0f };
            float blue_specular[] = { 0.1f, 1.0f, 1.0f, 1.0f };
            
            float position_one[] = { 1.0f, 1.0f, 1.0f, 0.0f };
            
            gl.glNewList(YELLOWMAT, GL_COMPILE);
            gl.glMaterialfv(GL_FRONT, GL_DIFFUSE, yellow_diffuse);
            gl.glMaterialfv(GL_FRONT, GL_SPECULAR, yellow_specular);
            gl.glMaterialf(GL_FRONT, GL_SHININESS, 64.0f);
            gl.glEndList();
            
            gl.glNewList(BLUEMAT, GL_COMPILE);
            gl.glMaterialfv(GL_FRONT, GL_DIFFUSE, blue_diffuse);
            gl.glMaterialfv(GL_FRONT, GL_SPECULAR, blue_specular);
            gl.glMaterialf(GL_FRONT, GL_SHININESS, 45.0f);
            gl.glEndList();
            
            gl.glLightfv(GL_LIGHT0, GL_POSITION, position_one);
            
            gl.glEnable(GL_LIGHT0);
            gl.glEnable(GL_LIGHTING);
            gl.glEnable(GL_DEPTH_TEST);
            
            gl.glClearStencil(0);
            gl.glEnable(GL_STENCIL_TEST);

            glj.gljCheckGL();

            initdone = true;
            reshape(getSize().width, getSize().height);
        }
    
        public void cvsDispose()
        {
            System.out.println("destroy(): " + this);
            super.cvsDispose();
        }
    
        public void reshape(int width, int height)
        {
            gl.glViewport(0,0,width,height);

            if (initdone)
            {
                    /* create a diamond shaped stencil area */
                gl.glMatrixMode(GL_PROJECTION);
                gl.glLoadIdentity();
                if (width <= height)
                    glu.gluOrtho2D
                        (-3.0f,
                         3.0f,
                         -3.0f*(float)height/(float)width,
                         3.0f*(float)height/(float)width);
                else
                    glu.gluOrtho2D
                        (-3.0f*(float)width/(float)height,
                         3.0f*(float)width/(float)height,
                         -3.0f,
                         3.0f);
                gl.glMatrixMode(GL_MODELVIEW);
                gl.glLoadIdentity();

                gl.glClear(GL_STENCIL_BUFFER_BIT);
                gl.glStencilFunc(GL_ALWAYS, 1, 1);
                gl.glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
                gl.glBegin(GL_QUADS);
                gl.glVertex2f(-1.0f, 0.0f);
                gl.glVertex2f(0.0f, 1.0f);
                gl.glVertex2f(1.0f, 0.0f);
                gl.glVertex2f(0.0f, -1.0f);
                gl.glEnd();
            }

            gl.glMatrixMode(GL_PROJECTION);
            gl.glLoadIdentity();
            glu.gluPerspective(45.0f, (float)width/(float)height, 3.0f, 7.0f);
            gl.glMatrixMode(GL_MODELVIEW);
            gl.glLoadIdentity();
            gl.glTranslatef(0.0f, 0.0f, -5.0f);
        }

        public void display()
        {
            if (glj.gljMakeCurrent() == false) return;

                /* Draw a sphere in a diamond-shaped section in the
                 * middle of a window with 2 torii.
                 */

            gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                /* draw blue sphere where the stencil is 1 */
            gl.glStencilFunc(GL_EQUAL, 1, 1);
            gl.glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
            gl.glCallList(BLUEMAT);

            long qobj = glu.gluNewQuadric();
            glu.gluQuadricOrientation(qobj,GLU_OUTSIDE);
            glu.gluQuadricNormals(qobj,GLU_SMOOTH);
            glu.gluQuadricTexture(qobj,false);
            glu.gluSphere(qobj,0.5f,15,15);
            glu.gluDeleteQuadric(qobj);

                /* draw the tori where the stencil is not 1 */
            gl.glStencilFunc(GL_NOTEQUAL, 1, 1);
            gl.glPushMatrix();
            gl.glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
            gl.glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
            gl.glCallList(YELLOWMAT);
            glutSolidTorus(0.275f, 0.85f, 15, 15);
            gl.glPushMatrix();
            gl.glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
            glutSolidTorus(0.275f, 0.85f, 15, 15);
            gl.glPopMatrix();
            gl.glPopMatrix();

            glj.gljSwap();
            glj.gljCheckGL();
            glj.gljFree();
        }

        // Imported from glut.
        private void glutSolidTorus
            (float innerRadius,
             float outerRadius,
             int nsides,
             int rings)
        {
            doughnut(innerRadius, outerRadius, nsides, rings);
        }

        // Imported from glut.
        private void doughnut(float r, float R, int nsides, int rings)
        {
            int i, j;
            float theta, phi, theta1;
            float cosTheta, sinTheta;
            float cosTheta1, sinTheta1;
            float ringDelta, sideDelta;

            ringDelta = 2.0f * M_PI / rings;
            sideDelta = 2.0f * M_PI / nsides;

            theta = 0.0f;
            cosTheta = 1.0f;
            sinTheta = 0.0f;
            for (i = rings - 1; i >= 0; i--)
            {
                theta1 = theta + ringDelta;
                cosTheta1 = (float)Math.cos(theta1);
                sinTheta1 = (float)Math.sin(theta1);
                gl.glBegin(GL_QUAD_STRIP);
                phi = 0.0f;
                for (j = nsides; j >= 0; j--)
                {
                    float cosPhi, sinPhi, dist;

                    phi += sideDelta;
                    cosPhi = (float)Math.cos(phi);
                    sinPhi = (float)Math.sin(phi);
                    dist = R + r * cosPhi;
                    
                    gl.glNormal3f(cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi);
                    gl.glVertex3f(cosTheta1 * dist, -sinTheta1 * dist, r * sinPhi);
                    gl.glNormal3f(cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi);
                    gl.glVertex3f(cosTheta * dist, -sinTheta * dist,  r * sinPhi);
                }
                gl.glEnd();
                theta = theta1;
                cosTheta = cosTheta1;
                sinTheta = sinTheta1;
            }
        }
    }
}