[Java 3D] Changer l'apparance d'objets WaveFront
Tout d'abord, bonjour tout le monde !
Alors voilà, mon problème est que je charge plusieurs objets wavefront (*.obj) dans un canvas3d, mais qu'ensuite je voudrais changer leur apparance comme la couleur ou passer en "fil de fer". Ceci marche lorsque je fais appel à une procédure "traverse" (qui me sert à énumérer tous les objets contenus dans ma scène) directement quand je les charge ; mais je voudrais changer l'apparence après avoir chargé mes objets en cliquant sur un bouton par exemple. Mais çà ne marche pas, j'ai toujours une erreur "Cannot modify capability bits on a live or compiled object", alors que j'ai mis des setCapability et sur l'apparance et sur mes Shape3D... :cry:
Voici le code associé :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| /**
* Chargement de l'objet Wavefront (014_110.obj) ainsi que les materiaux qui
* lui sont associes
* @return BranchGroup branch group contenant l'objet Wavefront
*/
private BranchGroup loadWavefrontObject(String filename) {
ObjectFile waveFrontObject = new ObjectFile(ObjectFile.RESIZE | ObjectFile.STRIPIFY | ObjectFile.TRIANGULATE);
int error = 0;
sceneNum++;
System.out.println("sceneNum = " + sceneNum);
Scene scene = null;
try {
if (!applet) {
scene = waveFrontObject.load(filename);
}
else {
URL url = this.getCodeBase();
String path = url.toString() + filename;
try {
URL fileUrl = new URL(path);
scene = waveFrontObject.load(fileUrl);
}
catch (MalformedURLException ex2) {
System.out.println("Impossible de charger le fichier : " + filename);
error = -1;
}
} // fin else
} // fin try
catch (ParsingErrorException ex) {
System.out.println("Fichier defectueux");
error = -1;
}
catch (IncorrectFormatException ex) {
System.out.println("Mauvais format de fichier");
error = -1;
}
catch (FileNotFoundException ex) {
System.out.println("Fichier " + filename + " non trouve");
error = -1;
}
if (error != 0)
System.exit(error);
//Construct and return branch group containing our OBJ scene.
BranchGroup contentBranchGroup = new BranchGroup();
contentBranchGroup.setCapability(BranchGroup.ALLOW_DETACH);
contentBranchGroup.addChild(scene.getSceneGroup());
en = contentBranchGroup.getAllChildren();
traverse(en);
return(contentBranchGroup);
} |
Et voici le code de la fonction "traverse" :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
public void traverse (Enumeration e) {
int i = 0;
while(e.hasMoreElements()){
Object o = e.nextElement();
if (o instanceof Shape3D){
i++;
System.out.println("Enum" + i);
Shape3D shape = (Shape3D) o;
/*PolygonAttributes pa = new PolygonAttributes();
pa.setPolygonMode(PolygonAttributes.POLYGON_LINE);
pa.setCullFace(PolygonAttributes.CULL_NONE);
app.setPolygonAttributes(pa);*/
System.out.println("Appearance");
shape.setCapability(Shape3D.ALLOW_APPEARANCE_OVERRIDE_WRITE);
shape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
shape.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
shape.setCapability(Shape3D.ALLOW_APPEARANCE_OVERRIDE_READ);
Material material = new Material(new Color3f(0.0f, 0.0f, 1.0f),
new Color3f(0.0f, 0.0f, 0.0f),
new Color3f(0.5f, 0.5f, 0.5f),
new Color3f(1.0f, 1.0f, 1.0f),
64);
appObj.setMaterial(material);
System.out.println("Capability");
shape.setAppearance(appObj);
System.out.println("SetAppearance");
}
if (o instanceof Group) {
Group g = (Group) o;
this.traverse(g.getAllChildren());
}
} // end while
} // end traverse |
Et pour finir le bouton (jButton) par lequel j'essaye d'appeler la fonction "traverse" afin de modifier l'apparence de mes objets :
Code:
1 2 3 4 5 6
| private void processCB(ActionEvent e) {
if (e.getSource().equals(jButton)) {
System.out.println("Push");
traverse(en);
}
} |
Si quelqu'un avait une idée, ce serait vraiment sympa, parce que là je galère... :oops:
Merci d'avance !!! :D