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.getMipmapForScale(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

    • getDownscaleMipmapLevel

      public int getDownscaleMipmapLevel(double scale)
      Determines the appropriate downscale mipmap level for a given scale factor.

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

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

      public int getUpscaleMipmapLevel(double scale)
      Determines the appropriate upscale mipmap level for a given scale factor.

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

      Parameters:
      scale - the scale factor (typically greater than 2.0 for upscaling)
      Returns:
      the index into the upSampled array to use, or -1 if no upscale is needed or available
    • 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
    • getMipmapForScale

      public TextureBitmap getMipmapForScale(double scale)
      Returns the appropriate mipmap level for rendering at the given scale.

      Scale factor represents how large the texture appears on screen relative to its native resolution:

      • scale < 1.0: texture appears smaller (use downscaled mipmap)
      • scale 1.0-2.0: texture appears near native size (use primary bitmap)
      • scale > 2.0: texture appears much larger (use upscaled mipmap)
      Parameters:
      scale - the apparent scale factor of the texture on screen
      Returns:
      the best-fit mipmap level as a TextureBitmap
    • 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