/* * Copyright (c) 2009-2010 jMonkeyEngine * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name of 'jMonkeyEngine' nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.jme3.scene.plugins.blender.textures; import com.jme3.math.FastMath; import com.jme3.scene.plugins.blender.BlenderContext; import com.jme3.scene.plugins.blender.exceptions.BlenderFileException; import com.jme3.scene.plugins.blender.file.DynamicArray; import com.jme3.scene.plugins.blender.file.Pointer; import com.jme3.scene.plugins.blender.file.Structure; import com.jme3.texture.Texture; import java.util.Map; import java.util.TreeMap; import java.util.logging.Level; import java.util.logging.Logger; /** * This class is a base class for texture generators. * @author Marcin Roguski (Kaelthas) */ /* package */abstract class TextureGenerator { private static final Logger LOGGER = Logger.getLogger(TextureGenerator.class.getName()); protected NoiseGenerator noiseGenerator; public TextureGenerator(NoiseGenerator noiseGenerator) { this.noiseGenerator = noiseGenerator; } /** * This method generates the texture. * @param tex * texture's structure * @param width * the width of the result texture * @param height * the height of the result texture * @param depth * the depth of the texture * @param blenderContext * the blender context * @return newly generated texture */ protected abstract Texture generate(Structure tex, int width, int height, int depth, BlenderContext blenderContext); /** * This method reads the colorband data from the given texture structure. * * @param tex * the texture structure * @param blenderContext * the blender context * @return read colorband or null if not present */ private ColorBand readColorband(Structure tex, BlenderContext blenderContext) { ColorBand result = null; int flag = ((Number) tex.getFieldValue("flag")).intValue(); if ((flag & NoiseGenerator.TEX_COLORBAND) != 0) { Pointer pColorband = (Pointer) tex.getFieldValue("coba"); Structure colorbandStructure; try { colorbandStructure = pColorband.fetchData(blenderContext.getInputStream()).get(0); result = new ColorBand(colorbandStructure); } catch (BlenderFileException e) { LOGGER.log(Level.WARNING, "Cannot fetch the colorband structure. The reason: {0}", e.getLocalizedMessage()); } } return result; } protected float[][] computeColorband(Structure tex, BlenderContext blenderContext) { ColorBand colorBand = this.readColorband(tex, blenderContext); float[][] result = null; if(colorBand!=null) { result = new float[1001][4];//1001 - amount of possible cursor positions; 4 = [r, g, b, a] ColorBandData[] dataArray = colorBand.data; if(dataArray.length==1) {//special case; use only one color for all types of colorband interpolation for(int i=0;i cbDataMap = new TreeMap(); for(int i=0;i 1.0f) { texres.intensity = 1.0f; } } /** * A class constaining the colorband data. * * @author Marcin Roguski (Kaelthas) */ protected static class ColorBand { //interpolation types public static final int IPO_LINEAR = 0; public static final int IPO_EASE = 1; public static final int IPO_BSPLINE = 2; public static final int IPO_CARDINAL = 3; public static final int IPO_CONSTANT = 4; public int cursorsAmount, ipoType; public ColorBandData[] data; /** * Constructor. Loads the data from the given structure. * * @param cbdataStructure * the colorband structure */ @SuppressWarnings("unchecked") public ColorBand(Structure colorbandStructure) { this.cursorsAmount = ((Number) colorbandStructure.getFieldValue("tot")).intValue(); this.ipoType = ((Number) colorbandStructure.getFieldValue("ipotype")).intValue(); this.data = new ColorBandData[this.cursorsAmount]; DynamicArray data = (DynamicArray) colorbandStructure.getFieldValue("data"); for (int i = 0; i < this.cursorsAmount; ++i) { this.data[i] = new ColorBandData(data.get(i)); } } } /** * Class to store the single colorband cursor data. * * @author Marcin Roguski (Kaelthas) */ protected static class ColorBandData implements Cloneable { public final float r, g, b, a; public int pos; /** * Copy constructor. */ private ColorBandData(ColorBandData data) { this.r = data.r; this.g = data.g; this.b = data.b; this.a = data.a; this.pos = data.pos; } /** * Constructor. Loads the data from the given structure. * * @param cbdataStructure * the structure containing the CBData object */ public ColorBandData(Structure cbdataStructure) { this.r = ((Number) cbdataStructure.getFieldValue("r")).floatValue(); this.g = ((Number) cbdataStructure.getFieldValue("g")).floatValue(); this.b = ((Number) cbdataStructure.getFieldValue("b")).floatValue(); this.a = ((Number) cbdataStructure.getFieldValue("a")).floatValue(); this.pos = (int) (((Number) cbdataStructure.getFieldValue("pos")).floatValue() * 1000.0f); } @Override public ColorBandData clone() { try { return (ColorBandData) super.clone(); } catch (CloneNotSupportedException e) { return new ColorBandData(this); } } @Override public String toString() { return "P: " + this.pos + " [" + this.r+", "+this.g+", "+this.b+", "+this.a+"]"; } } /** * This class contains brightness and contrast data. * @author Marcin Roguski (Kaelthas) */ protected static class BrightnessAndContrastData { public final float contrast; public final float brightness; public final float rFactor; public final float gFactor; public final float bFactor; /** * Constructor reads the required data from the given structure. * @param tex texture structure */ public BrightnessAndContrastData(Structure tex) { contrast = ((Number) tex.getFieldValue("contrast")).floatValue(); brightness = ((Number) tex.getFieldValue("bright")).floatValue() - 0.5f; rFactor = ((Number) tex.getFieldValue("rfac")).floatValue(); gFactor = ((Number) tex.getFieldValue("gfac")).floatValue(); bFactor = ((Number) tex.getFieldValue("bfac")).floatValue(); } } }