Skip to main content

add a new shape to my branchgroup when I make a pick

2 replies [Last post]
mkarmen
Offline
Joined: 2006-08-21

Hi, I'm working on my university project and I have one problem. I have a branch group with all the scene.
And there I can pick a shape and which shae i have selected.
But now I want to draw the boundingbox of the shape I have selected, but when I tried to add the branchgroup with the boundingbox, it failed, and return and exception that says that i can add to a compiled or live node, but i don't know what to do. Please some help.

Code

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.*;
import java.awt.GraphicsConfiguration;
import java.util.*;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.behaviors.mouse.*;
import com.sun.j3d.utils.geometry.Box;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.image.codec.jpeg.*;

import java.util.Random;

import com.sun.j3d.utils.picking.behaviors.*;
import com.sun.j3d.utils.picking.*;
import com.sun.j3d.utils.picking.PickTool;

public class VerJuego extends Canvas3D{

private Canvas3D canvas3D;

public class DibujoFragmento{
private BranchGroup fragmento;

private BranchGroup drawBoundingBox(BoundingBox bounds){
BranchGroup bb = new BranchGroup();
Appearance app = createMaterialAppearance();
bb.setCapability(BranchGroup.ALLOW_DETACH);

Point3d upper = new Point3d();
Point3d lower = new Point3d();
Point3d c = new Point3d();
bounds.getUpper(upper);
bounds.getLower(lower);

Vector3f size = new Vector3f();
size.x = (float)(upper.x - lower.x);
size.y = (float)(upper.y - lower.y);
size.z = (float)(upper.z - lower.z);
c.x = lower.x + size.x/2f;
c.y = lower.y + size.y/2f;
c.z = lower.z + size.z/2f;

Primitive primitive = new Box(size.x/2f, size.y/2f, size.z/2f, app);

Transform3D trans = new Transform3D();
trans.set(new Vector3f((float)c.x, (float)c.y, (float)c.z));
TransformGroup tg = new TransformGroup(trans);
bb.addChild(tg);
tg.addChild(primitive);

return bb;
}

public DibujoFragmento(Fragmento fr, Vector malla, int name){
fragmento = new BranchGroup();
fragmento.setCapability(BranchGroup.ENABLE_PICK_REPORTING);
GeometryInfo gi = new GeometryInfo(GeometryInfo.TRIANGLE_STRIP_ARRAY);
NormalGenerator ng = new NormalGenerator();
Stripifier st = new Stripifier();
float [] coordenadas = fr.obtenerMalla(malla);
int [] strip = fr.StripCount();

gi.setCoordinates(coordenadas);
gi.setStripCounts(strip);

ng.generateNormals(gi);
gi.recomputeIndices();

st.stripify(gi);
gi.recomputeIndices();

Shape3D part = new Shape3D();
part.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
part.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
part.setCapability(Shape3D.ENABLE_PICK_REPORTING);

Color3f c = new Color3f();
c.set(fr.verColor());
part.setAppearance(createMaterialAppearanceColor(c));

part.setGeometry(gi.getGeometryArray());

//Traslacion del objeto
Transform3D traslate = new Transform3D();
traslate.set(new Vector3f(fr.posicionFragmentoX(), fr.posicionFragmentoY(), 0.0f));
TransformGroup objTraslacion = new TransformGroup(traslate);
objTraslacion.setCapability( Node.ALLOW_BOUNDS_READ );
objTraslacion.setCapability( Node.ALLOW_LOCAL_TO_VWORLD_READ );
objTraslacion.setCapability( TransformGroup.ALLOW_TRANSFORM_READ );
fragmento.addChild(objTraslacion);

//Escalado del objeto
Transform3D escalado = new Transform3D();
escalado.setScale(0.5);
TransformGroup objEscalado = new TransformGroup(escalado);
objEscalado.setCapability( Node.ALLOW_BOUNDS_READ );
objEscalado.setCapability( Node.ALLOW_LOCAL_TO_VWORLD_READ );
objEscalado.setCapability( TransformGroup.ALLOW_TRANSFORM_READ);
objEscalado.addChild(part);
objTraslacion.addChild(objEscalado);

Integer nombre = new Integer(name);
fragmento.setUserData(nombre.toString());
fragmento.setPickable(true);
fragmento.compile();
}

public BranchGroup getBG(){
return fragmento;
}
}

public class SimplePick extends Behavior{
private TransformGroup targetTG;
private Transform3D rotation = new Transform3D();
private WakeupCondition wCond;
private PickCanvas pickCanvas;
private Vector f;
private int fsel;
ShowBoundsBehavior sbb;

public SimplePick(BranchGroup targetBG, Canvas3D canvas, Vector elem, BranchGroup parent){
sbb = new ShowBoundsBehavior(parent);

wCond = new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED);
pickCanvas = new PickCanvas(canvas, targetBG);
pickCanvas.setTolerance(6.0f);
pickCanvas.setMode(PickCanvas.BOUNDS);
f = elem;
fsel = 0;
}

public void initialize(){
//condicion inicial
this.setSchedulingBounds(new BoundingSphere(new Point3d(), 1000));
this.wakeupOn(wCond);
}

public void processStimulus(Enumeration criteria){
PickResult pickResult;
MouseEvent event=(MouseEvent)((WakeupOnAWTEvent) criteria.nextElement()).getAWTEvent()[0];

int x = event.getX();
int y = event.getY();
pickCanvas.setShapeLocation(x, y);

if (pickCanvas.pickClosest() != null){ //si hay algo cerca
pickResult = pickCanvas.pickClosest();
System.out.println("--> PICK");
Node po = pickResult.getNode(PickResult.BRANCH_GROUP);
System.out.println("objeto = " + po.getUserData());
String name = (String)po.getUserData();
Integer n = new Integer(name);
((Fragmento)(f.elementAt(n.intValue()))).seleccionarFrag();
((Fragmento)(f.elementAt(fsel))).desSeleccionarFrag();

sbb.showBounds(po);
}

else{
((Fragmento)(f.elementAt(fsel))).desSeleccionarFrag();
//borro el boundingbox
}

this.wakeupOn(wCond);
}
}

(.....................)

public BranchGroup createSceneGraph(Partida p) {
int total = 0;

//GeometryInfo gi = new GeometryInfo(GeometryInfo.TRIANGLE_ARRAY);
GeometryInfo gi = new GeometryInfo(GeometryInfo.TRIANGLE_STRIP_ARRAY);
BranchGroup contentRoot = new BranchGroup();
NormalGenerator ng = new NormalGenerator();
Stripifier st = new Stripifier();
Shape3D part;
float[] coordenadas;
int[] strip;
TransformGroup objSpin;
BoundingSphere behaveBounds = new BoundingSphere();

Fragmento f;

for (int i = 0; i < p.numFragmentos(); i++) {
System.out.println("+++++++++ fragmento " + i + " +++++++++");

// Create the transform group node and initialize it to the
// identity. Add it to the root of the subgraph.
objSpin = new TransformGroup();
objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objSpin.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
contentRoot.addChild(objSpin);
f = p.verFragmento(i);
objSpin.addChild(new DibujoFragmento(f, (p.verPiezaR()).verMalla(), i).getBG());
}

// a bounding sphere specifies a region a behavior is active
// create a sphere centered at the origin with radius of 1
BoundingSphere bounds = new BoundingSphere();
addLights(contentRoot);

Background background = new Background();
background.setColor(0.0f, 0.169f, 0.317f);
background.setApplicationBounds(bounds);
contentRoot.addChild(background);

//para seleccionar los objetos
SimplePick picking = new SimplePick(contentRoot, this, p.vectorFragmentos(), contentRoot);
picking.setSchedulingBounds(new BoundingSphere());
contentRoot.addChild(picking);

/*PickRotateBehavior pickRotate = new PickRotateBehavior(contentRoot, this, behaveBounds);
contentRoot.addChild(pickRotate);*/

// Let Java 3D perform optimizations on this scene graph.
contentRoot.compile();

return contentRoot;
}

public VerJuego(GraphicsConfiguration config, Partida partida) {
super(config);
this.setSize(900, 500);

BranchGroup scene = createSceneGraph(partida);

// SimpleUniverse is a Convenience Utility class
SimpleUniverse simpleU = new SimpleUniverse(this);

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

simpleU.addBranchGraph(scene);
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mkarmen
Offline
Joined: 2006-08-21

No matter I have done this.

mkarmen
Offline
Joined: 2006-08-21

I have change the code of the class SimplePick:

public class SimplePick extends Behavior{
private TransformGroup targetTG;
private Transform3D rotation = new Transform3D();
private WakeupCondition wCond;
private PickCanvas pickCanvas;
private Vector f;
private int fsel;
private BranchGroup root;

private BranchGroup showBoundingBox(Node node){
node.setCapability( Node.ALLOW_BOUNDS_READ );
node.setCapability( Node.ALLOW_LOCAL_TO_VWORLD_READ );

BranchGroup bb = new BranchGroup();
Appearance app = createMaterialAppearance();
bb.setCapability(BranchGroup.ALLOW_DETACH);
BoundingBox bounds = new BoundingBox(node.getBounds());

Point3d upper = new Point3d();
Point3d lower = new Point3d();
Point3d c = new Point3d();
bounds.getUpper(upper);
bounds.getLower(lower);

Vector3f size = new Vector3f();
size.x = (float)(upper.x - lower.x);
size.y = (float)(upper.y - lower.y);
size.z = (float)(upper.z - lower.z);
c.x = lower.x + size.x/2f;
c.y = lower.y + size.y/2f;
c.z = lower.z + size.z/2f;

Primitive primitive = new Box(size.x/2f, size.y/2f, size.z/2f, app);

Transform3D trans = new Transform3D();
trans.set(new Vector3f((float)c.x, (float)c.y, (float)c.z));
TransformGroup tg = new TransformGroup(trans);
bb.addChild(tg);
tg.addChild(primitive);

return bb;
}

public SimplePick(BranchGroup targetBG, Canvas3D canvas, Vector elem){
root = new BranchGroup();
root.setCapability(BranchGroup.ALLOW_DETACH);
root.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
root.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
root.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);

wCond = new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED);
pickCanvas = new PickCanvas(canvas, targetBG);
pickCanvas.setTolerance(6.0f);
pickCanvas.setMode(PickCanvas.BOUNDS);
f = elem;
fsel = 0;
}

public void initialize(){
//condicion inicial
this.setSchedulingBounds(new BoundingSphere(new Point3d(), 1000));
this.wakeupOn(wCond);
}

public void processStimulus(Enumeration criteria){
PickResult pickResult;
MouseEvent event=(MouseEvent)((WakeupOnAWTEvent) criteria.nextElement()).getAWTEvent()[0];

int x = event.getX();
int y = event.getY();
pickCanvas.setShapeLocation(x, y);

if (pickCanvas.pickClosest() != null){ //si hay algo cerca
pickResult = pickCanvas.pickClosest();
System.out.println("--> PICK");
Node po = pickResult.getNode(PickResult.BRANCH_GROUP);
System.out.println("objeto = " + po.getUserData());
String name = (String)po.getUserData();
Integer n = new Integer(name);
((Fragmento)(f.elementAt(n.intValue()))).seleccionarFrag();
((Fragmento)(f.elementAt(fsel))).desSeleccionarFrag();
root.addChild(showBoundingBox(po));
}

else{
((Fragmento)(f.elementAt(fsel))).desSeleccionarFrag();
//borro el boundingbox
}

this.wakeupOn(wCond);
}
}

but it is the same, it returns me the next Exception:

Exception occurred during Behavior execution:

javax.media.j3d.RestrictedAccessException: Cannot modify capability bits on a live or compiled object

at javax.media.j3d.SceneGraphObject.setCapability(SceneGraphObject.java:101)

at magicpotter0.VerJuego$SimplePick.showBoundingBox(VerJuego.java:150)

at magicpotter0.VerJuego$SimplePick.processStimulus(VerJuego.java:226)

at javax.media.j3d.BehaviorScheduler.doWork(BehaviorScheduler.java:172)

at javax.media.j3d.J3dThread.run(J3dThread.java:250)