sharedCopy behaviour?

Hi,

just tried to make a simple 3D maze viewer. Therefore I used only cubes as elements (com.xith3d. test.Cube). At first I tried to create one default cube and used sharedCopy to clone them (just moving the clones to their wanted positions). As far as I do understand it by reading the javadoc sharedcopy will copy an object’s geometry and appearance but nothing more.
Guess what happened: As soon as the first cube (the one the others are duplicated from) leaves the view all other will be hidden also. Did I miss something or is sharedCopy not to be used for such a purpose?.

Nevertheless it worked by creating all new objects and sharing appearance objects by hand …

Try to make a test that uses SharedGroup and Link - it should work.

As of using sharedCopy, I would again ask for a test case and an issue filled in IssueZilla.

Yuri

Hi Yuri,

you are right I should have posted an example. You can find it at:
www.m42.de/projects/xith3d/sharedcopy.zip
Any required additional lib should be in www.m42.de/projects/xith3d/xith_libs.zip.
(I am sorry not to have a working jnlp environment at the moment - therefore it is a bit more complicated than it could be).

It uses Xith3DCamera for camera movement and some basic code I got from JDC’s demos.

The relevant class is Levelold (sharedCopy is used in method getFullLevelNode):
`


package de.game.pac3d.xith3d.level;

import com.xith3d.scenegraph.*;
import com.xith3d.test.Cube;

import javax.vecmath.Color3f;
import javax.vecmath.Vector3f;
import javax.vecmath.Point3f;
import java.io.*;
import java.util.Vector;
import java.util.Hashtable;

/**
 * Created by IntelliJ IDEA.
 * User: Stefan
 * Date: 01.02.2004
 * Time: 13:40:06
 *
 *        __I__
 *    .-'"  .  "'-.
 *  .'  / . ' . \  '.
 * /_.-..-..-..-..-._\ .--------------------------------.
 *          #  _,,_   ( I hear it might rain code today )
 *          #/`    `\ /'-------------------------------'
 *          / / 6 6\ \
 *          \/\  Y /\/       /\-/\
 *          #/ `'U` \       /a a  \               _
 *        , (  \   | \     =\ Y  =/-~~~~~~-,_____/ )
 *        |\|\_/#  \_/       '^--'          ______/
 *        \/'.  \  /'\         \           /
 *         \    /=\  /         ||  |---'\  \
 *         /____)/____)       (_(__|   ((__|
 *
 */
public class Levelold
{
      public static final int SIZE = 65536;
      public static final float ITEM_LENGTH = 1f / SIZE;

      Hashtable elements;
      char fields[][];

      // boundary of field - surounding walkable level
      private final char BOUNDARY = '#';

      public Levelold(String name)
      {
            elements = new Hashtable();

            Vector lines = new Vector();
            int width = 0;

            BufferedReader myReader = null;
            try
            {
                  String data =
                        "####################" + "\n" +
                        "#                  #" + "\n" +
                        "# ABC   DEF   GHI  #" + "\n" +
                        "#                  #" + "\n" +
                        "#                  #" + "\n" +
                        "# ABC   DEF   GHI  #" + "\n" +
                        "# ABC   DEF   GHI  #" + "\n" +
                        "# ABC   DEF   GHI  #" + "\n" +
                        "#                  #" + "\n" +
                        "#       J J        #" + "\n" +
                        "#       J J        #" + "\n" +
                        "#                  #" + "\n" +
                        "# ABC   DEF   GHI  #" + "\n" +
                        "# ABC   DEF   GHI  #" + "\n" +
                        "#                  #" + "\n" +
                        "#                  #" + "\n" +
                        "# ABC   DEF   GHI  #" + "\n" +
                        "#    ..            #" + "\n" +
                        "#    .  S          #" + "\n" +
                              "####################" + "\n";

                  myReader = new BufferedReader(new StringReader(data));
                  String line = null;
                  while ((line = myReader.readLine()) != null)
                  {
                        line = line.trim();
                        if ("".equals(line) || line.startsWith("//"))
                        {
                              continue;
                        }

                        //width is number of chars in first line
                        // height depends upon
                        if (width == 0)
                        {
                              width = line.length();
                        }

                        lines.add(line);
                        System.out.println("line=" + line);
                  }
            }
            catch (FileNotFoundException e)
            {
                  e.printStackTrace();
                  throw new RuntimeException(e);
            }
            catch (IOException e)
            {
                  e.printStackTrace();
                  throw new RuntimeException(e);
            }

            // finally close
            try
            {
                  if (myReader != null)
                  {
                        myReader.close();
                  }
            }
            catch (IOException e)
            {
                  e.printStackTrace();
                  throw new RuntimeException(e);
            }

            // build level field
            int height = lines.size();
            fields = new char[height][width];
            for (int y = 0; y < height; y++)
            {
                  String line = (String) lines.elementAt(y);
                  for (int x = 0; x < width; x++)
                  {
                        // handle incomplete lines as normal but add boundaries for missing chars
                        char c = BOUNDARY;
                        // normally get char from file
                        if (x < line.length())
                        {
                              c = line.charAt(x);
                        }

                        fields[y][x] = c;
                        updateElements(c);
                  }
            }
      }

      private void updateElements(char aX)
      {
            String key = "" + aX;
            if (elements.contains(key))
            {
                  return;
            }

            Shape3D myShape3D = getShape3DFor(key);
            if (myShape3D != null)
            {
                  elements.put(key, myShape3D);
            }
            System.out.println("Key=" + key);
      }

      private Shape3D getShape3DFor(String aKey)
      {
            // currently we do allow empty spaces
            if (" ".equals(aKey))
            {
                  return null;
            }

            Shape3D myShape3D = new Shape3D();

            myShape3D.setGeometry(createPrimitive(aKey));
            myShape3D.setAppearance(createAppearance(aKey));
            myShape3D.setCompiled(false);

            return myShape3D;
      }

      private Appearance createAppearance(String aKey)
      {
            Color3f myColor3f = new Color3f((float) Math.random(),
                                            (float) Math.random(),
                                            (float) Math.random());
            ColoringAttributes myColoringAttributes = new ColoringAttributes(myColor3f,
                                                                             ColoringAttributes.NICEST);
            Appearance myAppearance = new Appearance();
            myAppearance.setColoringAttributes(myColoringAttributes);
            return myAppearance;
      }

      private Geometry createPrimitive(String aKey)
      {
            if (".".equals(aKey))
            {
                  Point3f[] coords = new Point3f[]
                  {
                        new Point3f(-.5f, -.5f, -.5f),
                        new Point3f(.5f, -.5f, -.5f),
                        new Point3f(.5f, -.5f, .5f),
                  };

                  TriangleArray g = new TriangleArray(coords.length,
                                                      GeometryArray.COORDINATES);
                  g.setCoordinates(0, coords);
                  return g;

            }

            // default
            Geometry g = Cube.createCubeViaTriangles(0, 0, 0, 1, false);
            return g;
      }

      public Node getFullLevelNode()
      {
            Group myGroup = new Group();

            Node firstNode = (Node) elements.get("" + BOUNDARY);
            int maxY = fields.length;
            int maxX = fields[0].length;

            for (int y = 0; y < maxY; y++)
            {
                  for (int x = 0; x < maxX; x++)
                  {
                        // space again nothing to do
                        if (fields[y][x] == ' ')
                        {
                              continue;
                        }
                        // move objects to correct place
                        Transform3D move = new Transform3D();
                        move.set(new Vector3f(x, 0, y));
                        TransformGroup myTransformGroup = new TransformGroup(move);
                        // add the node itself
                        Node node = firstNode.sharedCopy(((Node) elements.get("" + fields[y][x])));
                        //Node node = getShape3DFor("" + fields[y][x]);
                        myTransformGroup.addChild(node);

                        myGroup.addChild(myTransformGroup);
                        System.out.println("X=" + x + " Y=" + y);
                  }
            }

            return myGroup;
      }
}

`

Simply start it (required jars:displaySettings.jar;joal.jar;jogl.jar;junit.jar;log4j.jar;vecmath.jar;xith3d.jar;xith_utilities.jar,

  • main class is de.game.pac3d.xith3d.Xith3DFrameold) and you will see a black window. By pressing ‘Cursor down’ you can see a colored line coming in sight. Go forward again and it vanishes …
    To see the whole view use ‘Page up’ or ‘Page down’ to see the level from above or below (it’s a 20x20 field)

I am not sure if I am doing something unexpected or if it is an error. That is why I have not filled an IssueZilla but asked in this forum.