Faire une surface dans l'espace 3D avec Java3D

vinvay Messages postés 36 Date d'inscription mardi 7 mai 2002 Statut Membre Dernière intervention 31 janvier 2007 - 13 oct. 2004 à 13:59
cs_olaaa Messages postés 48 Date d'inscription jeudi 20 janvier 2005 Statut Membre Dernière intervention 11 mai 2005 - 20 janv. 2005 à 01:47
Je souhaite faire une surface plane dans l'espace 3D avec Java3D.
Pour cela, j'ai écrit une classe ("MyShape3D") qui contient une fonction grâce à laquelle on peut ajouter des points 3D ("addPoint"). Cette classe possède aussi une méthode ("getShape") qui renvoie un Shape3D fait à partir de tous les points que l'on a inséré avec addPoint.
Je souhaite pouvoir appliquer une couleur ou une texture a cette surface.
Lorsque j'utilise ma classe, je n'obtiens rien sur ma fenêtre graphique. D'où vient le problème ??
Autre question, lorsque j'applique une couleur de fond au canvas3D ("setBackground"), lors de l'ouverture de la fenêtre qui le contient ou de son redimensionnement, cette couleur apparait bien puis le Cannvas3D devient noir. Pourquoi ??

Code de MyShape3D ainsi que de la fenetre qui l'utilise :

// Essai3DFrame.java

import java.awt.GraphicsConfiguration;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.swing.JFrame;

import com.sun.j3d.utils.universe.SimpleUniverse;

class Essai3DFrame extends JFrame
{
private SimpleUniverse u;
private Canvas3D canvas3D;
private GraphicsConfiguration config = null;

Essai3DFrame()
{
this.setSize(640, 480);
this.setTitle("Essai3DFrame");
config = SimpleUniverse.getPreferredConfiguration();
canvas3D = new Canvas3D(config);
u = new SimpleUniverse(canvas3D);
u.addBranchGraph(createBranchGroup());
this.getContentPane().add("Center", canvas3D);
this.setVisible(true);
activateEvents();
}

private BranchGroup createBranchGroup()
{
BranchGroup bg = new BranchGroup();

MyShape3D shape = new MyShape3D();
shape.addPoint(0, 0, 0);
shape.addPoint(0, 10, 0);
shape.addPoint(10, 10, 0);
shape.addPoint(10, 0, 0);

bg.addChild(shape.getShape());

return bg;
}

private void activateEvents()
{
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.out.println("Frame closed !!");
System.exit(0);
}
});
}

public static void main(String[] argv)
{
new Essai3DFrame();
}
}

// MyShape3D.java
import javax.media.j3d.*;
import javax.vecmath.*;
import java.util.*;
import com.sun.j3d.*;

public class MyShape3D
{
private ArrayList coordList = new ArrayList();

public MyShape3D()
{
}

public void addPoint(long x, long y, long z)
{
Point3d p = new Point3d(x, y, z);
addPoint(p);
}

public void addPoint(Point3d p)
{
coordList.add(p);
}

Shape3D getShape()
{
int nbPoints = coordList.size();
if(nbPoints < 3)
return null;

Shape3D shape = new Shape3D();

GeometryArray ga = new PointArray(nbPoints, PointArray.COORDINATES | PointArray.COLOR_3);
Color3f white = new Color3f(1.0f, 1.0f, 1.0f);

for(int i = 0; i < nbPoints; i++)
{
ga.setCoordinate(i, (Point3d)coordList.get(i));
ga.setColor(i, white);
}

NormalGenerator ng = new NormalGenerator();

shape.addGeometry(ga);
shape.setAppearance(createAppearance());

return shape;
}

private Appearance createAppearance()
{
Appearance a = new Appearance();
Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
Color3f white = new Color3f(1.0f, 1.0f, 1.0f);

try
{
a.setMaterial(new Material(white, black, white, black, 1.0f));
}
catch(Exception e)
{
}

return a;
}
}

1 réponse

cs_olaaa Messages postés 48 Date d'inscription jeudi 20 janvier 2005 Statut Membre Dernière intervention 11 mai 2005
20 janv. 2005 à 01:47
Déja, pour changer le fond d'une scene3D, c pas avec la methode setBackground de Canvas3D (ca ca change juste le fond du canvas3D, pas deta scene, le canvas3D c juste le conteneur de ta scene.)
Pour changer le donc de ta scene, tu doi utiliser la classe Background, et ajouter une foi crée ton background a ton BG principal.
Exemple: (la je met un background a l'aide d'un fichier jpg)

TextureLoader text = new TextureLoader ("espace.JPG", this);
Background fondEtoile = new Background (text.getScaledImage (400, 400));
fondEtoile.setImageScaleMode(Background.SCALE_REPEAT); // Mozaique
fondEtoile.setApplicationBounds (new BoundingSphere(new Point3d(),70));//zone d'influence

tonBranchGroup.addChild(fondEtoile);

Pour changer uniquement la couleur du fond c plus simple encore...

Pour ton pb,de texture je voi pas ms ya p^lein de tuto qui peuvent t'aider:
http://rvirtual.free.fr/programmation/java3d/chap4/ch4.htm par exemple
0
Rejoignez-nous