j3d.org Code

org.j3d.texture.procedural
Class TextureGenerator

java.lang.Object
  extended by org.j3d.texture.procedural.TextureGenerator

public class TextureGenerator
extends java.lang.Object

Various utility methods for creating textures from procedural means.

Using Synthesis Textures

Synthesis textures require the user to make multiple passes to generate the desired output. Each passes adds to the previous pass at, hopefully, higher and higher frequency. This can then be turned into a texture by finding the height range and multiplying through the values to generate an appropriate set of bytes.

An example usage of this code is:

  int width = 256;
  int depth = 256;
  int passes = 5;
  float scale = 8;
  float freq = 4;
  float y_scale = 0.4f;
  float freq_diff = 6;

  float[] raw_img = new float[width * depth];

  imageGen.generateSynthesisTexture(raw_img,
                                    freq,
                                    scale,
                                    width,
                                    depth);

  for(int i = 1; i < passes; i++)
  {
      freq += freq_diff;
      scale *= y_scale;
      imageGen.generateSynthesisTexture(raw_img,
                                        freq,
                                        scale,
                                        width,
                                        depth);
  }

  // find min and max values of the floats
  float min_y = raw_img[0];
  float max_y = raw_img[0];

  for(int i = 0; i < width * depth; i++)
  {
      if(raw_img[i] > max_y)
          max_y = raw_img[i];
      else if(raw_img[i] < min_y)
          min_y = raw_img[i];
  }

  byte[] pixels = new byte[width * depth];
  float diff = 1 / (max_y - min_y);

  for(int i = 0; i < width * depth; i++)
      pixels[i] = (byte)(((raw_img[i] - min_y) * diff) * 255);

  WritableRaster raster =
      Raster.createPackedRaster(DataBuffer.TYPE_BYTE,
                                width,
                                depth,
                                1,
                                8,
                                null);

  raster.setDataElements(0, 0, width, depth, pixels);
  BufferedImage img =
      new BufferedImage(width, depth, BufferedImage.TYPE_BYTE_GRAY);

  img.setData(raster);
 

Sources

The synthesis texture generation comes from A GameDev.net article by Druid
The texture mixing code comes from a FlipCode tutorial by Tobias Franke.

Version:
$Revision: 1.1 $
Author:
Justin Couch

Constructor Summary
TextureGenerator()
          Create a new instance of this generator with default values set.
 
Method Summary
 byte[] generateMixedTerrainTexture(byte[] outputImage, float[][] heightMap, java.awt.image.BufferedImage[] colorTextures, float[] textureHeight, int numColorTextures)
          From the given height map and input textures, generate a single mixed RGB texture that can be drapped over the terrain.
 byte[] generateMixedTerrainTexture(byte[] outputImage, float[] heightMap, int width, int height, java.awt.image.BufferedImage[] colorTextures, float[] textureHeight, int numColorTextures)
          From the given height map and input textures, generate a single mixed RGB texture that can be drapped over the terrain.
 int[] generateMixedTerrainTexture(int[] outputImage, float[][] heightMap, java.awt.image.BufferedImage[] colorTextures, float[] textureHeight, int numColorTextures)
          From the given height map and input textures, generate a single mixed RGB texture that can be drapped over the terrain.
 int[] generateMixedTerrainTexture(int[] outputImage, float[] heightMap, int width, int height, java.awt.image.BufferedImage[] colorTextures, float[] textureHeight, int numColorTextures)
          From the given height map and input textures, generate a single mixed RGB texture that can be drapped over the terrain.
 float[] generateSynthesisTexture(float[] outputImage, float freq, float zScale, int width, int height)
          Generate a texture using spectral synthesis techniques into a height field of floats.
 void setRandomSeed(long seed)
          Reset the random number generator using the new seed value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TextureGenerator

public TextureGenerator()
Create a new instance of this generator with default values set.

Method Detail

setRandomSeed

public void setRandomSeed(long seed)
Reset the random number generator using the new seed value.

Parameters:
seed - The new seed value to use

generateSynthesisTexture

public float[] generateSynthesisTexture(float[] outputImage,
                                        float freq,
                                        float zScale,
                                        int width,
                                        int height)
Generate a texture using spectral synthesis techniques into a height field of floats. This takes various sine waves and combines harmonics of them for a single pass. Each time this is called with the same outputImage provided, the values are added to the previous values, so that mutli-pass techniques give a nicer terrain.

Parameters:
outputImage - An optional array to write the output values to
freq - The number of passes to add to the image
zScale - A scaling factor for the heights (0, 1]
width - The width in pixels for the output image
height - The height in pixels for the output image
Returns:
A float of greyscale heights for the output image

generateMixedTerrainTexture

public int[] generateMixedTerrainTexture(int[] outputImage,
                                         float[] heightMap,
                                         int width,
                                         int height,
                                         java.awt.image.BufferedImage[] colorTextures,
                                         float[] textureHeight,
                                         int numColorTextures)
From the given height map and input textures, generate a single mixed RGB texture that can be drapped over the terrain. The input for this method is expected to be something similar to the output from the FractalTerrainGenerator.

There must be at least one more textureHeight than there is numColorTextures. Each index in the array represents where the texture for that index is blended at 0%. The last value indicates where the highest texture is blended at 100%.

Height values must be monotonically increasing.

The height colour textures are not required to be the same size as the height map. If they are not, then the pixels are retrieved using a wrapping function.

Parameters:
heightMap - The list of heights in [height][width] order
colorTextures - Images to read color values from for mixing
textureHeight - List of heights that each texture applies to
numColorTextures - The number of textures to use in heights
Returns:
A flat array in [r,g,b,...] order suitable for dumping to a texture or Image object.

generateMixedTerrainTexture

public byte[] generateMixedTerrainTexture(byte[] outputImage,
                                          float[] heightMap,
                                          int width,
                                          int height,
                                          java.awt.image.BufferedImage[] colorTextures,
                                          float[] textureHeight,
                                          int numColorTextures)
From the given height map and input textures, generate a single mixed RGB texture that can be drapped over the terrain. The input for this method is expected to be something similar to the output from the FractalTerrainGenerator.

There must be at least one more textureHeight than there is numColorTextures. Each index in the array represents where the texture for that index is blended at 0%. The last value indicates where the highest texture is blended at 100%.

Height values must be monotonically increasing.

The height colour textures are not required to be the same size as the height map. If they are not, then the pixels are retrieved using a wrapping function.

Parameters:
heightMap - The list of heights in [height][width] order
colorTextures - Images to read color values from for mixing
textureHeight - List of heights that each texture applies to
numColorTextures - The number of textures to use in heights
Returns:
A flat array in [r,g,b,...] order suitable for dumping to a texture or Image object.

generateMixedTerrainTexture

public int[] generateMixedTerrainTexture(int[] outputImage,
                                         float[][] heightMap,
                                         java.awt.image.BufferedImage[] colorTextures,
                                         float[] textureHeight,
                                         int numColorTextures)
From the given height map and input textures, generate a single mixed RGB texture that can be drapped over the terrain. The input for this method is expected to be something similar to the output from the FractalTerrainGenerator.

There must be at least one more textureHeight than there is numColorTextures. Each index in the array represents where the texture for that index is blended at 0%. The last value indicates where the highest texture is blended at 100%.

Height values must be monotonically increasing

Parameters:
heightMap - The list of heights in [height][width] order
colorTextures - Images to read color values from for mixing
textureHeight - List of heights that each texture applies to
numColorTextures - The number of textures to use in heights
Returns:
A flat array in [r,g,b,...] order suitable for dumping to a texture or Image object.

generateMixedTerrainTexture

public byte[] generateMixedTerrainTexture(byte[] outputImage,
                                          float[][] heightMap,
                                          java.awt.image.BufferedImage[] colorTextures,
                                          float[] textureHeight,
                                          int numColorTextures)
From the given height map and input textures, generate a single mixed RGB texture that can be drapped over the terrain. The input for this method is expected to be something similar to the output from the FractalTerrainGenerator.

There must be at least one more textureHeight than there is numColorTextures. Each index in the array represents where the texture for that index is blended at 0%. The last value indicates where the highest texture is blended at 100%.

Height values must be monotonically increasing

Parameters:
heightMap - The list of heights in [height][width] order
colorTextures - Images to read color values from for mixing
textureHeight - List of heights that each texture applies to
numColorTextures - The number of textures to use in heights
Returns:
A flat array in [r,g,b,...] order suitable for dumping to a texture or Image object.

j3d.org Code

Latest Info from http://code.j3d.org/
Copyright © 2001 - j3d.org