Hi,
I've been racking my brain for over a day on this and can't figure out
what I'm doing wrong or not doing.
I place a 3D Sphere into my scenegraph, and later I want to change the
color of it. But when I press the button to reset it's appearance to the
new color/material:
public void setSphereAppearance(Material newmat) {
planetApp.setMaterial(newmat);
mySphere.setAppearance(planetApp);
}
I get a error saying: Shape3D: no capability to set appearance
Although...it changes the color...wierd. I'm pretty sure I set all
the needed capability flags but maybe I'm missing something. I've copied
the code for my 3D sphere below...it's a BranchGroup so it can be
added to a live scenegraph.
I'd appreciate any suggestions..
Mario
CODE:
==================================================
// Import the 3D API libraries for viewing 3D environments.
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;
// Import the math library.
import java.lang.Math.*;
/**
*
*/
public class Sphere3d extends BranchGroup {
//
// Variable declarations
//
private double _scaleValue = 1.0f;
private Vector3f _transValue = new Vector3f(0.0f, 0.0f, 0.0f);
private Transform3D _scaleTran; // Scale transform
private Transform3D _transTran; // Translation transform
private Transform3D _oldTrans = new Transform3D();
private TransformGroup mainTG = new TransformGroup();
private boolean _solid = false;
private Color3f _color = new Color3f();
private Vector3f _base = new Vector3f();
private float _scale = 1.0f;
private float _dim1 = 0.0f;
private Appearance planetApp;
private Sphere mySphere;
Color3f black = new Color3f(0.20f, 0.20f, 0.20f);
/**
* Calls the 'defineVrml' function to define the VRML model
* by setting the initial scale, translation and rotation.
*
*/
public Sphere3d(boolean solid, Color3f color, Vector3f base, float scale,
float dim1){
this.addChild(mainTG);
this.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
System.out.println("\n Cannot simply define blank Sphere3d");
setVariables(solid, color, base, scale, dim1);
definePrimitive();
drawSphere(solid,dim1);
setPosition(base.x, base.y, base.z);
setScale(scale);
}
/**
*
*
*
*/
private void setVariables(boolean solid, Color3f color, Vector3f base,
float scale, float dim1) {
_solid = solid;
_color = color;
_base = base;
_scale = scale;
_dim1 = dim1;
}
public boolean getSolid() {
return _solid;
}
public Color3f getColor() {
return _color;
}
public Vector3f getBase() {
return _base;
}
public float getScaleVal() {
return _scale;
}
public float getDim1() {
return _dim1;
}
/**
*
*
*
*/
private void drawSphere(boolean solid, float radius) {
if (!solid) {
ColoringAttributes ca = new ColoringAttributes();
ca.setColor(_color);
PolygonAttributes pa = new PolygonAttributes();
pa.setPolygonMode (PolygonAttributes.POLYGON_LINE);
Appearance ap = new Appearance();
ap.setColoringAttributes (ca);
ap.setPolygonAttributes (pa);
mainTG.addChild(new Sphere(radius,ap));
} else if (solid) {
planetApp = new Appearance();
planetApp.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
planetApp.setCapability.Appearance.ALLOW_COLORING_ATTRIBUTES_WRITE);
TransparencyAttributes _transAttrs;
float _transparency = 0.0f;
float _curtransparency = 0.0f;
Transform3D _transTrans = new Transform3D();
_transAttrs = new TransparencyAttributes(TransparencyAttributes.NONE,
_transparency);
// Allow the transparency value to be overwritten.
_transAttrs.setCapability(TransparencyAttributes.ALLOW_VALUE_WRITE);
// Allow the transparency mode to be overwritten.
_transAttrs.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE);
planetApp.setTransparencyAttributes(_transAttrs);
Material m = new Material(_color,black,_color,black,64.0f);
planetApp.setMaterial(m);
// Draw the sphere for the planet and add the texture to it
mainTG.addChild(mySphere = new Sphere(radius,
Sphere.GENERATE_TEXTURE_COORDS |
Sphere.GENERATE_NORMALS,
8, planetApp));
mySphere.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
mySphere.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
mySphere.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
}
}
/**
* Changes the appearance
*
*/
public void setSphereAppearance(Material newmat) {
planetApp.setMaterial(newmat);
mySphere.setAppearance(planetApp);
}
/**
* Sets up the VRML model (does not specifically load one
* until needed, but sets up the necessary Transforms and
* BranchGroups)
*
*/
private void definePrimitive() {
// this will allow arm to be dynamic.
mainTG.setCapability( TransformGroup.ALLOW_TRANSFORM_READ );
mainTG.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
mainTG.setCapability( TransformGroup.ALLOW_CHILDREN_EXTEND );
mainTG.setCapability( TransformGroup.ALLOW_CHILDREN_READ );
mainTG.setCapability( TransformGroup.ALLOW_CHILDREN_WRITE );
this.setCapability( BranchGroup.ALLOW_CHILDREN_WRITE );
// Allow this primitive to be detached from wherever it is attached
this.setCapability( BranchGroup.ALLOW_DETACH );
//
// Multiply together the SCALING, TRANSLATION matrices
// to form one TRANSFORMATION matrix.
//
// newMatrix = [S][T]
//
// [S] = Scaling Matrix
// [T] = Translation Matrix
//
// SCALING transformation matrix
_scaleTran = new Transform3D();
_scaleTran.setScale(_scaleValue);
// TRANSLATION transformation matrix (combined with previous scaling
matrix)
_transTran = new Transform3D();
_transTran.setTranslation(_transValue);
// Set the final transformation for 'this' transform group to be the
composite
mainTG.setTransform(_transTran);
// UPDATE all of the transformations (multiply them together into one)
// ...then set the Transform3D of this TransformGroup
update();
}
/**
* Scales the size of the model in all directions.
* @param newscale scaling factor for the VRML object
*/
public void setScale(double newscale) {
_scaleValue = newscale;
_scaleTran.setIdentity();
_scaleTran.setScale(_scaleValue);
update();
}
/**
* Moves the model to any 3D position.
* @param x x position
* @param y y position
* @param z z position
*/
public void setPosition( double x, double y, double z ) {
_transValue.x = (float)x;
_transValue.y = (float)y;
_transValue.z = (float)z;
_transTran.setIdentity();
_transTran.setTranslation(_transValue);
update();
}
/**
* Sets the scaling, position and rotation transforms and
* set the combined Transform (this.setTransform) of 'this'
TransformGroup.
*
*/
private void setTransformations() {
// Multiply all the rotation, translation and scaling matrices together
_oldTrans.setIdentity();
// Multiply identity matrix by the TRANSLATION matrix
_oldTrans.mul(_transTran);
// Multiply the combined TRANSLATION-ROTATION matrix by the SCALE matrix
_oldTrans.mul(_scaleTran);
// set the TRANSFORM of this TransformGroup (TRANSLATION-ROTATION-SCALE)
mainTG.setTransform(_oldTrans);
}
/**
* Updates the transformations. This call is needed following any change
in the planet.
*
*
*/
public void update() {
setTransformations();
}
/**
* Returns scale value.
* @return double _scaleValue - value of the current scaling factor
*/
public double getScale() {
return _scaleValue;
}
/**
* Returns the current position as a vector.
* @return Vector3f _transValue - current position vector
*/
public Vector3f getPosition() {
return _transValue;
}
} // end of 'Sphere3d' class
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".