Texturing component

This component provides extensive textures support. 2D textures may be loaded from image files (ImageTexture), movie files (MovieTexture) or encoded directly in VRML/X3D files (PixelTexture, also ImageTexture with data urls). Multiple textures may be overlayed on a single polygon in a variety of ways. Texture coordinates may be explicitly provided or automatically calculated, and may be transformed.

See also X3D specification of the Texturing component.


See also Castle Game Engine (and view3dscene) extensions related to texturing.

1. Demos

For demos and tests of these features, see the texturing_advanced and movie_texture and multi_texturing subdirectories inside our VRML/X3D demo models.

2. Supported nodes

  • ImageTexture (API reference),
    TextureTransform (API reference),
    TextureCoordinate (API reference),
    PixelTexture (API reference)

    Note: ImageTexture allows various texture formats, including JPEG, PNG, GIF, BMP, PPM, RGBE, KTX, DDS. See glViewImage documentation for more detailed list.

    Note about alpha channel: alpha channel of the textures is fully supported, both a simple yes-no transparency (done by alpha_test in OpenGL) and full range transparency (done by blending in OpenGL, just like partially transparent materials). See "override alpha channel detection" extension description for details. The bottom line is: everything will magically work fast and look perfect.

  • MovieTexture (API reference)

    TODO: for now, the sound of the movie is not played.


    • Current implementation keeps the whole encoded video in memory (images may be discarded after loading (by TCastleSceneCore.FreeResources feature), but still the textures for all frames are kept in memory). The disadvantage is that this makes it impractical to load "real" movies, normal 2-hour movie will most usually eat all of your memory. The advantage is that once the movie is loaded, the playback is super-fast, just like you would display normal nodes with static textures. Since there's no streaming, decoding etc. in the background while you browse your models.

      In other words, this is quite perfect for movie textures with game effects, like smoke or flame. But it's not a substitute for your "real" multimedia movie player.

    • ffmpeg must be installed and available on $PATH to actually open any movie format. See instructions for installing ffmpeg in view3dscene docs. Thanks to ffmpeg, we can handle probably any movie format you will ever need to open.

    • We can also open movies from images sequence. This doesn't require ffmpeg, and allows for some tricks (movie texture with alpha channel). See "Movies from images sequence" extension description.

  • MultiTexture (API reference),
    MultiTextureCoordinate (API reference),
    MultiTextureTransform (API reference)

    Support for all fields (unless mentioned below).

    Note that using MultiTexture.function forces shader pipeline for given shape (so it will not work on really old GPUs). There is no way to reasonably do this using OpenGL fixed-function pipeline, as corresponding OpenGL settings (GL_OPERANDx) operate before normal texture unit calculations are done, while X3D spec requires function to act afterwards.


    TODO: source values "DIFFUSE" and "SPECULAR" are treated the same, as PRIMARY_COLOR (in the sense of OpenGL ARB_texture_env_combine extension). Primary color contains material ambient, diffuse and specular factors, multiplied by lighting properties, summed over all lights. I don't know of any way to efficiently implement separate diffuse / specular sources — please report if you do, otherwise there's no way this can be fixed (note that engine's multi-texturing must work without shaders too).

  • TextureCoordinateGenerator (API reference)


    Note that "CAMERASPACEPOSITION" and "COORD-EYE" are exactly the same thing. Google confirms it (e.g. this source code also treats them as equal and in this old bitmanagement spec they mention they are equal).

    As an extension, we also allow "WORLDSPACEREFLECTIONVECTOR" and "WORLDSPACENORMAL" texture generation modes.


  • TextureProperties (API reference)

    minificationFilter, magnificationFilter, anisotropicDegree fields are supported.

    TODO: the rest of the TextureProperties fields are not supported yet. In particular, if you're looking for a way to use GPU compressed textures, simply place the GPU compressed texture data in DDS files (it's a better idea than using the TextureProperties.textureCompression field).

    Note a common mistake when using the TextureProperties node: texture filtering gets ugly. This is a common mistake, because the X3D specification makes the behavior unexpected: TextureProperties node by default has magnificationFilter and minificationFilter fields set to "FASTEST", which instructs us (and every other valid X3D browser) to use the most ugly filtering (without mipmaps, without even linear filtering). To avoid it, always override these fields to "DEFAULT". For example, instead of using this:

    <TextureProperties anisotropicDegree="4"/>

    use this:

      minificationFilter="DEFAULT" />

    The "DEFAULT" above means to use the browser-specific texture filtering modes, for us this means to use values in Scene.Attributes.MinificationFilter, Scene.Attributes.MagnificationFilter which by default are nice (minLinearMipmapLinear, magLinear).

3. Supported image file formats

See glViewImage features for the full list of 2D image formats we can handle.

See also lower on this page for details about DDS format support and KTX format support.

4. Clarifications to X3D multi-texturing specification

Comments about X3D MultiTexturing problems and solutions (used in our engine, and proposed for future X3D spec) are here.

5. DDS (Texture format) support details

DirectDraw Surface (DDS) image format is supported. This format allows to store textures compressed for GPU (S3TC), storing mipmaps, cube maps, and volume (3D) textures. A number of technical details about DDS implementation are below, but in short: we try to support all formats and all options of DDS in a standard way. You may find example DDS images inside our VRML/X3D demo models (look inside textures subdirectory for *.dds files). You can open them directly with our image viewer glViewImage.

Implementation history:

  • DDS support is natively built into the engine. Since I knew that I want to use many of DDS features, like cube maps, 3D textures, mipmaps, compression, I decided the best way to go will be to create my own reader, instead of relying on external tools.
  • Other notable open-source implementations of DDS are by GIMP-DDS plugin and new ImageMagick (since 6.3.9).
  • While implementing, I was looking at GIMP DDS source code (it's on GNU GPL >= 2 :) ) and MS documentation for DDS.

Cube maps in DDS are supposed to be oriented as usual for DDS:

  1. Which means that they match Direct X "positive/negative x/y/z". For OpenGL rendering we swap positive/negative Y faces (because Direct X has left-handed coordinate system, see here for drawing of DirectX cube map images orientation and compare with OpenGL cube map orientation).

  2. It's also a different orientation then the one of X3D ComposedCubeMap specification (left/right, bottom/top, front/back, with bottom/top on Y axis; X3D orientation needs rotating left,right,front,back images by 180 degrees for OpenGL orientation).

Images in DDS are supposed to be written from top to bottom row, as is the standard in DDS. (One particular tool, AMD CubeMapGen, allows to invert rows of the DDS images to match OpenGL bottom-to-top ordering; don't use this — we expect rows ordered as is standard in DDS, top-to-bottom.) Internally, our engine just inverts the rows for OpenGL (yes, this is doable also for S3TC compressed images.)

Pixel formats supported:

  1. Absolutely all uncompressed non-float pixel formats are supported.


    The formats that are currently loaded optimally are ABGR8, BGR8, AL8, L8. They translate to RGBA8, RGB8 etc. OpenGL formats (reversed order, as DDS color masks are little-endian). Popular ARGB8 and RGB8 are also loaded very fast.

    Grayscale (luminance) images are allowed. AL8 and L8 are optimized. Note that grayscale images aren't officially allowed by DDS docs, but at least GIMP-DDS plugin can write it (just sets all R, G and B masks equal, and doesn't set any of DDPF_RGB, DDPF_FOURCC, DDPF_PALETTEINDEXED8).

    Also only-alpha images are allowed (another undocumented DDS feature, GIMP-DDS can write it, for now they will result in grayscale(white) with alpha image).

  2. Compressed texture formats handled: DXT1, DXT3, DXT5 are supported. Texture with DXT1 is always treated like a texture with simple (yes/no) alpha channel (so it will be rendered with alpha testing) and DXT3 / DXT5 are always treated like a texture with full range alpha channel (so they will be rendered with blending).

    Texture compression formats commonly used on mobile platforms are supported as well: ATITC, PVRTC, ETC1.

    Both normal (2D) textures and cube maps may be compressed. (There is no compression possible for 3D textures — neither DDS format allows it, nor do common graphic cards.)

  3. Reading float textures from DDS is for now not supported. Our engine supports float textures (see TRGBFloatImage class), but DDS reader doesn't support them yet. Please submit a feature request (through forum or ticket system), preferably with some test images, if you need them.

If DDS file includes mipmaps, and mipmaps are required for texture minification filter, we will use DDS mipmaps (instead of generating mipmaps automatically). Works for all 2D, 3D, cubemap DDS files.

6. KTX (Khronos Texture format) support details

Since Castle Game Engine 6.3 we support Khronos KTX texture format. KTX is in many ways an alternative to DDS, with a clean specification, supporting the same features (GPU compression, 2D and 3D textures...). The KTX Khronos page has links to various tools that can create KTX files, and to the KTX specification.

The features of KTX that we support:

  • 1D, 2D and 3D images (textures).

  • Various uncompressed texture formats. RGB + alpha, RGB, luminance, luminance + alpha, luminance 16-bit, luminance float32 texture... RGB can be swapped as BGR (it doesn't matter for performance in practice, either way).

    More uncompressed formats can be trivially added, just submit a bugreport with a sample KTX file.

  • Texture arrays. Programmers: iterate over TCompositeImage.Images to access all the items of the array.

  • Configurable orientation. KTX data may be specified in top-to-bottom or bottom-to-top order. For 3D images, the slices can additionally be in front-to-back or back-to-front order. This is specified by a special KTXorientation field inside the KTX file (see the KTX specification for details) and we support it fully.

  • GPU-compressed data, like

    • S3TC (DXT* formats, often available on desktops),
    • ATITC (ATI compression, available on some mobile devices),
    • PVRTC (PowerVR compression, available on some mobile devices),
    • ETC1 (Ericsson compression, ofen available on mobile devices, but without alpha support).
    • More compresssed formats can be trivially added, just submit a bugreport with a sample KTX file.

We don't support (yet):

  • mipmaps,
  • cubemaps.

Both of these features are quite trivial to implement now, so please ask if you need them :)