Paula:
        Are you using 1.2.1? Using your code sample as guide, I modified
the texture program under demo/java3d/TextureTest/TextureImage and it works
correctly.  I have attached the modified program.



-Uma
Java3D Team

> MIME-version: 1.0
> X-MIMEOLE: Produced By Microsoft MimeOLE V5.00.2615.200
> Content-transfer-encoding: 7bit
> X-Priority: 3
> X-MSMail-priority: Normal
> Date: Sun, 1 Apr 2001 16:14:25 +0100
> From: Paula Keohan <[EMAIL PROTECTED]>
> Subject: [JAVA3D] Textures and transparency
> To: [EMAIL PROTECTED]
>
> Hi all,
>
> I've created an Appearance object from a Texture2D object. I've set the
> transparency attributes of the appearance also. But, when I change the
alpha
> value, it doesn't have any effect on the appearance. Anyone got any ideas
> why the transparency doesn't change? Is it just not possible with
textures
> created from images? I've included code below.
>
> Thanks,
> Paula.
>
>
> Appearance textureAppearance = new Appearance();
>   TransparencyAttributes transparency = new TransparencyAttributes(
>         TransparencyAttributes.NICEST, .85f);
>
>   PolygonAttributes polyAttrib = new PolygonAttributes();
>   polyAttrib.setCullFace(PolygonAttributes.CULL_BACK);
>   textureAppearance.setPolygonAttributes(polyAttrib);
>
>   TexCoordGeneration tcg = new
> TexCoordGeneration(TexCoordGeneration.OBJECT_LINEAR,
>        TexCoordGeneration.TEXTURE_COORDINATE_2);
>   textureAppearance.setTexCoordGeneration(tcg);
>
>   String filename = texturefilepath.getAbsolutePath() +
File.separatorChar +
> texturename;
>
>   TextureLoader loader = new TextureLoader(filename, this);
>   ImageComponent2D image = loader.getScaledImage(1024, 1024);
>
>   Texture2D texture = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA,
> image.getWidth(),
>               image.getHeight());
>   texture.setImage(0, image);
>   texture.setEnable(true);
>
>   textureAppearance.setTexture(texture);
>   textureAppearance.setTransparencyAttributes(transparency);
>
>
===========================================================================
> 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".
/*
 *      %Z%%M% %I% %E% %U%
 *
 * Copyright (c) 1996-2001 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Sun.
 *
 * 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 AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE 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 SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */

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 com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.geometry.Box;
import javax.media.j3d.*;
import javax.vecmath.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;

public class TextureImage extends JApplet implements ActionListener {

    private java.net.URL texImage = null;

    private SimpleUniverse u = null;

    JCheckBox changeTransp;

    TransparencyAttributes transparency1,   transparency2;
    Appearance app;

    public BranchGroup createSceneGraph() {
        // Create the root of the branch graph
        BranchGroup objRoot = new BranchGroup();

        // Create the transform group node and initialize it to the
        // identity.  Enable the TRANSFORM_WRITE capability so that
        // our behavior code can modify it at runtime.  Add it to the
        // root of the subgraph.
        TransformGroup objTrans = new TransformGroup();
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot.addChild(objTrans);

        // Create appearance object for textured cube
        app = new Appearance();
        Texture tex = new TextureLoader(texImage, this).getTexture();
        app.setTexture(tex);
        TextureAttributes texAttr = new TextureAttributes();
        texAttr.setTextureMode(TextureAttributes.MODULATE);
        app.setTextureAttributes(texAttr);

        transparency1 = new TransparencyAttributes(
                                                                         
TransparencyAttributes.NICEST, .85f);

        transparency2 = new TransparencyAttributes(
                                                                         
TransparencyAttributes.NICEST, .3f);

        PolygonAttributes polyAttrib = new PolygonAttributes();
        polyAttrib.setCullFace(PolygonAttributes.CULL_BACK);
        app.setPolygonAttributes(polyAttrib);
        app.setTransparencyAttributes(transparency1);
        app.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_WRITE);

  // Create textured cube and add it to the scene graph.
        Box textureCube = new Box(0.4f, 0.4f, 0.4f,
                                  Box.GENERATE_TEXTURE_COORDS, app);
        objTrans.addChild(textureCube);

        // Create a new Behavior object that will perform the desired
        // operation on the specified transform object and add it into
        // the scene graph.
        Transform3D yAxis = new Transform3D();
        Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
                                        0, 0,
                                        4000, 0, 0,
                                        0, 0, 0);

        RotationInterpolator rotator =
            new RotationInterpolator(rotationAlpha, objTrans, yAxis,
                                     0.0f, (float) Math.PI*2.0f);
        BoundingSphere bounds =
            new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
        rotator.setSchedulingBounds(bounds);
        objTrans.addChild(rotator);

        // Have Java 3D perform optimizations on this scene graph.
        objRoot.compile();

        return objRoot;
    }

    public TextureImage() {
    }

    public TextureImage(java.net.URL url) {
        texImage = url;
    }

    public void init() {
        Container contentPane = getContentPane();

        if (texImage == null) {
            // the path to the image for an applet
            try {
                texImage = new java.net.URL(getCodeBase().toString() +
                                            "../images/stone.jpg");
            }
            catch (java.net.MalformedURLException ex) {
                System.out.println(ex.getMessage());
                System.exit(1);
            }
        }
        GraphicsConfiguration config =
           SimpleUniverse.getPreferredConfiguration();

        Canvas3D c = new Canvas3D(config);
        contentPane.add("Center", c);

        // Create a simple scene and attach it to the virtual universe
        BranchGroup scene = createSceneGraph();
        u = new SimpleUniverse(c);

        // This will move the ViewPlatform back a bit so the
        // objects in the scene can be viewed.
        u.getViewingPlatform().setNominalViewingTransform();

        u.addBranchGraph(scene);
        JPanel p = new JPanel();
        BoxLayout boxlayout = new BoxLayout(p,
                                            BoxLayout.Y_AXIS);
        p.setLayout(boxlayout);

        changeTransp = new JCheckBox("Change Transparency",
                                      false);
        changeTransp.addActionListener(this);
        p.add(changeTransp);

        contentPane.add("South", p);

    }

    public void destroy() {
        u.removeAllLocales();
    }

    //
    // The following allows TextureImage to be run as an application
    // as well as an applet
    //
    public static void main(String[] args) {
        java.net.URL url = null;
        if (args.length > 0) {
            try {
                url = new java.net.URL("file:" + args[0]);
            }
            catch (java.net.MalformedURLException ex) {
                System.out.println(ex.getMessage());
                System.exit(1);
            }
        }
        else {
            // the path to the image for an application
            try {
                url = new java.net.URL("file:../images/stone.jpg");
            }
            catch (java.net.MalformedURLException ex) {
                System.out.println(ex.getMessage());
                System.exit(1);
            }
        }
        new MainFrame(new TextureImage(url), 256, 256);
    }
    public void actionPerformed(ActionEvent e) {
        int i;

        Object target = e.getSource();
        if (target == changeTransp) {
            if ( changeTransp.isSelected()) {
                app.setTransparencyAttributes(transparency2);
            }
            else {
                app.setTransparencyAttributes(transparency1);
            }
        }

    }


}

Reply via email to