java.lang.Object
eu.svjatoslav.sixth.e3d.renderer.raster.texture.Texture

public class Texture extends Object
Represents a 2D texture with mipmap support for level-of-detail rendering.

A Texture contains a primary bitmap at native resolution, along with cached upscaled and downscaled versions (mipmaps) that are lazily generated on demand. This mipmap chain enables efficient texture sampling at varying distances from the camera, avoiding aliasing artifacts for distant surfaces and pixelation for close-up views.

The texture also exposes a Graphics2D context backed by the primary bitmap's BufferedImage, allowing dynamic rendering of text, shapes, or other 2D content directly onto the texture surface. Anti-aliasing is enabled by default on this graphics context.

Mipmap levels

  • Primary bitmap -- the native resolution; always available.
  • Downsampled bitmaps -- up to 8 levels, each half the size of the previous. Used when the texture is rendered at zoom levels below 1.0.
  • Upsampled bitmaps -- configurable count (set at construction time), each double the size of the previous. Used when the texture is rendered at zoom levels above 2.0.

Usage example


 Texture tex = new Texture(256, 256, 3);
 // Draw content using the Graphics2D context
 tex.graphics.setColor(java.awt.Color.RED);
 tex.graphics.fillRect(0, 0, 256, 256);
 // Invalidate cached mipmaps after modifying the primary bitmap
 tex.resetResampledBitmapCache();
 // Retrieve the appropriate mipmap for a given zoom level
 TextureBitmap bitmap = tex.getZoomedBitmap(0.5);
 
See Also:
  • Field Details

    • primaryBitmap

      public final TextureBitmap primaryBitmap
      The primary (native resolution) bitmap for this texture. All dynamic drawing via graphics modifies this bitmap's backing data.
    • graphics

      public final Graphics2D graphics
      A Graphics2D context for drawing 2D content onto the primary bitmap. Anti-aliasing for both geometry and text is enabled by default.
  • Constructor Details

    • Texture

      public Texture(int width, int height, int maxUpscale)
      Creates a new texture with the specified dimensions and upscale capacity.

      The underlying BufferedImage is created using RenderingContext.bufferedImageType for compatibility with the raster rendering pipeline.

      Parameters:
      width - the width of the primary bitmap in pixels
      height - the height of the primary bitmap in pixels
      maxUpscale - the maximum number of upscaled mipmap levels to support (each level doubles the resolution)
  • Method Details

    • detectDownscaleFactorForZoom

      public int detectDownscaleFactorForZoom(double zoom)
      Determines the appropriate downscale factor index for a given zoom level.

      Iterates through the downsampled mipmap levels (each halving the size) and returns the index of the first level whose effective size falls below the requested zoom.

      Parameters:
      zoom - the zoom level (typically less than 1.0 for downscaling)
      Returns:
      the index into the downSampled array to use, clamped to the maximum available level
    • detectUpscaleFactorForZoom

      public int detectUpscaleFactorForZoom(double zoom)
      Determines the appropriate upscale factor index for a given zoom level.

      Iterates through the upsampled mipmap levels (each doubling the size) and returns the index of the first level whose effective size exceeds the requested zoom.

      Parameters:
      zoom - the zoom level (typically greater than 2.0 for upscaling)
      Returns:
      the index into the upSampled array to use, clamped to the maximum available level
    • downscaleBitmap

      public TextureBitmap downscaleBitmap(TextureBitmap originalBitmap)
      Downscale given bitmap by factor of 2.
      Parameters:
      originalBitmap - Bitmap to downscale.
      Returns:
      Downscaled bitmap.
    • getDownscaledBitmap

      public TextureBitmap getDownscaledBitmap(int scaleFactor)
      Returns a downscaled bitmap at the specified mipmap level, creating it lazily if needed.

      Level 0 is half the primary resolution, level 1 is a quarter, and so on. Each level is derived by downscaling the previous level by a factor of 2.

      Parameters:
      scaleFactor - the downscale level index (0 = 1/2 size, 1 = 1/4 size, etc.)
      Returns:
      the cached or newly created downscaled TextureBitmap
      See Also:
    • getUpscaledBitmap

      public TextureBitmap getUpscaledBitmap(int scaleFactor)
      Returns the bitmap that should be used for rendering at the given zoom
      Parameters:
      scaleFactor - The upscale factor
      Returns:
      The bitmap
    • getZoomedBitmap

      public TextureBitmap getZoomedBitmap(double zoomLevel)
      Returns the bitmap that should be used for rendering at the given zoom
      Parameters:
      zoomLevel - The zoom level
      Returns:
      The bitmap
    • resetResampledBitmapCache

      public void resetResampledBitmapCache()
      Resets the cache of resampled bitmaps
    • upscaleBitmap

      public TextureBitmap upscaleBitmap(TextureBitmap originalBitmap)
      Upscales the given bitmap by a factor of 2
      Parameters:
      originalBitmap - The bitmap to upscale
      Returns:
      The upscaled bitmap