Use a 2d texture

From JReality Wiki
Jump to: navigation, search

Source file: TextureExample


Run as Java webstart


Please see introductory tutorial for a simple example of 2d textures.


The following example shows two alternative methods for providing an image to be used in a 2d texture. The first is to load the image using the Input class. The second option, used when no argument is given to the program, is to generate an image procedurally. See the class de.jreality.tutorial.util.SimpleTextureFactory for details.


Finally notice that the example includes a flat, untextured quadrilateral. This quadrilateral lies beneath the textured surface within the scene graph, so by default it would have the same texture applied to it. The code at the end of the example shows how to set the appropriate appearance attribute to override this inheritance.

 
...
public static void main(String[] args) throws IOException {
	  	IndexedFaceSet geom = new CatenoidHelicoid(40);
		SceneGraphComponent sgc = new SceneGraphComponent("TextureExample");
		sgc.setGeometry(geom);
		Appearance ap = new Appearance();
		sgc.setAppearance(ap);
		DefaultGeometryShader dgs = (DefaultGeometryShader) ShaderUtility.createDefaultGeometryShader(ap, true);
		dgs.setShowLines(false);
		dgs.setShowPoints(false);
		DefaultPolygonShader dps = (DefaultPolygonShader) dgs.createPolygonShader("default");
		dps.setDiffuseColor(Color.white);
		ImageData id = null;
		double scale = 1;
		// get the image for the texture first
		if (args.length > 0) {
			id = ImageData.load(Input.getInput(args[0]));
		} else { // use a procedural texture
			SimpleTextureFactory stf = new SimpleTextureFactory();
			stf.setColor(0, new Color(0,0,0,0));	// gap color in weave pattern is totally transparent
			stf.setColor(1, new Color(255,0,100));
			stf.setColor(2, new Color(255,255,0));
			stf.update();
			id = stf.getImageData();
			scale = 10;
			dps.setDiffuseColor(Color.white);
		}
		Texture2D tex = TextureUtility.createTexture(ap, POLYGON_SHADER, id);
		System.err.println(ap.toString());
		tex.setTextureMatrix(MatrixBuilder.euclidean().scale(scale).getMatrix());
		// Attach a node below the textured one and show how to turn off texturing in this node
		SceneGraphComponent sgc1 = new SceneGraphComponent("UnTextureExample");
		sgc.addChild(sgc1);
		sgc1.setGeometry(Primitives.texturedQuadrilateral());
		MatrixBuilder.euclidean().scale(5).translate(-.5, -.5, 0).assignTo(sgc1);
		ap = new Appearance();
		sgc1.setAppearance(ap);
		// TODO: there should be a method in TextureUtility to do this
   		ap.setAttribute("polygonShader.texture2d", Appearance.DEFAULT);
		JRViewer.display(sgc);
  }
}
Previous: Use a non-default shader Developer Tutorial: Contents Next: Use an animated 2d texture