summaryrefslogtreecommitdiffstats
path: root/src/demos/jrefract/JRefract.java
blob: d0686a6a76667836318904e2fc14b21316d7b1a8 (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
/*
 * 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.hdr.HDR;
import demos.hwShadowmapsSimple.HWShadowmapsSimple;
import demos.infiniteShadowVolumes.InfiniteShadowVolumes;
import demos.proceduralTexturePhysics.ProceduralTexturePhysics;
import demos.util.*;
import demos.vertexBufferObject.VertexBufferObject;
import demos.vertexProgRefract.VertexProgRefract;
import demos.vertexProgWarp.VertexProgWarp;

/**
  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 HDR       = 2;
  private static final int HWSHADOWS = 3;
  private static final int INFINITE  = 4;
  private static final int REFRACT   = 5;
  private static final int VBO       = 6;
  private static final int WARP      = 7;
  private static final int WATER     = 8;

  private JInternalFrame addWindow(int which) {
    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 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 == GEARS) {
      caps.setAlphaBits(8);
    }
    if (which == INFINITE) {
      caps.setStencilBits(16);
    }
    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();
              }
            });
        }

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

    switch (which) {
      case GEARS: {
        canvas.addGLEventListener(new Gears());
        break;
      }

      case HDR: {
        HDR demo = new HDR();
        demo.setDemoListener(demoListener);
        demo.setup(null);
        inner.setSize(demo.getPreferredWidth(), demo.getPreferredHeight());
        canvas.addGLEventListener(demo);
        break;
      }

      case HWSHADOWS: {
        HWShadowmapsSimple demo = new HWShadowmapsSimple();
        demo.setDemoListener(demoListener);
        canvas.addGLEventListener(demo);
        break;
      }

      case INFINITE: {
        InfiniteShadowVolumes demo = new InfiniteShadowVolumes();
        demo.setDemoListener(demoListener);
        canvas.addGLEventListener(demo);
        break;
      }

      case REFRACT: {
        VertexProgRefract demo = new VertexProgRefract();
        demo.setDemoListener(demoListener);
        canvas.addGLEventListener(demo);
        break;
      }

      case VBO: {
        VertexBufferObject demo = new VertexBufferObject();
        demo.setDemoListener(demoListener);
        canvas.addGLEventListener(demo);
        break;
      }

      case WARP: {
        VertexProgWarp demo = new VertexProgWarp();
        demo.setDemoListener(demoListener);
        demo.setTitleSetter(new VertexProgWarp.TitleSetter() {
            public void setTitle(final String title) {
              SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                    inner.setTitle(title);
                  }
                });
            }
          });
        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 == REFRACT) {
      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);
    }

    if (which != HDR) {
      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;

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

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