Hello,

If I apply multiple textures to multiple geometries in the same Shape3D
using a custom COMBINE mode, textures disappear.

As a test, I implemented a COMBINE mode version of j3d's built-in DECAL
mode. Then, in turn, I applied the custom version and the built-in version
to the same multi-geometried, mult-textured shape. The results were
different. The built-in DECAL mode has the desired effect, but the COMBINE
version causes textures to unexpectedly disappear. (Test code is given
below).

Are COMBINE modes necessarily inferior to built-in modes when used with
multiple textures and geometries, or is this a bug? Am I doing something
wrong? Is it my hardware (I do not have access to different platforms)? Any
other ideas?

Thanks,
Stoney

P.S. Is there an archive for this email list?

/* To reproduce my results, construct a Shape with a BuiltinDecal or a
 * CombineDecal and insert the Shape into a scene graph.
 */

/**
 * TextureAttribute using the built-in DECAL mode.
 */
public class BuiltinDecal extends TextureAttributes {
  public BuiltinDecal() { setTextureMode(DECAL); }
}

/**
 * A custom "decal" implementation using j3d's "combine" api.
 */
public class CombineDecal extends TextureAttributes {
  public CombineDecal() {
    setCombineRgbMode(COMBINE_INTERPOLATE);
    setCombineRgbSource(0, COMBINE_TEXTURE_COLOR);
    setCombineRgbFunction(0, COMBINE_SRC_COLOR);
    setCombineRgbSource(1, COMBINE_OBJECT_COLOR);
    setCombineRgbFunction(1, COMBINE_SRC_COLOR);
    setCombineRgbSource(2, COMBINE_TEXTURE_COLOR);
    setCombineRgbFunction(2, COMBINE_SRC_ALPHA);
    setCombineAlphaMode(COMBINE_REPLACE);
    setCombineAlphaSource(0, COMBINE_OBJECT_COLOR);
    setCombineAlphaFunction(0, COMBINE_SRC_ALPHA);
  }
}

/**
 * A shape with two geometries and two textures. The geometries are left and
 * right adjacent rectangles colored opaque black. The textures are of a "0"
 * and a "1" rendered in opaque white on a transparent black background. The
 * "0" is applied to the left rectangle, and the "1" is applied to the
right.
 * The textures are applied according the TextureMode in the
 * TextureAttributes supplied in the constructor.
 *
 * Construct by passing a BuiltinDecal or CombineDecal.
 */
public class Shape extends Shape3D {
  Font FONT = new Font("Courier", Font.PLAIN, 20);
  FontRenderContext CONTEXT = new FontRenderContext(null, false, true);
  Color FG_COLOR = Color.white;
  Color BG_COLOR = new Color(0f, 0f, 0f, 0f);
  Color3f GEOM_COLOR = new Color3f(0f, 0f, 0f);

  public Shape(TextureAttributes attributes) {
    // to hold the textures
    TextureUnitState[] units = new TextureUnitState[2];

    // construct the two textures
    BufferedImage bi =
      new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
    drawText(bi, "0", FONT, CONTEXT, FG_COLOR, BG_COLOR);
    ImageComponent ic =
      new ImageComponent2D(ImageComponent.FORMAT_RGBA, bi);
    Texture t = new Texture2D(Texture2D.BASE_LEVEL, Texture.RGBA, 32, 32);
    t.setImage(0, ic);

    units[0] = new TextureUnitState();
    units[0].setTexture(t);
    units[0].setTextureAttributes(attributes);

    bi = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
    drawText(bi, "1", FONT, CONTEXT, FG_COLOR, BG_COLOR);
    ic = new ImageComponent2D(ImageComponent.FORMAT_RGBA, bi);
    t = new Texture2D(Texture2D.BASE_LEVEL, Texture.RGBA, 32, 32);
    t.setImage(0, ic);

    units[1] = new TextureUnitState();
    units[1].setTexture(t);
    units[1].setTextureAttributes(attributes);

    Appearance a = new Appearance();
    a.setTextureUnitState(units);

    this.setAppearance(a);

    this.removeAllGeometries();

    float[] texCoords = getRectangle2d(0f, 0f, 1f, 1f);
    Color3f[] colors = getColors(GEOM_COLOR);

    QuadArray first =
      new QuadArray(
        4,
        QuadArray.COORDINATES
          | QuadArray.TEXTURE_COORDINATE_2
          | QuadArray.COLOR_3,
        1,
        new int[] { 0, -1 });
    first.setCoordinates(0, getRectangle3d(0f, 0f, 0f, .5f, .5f));
    first.setTextureCoordinates(0, 0, texCoords);
    first.setColors(0, colors);
    this.addGeometry(first);

    QuadArray second =
      new QuadArray(
        4,
        QuadArray.COORDINATES
          | QuadArray.TEXTURE_COORDINATE_2
          | QuadArray.COLOR_3,
        1,
        new int[] { -1, 0 });
    second.setCoordinates(0, getRectangle3d(.5f, 0f, 0f, .5f, .5f));
    second.setTextureCoordinates(0, 0, texCoords);
    second.setColors(0, colors);
    this.addGeometry(second);
  }

  private static void drawText(
    BufferedImage bi,
    String s,
    Font f,
    FontRenderContext c,
    Color foreground,
    Color background) {

    Graphics2D g = bi.createGraphics();

    g.setPaint(background);
    g.fillRect(0, 0, bi.getWidth(), bi.getHeight());

    g.setPaint(foreground);
    TextLayout l = new TextLayout(s, f, c);
    l.draw(g, 0, bi.getHeight() - l.getDescent());

    g.dispose();
  }

  private static float[] getRectangle2d(
    float x, float y, float w, float h) {
    return new float[] { x, y, x + w, y, x + w, y + h, x, y + h };
  }

  private static float[] getRectangle3d(
    float x, float y, float z, float w, float h) {
    return new float[] {
      x, y, z,
      x + w, y, z,
      x + w, y + h, z,
      x, y + h, z };
  }

  private static Color3f[] getColors(Color3f color) {
    return new Color3f[] { color, color, color, color };
  }
}

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to