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");
		Appearance ap = new Appearance();
		DefaultGeometryShader dgs = (DefaultGeometryShader) ShaderUtility.createDefaultGeometryShader(ap, true);
		DefaultPolygonShader dps = (DefaultPolygonShader) dgs.createPolygonShader("default");
		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));
			id = stf.getImageData();
			scale = 10;
		Texture2D tex = TextureUtility.createTexture(ap, POLYGON_SHADER, id);
		// Attach a node below the textured one and show how to turn off texturing in this node
		SceneGraphComponent sgc1 = new SceneGraphComponent("UnTextureExample");
		MatrixBuilder.euclidean().scale(5).translate(-.5, -.5, 0).assignTo(sgc1);
		ap = new Appearance();
		// TODO: there should be a method in TextureUtility to do this
   		ap.setAttribute("polygonShader.texture2d", Appearance.DEFAULT);
Previous: Use a non-default shader Developer Tutorial: Contents Next: Use an animated 2d texture