|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Junior Member
Iscritto dal: Feb 2009
Messaggi: 8
|
[JAVA3D] Problema con la Texture
Salve,
sapete dove ho sbagliato a fare la Texture? Riesco a vedere la texture solo su due faccie le altre sembrano solo striature... Codice:
Appearance appearance = new Appearance();
Texture texture = (new TextureLoader("C:/Mattoni.jpg",this)).getTexture();
appearance.setTexCoordGeneration(new TexCoordGeneration());
texture.setBoundaryModeS(Texture.WRAP);
texture.setBoundaryModeT(Texture.WRAP);
appearance.setTexture(texture);
Box box = new Box(xdim, ydim, zdim, Box.GENERATE_TEXTURE_COORDS, appearance);
Grazie Maurizio |
|
|
|
|
|
#2 |
|
Junior Member
Iscritto dal: Feb 2009
Messaggi: 8
|
domanda stroppo difficile?
|
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Nov 2004
Città: Tra Verona e Mantova
Messaggi: 4553
|
La tua texture è corretta, è il mapping di Box che crea quell'effetto "strisciata" (che è poi una proiezione parallela dell'immagine).
Per avere una visione "frontale" dell'immagine sulle sei facce di un Box dovresti applicare la texture alle sei facce separatamente. Scrivo un esempio completo ma il metodo pertinente è createTexturedBox: Codice:
package j3dcollisions;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.image.*;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.awt.image.*;
import javax.imageio.*;
import javax.media.j3d.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.vecmath.*;
public class TexturedBox {
public static void main(String[] args) {
String imageFilePath = "z:\\texture.png";
final BufferedImage texture = loadImageOrFlee(imageFilePath);
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
TexturedBox.setupAndShow(texture);
}
});
}
private static BufferedImage loadImageOrFlee(String imageFilePath) {
try {
return ImageIO.read(new java.io.File(imageFilePath));
} catch(IOException ex) {
ex.printStackTrace();
System.exit(0);
return null;
}
}
private static Frame createWindow() {
Frame window = new Frame("Java3D TexturedBox");
window.addWindowListener(new WindowAdapter() {
@Override public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
window.setSize(400, 400);
return window;
}
private static TransformGroup createMouseRotateGroup(Box texturedBox) {
TransformGroup boxTransform = new TransformGroup();
boxTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
boxTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
BoundingSphere infinity = new BoundingSphere(new Point3d(),
Double.POSITIVE_INFINITY);
MouseRotate mouseRotate = new MouseRotate();
mouseRotate.setTransformGroup(boxTransform);
mouseRotate.setSchedulingBounds(infinity);
boxTransform.addChild(mouseRotate);
boxTransform.addChild(texturedBox);
return boxTransform;
}
private static Appearance createTexturedAppearance(BufferedImage textureImage) {
/* da BufferedImage a Texture... */
Texture texture = new TextureLoader(textureImage).getTexture();
Appearance faceAppearance = new Appearance();
TextureUnitState textureUnit = new TextureUnitState();
textureUnit.setTexture(texture);
TextureUnitState[] textureUnits = new TextureUnitState[1];
textureUnits[0] = textureUnit;
faceAppearance.setTextureUnitState(textureUnits);
return faceAppearance;
}
private static Box createTexturedBox(BufferedImage textureImage) {
Appearance faceAppearance = createTexturedAppearance(textureImage);
Box box = new Box(0.2f, 0.2f, 0.2f, Box.GENERATE_NORMALS| Box.GENERATE_TEXTURE_COORDS, new Appearance());
int[] faceIndices = {Box.FRONT, Box.BACK, Box.BOTTOM, Box.TOP, Box.LEFT, Box.RIGHT};
for (int faceIndex : faceIndices) {
Shape3D face = box.getShape(faceIndex);
face.setAppearance(faceAppearance);
}
return box;
}
private static void setupAndShow(BufferedImage textureImage) {
Box texturedBox = createTexturedBox(textureImage);
TransformGroup boxTransform = createMouseRotateGroup(texturedBox);
BranchGroup boxGroup = new BranchGroup();
boxGroup.addChild(boxTransform);
Canvas3D canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
SimpleUniverse universe = new SimpleUniverse(canvas);
universe.getViewingPlatform().setNominalViewingTransform();
universe.addBranchGraph(boxGroup);
Frame window = createWindow();
window.add(canvas);
window.setVisible(true);
}
}
__________________
Uilliam Scecspir ti fa un baffo? Gioffri Cioser era uno straccione? E allora blogga anche tu, in inglese come me! |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 16:47.



















