Java Code Examples for com.jme3.math.FastMath#isPowerOfTwo()

The following examples show how to use com.jme3.math.FastMath#isPowerOfTwo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TerrainQuad.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected TerrainQuad(String name, int patchSize, int quadSize,
                        Vector3f scale, float[] heightMap, int totalSize,
                        Vector2f offset, float offsetAmount)
{
    super(name);

    if (heightMap == null)
        heightMap = generateDefaultHeightMap(quadSize);

    if (!FastMath.isPowerOfTwo(quadSize - 1)) {
        throw new RuntimeException("size given: " + quadSize + "  Terrain quad sizes may only be (2^N + 1)");
    }
    if (FastMath.sqrt(heightMap.length) > quadSize) {
        Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Heightmap size is larger than the terrain size. Make sure your heightmap image is the same size as the terrain!");
    }

    this.offset = offset;
    this.offsetAmount = offsetAmount;
    this.totalSize = totalSize;
    this.size = quadSize;
    this.patchSize = patchSize;
    this.stepScale = scale;
    split(patchSize, heightMap);
}
 
Example 2
Source File: TerrainQuad.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected TerrainQuad(String name, int patchSize, int quadSize,
                        Vector3f scale, float[] heightMap, int totalSize,
                        Vector2f offset, float offsetAmount)
{
    super(name);
    
    if (heightMap == null)
        heightMap = generateDefaultHeightMap(quadSize);
    
    if (!FastMath.isPowerOfTwo(quadSize - 1)) {
        throw new RuntimeException("size given: " + quadSize + "  Terrain quad sizes may only be (2^N + 1)");
    }
    if (FastMath.sqrt(heightMap.length) > quadSize) {
        Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Heightmap size is larger than the terrain size. Make sure your heightmap image is the same size as the terrain!");
    }
    
    this.offset = offset;
    this.offsetAmount = offsetAmount;
    this.totalSize = totalSize;
    this.size = quadSize;
    this.patchSize = patchSize;
    this.stepScale = scale;
    split(patchSize, heightMap);
}
 
Example 3
Source File: TextureUtil.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
     * <code>uploadTextureBitmap</code> uploads a native android bitmap
     * @param target
     * @param bitmap
     * @param generateMips
     * @param powerOf2
     */
    private static void uploadTextureBitmap2(final int target, Bitmap bitmap, boolean generateMips, boolean powerOf2)
    {
        if (bitmap.isRecycled()) {
            throw new RuntimeException("bitmap is recycled.");
        }
        if (!powerOf2)
        {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            if (!FastMath.isPowerOfTwo(width) || !FastMath.isPowerOfTwo(height)
                    /*|| width >= 512 || height >= 512*/)
            {
                // scale to power of two
                width = FastMath.nearestPowerOfTwo(width);
                height = FastMath.nearestPowerOfTwo(height);
//                while(width >= 512) {
//                    width = width / 2;
//                }
//                while(height >= 512) {
//                    height = height / 2;
//                }
            Logger.getLogger(TextureUtil.class.getName()).warning("texture size changed.");
                Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap, width, height, true);
                bitmap.recycle();
                bitmap = bitmap2;
            }
        }

        if (generateMips)
        {
            buildMipmap(bitmap);
        }
        else
        {
            GLUtils.texImage2D(target, 0, bitmap, 0);
//checkGLError();
//bitmap.recycle();
        }
    }
 
Example 4
Source File: MidpointDisplacementHeightMap.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * The constructor generates the heightmap. After the first 4 corners are
 * randomly given a height, the center will be heighted to the average of
 * the 4 corners to which a random value is added. Then other passes fill
 * the heightmap by the same principle.
 * The random value is generated between the values <code>-range</code>
 * and <code>range</code>. The <code>range</code> parameter is multiplied by
 * the <code>persistence</code> parameter each pass to smoothen close cell heights.
 * Extends this class and override the getOffset function for more control of
 * the randomness (you can use the coordinates and/or the computed average height
 * to influence the random amount added.
 *
 * @param size
 *          The size of the heightmap, must be 2^N+1
 * @param range
 *          The range in which randomness will be added. A value of 1 will
 *          allow -1 to 1 value changes.
 * @param persistence
 *          The factor by which the range will evolve at each iteration.
 *          A value of 0.5f will halve the range at each iteration and is
 *          typically a good choice
 * @param seed
 *          A seed to feed the random number generator.
 * @throws IllegalArgumentException if size is not a power of two plus one.
 */
public MidpointDisplacementHeightMap(int size, float range, float persistence, long seed) {
    if (size < 0 || !FastMath.isPowerOfTwo(size - 1)) {
        throw new IllegalArgumentException("The size is negative or not of the form 2^N +1"
                + " (a power of two plus one)");
    }
    this.size = size;
    this.range = range;
    this.persistence = persistence;
    this.seed = seed;
    load();
}
 
Example 5
Source File: MidpointDisplacementHeightMap.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * The constructor generates the heightmap. After the first 4 corners are
 * randomly given an height, the center will be heighted to the average of
 * the 4 corners to which a random value is added. Then other passes fill
 * the heightmap by the same principle.
 * The random value is generated between the values <code>-range</code>
 * and <code>range</code>. The <code>range</code> parameter is multiplied by
 * the <code>persistence</code> parameter each pass to smoothen close cell heights.
 * Extends this class and override the getOffset function for more control of
 * the randomness (you can use the coordinates and/or the computed average height
 * to influence the random amount added.
 *
 * @param size
 *          The size of the heightmap, must be 2^N+1
 * @param range
 *          The range in which randomness will be added. A value of 1 will
 *          allow -1 to 1 value changes.
 * @param persistence
 *          The factor by which the range will evolve at each iteration.
 *          A value of 0.5f will halve the range at each iteration and is
 *          typically a good choice
 * @param seed
 *          A seed to feed the random number generator.
 * @throw JMException if size is not a power of two plus one.
 */
public MidpointDisplacementHeightMap(int size, float range, float persistence, long seed) throws Exception {
    if (size < 0 || !FastMath.isPowerOfTwo(size - 1)) {
        throw new JMException("The size is negative or not of the form 2^N +1"
                + " (a power of two plus one)");
    }
    this.size = size;
    this.range = range;
    this.persistence = persistence;
    this.seed = seed;
    load();
}
 
Example 6
Source File: Image.java    From jmonkeybuilder with Apache License 2.0 2 votes vote down vote up
/**
 * Determine if the image is NPOT.
 *
 * @return if the image is a non-power-of-2 image, e.g. having dimensions
 * that are not powers of 2.
 */
public boolean isNPOT() {
    return width != 0 && height != 0
            && (!FastMath.isPowerOfTwo(width) || !FastMath.isPowerOfTwo(height));
}
 
Example 7
Source File: Image.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Determine if the image is NPOT.
 *
 * @return if the image is a non-power-of-2 image, e.g. having dimensions
 * that are not powers of 2.
 */
public boolean isNPOT() {
    return width != 0 && height != 0
            && (!FastMath.isPowerOfTwo(width) || !FastMath.isPowerOfTwo(height));
}