/**********************************************************
Copyright (C) 2001 Daniel Selman
First distributed with the book "Java 3D Programming"
by Daniel Selman and published by Manning Publications.
http://manning.com/selman
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, version 2.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
The license can be found on the WWW at:
http://www.fsf.org/copyleft/gpl.html
Or by writing to:
Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
If you make changes you think others would like, please
contact one of the authors or someone at the
www.j3d.org web site.
**************************************************************/
/**
* This example loads a VRML file, automatically computes the view point to view
* the objects in the file, and then mouse picks. For each pick all the selected
* components of the scene are reported (by their VRML name). The VRML scene can
* be rotates, scaled and translated using the mouse.
*/ public class VrmlPickingTest extends Java3dApplet implements MouseListener {
PickCanvas pickCanvas = null;
public VrmlPickingTest() {
}
public VrmlPickingTest(String[] args) {
saveCommandLineArguments(args);
initJava3d();
}
public void start() { if (pickCanvas == null)
initJava3d();
}
// point the view at the center of the object
Point3d center = new Point3d();
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.4 * radius / Math.tan(Math.toRadians(40) / 2.0);
temp.x = 0.0;
temp.y = 0.0;
temp.z = eyeDist;
eyeTrans.set(temp);
viewTrans.mul(eyeTrans);
// set the view transform
tgArray[0].setTransform(viewTrans);
Hashtable namedObjects = scene.getNamedObjects();
System.out.println("*** Named Objects in VRML file: \n"
+ namedObjects);
// recursively set the user data here
// so we can find our objects when they are picked
java.util.Enumeration enumValues = namedObjects.elements();
java.util.Enumeration enumKeys = namedObjects.keys();
if (enumValues != null) { while (enumValues.hasMoreElements() != false) {
Object value = enumValues.nextElement();
Object key = enumKeys.nextElement();
recursiveSetUserData(value, key);
}
}
}
return sceneGroup;
}
// method to recursively set the user data for objects in the scenegraph
// tree
// we also set the capabilites on Shape3D and Morph objects required by the
// PickTool void recursiveSetUserData(Object value, Object key) { if (value instanceof SceneGraphObject != false) {
// set the user data for the item
SceneGraphObject sg = (SceneGraphObject) value;
sg.setUserData(key);
// recursively process group if (sg instanceof Group) {
Group g = (Group) sg;
// recurse on child nodes
java.util.Enumeration enumKids = g.getAllChildren();
VrmlPickingTest pickingTest = new VrmlPickingTest(args);
new MainFrame(pickingTest, 400, 400);
}
}
/*******************************************************************************
* Copyright (C) 2001 Daniel Selman
*
* First distributed with the book "Java 3D Programming" by Daniel Selman and
* published by Manning Publications. http://manning.com/selman
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, version 2.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* The license can be found on the WWW at: http://www.fsf.org/copyleft/gpl.html
*
* Or by writing to: Free Software Foundation, Inc., 59 Temple Place - Suite
* 330, Boston, MA 02111-1307, USA.
*
* Authors can be contacted at: Daniel Selman: [email protected]
*
* If you make changes you think others would like, please contact one of the
* authors or someone at the www.j3d.org web site.
******************************************************************************/
//*****************************************************************************
/**
* Java3dApplet
*
* Base class for defining a Java 3D applet. Contains some useful methods for
* defining views and scenegraphs etc.
*
* @author Daniel Selman
* @version 1.0
*/
//*****************************************************************************
abstract class Java3dApplet extends Applet { public static int m_kWidth = 300;
public TransformGroup[] getViewTransformGroupArray() {
TransformGroup[] tgArray = new TransformGroup[1];
tgArray[0] = new TransformGroup();
// move the camera BACK a little...
// note that we have to invert the matrix as
// we are moving the viewer
Transform3D t3d = new Transform3D();
t3d.setScale(getScale());
t3d.setTranslation(new Vector3d(0.0, 0.0, -20.0));
t3d.invert();
tgArray[0].setTransform(t3d);