From JReality Wiki
Jump to: navigation, search

Apply a texture

Source file: Intro07

JavaDoc: Texture2D

Run as Java webstart

This example shows how to apply a texture to the white cylinder created in the previous example.


  • The file "gridSmall.jpg" needs to be located in the same folder with the source code you are executing, just as the file "" in the earlier example.
  • In order for texture mapping to work, you need to have a geometry with texture coordinates. I happen to know that the Primitives.cylinder() class returns such a geometry.

The code first creates an instance of Texture2D, then loads an image file which is set to be the image of the texture object.

import de.jreality.shader.Texture2D;
import de.jreality.shader.ImageData;
import de.jreality.math.MatrixBuilder;
    Texture2D tex2d = (Texture2D) AttributeEntityUtility.
    createAttributeEntity(Texture2D.class, POLYGON_SHADER+"."+TEXTURE_2D,ap, true);
    URL is = Intro07.class.getResource("gridSmall.jpg");
    ImageData id = null;
    try {
      id = ImageData.load(new Input(is));
    } catch (IOException ee) {

It's also possible to replace the Java resource reference above by an http:// address as with the geometry file above. If you want to do this, replace the code with the following:

    Texture2D tex2d = null;
    String textureFileURL = "";
    try {
      tex2d = TextureUtility.createTexture(ap, POLYGON_SHADER,textureFileURL);
    } catch (IOException e) {

Finally, apply a scaling matrix to the texture. There won't be many surprises in this section, if you've worked through the User Tutorial since we did everything there using the Bean shell, which means you've already programmed this in Java.

    Matrix foo = new Matrix();
    MatrixBuilder.euclidean().scale(10, 5, 1).assignTo(foo);

You should now see a picture that looks like this.

Texture mapping

Further options for the Texture2D can be found in the interface de.jreality.shader.Texture2D. There are a quite a large number of options borrowed from the OpenGL texturing model, most of which are only implemented in the JOGL backend.

Previous: Intro06 Developer Tutorial: Contents Next: Intro08