coordinate confusion

I do not understand why the output of this code does not form a rectangle. It looks more like a large #.

I've pulled my hair out trying all sorts of combinations w/o luck.

What did I do wrong?

package java3dtest;

import com.sun.j3d.utils.geometry.Box;

import com.sun.j3d.utils.universe.SimpleUniverse;

import javax.media.j3d.*;

import javax.vecmath.*;

import javax.swing.JFrame;

import java.awt.*;

public class SampleProblem extends JFrame {

public SampleProblem() {

super("SampleProblem");

setLayout(new BorderLayout());

GraphicsConfiguration config =

SimpleUniverse.getPreferredConfiguration();

Canvas3D canvas3D = new Canvas3D(config);

add("Center", canvas3D);

BranchGroup scene = createSceneGraph();

SimpleUniverse simpleU = new SimpleUniverse(canvas3D);

simpleU.getViewingPlatform().setNominalViewingTransform();

simpleU.addBranchGraph(scene);

setSize(900, 900);

}

protected BranchGroup createSceneGraph() {

BranchGroup bg = new BranchGroup();

TransformGroup tg = new TransformGroup();

bg.addChild(tg);

TransparencyAttributes ta = new TransparencyAttributes();

ta.setTransparencyMode(ta.BLENDED);

ta.setTransparency(0.5f);

Appearance appearanceR = new Appearance();

Appearance appearanceG = new Appearance();

Appearance appearanceB = new Appearance();

Appearance appearanceY = new Appearance();

Material materialR = new Material(new Color3f(Color.lightGray),

new Color3f(Color.red), new Color3f(Color.gray),

new Color3f(Color.gray), 133);

Material materialG = new Material(new Color3f(Color.lightGray),

new Color3f(Color.green), new Color3f(Color.gray),

new Color3f(Color.gray), 133);

Material materialB = new Material(new Color3f(Color.lightGray),

new Color3f(Color.blue), new Color3f(Color.gray),

new Color3f(Color.gray), 133);

Material materialY = new Material(new Color3f(Color.lightGray),

new Color3f(Color.yellow), new Color3f(Color.gray),

new Color3f(Color.gray), 133);

appearanceR.setMaterial(materialR);

appearanceR.setTransparencyAttributes(ta);

appearanceG.setMaterial(materialG);

appearanceG.setTransparencyAttributes(ta);

appearanceB.setMaterial(materialB);

appearanceB.setTransparencyAttributes(ta);

appearanceY.setMaterial(materialY);

appearanceY.setTransparencyAttributes(ta);

addBox(new Point3f(78f, 0, 0f),

new Point3f(80f, 40f, 0f), tg, appearanceY); // right side

addBox(new Point3f(0, 0, 0),

new Point3f(2f, 40, 0f), tg, appearanceR); //left side

addBox(new Point3f(0, 0, 0),

new Point3f(80f, 2f, 0f), tg, appearanceG); // bottom

addBox(new Point3f(0, 38f, 0),

new Point3f(80f, 40f, 0f), tg, appearanceB); //top

bg.compile();

return bg;

}

protected void addBox(Point3f lowIn,

Point3f highIn,

TransformGroup tg,

Appearance appearance) {

Point3f low = convertPointToMeters(lowIn);

Point3f high = convertPointToMeters(highIn);

Point3f extents = new Point3f(high);

extents.sub(low);

Box b1 = new Box(extents.x,

extents.y,

extents.z,

Box.GENERATE_NORMALS,

appearance);

// origin = center of box, move it so lower left of box = low

float scale = .5f;

Vector3f v = new Vector3f(low.x + (scale * extents.x),

low.y + (scale * extents.y),

low.z + ( scale * extents.z));

Transform3D b1Xfm = new Transform3D();

b1Xfm.setTranslation(v);

TransformGroup b1Grp = new TransformGroup(b1Xfm);

b1Grp.addChild(b1);

tg.addChild(b1Grp);

}

public float convertInchesToMeters(float inch) {

return inch * 0.00254f;

}

public Point3f convertPointToMeters(Point3f p) {

return new Point3f(convertInchesToMeters(p.x),

convertInchesToMeters(p.y),

convertInchesToMeters(p.z));

}

public static void main(String[] args) {

Frame frame = new SampleProblem();

frame.setVisible(true);

}

}

[4196 byte] By [Jared-Davisa] at [2007-10-2 21:08:44]
# 1

Google is my friend.

The answer is not in the transform but the Box class extents.

Box b1 = new Box(extents.x,

extents.y,

extents.z,

Box.GENERATE_NORMALS,

appearance);

Should be

Box b1 = new Box(extents.x / 2.0f,

extents.y / 2.0f,

extents.z / 2.0f,

Box.GENERATE_NORMALS,

appearance);

Then it works!

Jared-Davisa at 2007-7-13 23:54:35 > top of Java-index,Security,Cryptography...