summaryrefslogtreecommitdiffstats
path: root/src/demos/jrefract/JRefract.java
blob: d625b4315dab34685e118b47bf278e511601e58f (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
465
466
467
468
469
470
471
472
/*
 * 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 demos.common.Demo;
import demos.common.DemoListener;
// FIXME: commenting out demos that don't compile right now
//import demos.hdr.HDR;
import demos.hwShadowmapsSimple.HWShadowmapsSimple;
import demos.infiniteShadowVolumes.InfiniteShadowVolumes;
import demos.j2d.FlyingText;
import demos.jgears.JGears;
import demos.proceduralTexturePhysics.ProceduralTexturePhysics;
import demos.vertexBufferObject.VertexBufferObject;
import demos.vertexProgRefract.VertexProgRefract;
import demos.vertexProgWarp.VertexProgWarp;
import demos.xtrans.XTDesktopPane;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.awt.GLJPanel;
import com.sun.opengl.util.Animator;
import com.sun.opengl.util.FPSAnimator;
import javax.swing.JCheckBox;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;


/**
  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 Animator animator;
  private JDesktopPane desktop;

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

  private static final int GEARS     = 1;
  //  private static final int HDR       = 2;
  private static final int HWSHADOWS = 3;
  private static final int INFINITE  = 4;
  private static final int REFRACT   = 5;
  private static final int TEXT      = 6;
  private static final int VBO       = 7;
  private static final int WARP      = 8;
  private static final int WATER     = 9;

  private JInternalFrame addWindow(int which) {
    // FIXME: workaround for problem in 1.6 where ALL Components,
    // including Swing components, are Finalizable, requiring two full
    // GC cycles (and running of finalizers) to reclaim
    System.gc();
    // Try to get finalizers run
    try {
      Thread.sleep(1);
    } catch (InterruptedException e) {
    }
    System.gc();

    String str = "";
    switch (which) {
    case GEARS:     str = "Gears Demo"; break;
    //    case HDR:       str = "High Dynamic Range Rendering Demo"; break;
    case HWSHADOWS: str = "ARB_shadow Shadows"; break;
    case INFINITE:  str = "Infinite Shadow Volumes"; break;
    case REFRACT:   str = "Refraction Using Vertex Programs"; break;
    case TEXT:      str = "Flying Text"; break;
    case VBO:       str = "Very Simple vertex_buffer_object demo"; break;
    case WATER:     str = "Procedural Texture Waves"; break;
    }
    final JInternalFrame inner = new JInternalFrame(str);
    inner.setResizable(true);
    inner.setClosable(true);
    inner.setVisible(true);

    GLCapabilities caps = new GLCapabilities();
    if (which == INFINITE) {
      caps.setStencilBits(16);
    }
    final GLJPanel canvas =
      (which == GEARS) ?
      new JGears() :
      new GLJPanel(caps);
    final DemoListener demoListener = new DemoListener() {
        public void shutdownDemo() {
          removeJPanel(canvas);
          SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                inner.doDefaultCloseAction();
              }
            });
        }

        public void repaint() {
          canvas.repaint();
        }
      };

    Demo demo = null;
    switch (which) {
      case GEARS: {
        // GLEventListener already added
        break;
      }

      /*
      case HDR: {
        demo = new HDR();
        ((HDR) demo).setup(null);
        inner.setSize(((HDR) demo).getPreferredWidth(), ((HDR) demo).getPreferredHeight());
        break;
      }
      */

      case HWSHADOWS: {
        demo = new HWShadowmapsSimple();
        break;
      }

      case INFINITE: {
        demo = new InfiniteShadowVolumes();
        break;
      }

      case REFRACT: {
        demo = new VertexProgRefract();
        break;
      }

      case TEXT: {
        demo = new FlyingText();
        break;
      }

      case VBO: {
        demo = new VertexBufferObject();
        break;
      }

      case WARP: {
        demo = new VertexProgWarp();
        ((VertexProgWarp) demo).setTitleSetter(new VertexProgWarp.TitleSetter() {
            public void setTitle(final String title) {
              SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                    inner.setTitle(title);
                  }
                });
            }
          });
        break;
      }

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

    addJPanel(canvas);

    final Demo fDemo = demo;

    inner.addInternalFrameListener(new InternalFrameAdapter() {
        public void internalFrameClosed(InternalFrameEvent e) {
          if (fDemo != null) {
            fDemo.shutdownDemo();
          }
        }
      });

    inner.getContentPane().setLayout(new BorderLayout());
    /*    if (which == REFRACT) {
      // Testing scrolling
      canvas.setSize(512, 512);
      canvas.setPreferredSize(new Dimension(512, 512));
      JScrollPane scroller = new JScrollPane(canvas);
      inner.getContentPane().add(scroller);
      } else */ if (which == GEARS) {
      // Provide control over transparency of gears background
      canvas.setOpaque(false);
      JPanel gradientPanel = JGears.createGradientPanel();
      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 if (which == TEXT) {
      FlyingText text = (FlyingText) demo;
      inner.getContentPane().add(text.buildGUI(), BorderLayout.NORTH);
      inner.getContentPane().add(canvas, BorderLayout.CENTER);
    } else {
      inner.getContentPane().add(canvas, BorderLayout.CENTER);
    }

    // if (which != HDR) {
    inner.setSize(512, 512);
    // }
    desktop.add(inner);

    return inner;
  }

  public void run(String[] args) {
    JFrame frame = new JFrame("JOGL and Swing Interoperability");
    if ((args.length > 0) && args[0].equals("-xt")) {
      desktop = new XTDesktopPane();
      // FIXME: this is a hack to get the repaint behavior to work correctly
      ((XTDesktopPane) desktop).setAlwaysRedraw(true);
    } else {
      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;

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

    //    item = new JMenuItem("High Dynamic Range");
    //    item.addActionListener(new ActionListener() {
    //        public void actionPerformed(ActionEvent e) {
    //          addWindow(HDR);
    //        }
    //      });
    //    menu.add(item);

    item = new JMenuItem("Hardware Shadow Maps");
    item.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          addWindow(HWSHADOWS);
        }
      });
    menu.add(item);

    item = new JMenuItem("Infinite Shadow Volumes");
    item.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          addWindow(INFINITE);
        }
      });
    menu.add(item);

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

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

    item = new JMenuItem("Vertex Buffer Object");
    item.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          addWindow(VBO);
        }
      });
    menu.add(item);

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

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

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

    item = new JMenuItem("Quit");
    item.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          runExit();
        }
      });
    item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_MASK));
    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);

    animator = new FPSAnimator(60);
    animator.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() {
          animator.stop();
          System.exit(0);
        }
      }).start();
  }

  private synchronized void addJPanel(GLJPanel panel) {
    animator.add(panel);
  }

  private synchronized void removeJPanel(GLJPanel panel) {
    animator.remove(panel);
  }

  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();
  }
}