I'm new in the 3D world, therefore I apology whether my questions are stupide.
I'm trying to make a cube to rotate with different pictures on its 6 faces. I looked at different forums to understand how things work: The only examples I have found was with different colors (not images).
Unfortunately I spent many hours without success.
When I start my class below, (I'm testing under Eclipse for the moment), I get the message : Texture: mipmap image not set at level0
After many tries and reading a lot of documents, I should admit I do not understand the link betweeen the way to define a TextureCubeMap occurence and the way to specify the level of added images.
Thanks in advance for any help, and sorry for my bad English.
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.Texture;
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(Texture.BASE_LEVEL,Texture.RGB, IMG_WIDTH);
tex.setCapability(Texture.ALLOW_ENABLE_WRITE);
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();
}
}