summaryrefslogtreecommitdiffstats
path: root/src/TextureByReference/TextureByReference.java
blob: 8e2507c19ec8433a9cc8165bfd8cffcdc8ea95d1 (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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*
 * $RCSfile$
 *
 * Copyright (c) 2006 Sun Microsystems, Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistribution of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any
 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
 * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
 * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
 * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
 * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
 * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
 * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that this software is not designed, licensed or
 * intended for use in the design, construction, operation or
 * maintenance of any nuclear facility.
 *
 * $Revision$
 * $Date$
 * $State$
 */

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.image.*;
import java.awt.color.ColorSpace;
import com.sun.j3d.utils.image.TextureLoader;
import javax.swing.*;
import javax.swing.event.*;


public class TextureByReference extends Applet 
implements ItemListener, ActionListener, ChangeListener {

  // need reference to animation behavior
  private AnimateTexturesBehavior animate;

  // need reference to tetrahedron
  private Tetrahedron tetra;
  
  // the gui buttons
   private JCheckBox flipB;
  private JRadioButton texByRef;
  private JRadioButton texByCopy;
  private JRadioButton geomByRef;
  private JRadioButton geomByCopy;
  private JRadioButton img4ByteABGR;
  private JRadioButton img3ByteBGR;
  private JRadioButton imgIntARGB;
  private JRadioButton imgCustomRGBA;
  private JRadioButton imgCustomRGB;
  private JRadioButton yUp;
  private JRadioButton yDown;
  private JButton animationB;
  private JSlider frameDelay;

    private SimpleUniverse universe = null;

  // image files used for the Texture animation for the applet,
  // or if no parameters are passed in for the application
  public static final String[] defaultFiles = {
    "../images/animation1.gif",
    "../images/animation2.gif",
    "../images/animation3.gif",
    "../images/animation4.gif",
    "../images/animation5.gif",
    "../images/animation6.gif",
    "../images/animation7.gif",
    "../images/animation8.gif",
    "../images/animation9.gif",
    "../images/animation10.gif"};

  private java.net.URL[] urls = null;

  
  public TextureByReference() {
  }

   public TextureByReference(java.net.URL[] fnamesP) {
     urls = fnamesP;
   }

  public void init() {
    if (urls == null) {
      urls = new java.net.URL[defaultFiles.length];
      for (int i = 0; i < defaultFiles.length; i++) {
	try {
	  urls[i] = new java.net.URL(getCodeBase().toString() + 
				       defaultFiles[i]);
	}
	catch (java.net.MalformedURLException ex) {
	  System.out.println(ex.getMessage());
	  System.exit(1);
	}
      }
    }
    setLayout(new BorderLayout());
    GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();

    Canvas3D canvas = new Canvas3D(config);

    add("Center", canvas);

    // create a simple scene graph and attach it to a simple universe
    BranchGroup scene = createSceneGraph();
    universe = new SimpleUniverse(canvas);
    universe.getViewingPlatform().setNominalViewingTransform();
    universe.addBranchGraph(scene);

    // create the gui
    JPanel gui = buildGui();
    
    this.add("South", gui);
  }

    public void destroy() {
	universe.cleanup();
    }

  public JPanel buildGui() {
    flipB = new JCheckBox("flip image", true);
    flipB.addItemListener(this);
    javax.swing.Box flipBox = new javax.swing.Box(BoxLayout.Y_AXIS);
    flipBox.add(flipB);
    Component strut1 = flipBox.createVerticalStrut(flipB.getPreferredSize().height);
    Component strut2 = flipBox.createVerticalStrut(flipB.getPreferredSize().height);
    Component strut3 = flipBox.createVerticalStrut(flipB.getPreferredSize().height);
    Component strut4 = flipBox.createVerticalStrut(flipB.getPreferredSize().height);
    Component strut5 = flipBox.createVerticalStrut(flipB.getPreferredSize().height);
    flipBox.add(strut1);
    flipBox.add(strut2);
    flipBox.add(strut3);
    flipBox.add(strut4);
    flipBox.add(strut5);

    yUp = new JRadioButton("y up");
    yUp.addActionListener(this);
    yUp.setSelected(true);
    yDown = new JRadioButton("y down");
    yDown.addActionListener(this);
    ButtonGroup yGroup = new ButtonGroup();
    yGroup.add(yUp);
    yGroup.add(yDown);
    JLabel yLabel = new JLabel("Image Orientation:");
    javax.swing.Box yBox = new javax.swing.Box(BoxLayout.Y_AXIS);
    yBox.add(yLabel);
    yBox.add(yUp);
    yBox.add(yDown);
    strut1 = yBox.createVerticalStrut(yUp.getPreferredSize().height);
    strut2 = yBox.createVerticalStrut(yUp.getPreferredSize().height);
    strut3 = yBox.createVerticalStrut(yUp.getPreferredSize().height);
    yBox.add(strut1);
    yBox.add(strut2);
    yBox.add(strut3);

    texByRef = new JRadioButton("by reference");
    texByRef.addActionListener(this);
    texByRef.setSelected(true);
    texByCopy = new JRadioButton("by copy");
    texByCopy.addActionListener(this);
    ButtonGroup texGroup = new ButtonGroup();
    texGroup.add(texByRef);
    texGroup.add(texByCopy);
    JLabel texLabel = new JLabel("Texture:*");
    javax.swing.Box texBox = new javax.swing.Box(BoxLayout.Y_AXIS);
    texBox.add(texLabel);
    texBox.add(texByRef);
    texBox.add(texByCopy);
    strut1 = texBox.createVerticalStrut(texByRef.getPreferredSize().height);
    strut2 = texBox.createVerticalStrut(texByRef.getPreferredSize().height);
    strut3 = texBox.createVerticalStrut(texByRef.getPreferredSize().height);
    texBox.add(strut1);
    texBox.add(strut2);
    texBox.add(strut3);

    geomByRef = new JRadioButton("by reference");
    geomByRef.addActionListener(this);
    geomByRef.setSelected(true);
    geomByCopy = new JRadioButton("by copy");
    geomByCopy.addActionListener(this);
    ButtonGroup geomGroup = new ButtonGroup();
    geomGroup.add(geomByRef);
    geomGroup.add(geomByCopy);
    JLabel geomLabel = new JLabel("Geometry:");
    javax.swing.Box geomBox = new javax.swing.Box(BoxLayout.Y_AXIS);
    geomBox.add(geomLabel);
    geomBox.add(geomByRef);
    geomBox.add(geomByCopy);
    strut1 = geomBox.createVerticalStrut(geomByRef.getPreferredSize().height);
    strut2 = geomBox.createVerticalStrut(geomByRef.getPreferredSize().height);
    strut3 = geomBox.createVerticalStrut(geomByRef.getPreferredSize().height);
    geomBox.add(strut1);
    geomBox.add(strut2);
    geomBox.add(strut3);
    
    img4ByteABGR = new JRadioButton("TYPE_4BYTE_ABGR");
    img4ByteABGR.addActionListener(this);
    img4ByteABGR.setSelected(true);
    img3ByteBGR = new JRadioButton("TYPE_3BYTE_BGR");
    img3ByteBGR.addActionListener(this);
    imgIntARGB = new JRadioButton("TYPE_INT_ARGB");
    imgIntARGB.addActionListener(this);
    imgCustomRGBA = new JRadioButton("TYPE_CUSTOM RGBA");
    imgCustomRGBA.addActionListener(this);
    imgCustomRGB = new JRadioButton("TYPE_CUSTOM RGB");
    imgCustomRGB.addActionListener(this);
    ButtonGroup imgGroup = new ButtonGroup();
    imgGroup.add(img4ByteABGR);
    imgGroup.add(img3ByteBGR);
    imgGroup.add(imgIntARGB);
    imgGroup.add(imgCustomRGBA);
    imgGroup.add(imgCustomRGB);
    JLabel imgLabel = new JLabel("Image Type:*");
    javax.swing.Box imgBox = new javax.swing.Box(BoxLayout.Y_AXIS);
    imgBox.add(imgLabel);
    imgBox.add(img4ByteABGR);
    imgBox.add(img3ByteBGR);
    imgBox.add(imgIntARGB);
    imgBox.add(imgCustomRGBA);
    imgBox.add(imgCustomRGB);

    javax.swing.Box topBox = new javax.swing.Box(BoxLayout.X_AXIS);
    topBox.add(flipBox);
    topBox.add(texBox);
    topBox.add(geomBox);
    topBox.add(yBox);
    Component strut = topBox.createRigidArea(new Dimension(10, 10));
    topBox.add(strut);
    topBox.add(imgBox);

    frameDelay = new JSlider(0, 50, 0);
    frameDelay.addChangeListener(this);
    frameDelay.setSnapToTicks(true);
    frameDelay.setPaintTicks(true);
    frameDelay.setPaintLabels(true);
    frameDelay.setMajorTickSpacing(10);
    frameDelay.setMinorTickSpacing(1);
    frameDelay.setValue(20);
    JLabel delayL = new JLabel("frame delay");
    javax.swing.Box delayBox = new javax.swing.Box(BoxLayout.X_AXIS);
    delayBox.add(delayL);
    delayBox.add(frameDelay);
    
    animationB = new JButton(" stop animation ");
    animationB.addActionListener(this);

    JLabel texInfo1 = new JLabel("*To use ImageComponent by reference feature, use TYPE_4BYTE_ABGR on Solaris");
    JLabel texInfo2 = new JLabel("and TYPE_3BYTE_BGR on Windows");

    JPanel buttonP = new JPanel();
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    buttonP.setLayout(gridbag);
    c.anchor = GridBagConstraints.CENTER;
    c.gridwidth = GridBagConstraints.REMAINDER;
    gridbag.setConstraints(topBox, c);
    buttonP.add(topBox);
    gridbag.setConstraints(delayBox, c);
    buttonP.add(delayBox);
    gridbag.setConstraints(animationB, c);
    buttonP.add(animationB);
    gridbag.setConstraints(texInfo1, c);
    buttonP.add(texInfo1);
    gridbag.setConstraints(texInfo2, c);
    buttonP.add(texInfo2);

    return buttonP;

  }

  public BranchGroup createSceneGraph() {

    // create the root of the branch group
    BranchGroup objRoot = new BranchGroup();

    // create the transform group node and initialize it
    // enable the TRANSFORM_WRITE capability so that it can be modified
    // at runtime.  Add it to the root of the subgraph
    Transform3D rotate = new Transform3D();
    TransformGroup objTrans = new TransformGroup(rotate);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objRoot.addChild(objTrans);

    // bounds
    BoundingSphere bounds =
      new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);

    // set up some light
    Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
    Vector3f lDir1  = new Vector3f(-1.0f, -0.5f, -1.0f);
    Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
    
    AmbientLight aLgt = new AmbientLight(alColor);
    aLgt.setInfluencingBounds(bounds);
    DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
    lgt1.setInfluencingBounds(bounds);
    objRoot.addChild(aLgt);
    objRoot.addChild(lgt1);
    

    Appearance appearance = new Appearance();

    // enable the TEXTURE_WRITE so we can modify it at runtime
    appearance.setCapability(Appearance.ALLOW_TEXTURE_WRITE);

    // load the first texture
    TextureLoader loader = new TextureLoader(urls[0],
					     TextureLoader.BY_REFERENCE |
 					     TextureLoader.Y_UP,
 					     this);
    // get the texture from the loader
    Texture2D tex = (Texture2D)loader.getTexture();
    
    // get the BufferedImage to convert to TYPE_4BYTE_ABGR and flip
    // get the ImageComponent because we need it anyway
    ImageComponent2D imageComp = (ImageComponent2D)tex.getImage(0);
    BufferedImage bImage = imageComp.getImage();
    // convert the image
    bImage = ImageOps.convertImage(bImage, BufferedImage.TYPE_4BYTE_ABGR);
    // flip the image
    ImageOps.flipImage(bImage);
    imageComp.set(bImage);

    tex.setCapability(Texture.ALLOW_IMAGE_WRITE);
    tex.setBoundaryModeS(Texture.CLAMP);
    tex.setBoundaryModeT(Texture.CLAMP);
    tex.setBoundaryColor(1.0f, 1.0f, 1.0f, 1.0f);
   
    // set the image of the texture
    tex.setImage(0, imageComp);

    // set the texture on the appearance
    appearance.setTexture(tex);

    // set texture attributes
    TextureAttributes texAttr = new TextureAttributes();
    texAttr.setTextureMode(TextureAttributes.MODULATE);
    appearance.setTextureAttributes(texAttr);

    // set material properties
    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
    appearance.setMaterial(new Material(white, black, white, black, 1.0f));

    // create a scale transform
    Transform3D scale = new Transform3D();
    scale.set(.6);
    TransformGroup objScale = new TransformGroup(scale);
    objTrans.addChild(objScale);

    tetra = new Tetrahedron(true);
    tetra.setAppearance(appearance);
    objScale.addChild(tetra);

    // create the behavior
    animate = new AnimateTexturesBehavior(tex, 
					  urls,
					  appearance,
					  this);
    animate.setSchedulingBounds(bounds);

    objTrans.addChild(animate);

    // add a rotation behavior so we can see all sides of the tetrahedron
      Transform3D yAxis = new Transform3D();
  	Alpha rotorAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
  				     0, 0,
  				     4000, 0, 0,
  				     0, 0, 0);
  	RotationInterpolator rotator =
  	    new RotationInterpolator(rotorAlpha,
  				     objTrans,
  				     yAxis,
  				     0.0f, (float) Math.PI*2.0f);
  	rotator.setSchedulingBounds(bounds);
	objTrans.addChild(rotator);       


    // have java3d perform optimizations on this scene graph
    objRoot.compile();

    return objRoot;
  }

  // callback for the animation button and delay text field
  public void actionPerformed(ActionEvent e) {
    Object o = e.getSource();

    // for the animation button
    if (o == animationB) {
      if (animate.getEnable()) {
	animate.setEnable(false);
	animationB.setText("start animation");
      }
      else {
	animate.setEnable(true);
	animationB.setText(" stop animation ");
      }
    }

    // for the texByRef button
    else if (o == texByRef && texByRef.isSelected()) {
      animate.setByReference(true);
    }
    // texByCopy button
    else if (o == texByCopy && texByCopy.isSelected()) {
      animate.setByReference(false);
    }
    // yUp button
    else if (o == yUp && yUp.isSelected()) {
      animate.setYUp(true);
    }
    // ydown button
    else if (o == yDown && yDown.isSelected()) {
      animate.setYUp(false);
    }
    //geomByRef button
    else if (o == geomByRef) {
      tetra.setByReference(true);
    }
    // geomByCopy button
    else if (o == geomByCopy) {
      tetra.setByReference(false);
    }
    // TYPE_INT_ARGB
    else if (o == imgIntARGB) {
      animate.setImageType(BufferedImage.TYPE_INT_ARGB);
    }
    // TYPE_4BYTE_ABGR
    else if (o == img4ByteABGR) {
      animate.setImageType(BufferedImage.TYPE_4BYTE_ABGR);
    }
    // TYPE_3BYTE_BGR
    else if (o == img3ByteBGR) {
      animate.setImageType(BufferedImage.TYPE_3BYTE_BGR);
    }
    // TYPE_CUSTOM RGBA
    else if (o == imgCustomRGBA) {
      animate.setImageTypeCustomRGBA();
    }
    // TYPE_CUSTOM RGB
    else if (o == imgCustomRGB) {
      animate.setImageTypeCustomRGB();
    }
  }

  // callback for the checkboxes
  public void itemStateChanged(ItemEvent e) {
    Object o = e.getSource();
    // for the flip checkbox
    if (o == flipB) {
      if (e.getStateChange() == ItemEvent.DESELECTED) {
	animate.setFlipImages(false);
      }
      else animate.setFlipImages(true);
    }
  }

  // callback for the slider
  public void stateChanged(ChangeEvent e) {
    Object o = e.getSource();
    // for the frame delay
    if (o == frameDelay) {
      animate.setFrameDelay(frameDelay.getValue());
    }
  }

  // allows TextureByReference to be run as an application as well as an applet
  public static void main(String[] args) {
    java.net.URL fnames[] = null;
    if (args.length > 1) {
      fnames = new java.net.URL[args.length];
      for (int i = 0; i < args.length; i++) {
	try {
	  fnames[i] = new java.net.URL("file:" + args[i]);
	}
	catch (java.net.MalformedURLException ex) {
	  System.out.println(ex.getMessage());
	}
      }
    }
    else {
      fnames = new java.net.URL[TextureByReference.defaultFiles.length];
      for (int i = 0; i < TextureByReference.defaultFiles.length; i++) {
	try {
	  fnames[i] = new java.net.URL("file:" +
				     TextureByReference.defaultFiles[i]);
	}
	catch (java.net.MalformedURLException ex) {
	  System.out.println(ex.getMessage());
	  System.exit(1);
	}
      }
    }
    new MainFrame((new TextureByReference(fnames)), 650, 750);    
  }
}