Class SoIndexedTexture2

  • All Implemented Interfaces:
    SafeDisposable

    public class SoIndexedTexture2
    extends SoTexture
    Class for 2D indexed texture. This property node defines an "indexed" texture image. In a standard texture image, e.g. SoTexture2, each texel contains a color value that is directly applied to the geometry. In this node each texel contains a data value that is mapped to a color value using the color map defined by the current SoColorMap node.

    This image is stored in the current texture unit (see SoTextureUnit) and the texture is applied to subsequent shapes as they are rendered.

    The image data is stored in an SoSFArray2D. This array can contain different types of data (UNSIGNED_BYTE, UNSIGNED_SHORT, UNSIGNED_INT32, SIGNED_BYTE, SIGNED_SHORT, SIGNED_INT32, FLOAT).

    When Open Inventor creates the OpenGL texture image from the data array, the data values must be scaled into the range of values of the texture (0-255 for an 8-bit indexed texture). The minValue and maxValue fields specify the range of data values that will be scaled into the range of texture values. For example, if minValue is set to 10000 and maxValue to 38000, all values less than or equal to 10000 will be mapped to the entry 0 of the color map and all values greater than or equal to 38000 to the last entry. The image below illustrates the process of mapping and shows how it can be used to map only the used data range to the color map:

    If minValue or maxValue change, then the OpenGL texture must be recreated. If the texture is large, this may be time consuming. Generally it's more efficient to modify the SoColorMap node because the OpenGL texture corresponding to the data does not need to be recreated only the texture containing the colormap, which is normally quite small.

    NOTE: Texture data values are stored with 12 bits of precision instead of the usual 8 bits, when:

    • ARB_fragment_program is supported by the graphics card.
    • The number of color map entries is greater than 256.
    • The texture data type has more than 8 bits of precision (i.e., not UNSIGNED_BYTE or SIGNED_BYTE).

    EXAMPLE

    The following example shows how to apply an indexed texture on a geometry node:

     SoColorMap colorMap = new SoColorMap();
       colorMap.predefinedColorMap.setValue( SoColorMap.PredefinedColorMaps.TEMPERATURE );
       colorMap.min.setValue( -1000 );
       colorMap.max.setValue( 20000 );
     
     SoIndexedTexture2 indexedTexture = new SoIndexedTexture2();
       indexedTexture.imageIndex.setValue(size, SoSFArray.DataTypes.SIGNED_SHORT, data);
     
     root.addChild( colorMap );
     root.addChild( indexedTexture );
     root.addChild( geometry ); 

    EXAMPLE

    On graphics cards supporting ARB_fragment_program , the model field will be ignored and multitexturing will not work. It is possible to circumvent this limitation by using a custom shader. The following GLSL code shows how to do multitexturing between two indexed textures sharing the same color map:

     // Fragment shader
     uniform sampler2D colormap;
     uniform sampler2D indexedTex1; 
     uniform sampler2D indexedTex2;  
     
     void main()
     {
       // Fetch first value and its assigned color
       float value1 = texture2D(indexedTex1, gl_TexCoord[0].xy).x;
       vec4  color1 = texture2D(colormap, vec2(value1, 0));
     
       // Fetch second and its assigned color
       float value2 = texture2D(indexedTex2, gl_TexCoord[0].xy).x;
       vec4  color2 = texture2D(colormap, vec2(value2, 0));
     
       // Combine the colors
       gl_FragColor = color1 * color2 * gl_Color;
     }

    The scene graph would be initialized like this:

     SoColorMap colorMap = new SoColorMap();
     . . .
     SoIndexedTexture2 indexedTex1 = new SoIndexedTexture2();
     . . .
     SoIndexedTexture2 indexedTex2 = new SoIndexedTexture2();
     . . .
     
     SoFragmentShader fragShader = new SoFragmentShader();
       fragShader.sourceProgram.setValue( "filename.glsl" );
     
     SoShaderParameter1i parameter = new SoShaderParameter1i();
       parameter.name.setValue( "colorMap" );
       parameter.value.setValue( 0 );
       fragShader.parameter.set1Value( 0, parameter );
     
     parameter = new SoShaderParameter1i();
       parameter.name.setValue( "indexedTex1" );
       parameter.value.setValue( 1 );
       fragShader.parameter.set1Value( 1, parameter );
     
     parameter = new SoShaderParameter1i();
       parameter.name.setValue( "indexedTex2" );
       parameter.value.setValue( 2 );
       fragShader.parameter.set1Value( 2, parameter );
     
     SoShaderProgram shaderProgram = new SoShaderProgram();
       shaderProgram.shaderObject.setValue(fragShader);
     
     SoTextureUnit textureUnit = new SoTextureUnit();
       textureUnit.unit.setValue( 2 );  //Texture unit for the second indexed texture
     
     root.addChild(colorMap);      //Color map must come first (texture unit 1)
     root.addChild(indexedTex1);   //First indexed texture (texture unit 0)
     root.addChild(textureUnit);
     root.addChild(indexedTex2);   //Second indexed texture (texture unit 2)
     root.addChild(shaderProgram);
     root.addChild(geometry);

    File format/default:

    IndexedTexture2 {

      minValue 0
      maxValue 0
      imageIndex 0 0
      rescaleTexCoord false
      wrapS REPEAT
      wrapT REPEAT
      model MODULATE
      blendColor 0 0 0
      enableBorder false
      borderColor 0 0 0 0
      maxAnisotropy 1.0
      minFilter AUTO
      magFilter AUTO
      useAutoMipmap false
      internalFormat AUTO_INTERNAL_FORMAT
    }

    Action behavior:

    SoGLRenderAction, SoCallbackAction
    Sets: SoTextureImageElement

    See Also:
    SoTexture2Transform, SoTextureCoordinate2, SoTextureCoordinateFunction, SoColorMap, SoSFArray2D
    • Field Detail

      • minValue

        public final SoSFFloat minValue
        Specifies the range of values which is mapped onto the color map (see SoColorMap). When minValue and maxValue are equal to 0 (the default), the entire range of the data type is mapped onto the color map, except in the case of float data. For example, for a color map of size N:
        • With unsigned byte values, [0-255] is mapped onto the color map [0 - N-1]
        • With unsigned short values, [0-65535] is mapped onto the color map [0 - N-1]
        • With signed short values, [-32768 - 32767] is mapped onto the color map [0 - N-1].
        • With float data type, [0-1] is mapped onto the color map [0 - N-1]
        All values less than or equal to minValue will be mapped to the first entry of the color map. Likewise, all values greater than or equal to maxValue will be mapped to the last entry of the color map.
      • imageIndex

        public final SoSFArray2D imageIndex
        This field contains the in-memory representation of the indexed texture image. Each value of this array is a color index. In the case of float values, data must be between 0 and 1.
      • rescaleTexCoord

        public final SoSFBool rescaleTexCoord
        This field controls the way an image with non-power-of-two dimension is handled:
        • If the graphics card supports GL_ARB_texture_non_power_of_two , this field is ignored and the image is sent directly to OpenGL.
        • If rescaleTexCoord is false (the default), the image is rescaled to the next lower power-of-two dimensions. The scaling is done with a box filter.
        • If rescaleTexCoord is true, the image is not rescaled but is only copied into a texture with the next higher power-of-two dimension, and its texture coordinates are scaled by applying a texture transform in order to only display the actual image.

        Default is false.

        When this field is enabled, if other texture transforms are applied to the texture coordinates (especially if the texture is repeated), the unused part of the larger texture could be displayed. Furthermore, in this mode, if non-power-of-two textures occur one after another in the scene graph, an incorrect image may result because texture transforms are cumulative. For example, the following code would give incorrect results:

         root.addChild( colorMap );
         root.addChild( nonPowerOfTwoIndexedTexture1 );
         root.addChild( nonPowerOfTwoIndexedTexture2 );
         root.addChild( nonPowerOfTwoIndexedTexture3 );

        The correct way is to use a separator above each texture, as shown:

         root.addChild( colorMap );
         root.addChild( separator1 );
           separator1.addChild( nonPowerOfTwoIndexedTexture1 );
         root.addChild( separator2 );
           separator2.addChild( nonPowerOfTwoIndexedTexture2 );
         root.addChild( separator3 );
           separator3.addChild( nonPowerOfTwoIndexedTexture3 );

      • wrapT

        public final SoSFEnum<SoTexture.WrapType> wrapT
        Indicates what to do when texture coordinates in the T (vertical) direction lie outside the range 0-1. . Default is REPEAT.
    • Constructor Detail

      • SoIndexedTexture2

        public SoIndexedTexture2()
        Constructor.
    • Method Detail

      • computeDataRange

        public void computeDataRange()
        Compute the min and max value of the indexed image and put the result in minValue and maxValue.