# Java3D: Texture-Frage



## jagdfalke (27. Mai 2006)

Hi,
ich habe eine Box die meinen "Boden" darstellen soll, also Breite 2, Länge 2, Dicke 0.01. Jetzt möchte ich, dass das ganze aussieht wie ein Pflaster also hab ich mir eine .gif-Datei geholt die so aussieht und versuche sie als Texture drüberzulegen. Wenn ich jetzt aber meinen Boden drehe verschiebt sich die ganze Textur ziemlich seltsam. Eigentlich sollte sie doch so mitroteren wie die Box selber, sodass es wie "Eins" aussieht. Naja sehts euch am Besten selber an:


```
import javax.swing.JFrame;
import javax.media.j3d.*;
import com.sun.j3d.utils.universe.*;
import javax.vecmath.*;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.image.*;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;

public class Main extends JFrame {

	private SimpleUniverse un;
	
	public Main() {
		setLayout(new BorderLayout());
		setSize(500,500);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
		
		Canvas3D c=new Canvas3D(config);
		getContentPane().add(c);
		
		un = new SimpleUniverse(c);
	    	un.getViewingPlatform().setNominalViewingTransform();
	    	un.addBranchGraph(createSceneGraph());

		setVisible(true);
	}

	private BranchGroup createSceneGraph() {
		BranchGroup bg = new BranchGroup();
		
		Transform3D superTrans = new Transform3D();
			//superTrans.setRotation(new AxisAngle4d(1f, 1f, 0f, Math.toRadians(10)));
			superTrans.setTranslation(new Vector3d(0f, 0f, -2f));
		TransformGroup superGroup = new TransformGroup(superTrans);
			superGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
			superGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
		
		///////////////////////////////////////////////////
		// LIGHT
		///////////////////////////////////////////////////
		BoundingSphere bndSphere = new BoundingSphere(new Point3d(0f,0f,0f),100000);
		
		AmbientLight ambLight = new AmbientLight(new Color3f(1f,1f,1f));
			ambLight.setInfluencingBounds(bndSphere);
		bg.addChild(ambLight);
		
        SpotLight spot = new SpotLight(	new Color3f(1f,1f,1f), //color
        								new Point3f(0f,2f,0f), //position
        								new Point3f(0f,0f,0f), //attenuation
        								new Vector3f(0f,-1f,0f), //direction
        								(float)Math.toRadians(10),100f);
        	spot.setInfluencingBounds(bndSphere);
        bg.addChild(spot);
        
        DirectionalLight dirLight = new DirectionalLight(new Color3f(1f,0f,0f), new Vector3f(0f,0f,-1f));
        	dirLight.setInfluencingBounds(bndSphere);
    	bg.addChild(dirLight);
	    	
        MouseRotate rotor = new MouseRotate();
	        rotor.setSchedulingBounds(bndSphere);
	        rotor.setTransformGroup(superGroup); 
		bg.addChild(rotor);
			
			
	
		/////////////////////////////////////////////////
	    // BOX
	    /////////////////////////////////////////////////
		Appearance boxAppearance = new Appearance();
			boxAppearance.setMaterial(new Material(new Color3f(1f,0.5f,0.5f),new Color3f(0f,0f,0f),new Color3f(0.9f,0.9f,0.9f),new Color3f(0.8f,0.8f,0.8f),1f));
			boxAppearance.setTexture(new TextureLoader("cobblestone.gif", null).getTexture());
			boxAppearance.setTexCoordGeneration(new TexCoordGeneration(TexCoordGeneration.SPHERE_MAP,TexCoordGeneration.TEXTURE_COORDINATE_2));
			boxAppearance.setTextureAttributes(new TextureAttributes(TextureAttributes.REPLACE,new Transform3D(),new Color4f(), TextureAttributes.NICEST));
		Box box = new Box(1f,0.05f,1f,Sphere.GENERATE_NORMALS, boxAppearance);
        superGroup.addChild(box);
			
		///////////////////////////////////////////////////
		// SPHERE
		///////////////////////////////////////////////////
		Transform3D t3d = new Transform3D();
			t3d.setTranslation(new Vector3f(1f,0f,-1f));
		TransformGroup tg = new TransformGroup();
			tg.setTransform(t3d);
		Appearance sphereAppearance = new Appearance();
        	sphereAppearance.setMaterial(new Material(new Color3f(0.1f,0.1f,0.1f),new Color3f(0.5f,0.5f,0.5f),new Color3f(0.8f,0.8f,0.8f),new Color3f(0.6f,0.6f,0.6f),1f));
		Sphere sphere = new Sphere(.5f,Sphere.GENERATE_NORMALS, 100,sphereAppearance);
		tg.addChild(sphere);
		superGroup.addChild(tg);
		
		/////////////////////////////////////////////////
	    // Cylinder
	    /////////////////////////////////////////////////
		Transform3D cylinderT3D = new Transform3D();
			cylinderT3D.setTranslation(new Vector3d(-0.5, 0f,0f));
		TransformGroup cylinderTG = new TransformGroup();
		cylinderTG.setTransform(cylinderT3D);
		Appearance cylinderAppearance = new Appearance();
	     	cylinderAppearance.setMaterial(new Material(new Color3f(0f,0f,1f),new Color3f(0f,0f,0f),new Color3f(1f,0f,0f),new Color3f(1f,1f,1f),100f));
     	Cylinder cylinder = new Cylinder(0.3f,1f,Cylinder.GENERATE_NORMALS,40,1,cylinderAppearance);
     	cylinderTG.addChild(cylinder);
     	superGroup.addChild(cylinderTG);
		
	    
     	///////////////////////////////////////////////
     	// Box 2
     	///////////////////////////////////////////////
     	Transform3D box2T3D = new Transform3D();
     		box2T3D.setTranslation(new Vector3d(0f,0.15f,1f));
 		TransformGroup box2TG = new TransformGroup(box2T3D);
		Appearance box2Appearance = new Appearance();
			box2Appearance.setMaterial(new Material(new Color3f(0.8f,0.8f,0.8f),new Color3f(0f,0f,0f),new Color3f(0.9f,0.9f,0.9f),new Color3f(0.8f,0.8f,0.8f),1f));
     	Box box2 = new Box(0.3f,0.3f,0.3f,Sphere.GENERATE_NORMALS, box2Appearance);
 		box2TG.addChild(box2);
 		superGroup.addChild(box2TG);
     	
        bg.addChild(superGroup);
	    
		bg.compile();
		return bg;
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Main();
	}

}
```

cobblestone.gif findet ihr über google, ich kann ja hier nix anhängen. Also es ist sicher normal so wie es ist aber gibts nicht die Möglichkeit es so hinzubekommen, wie ich es oben beschrieben habe? Also wirklich eine Textur und nicht ein Abgeschnittenes Bild in der Box, das sich je nach Boxdrehung ändert?

mfg
jagdfalke


----------



## Illuvatar (27. Mai 2006)

Also ich weiß nicht, was du da mit TexCoordGeneration machst, die Klasse hab ich noch nie verwendet. Auf jeden Fall liegt da der Fehler 
Änder mal Zeilen 76 - 78 so:


```
//boxAppearance.setTexCoordGeneration(new TexCoordGeneration(TexCoordGeneration.NORMAL_MAP,TexCoordGeneration.TEXTURE_COORDINATE_2));
         boxAppearance.setTextureAttributes(new TextureAttributes(TextureAttributes.REPLACE,new Transform3D(),new Color4f(), TextureAttributes.NICEST));
      Box box = new Box(1f,0.05f,1f,Primitive.GENERATE_NORMALS | Primitive.GENERATE_TEXTURE_COORDS, boxAppearance);
```


----------



## jagdfalke (27. Mai 2006)

Jaaa, jetzt gehts. Aber was genau macht das hier:

```
Primitive.GENERATE_NORMALS | Primitive.GENERATE_TEXTURE_COORDS
```
Was bedeutet der | ?
Dieser TexCoordsGeneration ständ so ein meinem Java3D Tutorial, es wurde aber bei einer Sphere verwendet.

mfg
jagdfalke


----------



## Illuvatar (27. Mai 2006)

Das sind sogenannte Bitflags. Also wenn zum Beispiel GENERATE_NORMALS 00000001 ist und GENERATE_TEXTURE_COORDS 00000010 dann macht der binäre Oder-Operator | da ein 00000011 draus, und die Sphere Klasse weiß, dass du die beiden Sachen gemacht haben willst 

Also ich hab nach dem Java3D - Tut von Sun gelernt, die haben das immer so gemacht


----------



## jagdfalke (27. Mai 2006)

ok danke, dann sollte ich vielleicht auch besser das von sun nehmen 

mfg
jagdfalke


----------

