A simple example of transparency does not work in Java 3D

I am full n00b in j3d (but an expert in Java). Just getting started, I am faced with the problem of playing with transparency. I have a simple example that draws a rotating flat square (disappears when displaying the back face, because I did not turn off the flipping of the back surface).

If the lines Color3b and COLOR_3 are uncommented (and commented on the corresponding lines Color4b and COLOR_4), I see a rotating square with colored red.

However, when I comment out the lines with color-3 and uncomment the color-4 lines, I see the BLACK square (on a white background), although the alpha value is set to 255 (completely opaque).

What am I doing wrong? Google doesn't help, and even the Java3D forum at java.forums.net is less useful. Stackoverflow, save me! You can copy and skip the program below, run it, and see what happens.

Here are my specs:

Java 6 on OSX 10.5.5 J3D 1.5.2 JOGL 1.1.1

Thanks,

- Rob

Here is the code:

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.applet.Applet;
import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;

public class Hello extends Applet
{
 public Hello() throws Exception
 {
  setLayout(new BorderLayout());
  GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
  Canvas3D canvas3D = new Canvas3D(config);

  add("Center", canvas3D);

  BranchGroup scene = createSceneGraph();
  scene.compile();

  SimpleUniverse univ = new SimpleUniverse(canvas3D);

  univ.getViewingPlatform().setNominalViewingTransform();

  univ.addBranchGraph(scene);
 }

 public BranchGroup createSceneGraph() throws Exception
 {
  BranchGroup root = new BranchGroup();

  // A white background.

  Background bgd = new Background(1.0f, 1.0f, 1.0f);
  root.addChild(bgd);

  // This will spin the quad around

  TransformGroup spin = new TransformGroup();
  spin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  root.addChild(spin);

  // define the quad:

  Point3d p1 = new Point3d(-0.5, -0.5, 0);
  Point3d p2 = new Point3d(0.5, -0.5, 0);
  Point3d p3 = new Point3d(0.5, 0.5, 0);
  Point3d p4 = new Point3d(-0.5, 0.5, 0);

  // colors

  Color4b c = new Color4b((byte)255, (byte)0, (byte)0, (byte)255);
  //Color3b c = new Color3b((byte)255, (byte)0, (byte)0);

  QuadArray quads = new QuadArray(4,
    GeometryArray.COORDINATES | GeometryArray.COLOR_4);
    // GeometryArray.COORDINATES | GeometryArray.COLOR_3);

  quads.setCoordinate(0, p1);
  quads.setCoordinate(1, p2);
  quads.setCoordinate(2, p3);
  quads.setCoordinate(3, p4);
  quads.setColor(0, c);
  quads.setColor(1, c);
  quads.setColor(2, c);
  quads.setColor(3, c);

  // Not sure if I need this. Doesn't seem to make a difference.

  Appearance appearance = new Appearance();
  TransparencyAttributes trans = new TransparencyAttributes();
  trans.setTransparencyMode(TransparencyAttributes.BLENDED);
  appearance.setTransparencyAttributes(trans);

  // Create the shape...

  Shape3D shape = new Shape3D();
  shape.setGeometry(quads);
  shape.setAppearance(appearance);

  spin.addChild(shape);

  Alpha rotationAlpha = new Alpha(-1, 4000);
  RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, spin);
  BoundingSphere bounds = new BoundingSphere();
  rotator.setSchedulingBounds(bounds);
  spin.addChild(rotator);

  return root;
 }

 public static void main(String[] args) throws Exception
 {
  Frame frame = new MainFrame(new Hello(), 256, 256);
 }
} 
+5
source share
1 answer

Replacing Color4b with Color4f, worked for me. Replace the corresponding lines in your code with the lines below,

Color color = new Color(255, 0, 0, 50);

Color4f c = new Color4f(color);
QuadArray quads = new QuadArray(4, 
           GeometryArray.COORDINATES | GeometryArray.COLOR_4);

AWT Color, ColorNx(), Color3b(), Color4b() Color4f() .. float , . - Color4f, AWT. AWT Color Color4b. Color4f, .

: Java- "1.6.0_10", Java 3D 1.5.2, Core2 Duo, OpenSUSE 11.0, Intel G33.

+1

All Articles