summaryrefslogtreecommitdiffstats
path: root/src/demos/jrefract/JRefract.java
blob: eb5b751f3b0dc12fc5aefe341cc101e0519395aa (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
/*
 * Portions Copyright (C) 2005 Sun Microsystems, Inc.
 * All rights reserved.
 */

/*
 *
 * COPYRIGHT NVIDIA CORPORATION 2003. ALL RIGHTS RESERVED.
 * BY ACCESSING OR USING THIS SOFTWARE, YOU AGREE TO:
 *
 *  1) ACKNOWLEDGE NVIDIA'S EXCLUSIVE OWNERSHIP OF ALL RIGHTS
 *     IN AND TO THE SOFTWARE;
 *
 *  2) NOT MAKE OR DISTRIBUTE COPIES OF THE SOFTWARE WITHOUT
 *     INCLUDING THIS NOTICE AND AGREEMENT;
 *
 *  3) ACKNOWLEDGE THAT TO THE MAXIMUM EXTENT PERMITTED BY
 *     APPLICABLE LAW, THIS SOFTWARE IS PROVIDED *AS IS* AND
 *     THAT NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES,
 *     EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED
 *     TO, IMPLIED WARRANTIES OF MERCHANTABILITY  AND FITNESS
 *     FOR A PARTICULAR PURPOSE.
 *
 * IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS BE LIABLE FOR ANY
 * SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
 * WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS
 * OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
 * INFORMATION, OR ANY OTHER PECUNIARY LOSS), INCLUDING ATTORNEYS'
 * FEES, RELATING TO THE USE OF OR INABILITY TO USE THIS SOFTWARE,
 * EVEN IF NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 */

package demos.jrefract;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.nio.*;
import java.util.*;
import javax.imageio.*;
import javax.imageio.stream.*;
import javax.swing.*;
import javax.swing.event.*;

import net.java.games.jogl.*;
import net.java.games.jogl.util.*;
import demos.gears.Gears;
import demos.proceduralTexturePhysics.ProceduralTexturePhysics;
import demos.util.*;
import demos.vertexProgRefract.VertexProgRefract;

/**
  Wavelength-dependent refraction demo<br>
  It's a chromatic aberration!<br>
  sgreen@nvidia.com 4/2001<br><p>

  Currently 3 passes - could do it in 1 with 4 texture units<p>

  Cubemap courtesy of Paul Debevec<p>

  Ported to Java, Swing and ARB_fragment_program by Kenneth Russell
*/

public class JRefract {
  private boolean useRegisterCombiners;
  private ArrayList canvases;

  private volatile boolean quit;
  private volatile boolean animatorStopped;
  private JDesktopPane desktop;

  public static void main(String[] args) {
    new JRefract().run(args);
  }

  private static final int GEARS = 1;
  private static final int BUNNY = 2;
  private static final int WATER = 3;

  private JInternalFrame addWindow(int which) {
    String str = null;
    switch (which) {
    case GEARS: str = "Gears Demo"; break;
    case BUNNY: str = "Refraction Using Vertex Programs"; break;
    case WATER: str = "Procedural Texture Waves"; break;
    default: throw new IllegalArgumentException("Invalid demo " + which);
    }
    final JInternalFrame inner = new JInternalFrame(str);
    inner.setResizable(true);
    inner.setClosable(true);
    inner.setVisible(true);

    GLCapabilities caps = new GLCapabilities();
    if (which == GEARS) {
      caps.setAlphaBits(8);
    }
    final GLJPanel canvas = GLDrawableFactory.getFactory().createGLJPanel(caps);
    final DemoListener demoListener = new DemoListener() {
        public void shutdownDemo() {
          removeJPanel(canvas);
          SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                inner.doDefaultCloseAction();
              }
            });
        }
      };

    switch (which) {
      case GEARS: {
        canvas.addGLEventListener(new Gears());
        break;
      }
      case BUNNY: {
        VertexProgRefract demo = new VertexProgRefract();
        demo.setDemoListener(demoListener);
        canvas.addGLEventListener(demo);
        break;
      }
      case WATER: {
        ProceduralTexturePhysics demo = new ProceduralTexturePhysics();
        demo.setDemoListener(demoListener);
        canvas.addGLEventListener(demo);
        break;
      }
    }
    canvas.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
          canvas.requestFocus();
        }
      });

    addJPanel(canvas);

    inner.addInternalFrameListener(new InternalFrameAdapter() {
        public void internalFrameClosed(InternalFrameEvent e) {
          removeJPanel(canvas);
          System.gc();
        }
      });

    inner.getContentPane().setLayout(new BorderLayout());
    if (which == BUNNY) {
      inner.getContentPane().add(canvas, BorderLayout.CENTER);
      inner.getContentPane().add(new JButton("West"), BorderLayout.WEST);
      inner.getContentPane().add(new JButton("East"), BorderLayout.EAST);
      inner.getContentPane().add(new JButton("North"), BorderLayout.NORTH);
      inner.getContentPane().add(new JButton("South"), BorderLayout.SOUTH);
    } else if (which == GEARS) {
      // Provide control over transparency of gears background
      canvas.setOpaque(false);
      JPanel gradientPanel = new JPanel() {
          public void paintComponent(Graphics g) {
            ((Graphics2D) g).setPaint(new GradientPaint(0, 0, Color.WHITE,
                                                        getWidth(), getHeight(), Color.DARK_GRAY));
            g.fillRect(0, 0, getWidth(), getHeight());
          }
        };
      gradientPanel.setLayout(new BorderLayout());
      inner.getContentPane().add(gradientPanel, BorderLayout.CENTER);
      gradientPanel.add(canvas, BorderLayout.CENTER);

      final JCheckBox checkBox = new JCheckBox("Transparent", true);
      checkBox.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            canvas.setOpaque(!checkBox.isSelected());
          }
        });
      inner.getContentPane().add(checkBox, BorderLayout.SOUTH);
    } else {
      inner.getContentPane().add(canvas, BorderLayout.CENTER);
    }

    inner.setSize(512, 512);
    desktop.add(inner);

    return inner;
  }

  public void run(String[] args) {

    canvases = new ArrayList();

    JFrame frame = new JFrame("JOGL and Swing Interoperability");
    desktop = new JDesktopPane();
    desktop.setSize(1024, 768);
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(desktop, BorderLayout.CENTER);

    JInternalFrame inner2 = new JInternalFrame("Hello, World");
    JLabel label = new JLabel("Hello, World!");
    label.setFont(new Font("SansSerif", Font.PLAIN, 128));
    inner2.getContentPane().add(label);
    inner2.pack();
    inner2.setResizable(true);
    desktop.add(inner2);
    inner2.setVisible(true);

    JMenuBar menuBar = new JMenuBar();

    JMenu menu = new JMenu("Actions");
    JMenuItem item = new JMenuItem("New bunny");
    item.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          addWindow(BUNNY);
        }
      });
    menu.add(item);

    item = new JMenuItem("New gears");
    item.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          addWindow(GEARS);
        }
      });
    menu.add(item);

    item = new JMenuItem("New water");
    item.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          addWindow(WATER);
        }
      });
    menu.add(item);

    item = new JMenuItem("Auto mode");
    item.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          startAutoMode();
        }
      });
    menu.add(item);

    item = new JMenuItem("Exit");
    item.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          runExit();
        }
      });
    menu.add(item);

    menuBar.add(menu);
    frame.setJMenuBar(menuBar);

    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          runExit();
        }
      });
    frame.setSize(desktop.getSize());
    frame.setVisible(true);

    new Thread(new ListAnimator()).start();
  }

  private void runExit() {
    // Note: calling System.exit() synchronously inside the draw,
    // reshape or init callbacks can lead to deadlocks on certain
    // platforms (in particular, X11) because the JAWT's locking
    // routines cause a global AWT lock to be grabbed. Instead run
    // the exit routine in another thread.
    new Thread(new Runnable() {
        public void run() {
          quit = true;
          while (!animatorStopped) {
            try {
              Thread.sleep(1);
            } catch (InterruptedException e) {
            }
          }
          System.exit(0);
        }
      }).start();
  }

  private synchronized void addJPanel(GLJPanel panel) {
    ArrayList newCanvases = (ArrayList) canvases.clone();
    newCanvases.add(panel);
    canvases = newCanvases;
  }

  private synchronized void removeJPanel(GLJPanel panel) {
    ArrayList newCanvases = (ArrayList) canvases.clone();
    newCanvases.remove(panel);
    canvases = newCanvases;
  }

  class ListAnimator implements Runnable {
    public void run() {
      while (!quit) {
        if (canvases.isEmpty()) {
          try {
            Thread.sleep(10);
          } catch (InterruptedException e) {
          }
        } else {
          for (Iterator iter = canvases.iterator(); iter.hasNext(); ) {
            GLJPanel panel = (GLJPanel) iter.next();
            panel.display();
          }
          try {
            Thread.sleep(1);
          } catch (InterruptedException e) {
          }
        }
      }
      animatorStopped = true;
    }
  }

  private JInternalFrame curFrame;
  private void startAutoMode() {
    new Thread(new Runnable() {
        public void run() {
          while (true) {
            try {
              SwingUtilities.invokeAndWait(new Runnable() {
                  public void run() {
                    curFrame = addWindow(GEARS);
                  }
                });
            } catch (Exception e) {
              e.printStackTrace();
            }

            try {
              Thread.sleep(2000);
            } catch (InterruptedException e) {
            }

            try {
              SwingUtilities.invokeAndWait(new Runnable() {
                  public void run() {
                    curFrame.doDefaultCloseAction();
                  }
                });
            } catch (Exception e) {
              e.printStackTrace();
            }

            try {
              Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
          }
        }
      }).start();
  }
}