aboutsummaryrefslogtreecommitdiffstats
path: root/demos/MiscDemos/alpha3D.java
blob: a98237508e8fe725f8b77621ec1a28294c5fd4e2 (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
/**
 * @(#) alpha3D.java
 * @(#) author: Silicon Graphics, Inc. (converted to Java by Sven Goethel)
 */

/*
 *  This program demonstrates lots of material properties.
 *  A single light source illuminates the objects.
 */

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;
import gl4java.awt.GLAnimCanvas;
import gl4java.applet.SimpleGLAnimApplet1;

import gl4java.utils.glut.*;

public class alpha3D extends SimpleGLAnimApplet1 
{
	static final float MAXZ = 8.0f;
	static final float MINZ = -8.0f;
	static final float ZINC = 0.4f;

        /* Initialize the applet */


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


	public static void main( String args[] ) {
		alpha3D applet = 
		         new alpha3D();

		Frame f = new Frame("alpha3D");

		f.addWindowListener( new WindowAdapter()
				{
					public void windowClosed(WindowEvent e)
					{
						System.exit(0);
					}
					public void windowClosing(WindowEvent e)
					{
						windowClosed(e);
					}
				}
			);

		f.setLayout(new BorderLayout());
		f.add("Center", applet);
		applet.setSize(500,300);
		applet.init();
		applet.start();
		Dimension ps = applet.getPreferredSize();
		f.setBounds(-100,-100,99,99);
		f.setVisible(true);
		f.setVisible(false);
		Insets i = f.getInsets();
		f.setBounds(0,0,
			    ps.width+i.left+i.right, 
		            ps.height+i.top+i.bottom);
		f.setVisible(true);
	}
        /* Local GLCanvas extension class */


    private class alpha3DCanvas extends GLAnimCanvas
        implements MouseListener, ActionListener
    {
        private PopupMenu menu = null;
        private boolean menu_showing = false;
        private boolean save_suspended = false;
   	private final String MENUE_0 = "Alpha Mode";
   	private final String MENUE_1 = "Real Mode";

	GLUTFunc glut = null;

	float solidZ = MAXZ;
	float transparentZ = MINZ;
	int sphereList, cubeList;

        public alpha3DCanvas(int w, int h)
        {
            super(w, h);
            GLContext.gljNativeDebug = false;
            GLContext.gljClassDebug = false;
            setAnimateFps(30.0);
        }
    
        public void preInit()
        {
            doubleBuffer = true;
            stereoView = false;
        }
    

	public void init()
	{
	    glut = new GLUTFuncLightImpl(gl, glu);
	
            reshape(getSize().width, getSize().height);

	   float mat_specular[] = { 1.0f, 1.0f, 1.0f, 0.15f };
	   float mat_shininess[] = { 100.0f };
	   float position[] = { 0.5f, 0.5f, 1.0f, 0.0f };

	   gl.glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
	   gl.glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
	   gl.glLightfv(GL_LIGHT0, GL_POSITION, position);

	   gl.glEnable(GL_LIGHTING);
	   gl.glEnable(GL_LIGHT0);
	   gl.glEnable(GL_DEPTH_TEST);

	   sphereList = gl.glGenLists(1);
	   gl.glNewList(sphereList, GL_COMPILE);
	      glut.glutSolidSphere (0.4f, 16, 16);
	   gl.glEndList();

	   cubeList = gl.glGenLists(1);
	   gl.glNewList(cubeList, GL_COMPILE);
	      glut.glutSolidCube (0.6f);
	   gl.glEndList();

		menu = new PopupMenu("Options");
		menu.add(MENUE_0);
		menu.add(MENUE_1);
		menu.addActionListener(this);
		add(menu);

                addMouseListener(this);
	}

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

	   float mat_solid[] = { 0.75f, 0.75f, 0.0f, 1.0f };
	   float mat_zero[] = { 0.0f, 0.0f, 0.0f, 1.0f };
	   float mat_transparent[] = { 0.0f, 0.8f, 0.8f, 0.6f };
	   float mat_emission[] = { 0.0f, 0.3f, 0.3f, 0.6f };

	   gl.glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	   gl.glPushMatrix ();
	      gl.glTranslatef (-0.15f, -0.15f, solidZ);
	      gl.glMaterialfv(GL_FRONT, GL_EMISSION, mat_zero);
	      gl.glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_solid);
	      gl.glCallList (sphereList);
	   gl.glPopMatrix ();

	   gl.glPushMatrix ();
	      gl.glTranslatef (0.15f, 0.15f, transparentZ);
	      gl.glRotatef (15.0f, 1.0f, 1.0f, 0.0f);
	      gl.glRotatef (30.0f, 0.0f, 1.0f, 0.0f);
	      gl.glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
	      gl.glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_transparent);
	      gl.glEnable (GL_BLEND);
	      gl.glDepthMask (GL_FALSE);
	      gl.glBlendFunc (GL_SRC_ALPHA, GL_ONE);
	      gl.glCallList (cubeList);
	      gl.glDepthMask (GL_TRUE);
	      gl.glDisable (GL_BLEND);
	   gl.glPopMatrix ();

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

	   animate();
	}

	public void reshape(int w, int h)
	{
	   gl.glViewport(0, 0, (int) w, (int) h);
	   gl.glMatrixMode(GL_PROJECTION);
	   gl.glLoadIdentity();
	   if (w <= h)
	      gl.glOrtho (-1.5f, 1.5f, -1.5f*(float)h/(float)w,
		       1.5f*(float)h/(float)w, -10.0f, 10.0f);
	   else
	      gl.glOrtho (-1.5f*(float)w/(float)h,
		       1.5f*(float)w/(float)h, -1.5f, 1.5f, -10.0f, 10.0f);
	   gl.glMatrixMode(GL_MODELVIEW);
	   gl.glLoadIdentity();
	}

	void animate()
	{
	   if (solidZ <= MINZ || transparentZ >= MAXZ)
	      setSuspended(true);
	   else {
	      solidZ -= ZINC;
	      transparentZ += ZINC;
	   }
	}

        // 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;
                    save_suspended = isSuspended();
                    if (!save_suspended)
                    {
                        setSuspended(true);
                        repaint(100);
                        try
                        {
                            Thread.currentThread().sleep(200);
                        }
                        catch (Exception e)
                        { }
                    }
                    menu.show(this,evt.getX(),evt.getY());
                }
                else
                {
                        // Must be left button.
                    if (isSuspended()) repaint();
                }
            }
            else
            {
                menu_showing = false;
                setSuspended(save_suspended);
            }
        }
    
        public void mouseReleased(MouseEvent evt)
        {
        }
    
        public void mouseClicked(MouseEvent evt)
        {
        }

        // Method required for the implementation of ActionListener
        public void actionPerformed(ActionEvent evt)
        {
            String c = evt.getActionCommand();
            if (c.equals(MENUE_0))
            {
		 solidZ = MAXZ;
		 transparentZ = MINZ;
	         setSuspended(false);
		 return ;
            }
            else if (c.equals(MENUE_1))
            {
		 solidZ = MAXZ;
		 transparentZ = MINZ;
	         setSuspended(true);
		 repaint();
            }
            if (menu_showing)
            {
                menu_showing = false;
                setSuspended(save_suspended);
            }
        }
   }
}