aboutsummaryrefslogtreecommitdiffstats
path: root/demos/RonsDemos/glutplane.java
blob: c1e0e52aac8501b0e923242d412424d4223bdde9 (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
/**
 * @(#) glutplane.java
 * @(#) author: Mark J. Kilgard (converted to Java by Ron Cemer)
 */

/* This program is freely distributable without licensing fees 
   and is provided without guarantee or warrantee expressed or 
   implied. This program is -not- in the public domain. */

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

public class glutplane extends SimpleGLAnimApplet1 
{
        /* Initialize the applet */


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


    private class planeobj
    {
        float speed = 0.0f;          /* zero speed means not flying */
        float red = 0.0f, green = 0.0f, blue = 0.0f;
        float theta = 0.0f;
        float x = 0.0f, y = 0.0f, z = 0.0f, angle = 0.0f;

        public void setColor(float r, float g, float b)
        {
            red = r;
            green = g;
            blue = b;
        }
    };


        /* Local GLAnimCanvas extension class */


    private class glutplaneCanvas extends GLAnimCanvas
        implements MouseListener, ActionListener
    {
        private final double M_PI = 3.14159265;
        private final double M_PI_2 = 1.57079632;
        private final int MAX_PLANES = 15;
        private final String ADD_PLANE = "Add plane";
        private final String REMOVE_PLANE = "Remove plane";
        private final String SUSPEND_RESUME = "Suspend/resume animation";

        private planeobj planes[] = null;
        private Random random = null;
        private PopupMenu menu = null;
        private boolean menu_showing = false;
        private boolean save_suspended = false;

        public glutplaneCanvas(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()
        {
            System.out.println("init(): " + this);
            reshape(getSize().width, getSize().height);

            gl.glClearDepth(1.0f);
            gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            gl.glMatrixMode(GL_PROJECTION);
            gl.glFrustum(-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 20.0f);
            gl.glMatrixMode(GL_MODELVIEW);
                /* add three initial random planes */
            planes = new planeobj[MAX_PLANES];
            for (int i = 0; i < MAX_PLANES; i++)
                planes[i] = new planeobj();
            random = new Random(System.currentTimeMillis());
            add_plane();
            add_plane();
            add_plane();

            glj.gljCheckGL();

            menu = new PopupMenu("Options");
            menu.add(ADD_PLANE);
            menu.add(REMOVE_PLANE);
            menu.add(SUSPEND_RESUME);
            menu.addActionListener(this);
            add(menu);

            addMouseListener(this);
        }
    
        public void doCleanup()
        {
            System.out.println("destroy(): " + this);
            removeMouseListener(this);
            menu.removeActionListener(this);
        }
    
        public void reshape(int width, int height)
        {
            gl.glViewport(0,0,width,height);
        }

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

            tick();

            float red, green, blue;

            gl.glClear(GL_DEPTH_BUFFER_BIT);
                /* paint black to blue smooth shaded polygon for background */
            gl.glDisable(GL_DEPTH_TEST);
            gl.glShadeModel(GL_SMOOTH);
            gl.glBegin(GL_POLYGON);
            gl.glColor3f(0.0f, 0.0f, 0.0f);
            gl.glVertex3f(-20f, 20f, -19f);
            gl.glVertex3f(20f, 20f, -19f);
            gl.glColor3f(0.0f, 0.0f, 1.0f);
            gl.glVertex3f(20f, -20f, -19f);
            gl.glVertex3f(-20f, -20f, -19f);
            gl.glEnd();
                /* paint planes */
            gl.glEnable(GL_DEPTH_TEST);
            gl.glShadeModel(GL_FLAT);
            for (int i = 0; i < MAX_PLANES; i++)
            {
                if (planes[i].speed != 0.0f)
                {
                    gl.glPushMatrix();
                    gl.glTranslatef(planes[i].x, planes[i].y, planes[i].z);
                    gl.glRotatef(290.0f, 1.0f, 0.0f, 0.0f);
                    gl.glRotatef(planes[i].angle, 0.0f, 0.0f, 1.0f);
                    gl.glScalef(1.0f / 3.0f, 1.0f / 4.0f, 1.0f / 4.0f);
                    gl.glTranslatef(0.0f, -4.0f, -1.5f);
                    gl.glBegin(GL_TRIANGLE_STRIP);
                        /* left wing */
                    gl.glVertex3f(-7.0f, 0.0f, 2.0f);
                    gl.glVertex3f(-1.0f, 0.0f, 3.0f);
                    red = planes[i].red;
                    green = planes[i].green;
                    blue = planes[i].blue;
                    gl.glColor3f(red,green,blue);
                    gl.glVertex3f(-1.0f, 7.0f, 3.0f);
                        /* left side */
                    gl.glColor3f(0.6f * red, 0.6f * green, 0.6f * blue);
                    gl.glVertex3f(0.0f, 0.0f, 0.0f);
                    gl.glVertex3f(0.0f, 8.0f, 0.0f);
                        /* right side */
                    gl.glVertex3f(1.0f, 0.0f, 3.0f);
                    gl.glVertex3f(1.0f, 7.0f, 3.0f);
                        /* final tip of right wing */
                    gl.glColor3f(red, green, blue);
                    gl.glVertex3f(7.0f, 0.0f, 2.0f);
                    gl.glEnd();
                    gl.glPopMatrix();
                }
            }

            glj.gljSwap();
            glj.gljCheckGL();
            glj.gljFree();
    
            // if (!isSuspended()) repaint();  // Animate at full speed.
        }

        // Methods required for the implementation of MouseListener
        public void mouseEntered(MouseEvent evt)
        {
        }
    
        public void mouseExited(MouseEvent evt)
        {
        }
    
        public void mousePressed(MouseEvent evt)
        {
                // If user presses right mouse button within canvas area,
                // suspend animation and pop up menu.
                // If menu was already popped up and user presses either
                // mouse button within canvas area, resume animation
                // because the menu will have been removed automatically.
            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(ADD_PLANE))
            {
                add_plane();
            }
            else if (c.equals(REMOVE_PLANE))
            {
                remove_plane();
            }
            else if (c.equals(SUSPEND_RESUME))
            {
                if (menu_showing)
                    save_suspended = !save_suspended;
                else
                    setSuspended(!isSuspended());   // not likely to happen
            }
            if (menu_showing)
            {
                menu_showing = false;
                setSuspended(save_suspended);
            }
        }
    
        private void tick_per_plane(int i)
        {
            float theta = planes[i].theta += planes[i].speed;
            planes[i].z = -9.0f + 4.0f * (float)Math.cos(theta);
            planes[i].x = 4 * (float)Math.sin(2.0f * theta);
            planes[i].y = (float)Math.sin(theta / 3.4f) * 3.0f;
            planes[i].angle = (float)
                (((Math.atan(2.0) + M_PI_2) * Math.sin(theta) - M_PI_2) *
                 180.0 / M_PI);
            if (planes[i].speed < 0.0f) planes[i].angle += 180.0f;
        }

        private void add_plane()
        {
            for (int i = 0; i < MAX_PLANES; i++)
            {
                if (planes[i].speed == 0.0f)
                {
                    int c = random.nextInt() & 0x07;
                    while (c == 0) c = random.nextInt() & 0x07;
                    float r = (float)((c >> 2) & 0x01);
                    float g = (float)((c >> 1) & 0x01);
                    float b = (float)(c & 0x01);
                        // Blue fades into the background; lighten it up.
                    if (c == 0x01) r = g = 0.4f;
                    planes[i].setColor(r,g,b);
                    planes[i].speed =
                        ((float)(random.nextInt() % 20)) * 0.001f + 0.02f;
                    if ((random.nextInt() & 0x01) != 0)
                        planes[i].speed *= -1.0f;
                    planes[i].theta =
                        ((float)(random.nextInt() % 257))*0.1111f;
                    tick_per_plane(i);
                    if (isSuspended()) repaint();
                    return;
                }
            }
        }

        private void remove_plane()
        {
            for (int i = MAX_PLANES - 1; i >= 0; i--)
            {
                if (planes[i].speed != 0.0f)
                {
                    planes[i].speed = 0.0f;
                    if (isSuspended()) repaint();
                    return;
                }
            }
        }

        private void tick()
        {
            for (int i = 0; i < MAX_PLANES; i++)
            {
                if (planes[i].speed != 0.0f)
                    tick_per_plane(i);
            }
        }
    }
}