Hi!
I've got some probs with my app:
I've implemented a Tool for showing a wireframe-model. The navigation is
made with the mousebehaviors, it's simply an extension of the
vrml-viewer. Two probs:
When I'm zooming or dragging in space, it's always in direction of the
global system. I would zoom in direction of my view, I've tried certain
methods, but without success.
Further I would have a second canvas with the coordinate system of the
first. The system should only rotate in the center of the universe, no
dragging or zooming. When I set back the viewpoint, the coordsystem
isn't fixed. Any solutions?
Thanx
Christoph
--
______________________________________________________
Christoph Helmert
TU Darmstadt, Germany
EPF Lausanne, Suisse
eMail: [EMAIL PROTECTED]
eMail: [EMAIL PROTECTED]
______________________________________________________
package com.helmert.feworld;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import java.util.Enumeration;
import javax.media.j3d.*;
import javax.vecmath.*;
import javax.swing.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import com.sun.j3d.utils.behaviors.picking.*;
import com.helmert.feworld.object.*;
import com.helmert.feworld.event.*;
public class FEViewFrame extends Frame implements
ActionListener,ShapeSelectionListener,PointSelectionListener{
private static final int SHAPE_TRANSLATION=1;
private static final int SHAPE_ROTATION=2;
private static final int REMOVE_SHAPE=3;
private static final int SHAPE_PROPERTY=4;
Canvas3D viewCanvas,coordinateViewCanvas;
SimpleUniverse u;
BranchGroup contentBranchGroup;
ViewPlatform thisViewPlatform;
View displayView,coordinateView;
ViewingPlatform ingPlatform;
TransformGroup viewTransformGroup;
private Vector selectionListeners,shapeSelectionListeners,shapeActionListeners;
Button translateButton,rotateButton,removeButton,propertyButton;
TextArea reportArea;
private int shapeAction;
private Vector selectedShapes,selectedPoints;
JDialog pointSelectionDialog;
JButton requestCancelButton;
public FEViewFrame(){
viewCanvas=new Canvas3D(null);
coordinateViewCanvas=new Canvas3D(null);
coordinateViewCanvas.setSize(new Dimension(50,50));
displayView=new View();
displayView.setBackClipDistance(1000);
displayView.addCanvas3D(viewCanvas);
coordinateView=new View();
Transform3D backTransform=new Transform3D();
backTransform.lookAt (new Point3d(0,0,5),new Point3d(0,0,-2),new Vector3d(0,0,0));
coordinateView.setCompatibilityModeEnable(true);
coordinateView.setVpcToEc(backTransform);
coordinateView.setFieldOfView(10);
coordinateView.addCanvas3D(coordinateViewCanvas);
displayView.setPhysicalBody(new PhysicalBody());
displayView.setPhysicalEnvironment(new PhysicalEnvironment());
coordinateView.setPhysicalBody(new PhysicalBody());
coordinateView.setPhysicalEnvironment(new PhysicalEnvironment());
VirtualUniverse viewUniverse = new VirtualUniverse();
VirtualUniverse coordinateUniverse = new VirtualUniverse();
Locale viewLocale = new Locale(viewUniverse);
Locale coordinateLocale = new Locale(coordinateUniverse);
BranchGroup viewBranchGroup = new BranchGroup();
viewTransformGroup = new TransformGroup();
viewTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
viewTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
BranchGroup coordinateBranchGroup = new BranchGroup();
coordinateBranchGroup.setCapability(Group.ALLOW_CHILDREN_EXTEND);
TransformGroup coordinateTransformGroup = new TransformGroup();
coordinateTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
coordinateTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
MouseRotate rotationBehavior=new MouseRotate(viewTransformGroup);
viewTransformGroup.addChild(rotationBehavior);
MouseRotate coordinateRotationBehavior=new MouseRotate(coordinateTransformGroup);
viewTransformGroup.addChild(coordinateRotationBehavior);
coordinateRotationBehavior.setSchedulingBounds(bounds);
rotationBehavior.setSchedulingBounds(bounds);
MouseTranslate translationBehavior=new MouseTranslate(viewTransformGroup);
viewTransformGroup.addChild(translationBehavior);
translationBehavior.setSchedulingBounds(bounds);
MouseZoom zoomingBehavior=new MouseZoom(viewTransformGroup);
viewTransformGroup.addChild(zoomingBehavior);
zoomingBehavior.setSchedulingBounds(bounds);
viewTransformGroup.setBounds(new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0));
thisViewPlatform = new ViewPlatform();
viewTransformGroup.addChild(thisViewPlatform);
viewBranchGroup.addChild(viewTransformGroup);
viewLocale.addBranchGraph(viewBranchGroup);
displayView.attachViewPlatform(thisViewPlatform);
ingPlatform=new ViewingPlatform();
ingPlatform.setViewPlatform(thisViewPlatform);
ingPlatform.setNominalViewingTransform();
ViewPlatform coordinateViewPlatform=new ViewPlatform();
coordinateTransformGroup.addChild(coordinateViewPlatform);
LineArray coordArray=new LineArray(6,LineArray.COORDINATES);
coordArray.setCoordinate(0,new Point3d(0,0,0));
coordArray.setCoordinate(1,new Point3d(1,0,0));
coordArray.setCoordinate(2,new Point3d(0,0,0));
coordArray.setCoordinate(3,new Point3d(0,1,0));
coordArray.setCoordinate(4,new Point3d(0,0,0));
coordArray.setCoordinate(5,new Point3d(0,0,1));
Shape3D coordShape=new Shape3D(coordArray);
coordinateBranchGroup.addChild(coordShape);
coordinateBranchGroup.addChild(coordinateTransformGroup);
contentBranchGroup=new BranchGroup();
contentBranchGroup.setCapability(Group.ALLOW_CHILDREN_EXTEND);
contentBranchGroup.setCapability(Group.ALLOW_CHILDREN_READ);
contentBranchGroup.setCapability(Group.ALLOW_CHILDREN_WRITE);
contentBranchGroup.setCapability(Group.ALLOW_BOUNDS_READ);
contentBranchGroup.setCapability(Group.ALLOW_PICKABLE_READ);
contentBranchGroup.setCapability(Group.ALLOW_PICKABLE_WRITE);
contentBranchGroup.setCapability(Group.ENABLE_PICK_REPORTING);
AmbientLight light=new AmbientLight(new Color3f(0.5f,0.5f,0.5f));
PointLight pl=new PointLight(new Color3f(0.8f,0.8f,0.8f),
new Point3f(0.0f,0,0),
new Point3f(1.0f,0,0.0f));
light.setInfluencingBounds(new BoundingSphere(new Point3d(0,0,0),1000));
light.setEnable(true);
pl.setInfluencingBounds(new BoundingSphere(new Point3d(0,0,0),1000));
pl.setEnable(true);
contentBranchGroup.addChild(light);
contentBranchGroup.addChild(pl);
AmbientLight coordLight=new AmbientLight(new Color3f(0.5f,0.5f,0.5f));
coordLight.setInfluencingBounds(new BoundingSphere(new Point3d(0,0,0),1000));
coordLight.setEnable(true);
coordinateBranchGroup.addChild(coordLight);
coordinateLocale.addBranchGraph(coordinateBranchGroup);
coordinateView.attachViewPlatform(coordinateViewPlatform);
PickHighLightBehavior pickBeh = new PickHighLightBehavior(viewCanvas,
contentBranchGroup,
new BoundingSphere(new
Point3d(0.0,0.0,0.0), 100.0));
viewLocale.addBranchGraph(contentBranchGroup);
add(BorderLayout.CENTER,viewCanvas);
Panel interactionPanel=new Panel(new GridLayout(2,0));
Panel buttonPanel=new Panel(new FlowLayout());
translateButton=new Button("T");
translateButton.addActionListener(this);
buttonPanel.add(translateButton);
rotateButton=new Button("R");
rotateButton.addActionListener(this);
buttonPanel.add(rotateButton);
removeButton=new Button("X");
removeButton.addActionListener(this);
buttonPanel.add(removeButton);
propertyButton=new Button("P");
propertyButton.addActionListener(this);
buttonPanel.add(propertyButton);
buttonPanel.add(coordinateViewCanvas);
interactionPanel.add(buttonPanel);
reportArea=new TextArea("",3,80,TextArea.SCROLLBARS_BOTH);
interactionPanel.add(reportArea);
add(BorderLayout.SOUTH,interactionPanel);
setViewPoint();
selectionListeners=new Vector();
shapeSelectionListeners=new Vector();
shapeActionListeners=new Vector();
selectedPoints=new Vector();
selectedShapes=new Vector();
requestCancelButton=new JButton("Abbruch");
requestCancelButton.addActionListener(this);
addPointSelectionListener(this);
addShapeSelectionListener(this);
}
public void addGraphicalObject(BranchGroup group){
contentBranchGroup.addChild(group);
}
public void removeGraphicalObject(BranchGroup group){
Enumeration enum=contentBranchGroup.getAllChildren();
for(int i=0;enum.hasMoreElements();i++){
Node currentChild=(Node)enum.nextElement();
if (currentChild==group){
contentBranchGroup.removeChild(i);
}
}
}
public void actionPerformed(ActionEvent e){
if (e.getSource()==translateButton){
selectedShapes.removeAllElements();
selectedPoints.removeAllElements();
shapeAction=SHAPE_TRANSLATION;
reportArea.append("[TRANSLATION BEGIN]\n");
}
if (e.getSource()==rotateButton){
selectedShapes.removeAllElements();
selectedPoints.removeAllElements();
shapeAction=SHAPE_ROTATION;
reportArea.append("[ROTATION BEGIN]\n");
}
if (e.getSource()==removeButton){
selectedShapes.removeAllElements();
selectedPoints.removeAllElements();
shapeAction=REMOVE_SHAPE;
reportArea.append("[REMOVING BEGIN]\n");
}
if (e.getSource()==propertyButton){
selectedShapes.removeAllElements();
selectedPoints.removeAllElements();
shapeAction=SHAPE_PROPERTY;
reportArea.append("[PROPERTY BEGIN]\n");
}
if (e.getSource()==requestCancelButton){
pointSelectionDialog.removeAll();
pointSelectionDialog.dispose();
}
}
public void shapeSelected(ShapeSelectionEvent e){
selectedShapes.addElement(e.getSelectedShape());
reportArea.append("Shape selected [Total:"+selectedShapes.size()+"]\n");
doAction();
}
public void pointSelected(PointSelectionEvent e){
selectedPoints.addElement(e.getSelectedPoint());
reportArea.append("Point selected [Total:"+selectedPoints.size()+"]\n");
doAction();
}
public void doAction(){
if (shapeAction==SHAPE_TRANSLATION){
if ((selectedShapes.size()==1)&&
(selectedPoints.size()==2)){
fireShapeAction(ShapeActionEvent.SHAPE_TRANSLATION);
reportArea.append("[TRANSLATION DONE]\n");
shapeAction=0;
}
}
if (shapeAction==SHAPE_ROTATION){
if ((selectedShapes.size()==1)&&
(selectedPoints.size()==2)){
pointSelectionDialog=new JDialog(this);
pointSelectionDialog.setTitle("Winkel");
pointSelectionDialog.getContentPane().
add(BorderLayout.CENTER,new JLabel("Drei Punkte waehlen"));
pointSelectionDialog.getContentPane().
add(BorderLayout.SOUTH,requestCancelButton);
pointSelectionDialog.pack();
pointSelectionDialog.show();
}
if ((selectedShapes.size()==1)&&
(selectedPoints.size()==5)){
fireShapeAction(ShapeActionEvent.SHAPE_ROTATION);
pointSelectionDialog.removeAll();
pointSelectionDialog.dispose();
reportArea.append("[ROTATION DONE]\n");
shapeAction=0;
}
}
if (shapeAction==REMOVE_SHAPE){
if (selectedShapes.size()>0){
fireShapeAction(ShapeActionEvent.REMOVE_SHAPE);
reportArea.append("[REMOVING DONE]\n");
shapeAction=0;
}
}
if (shapeAction==SHAPE_PROPERTY){
if (selectedShapes.size()==1){
fireShapeAction(ShapeActionEvent.SHAPE_PROPERTY);
reportArea.append("[PROPERTY DONE]\n");
shapeAction=0;
}
}
}
public Vector getSelectedPoints(){
return selectedPoints;
}
public Vector getSelectedShapes(){
return selectedShapes;
}
public TransformGroup getViewGroup(){
return null;
}
public synchronized void setViewPoint() {
Transform3D viewTrans = new Transform3D();
Transform3D eyeTrans = new Transform3D();
// point the view at the center of the object
Point3d center = new Point3d();
BoundingSphere sceneBounds=(BoundingSphere)contentBranchGroup.getBounds();
sceneBounds.getCenter(center);
double radius = sceneBounds.getRadius();
Vector3d temp = new Vector3d(center);
viewTrans.set(temp);
// pull the eye back far enough to see the whole object
double eyeDist = 1.2*radius / Math.tan(displayView.getFieldOfView() / 2.0);
//System.out.println (eyeDist+" "+displayView.getFieldOfView());
temp.x = 0;
temp.y = 0;
temp.z = eyeDist;
eyeTrans.set(temp);
viewTrans.mul(eyeTrans);
// set the view transform
viewTransformGroup.setTransform(viewTrans);
}
public synchronized void addPointSelectionListener(PointSelectionListener l){
if (selectionListeners.contains(l)){
return;
}
selectionListeners.addElement(l);
}
public synchronized void removePointSelectionListener (PointSelectionListener l){
selectionListeners.removeElement(l);
}
public synchronized void addShapeSelectionListener(ShapeSelectionListener l){
if (shapeSelectionListeners.contains(l)){
return;
}
shapeSelectionListeners.addElement(l);
}
public synchronized void removeShapeSelectionListener (ShapeSelectionListener l){
shapeSelectionListeners.removeElement(l);
}
public synchronized void addShapeActionListener(ShapeActionListener l){
if (shapeActionListeners.contains(l)){
return;
}
shapeActionListeners.addElement(l);
}
public synchronized void removeShapeActionListener (ShapeActionListener l){
shapeActionListeners.removeElement(l);
}
private void firePointSelected(Point3d point){
Vector tl;
synchronized (this) {
tl=(Vector)selectionListeners.clone();
}
int size = tl.size();
if (size == 0) {
return;
}
PointSelectionEvent event = new PointSelectionEvent(this,point);
for (int i = 0; i < size; ++i) {
PointSelectionListener listener = (PointSelectionListener)
tl.elementAt(i);
listener.pointSelected(event);
}
}
private void fireShapeSelected(Shape3D shape){
Vector tl;
synchronized (this) {
tl=(Vector)shapeSelectionListeners.clone();
}
int size = tl.size();
if (size == 0) {
return;
}
ShapeSelectionEvent event = new ShapeSelectionEvent(this,shape);
for (int i = 0; i < size; ++i) {
ShapeSelectionListener listener = (ShapeSelectionListener)
tl.elementAt(i);
listener.shapeSelected(event);
}
}
private void fireShapeAction(int actionCommand){
Vector tl;
synchronized (this) {
tl=(Vector)shapeActionListeners.clone();
}
int size = tl.size();
if (size == 0) {
return;
}
ShapeActionEvent event = new ShapeActionEvent(this,actionCommand);
for (int i = 0; i < size; ++i) {
ShapeActionListener listener = (ShapeActionListener)
tl.elementAt(i);
listener.shapeActionPerformed(event);
}
}
private class PickHighLightBehavior extends PickMouseBehavior {
public PickHighLightBehavior(Canvas3D canvas, BranchGroup root,
Bounds bounds) {
super(canvas, root, bounds);
this.setSchedulingBounds(bounds);
root.addChild(this);
}
public void updateScene(int xpos, int ypos) {
Shape3D pointShape,shapeShape;
pointShape = (Shape3D) pickScene.pickNode(pickScene.
pickClosest(xpos, ypos,
PickObject.
USE_GEOMETRY),
PickObject.SHAPE3D);
shapeShape = (Shape3D) pickScene.pickNode(pickScene.
pickClosest(xpos, ypos,
PickObject.USE_BOUNDS),
PickObject.SHAPE3D);
//System.out.println (pointShape.getClass().getName());
if (pointShape!=null){
if (pointShape.
getClass().getName().
compareTo("com.helmert.feworld.object.FEViewPoint")==0){
firePointSelected(((FEViewPoint)pointShape).getCenter());
}
}
else if (shapeShape!=null){
fireShapeSelected(shapeShape);
}
}
}
}