Try this..

>Date: Fri, 03 May 2002 09:46:21 +0200
>From: "Schäfer, Peter" <[EMAIL PROTECTED]>
>Subject: Re: [JAVA3D] TextureCubeMap
>To: [EMAIL PROTECTED]
>MIME-version: 1.0
>Delivered-to: [EMAIL PROTECTED]
>
>>
>> Hi Rob
>>
>> > Does anyone have a quick-and-dirty demo of the new TextureCubeMap
>> > feature ? I was hoping that there would be a demo in the
>>
>> Yes I've got some.
>>
>
>Great ;-)
>
>but where are they ?
>
>> > new J3D 1.3 beta 2 but I couldn't find one when I looked.
>>
>> What new Version? Wow this comes with a surpise, starting webbrowser
>> surfing to java.sun.com ......
>>
>> EOF,
>>  J.D.
>>
>> --
>> Explore SRT with the help of Java3D
>> (http://wwwvis.informatik.uni-stuttgart.de/relativity/minkowski)
>> (http://www.antiflash.net/java3d/relativity (mirror)
>>
>> ==============================================================
>> =============
>> 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".
>>
>
>===========================================================================
>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".
/*
 *      @(#)TextureCubeMapTest.java 1.14 98/04/13 13:07:16
 *
 * Copyright (c) 1996-1998 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 com.sun.j3d.utils.behaviors.mouse.*;
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.applet.MainFrame;
import java.awt.image.*;
import java.awt.color.ColorSpace;
import com.sun.j3d.utils.geometry.Sphere;

public class TextureCubeMapTest extends Applet implements ItemListener {

    TexCoordGeneration genEyeLinear;
    TexCoordGeneration genObjectLinear;
    TexCoordGeneration genSphereMap;
    TexCoordGeneration genNormalMap;
    TexCoordGeneration genReflectionMap;
    Checkbox cbEyeLinear;
    Checkbox cbObjectLinear;
    Checkbox cbSphereMap;
    Checkbox cbNormalMap;
    Checkbox cbReflectionMap;
    Appearance cubeAppearance;
    TextureUnitState [] texture;

   /**
    *  Create the scenegraph.
    */
   public BranchGroup createSceneGraph() {

        // Define colors
        Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
        Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
        Color3f red   = new Color3f(0.80f, 0.20f, 0.2f);
        Color3f ambientRed = new Color3f(0.2f, 0.05f, 0.0f);
        Color3f ambient = new Color3f(0.6f, 0.6f, 0.6f);
        Color3f diffuse = new Color3f(0.7f, 0.7f, 0.7f);
        Color3f specular = new Color3f(0.7f, 0.7f, 0.7f);
        Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f);

        // Create the branch group
        BranchGroup branchGroup = new BranchGroup();

        // Create a Transformgroup to scale all objects so they
        // appear in the scene.
        TransformGroup objScale = new TransformGroup();
        Transform3D t3d = new Transform3D();
        t3d.setScale(0.4);
        objScale.setTransform(t3d);
        branchGroup.addChild(objScale);

        // Create the bounding leaf node
        BoundingSphere bounds =
           new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
        BoundingLeaf boundingLeaf = new BoundingLeaf(bounds);
        objScale.addChild(boundingLeaf);

        // Create the ambient light
        AmbientLight ambLight = new AmbientLight(white);
        ambLight.setInfluencingBounds(bounds);
        objScale.addChild(ambLight);

        // Create the directional light
        Vector3f dir = new Vector3f(-1.0f, -1.0f, -1.0f);
        DirectionalLight dirLight = new DirectionalLight(white, dir);
        dirLight.setInfluencingBounds(bounds);
        objScale.addChild(dirLight);

        // Create the Cube appearance node
        Material cubeMaterial =
           new Material(ambient, black, white, white, 75.0f);
        cubeMaterial.setLightingEnable(true);
        cubeAppearance = new Appearance();
        cubeAppearance.setMaterial(cubeMaterial);


        // Adding Environment mapping:
        Vector4f planeS = new Vector4f(1.0f, 0.0f, 0.0f, 1.0f);
        Vector4f planeT = new Vector4f(0.0f, 1.0f, 0.0f, 1.0f);
        Vector4f planeR = new Vector4f(0.0f, 0.0f, 0.0f, 0.0f);

        genEyeLinear =  new TexCoordGeneration(TexCoordGeneration.EYE_LINEAR,
                                    TexCoordGeneration.TEXTURE_COORDINATE_2,
                                    planeS,
                                    planeT,
                                    planeR);

        genObjectLinear =  new TexCoordGeneration(TexCoordGeneration.OBJECT_LINEAR,
                                    TexCoordGeneration.TEXTURE_COORDINATE_2,
                                    planeS,
                                    planeT,
                                    planeR);

        genSphereMap =  new TexCoordGeneration(TexCoordGeneration.SPHERE_MAP,
                                    TexCoordGeneration.TEXTURE_COORDINATE_3,
                                    planeS,
                                    planeT,
                                    planeR);

        genNormalMap = new TexCoordGeneration(TexCoordGeneration.NORMAL_MAP,
                                    TexCoordGeneration.TEXTURE_COORDINATE_3);

        genReflectionMap = new TexCoordGeneration(
                                    TexCoordGeneration.REFLECTION_MAP,
                                    TexCoordGeneration.TEXTURE_COORDINATE_3);

        texture = createTexture();

        cubeAppearance.setTextureUnitState(texture);


        // Create the white appearance node
        Material whiteMaterial =
           new Material(ambient, black, diffuse, specular, 75.0f);
        whiteMaterial.setLightingEnable(true);
        Appearance whiteAppearance = new Appearance();
        whiteAppearance.setMaterial(whiteMaterial);

        // Create the transform node
        TransformGroup transformGroup = new TransformGroup();
        transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        transformGroup.addChild(new Sphere(0.8f, cubeAppearance));

        objScale.addChild(transformGroup);

        // Create the drag behavior node
        MouseRotate behavior = new MouseRotate();
        behavior.setTransformGroup(transformGroup);
        transformGroup.addChild(behavior);
        behavior.setSchedulingBounds(bounds);

        // Create the zoom behavior node
        MouseZoom behavior2 = new MouseZoom();
        behavior2.setTransformGroup(transformGroup);
        transformGroup.addChild(behavior2);
        behavior2.setSchedulingBounds(bounds);

        // Create the zoom behavior node
        MouseTranslate behavior3 = new MouseTranslate();
        behavior3.setTransformGroup(transformGroup);
        transformGroup.addChild(behavior3);
        behavior3.setSchedulingBounds(bounds);
        branchGroup.setCapability(BranchGroup.ALLOW_DETACH);
        // Let Java 3D perform optimizations on this scene graph.
        branchGroup.compile();

        return branchGroup;
    }

    TextureUnitState[] createTexture() {


        // create texture cube map

        Color3f cubeColor[] = new Color3f[6];

        cubeColor[0] = new Color3f(1.0f, 0.0f, 0.0f);
        cubeColor[1] = new Color3f(0.0f, 1.0f, 0.0f);
        cubeColor[2] = new Color3f(0.0f, 0.0f, 1.0f);
        cubeColor[3] = new Color3f(1.0f, 1.0f, 0.0f);
        cubeColor[4] = new Color3f(1.0f, 0.0f, 1.0f);
        cubeColor[5] = new Color3f(0.0f, 1.0f, 1.0f);

        int width = 16;
        int height = 16;
        int numFaces = 6;

        TextureCubeMap texCubeMap = new TextureCubeMap(Texture.BASE_LEVEL,
                                           Texture.RGB, width);

        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        int[] nBits = {8, 8, 8};
        ComponentColorModel colorModel =
          new ComponentColorModel(cs, nBits, false, false, Transparency.OPAQUE
, 0);
        for (int face = 0; face < numFaces; face++) {

            WritableRaster raster =
                colorModel.createCompatibleWritableRaster(width, height);

            BufferedImage image =
                new BufferedImage(colorModel, raster, false, null);

            byte[] byteData = ((DataBufferByte)raster.getDataBuffer()).getData
();

            int k = 0;
            for (int i = 0; i < height; i++){
                for (int j = 0;j < width; j++){
                    byteData[k++] = (byte)(255 * cubeColor[face].x);
                    byteData[k++] = (byte)(255 * cubeColor[face].y);
                    byteData[k++] = (byte)(255 * cubeColor[face].z);
                }
            }

            ImageComponent2D imgCmp = new ImageComponent2D(
                ImageComponent.FORMAT_RGB, width, height);

            imgCmp.set(image);

            texCubeMap.setImage(0, face, imgCmp);
        }

        texCubeMap.setCapability(Texture.ALLOW_ENABLE_WRITE);
        texCubeMap.setCapability(Texture.ALLOW_IMAGE_WRITE);
        texCubeMap.setEnable(true);

        // create the basic texture

        Texture2D tex = new Texture2D(Texture.BASE_LEVEL,
                                         Texture.RGB, width, height);

        WritableRaster raster =
                colorModel.createCompatibleWritableRaster(width, height);

        BufferedImage image =
                new BufferedImage(colorModel, raster, false, null);

        byte[] byteData = ((DataBufferByte)raster.getDataBuffer()).getData
();

        int k = 0;
        int cc;

        for (int i = 0; i < height; i++){
            for (int j = 0;j < width; j++){
                if (((i + j) % 2) == 0)
                    cc = 0;
                else
                    cc = 0x7f;

                byteData[k++] = (byte)(cc);
                byteData[k++] = (byte)(cc);
                byteData[k++] = (byte)(cc);
            }
        }

        ImageComponent2D imgCmp = new ImageComponent2D(
                ImageComponent.FORMAT_RGB, width, height);

        imgCmp.set(image);

        tex.setImage(0, imgCmp);

        tex.setCapability(Texture.ALLOW_ENABLE_WRITE);
        tex.setCapability(Texture.ALLOW_IMAGE_WRITE);
        tex.setEnable(true);

        TextureAttributes texAttrs = new TextureAttributes();
        texAttrs.setTextureMode(TextureAttributes.MODULATE);

        TextureUnitState texUnitState[] = new TextureUnitState[2];

        texUnitState[0] = new TextureUnitState(tex, texAttrs, genObjectLinear);
        texUnitState[1] = new TextureUnitState(texCubeMap, texAttrs, genNormalMap);
        texUnitState[1].setCapability(TextureUnitState.ALLOW_STATE_WRITE);

        return texUnitState;
    }


    /**
     * Calls the various methods necessary to initialize the
     * program.
     */
    public void init() {

        // Set the layout manager
        setLayout(new BorderLayout());

        // Create the 3D canvas
        Canvas3D canvas = new Canvas3D(null);
        add("Center", canvas);

        System.out.println("textureCubeMapAvailable= " + 
((Boolean)canvas.queryProperties().get("textureCubeMapAvailable")).booleanValue());


        // Create the scene branch graph
        BranchGroup scene = createSceneGraph();

        // Create the Universe, the Locale, and the view branch graph
        SimpleUniverse u = new SimpleUniverse(canvas);

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

        u.addBranchGraph(scene);

        Panel panel = new Panel();
        CheckboxGroup cbg = new CheckboxGroup();
        cbEyeLinear = new Checkbox("Eye Linear", false, cbg);
        cbObjectLinear = new Checkbox("Object Linear", false, cbg);
        cbSphereMap = new Checkbox("Sphere Map", false, cbg);
        cbNormalMap = new Checkbox("Normal Map", true, cbg);
        cbReflectionMap = new Checkbox("Reflection Map", false, cbg);
        panel.add(new Label("CubeMap TexCoordGeneration mode:"));
        panel.add(cbEyeLinear);
        panel.add(cbObjectLinear);
        panel.add(cbSphereMap);
        panel.add(cbNormalMap);
        panel.add(cbReflectionMap);
        cbEyeLinear.addItemListener(this);
        cbObjectLinear.addItemListener(this);
        cbSphereMap.addItemListener(this);
        cbNormalMap.addItemListener(this);
        cbReflectionMap.addItemListener(this);
        add("South", panel);
   }


    public void itemStateChanged(ItemEvent e) {

        if (e.getSource() == cbEyeLinear) {
            System.out.println("Set to Eye Linear");
            texture[1].setTexCoordGeneration(genEyeLinear);

        } else if (e.getSource() == cbObjectLinear) {
            System.out.println("Set to Object Linear");
            texture[1].setTexCoordGeneration(genObjectLinear);
        } else if (e.getSource() == cbSphereMap) {
            System.out.println("Set to Sphere Map");
            texture[1].setTexCoordGeneration(genSphereMap);
        } else if (e.getSource() == cbNormalMap) {
            System.out.println("Set to NormalMap");
            texture[1].setTexCoordGeneration(genNormalMap);
        } else if (e.getSource() == cbReflectionMap) {
            System.out.println("Set to ReflectionMap");
            texture[1].setTexCoordGeneration(genReflectionMap);
        }
    }

    public static void main(String[] args) {
        new MainFrame(new TextureCubeMapTest(), 800, 800);
    }
}

Reply via email to