IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Graphisme Java Discussion :

Java3D mipmap level problem.


Sujet :

Graphisme Java

  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Décembre 2009
    Messages
    265
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2009
    Messages : 265
    Par défaut Java3D mipmap level problem.
    Bonjour,

    Je débute dans le monde 3D et j'ai essayé de m'inspirer de quelques exemples trouvés dans différents forums pour faire tourner un Cube, chaque face ayant des photos différentes (je n'ai trouvé que des exemples avec différentes couleurs sur chaque face).
    Lorsque je lance mon programme (sous Eclipse pour l'instant), j'obtiens le message : Texture: mipmap image not set at level0

    Je dois avouer que malgré mes recherche je n'ai pas vraiment trouvé d'explications simples sur le rapport entre la définition de TextureCubeMap et la façon d'ajouter des images.
    Merci de me guider vers la bonne solution si possible. Mon code est ci-dessous.

    gégé

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
     
    package test;
     
    import java.awt.Graphics2D;
    import javax.imageio.ImageIO;
    import javax.media.j3d.Alpha;
    import javax.media.j3d.Appearance;
    import javax.media.j3d.BoundingSphere;
    import javax.media.j3d.BranchGroup;
    import javax.media.j3d.Canvas3D;
    import javax.media.j3d.ImageComponent2D;
    import javax.media.j3d.RotationInterpolator;
    import javax.media.j3d.TextureAttributes;
    import javax.media.j3d.TextureCubeMap;
    import javax.media.j3d.Transform3D;
    import javax.media.j3d.TransformGroup;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.vecmath.Point3d;
    import com.sun.j3d.utils.geometry.Box;
    import com.sun.j3d.utils.universe.SimpleUniverse;
    import java.awt.BorderLayout;
    import java.awt.image.BufferedImage;
    import java.io.File;
     
    public class Cube extends JFrame 
    {
    	private static final long serialVersionUID = 1L;
    	private static final int IMG_WIDTH = 128;
    	private static final int IMG_HEIGHT = 128;
    	private  ImageComponent2D[] myImages  = new ImageComponent2D[6] ;
    	private SimpleUniverse universe = null;
    	private BranchGroup root = null;
     
    	public Cube() 
    	{
    		process() ;
    	}
    	private void  process() 
    	{
    		créerImages();
    		getContentPane().setLayout(null);
    		setSize(300,300 );
    		JPanel canvasPanel = new JPanel();
    		canvasPanel.setLayout(new BorderLayout());
    		canvasPanel.setBounds(10, 11, 256, 256);
    		Canvas3D canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
    		universe = new SimpleUniverse(canvas);
    		universe.getViewingPlatform().setNominalViewingTransform();
    		root = new BranchGroup();
    		root = createSceneGraph();
    		universe.addBranchGraph(root);
     
    		canvasPanel.add(canvas);
    		getContentPane().add(canvasPanel);
    		this.setLocationRelativeTo(null);
    		setVisible(true);
    	}
     
    	 private  void créerImages()
    	 {
    		 File img = null;
    		 ImageComponent2D[] myImages  = new ImageComponent2D[6] ;
    		 BufferedImage buffImg = null, newImg = null;
    	     String[] photos = {"Steven.jpg", "Steven Paul.jpg","Steven.jpg", "Steven Paul.jpg", "Steven.jpg", "Steven Paul.jpg"};
    	     String  userdir  = 	System.getProperties().getProperty("user.dir") ;
    	     try 
    		 {
    	    	 for (int i=0; i<myImages.length; i++)
    			  {	  
    				  img = new File(userdir + File.separator + "Images" + File.separator + photos[i] );
    				  buffImg = ImageIO.read(img );
    				  int type = buffImg.getType() == 0? BufferedImage.TYPE_INT_ARGB : buffImg.getType();
    				  newImg  = resizeImage(buffImg, type);
    				  myImages[i] = new ImageComponent2D(ImageComponent2D.FORMAT_RGB,newImg );
    			  }
    		 } 
    		 catch (Exception ex) 
    		 {
    		        System.out.println(ex.getMessage());
    		        System.exit(1);
    		}
    	 }
    	public BranchGroup createSceneGraph() 
    	  {
    		 try 
    		   {
    		     int mipmaplevel = 0;
    			 TransformGroup objTrans = new TransformGroup();
    			 objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    			 root.addChild(objTrans);
     
    			 Appearance app = new Appearance();
    			 TextureCubeMap tex = new TextureCubeMap(TextureCubeMap.MULTI_LEVEL_MIPMAP,TextureCubeMap.RGB, IMG_WIDTH);
    			 tex.setImage(mipmaplevel, TextureCubeMap.POSITIVE_X,   myImages[0]);
    			 tex.setImage(mipmaplevel, TextureCubeMap.NEGATIVE_X,   myImages[1]);
    			 tex.setImage(mipmaplevel, TextureCubeMap.POSITIVE_Y,   myImages[2]);
    			 tex.setImage(mipmaplevel, TextureCubeMap.NEGATIVE_Y,   myImages[3]);
    			 tex.setImage(mipmaplevel, TextureCubeMap.POSITIVE_Z,   myImages[4]);
    			 tex.setImage(mipmaplevel, TextureCubeMap.NEGATIVE_Z,   myImages[5]);
    		     app.setTexture(tex);
    		    TextureAttributes texAttr = new TextureAttributes();
    		   texAttr.setTextureMode(TextureAttributes.MODULATE);
    		    app.setTextureAttributes(texAttr);
     
    		    Box textureCube = new Box(0.4f, 0.4f, 0.4f,   Box.GENERATE_TEXTURE_COORDS, app);
    		    objTrans.addChild(textureCube);
     
     
    		    Transform3D yAxis = new Transform3D();
    		    Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0);
     
    		    RotationInterpolator rotator = new RotationInterpolator(rotationAlpha,  objTrans, yAxis, 0.0f, (float) Math.PI * 2.0f);
    		    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
    		    rotator.setSchedulingBounds(bounds);
    		    objTrans.addChild(rotator);
     
    		    // Je compile ma scene. 
    		    	root.compile();
     
    	    	return root;
    	   } 
    	   catch (Exception ex) 
    	   	{
    		   	System.out.println(ex.toString()); System.exit(1);} 
    	   		return null;
    		}
     
    	 private static BufferedImage resizeImage(BufferedImage originalImage, int type)
    	  {
    			BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
    			Graphics2D g = resizedImage.createGraphics();
    			g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
    			g.dispose();
    			return resizedImage;
    	  }
    	 public static void main(String[] args) 
    	 {
    		 new Cube();
    	 }
    }

  2. #2
    Membre éclairé
    Profil pro
    Inscrit en
    Décembre 2009
    Messages
    265
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2009
    Messages : 265
    Par défaut
    Vu le manque de réponse, je suppose que personne n'a été confronté à ce problème. J'ai donc continué mes recherches, et après 2 jours de suées (il faisait 38° à Paris) j'ai enfin réussi à créer mon cube de photos.

    Au cas ou d'autres auraient le même besoin, je me permets de joindre ci-dessous mes classes exemples.

    1) La classe d'appel : Cube.java
    Il suffit de changer le nom des photos. J'ai supposé dans mon exemple que les photos résident dans le répertoire "Images".

    2) La création du cube proprement dit : CubeDePhotos
    Il s'agit d'une exteneson d'un JPanel.

    Bonne chance

    Gégé

    Cube.java
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    package exemples;
     
    import java.util.Vector;
    import javax.swing.JFrame;
     
    public class Cube extends JFrame 
    {
    	private static final long serialVersionUID = 1L;
    	Vector photos = new Vector();
     
    	public Cube()  
    	{
    		getContentPane().setLayout(null);
    		setSize(300,300 );
    		photos.add ("Images/Steven.jpg");
    		photos.add ("Images/Steven Paul.jpg");
    		photos.add ("Images/Alfred Hitchcock.jpg");
    		CubeDePhotos monCube = new CubeDePhotos();      // I get the same picture
     
    		monCube.setLocation(11,11);
    		monCube.process(photos);
    		getContentPane().add(monCube);
    		this.setLocationRelativeTo(null);
    		setVisible(true);
    	}
     
    	public static void main(String[] args) 
    	{
    	  new Cube(); 
    	}
    }
    CubeDePhotos
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
     
    package exemples;
     
    import javax.swing.JPanel;
    import javax.media.j3d.Alpha;
    import javax.media.j3d.Appearance;
    import javax.media.j3d.BoundingSphere;
    import javax.media.j3d.BranchGroup;
    import javax.media.j3d.Canvas3D;
    import javax.media.j3d.RotationInterpolator;
    import javax.media.j3d.Shape3D;
    import javax.media.j3d.Texture;
    import javax.media.j3d.TextureAttributes;
    import javax.media.j3d.TextureCubeMap;
    import javax.media.j3d.Transform3D;
    import javax.media.j3d.TransformGroup;
    import javax.vecmath.Point3d;
     
    import com.sun.j3d.utils.geometry.Box;
    import com.sun.j3d.utils.image.TextureLoader;
    import com.sun.j3d.utils.universe.SimpleUniverse;
     
    import java.awt.BorderLayout;
    import java.net.URL;
    import java.util.Vector;
    public class CubeDePhotos  extends JPanel 
    {
    	private static final long serialVersionUID = 1L;
    	private static final int IMG_SIZE = 128;
    	private SimpleUniverse universe = null;
    	private BranchGroup root = null;
    	private String[] photos = new String[6];
     
    	public  CubeDePhotos() 
    	{
    		super();
    		setLayout(new BorderLayout());
    		this.setBackground(java.awt.Color.red);
    		setSize(256, 256);
    	}	
     
    	public void process(String photo)  // unique photo. 
    	{
    		Vector v = new Vector<String>();
    		v.add(photo);
    	}
     
    	public void process(Vector<String> v) 
    	{
    		// fill-up the photo array from vector. If vector size  is less than 6 (number of cube faces) then iterate. 
    		int le = v.size(); // nombre d'entrées dans le vecteur
    		int j=0;
    		for (int i=0; i<6; i++)
    		{
    			photos[i] = v.elementAt(j++);
    			if (j==le) j=0;  // start from  the vector first entry. 
    		}
     
    		Canvas3D canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
    		universe = new SimpleUniverse(canvas);
    		universe.getViewingPlatform().setNominalViewingTransform();
    		root = new BranchGroup();
    		root = createSceneGraph();
    		universe.addBranchGraph(root);
    		 add(canvas);
    	}
     
    	 public BranchGroup createSceneGraph() 
    	  {
    		 try 
    		   {
    			 TransformGroup objTrans = new TransformGroup();
    			 objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    			 root.addChild(objTrans);
     
    			 TextureCubeMap tex = new TextureCubeMap(Texture.BASE_LEVEL,Texture.RGB, IMG_SIZE);
    		 	 tex.setCapability(Texture.ALLOW_ENABLE_WRITE);
    		 	 Box textureCube = new Box(0.4f, 0.4f, 0.4f, Box.GENERATE_TEXTURE_COORDS,  new Appearance ());
    		 	((Shape3D) textureCube.getChild(TextureCubeMap.POSITIVE_X)).setAppearance(getAppearance(photos[0])); 
    		 	((Shape3D) textureCube.getChild(TextureCubeMap.NEGATIVE_X)).setAppearance(getAppearance(photos[1])); 
    		 	((Shape3D) textureCube.getChild(TextureCubeMap.NEGATIVE_Y)).setAppearance(getAppearance(photos[2])); 
    		 	((Shape3D) textureCube.getChild(TextureCubeMap.POSITIVE_Y)).setAppearance(getAppearance(photos[3])); 
    		 	((Shape3D) textureCube.getChild(TextureCubeMap.POSITIVE_Z)).setAppearance(getAppearance(photos[4])); 
    		 	((Shape3D) textureCube.getChild(TextureCubeMap.NEGATIVE_Z)).setAppearance(getAppearance(photos[5]));   
     
    		     objTrans.addChild(textureCube);
     
    		    Transform3D yAxis = new Transform3D();
    		    Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0);
     
    		    RotationInterpolator rotator = new RotationInterpolator(rotationAlpha,  objTrans, yAxis, 0.0f, (float) Math.PI * 2.0f);
    		    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
    		    rotator.setSchedulingBounds(bounds);
    		    objTrans.addChild(rotator);
     
    		    // Je compile ma scene. 
    		    	root.compile();
     
    	    	return root;
    	   } 
    	   catch (Exception ex) 
    	   	{
    		   	System.out.println(ex.toString()); System.exit(1);} 
    	   		return null;
    		}
     
    	 private Appearance getAppearance(String f) throws Exception {
    		    Appearance app = new Appearance();
    		    URL texImage = new java.net.URL("file:" + f);
    		    Texture tex = new TextureLoader(texImage, this).getTexture();
    		    app.setTexture(tex);
    		    TextureAttributes texAttr = new TextureAttributes();
    		    texAttr.setTextureMode(TextureAttributes.MODULATE);
    		    app.setTextureAttributes(texAttr);
    		    return app;
    	 }		    
    }

Discussions similaires

  1. Réponses: 2
    Dernier message: 23/03/2009, 15h58
  2. Réponses: 1
    Dernier message: 04/09/2008, 21h22
  3. [Java3D] probleme affichage
    Par ccedced dans le forum 3D
    Réponses: 3
    Dernier message: 14/05/2007, 17h43
  4. [Java3D] Probleme avec les collisions
    Par akito dans le forum 3D
    Réponses: 1
    Dernier message: 17/04/2007, 19h41
  5. problémes avec java3d
    Par farah dans le forum 3D
    Réponses: 4
    Dernier message: 20/02/2006, 15h38

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo