• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * $Id: noise.c 14611 2008-04-29 08:24:33Z campbellbarton $
4  *
5  * ***** BEGIN GPL LICENSE BLOCK *****
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  *
21  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
22  * All rights reserved.
23  *
24  * The Original Code is: all of this file.
25  *
26  * Contributor(s): none yet.
27  *
28  * ***** END GPL LICENSE BLOCK *****
29  *
30  */
31 package com.jme3.scene.plugins.blender.textures;
32 
33 import com.jme3.math.FastMath;
34 import com.jme3.scene.plugins.blender.BlenderContext;
35 import com.jme3.scene.plugins.blender.file.Structure;
36 import com.jme3.texture.Image;
37 import com.jme3.texture.Image.Format;
38 import com.jme3.texture.Texture;
39 import com.jme3.texture.Texture3D;
40 import com.jme3.util.BufferUtils;
41 import java.nio.ByteBuffer;
42 import java.util.ArrayList;
43 
44 /**
45  * This class generates the 'wood' texture.
46  * @author Marcin Roguski (Kaelthas)
47  */
48 public class TextureGeneratorWood extends TextureGenerator {
49 	// tex->noisebasis2
50     protected static final int TEX_SIN = 0;
51     protected static final int TEX_SAW = 1;
52     protected static final int TEX_TRI = 2;
53 
54     // tex->stype
55     protected static final int TEX_BAND = 0;
56     protected static final int TEX_RING = 1;
57     protected static final int TEX_BANDNOISE = 2;
58     protected static final int TEX_RINGNOISE = 3;
59 
60     // tex->noisetype
61     protected static final int TEX_NOISESOFT = 0;
62     protected static final int TEX_NOISEPERL = 1;
63 
64 	/**
65 	 * Constructor stores the given noise generator.
66 	 * @param noiseGenerator the noise generator
67 	 */
TextureGeneratorWood(NoiseGenerator noiseGenerator)68 	public TextureGeneratorWood(NoiseGenerator noiseGenerator) {
69 		super(noiseGenerator);
70 	}
71 
72 	@Override
generate(Structure tex, int width, int height, int depth, BlenderContext blenderContext)73 	protected Texture generate(Structure tex, int width, int height, int depth, BlenderContext blenderContext) {
74 		float[] texvec = new float[] { 0, 0, 0 };
75 		TexturePixel texres = new TexturePixel();
76 		int halfW = width >> 1;
77 		int halfH = height >> 1;
78 		int halfD = depth >> 1;
79 		float wDelta = 1.0f / halfW, hDelta = 1.0f / halfH, dDelta = 1.0f / halfD;
80 
81 		float[][] colorBand = this.computeColorband(tex, blenderContext);
82 		Format format = colorBand != null ? Format.RGBA8 : Format.Luminance8;
83 		int bytesPerPixel = colorBand != null ? 4 : 1;
84 		WoodIntensityData woodIntensityData = new WoodIntensityData(tex);
85 		BrightnessAndContrastData bacd = new BrightnessAndContrastData(tex);
86 
87 		int index = 0;
88 		byte[] data = new byte[width * height * depth * bytesPerPixel];
89 		for (int i = -halfW; i < halfW; ++i) {
90 			texvec[0] = wDelta * i;
91 			for (int j = -halfH; j < halfH; ++j) {
92 				texvec[1] = hDelta * j;
93 				for(int k = -halfD; k < halfD; ++k) {
94 					texvec[2] = dDelta * k;
95 					texres.intensity = this.woodIntensity(woodIntensityData, texvec[0], texvec[1], texvec[2]);
96 
97 					if (colorBand != null) {
98 						int colorbandIndex = (int) (texres.intensity * 1000.0f);
99 						texres.red = colorBand[colorbandIndex][0];
100 						texres.green = colorBand[colorbandIndex][1];
101 						texres.blue = colorBand[colorbandIndex][2];
102 
103 						this.applyBrightnessAndContrast(bacd, texres);
104 
105 						data[index++] = (byte) (texres.red * 255.0f);
106 						data[index++] = (byte) (texres.green * 255.0f);
107 						data[index++] = (byte) (texres.blue * 255.0f);
108 						data[index++] = (byte) (colorBand[colorbandIndex][3] * 255.0f);
109 					} else {
110 						this.applyBrightnessAndContrast(texres, bacd.contrast, bacd.brightness);
111 						data[index++] = (byte) (texres.intensity * 255.0f);
112 					}
113 				}
114 			}
115 		}
116 
117 		ArrayList<ByteBuffer> dataArray = new ArrayList<ByteBuffer>(1);
118 		dataArray.add(BufferUtils.createByteBuffer(data));
119 		return new Texture3D(new Image(format, width, height, depth, dataArray));
120 	}
121 
122     protected static WaveForm[] waveformFunctions = new WaveForm[3];
123     static {
124         waveformFunctions[0] = new WaveForm() {// sinus (TEX_SIN)
125 
126             @Override
127             public float execute(float x) {
128                 return 0.5f + 0.5f * (float) Math.sin(x);
129             }
130         };
131         waveformFunctions[1] = new WaveForm() {// saw (TEX_SAW)
132 
133             @Override
134             public float execute(float x) {
135                 int n = (int) (x * FastMath.INV_TWO_PI);
136                 x -= n * FastMath.TWO_PI;
137                 if (x < 0.0f) {
138                     x += FastMath.TWO_PI;
139                 }
140                 return x * FastMath.INV_TWO_PI;
141             }
142         };
143         waveformFunctions[2] = new WaveForm() {// triangle (TEX_TRI)
144 
145             @Override
146             public float execute(float x) {
147                 return 1.0f - 2.0f * FastMath.abs((float) Math.floor(x * FastMath.INV_TWO_PI + 0.5f) - x * FastMath.INV_TWO_PI);
148             }
149         };
150     }
151 
152     /**
153      * Computes basic wood intensity value at x,y,z.
154      * @param woodIntData
155      * @param x X coordinate of the texture pixel
156      * @param y Y coordinate of the texture pixel
157      * @param z Z coordinate of the texture pixel
158      * @return wood intensity at position [x, y, z]
159      */
woodIntensity(WoodIntensityData woodIntData, float x, float y, float z)160     public float woodIntensity(WoodIntensityData woodIntData, float x, float y, float z) {
161         float result;
162 
163         switch(woodIntData.woodType) {
164 	        case TEX_BAND:
165 	        	result = woodIntData.waveformFunction.execute((x + y + z) * 10.0f);
166 	        	break;
167 	        case TEX_RING:
168 	        	result = woodIntData.waveformFunction.execute((float) Math.sqrt(x * x + y * y + z * z) * 20.0f);
169 	        	break;
170 	        case TEX_BANDNOISE:
171 	        	result = woodIntData.turbul * NoiseGenerator.NoiseFunctions.noise(x, y, z, woodIntData.noisesize, 0, woodIntData.noisebasis, woodIntData.isHard);
172 	            result = woodIntData.waveformFunction.execute((x + y + z) * 10.0f + result);
173 	        	break;
174 	        case TEX_RINGNOISE:
175 	        	result = woodIntData.turbul * NoiseGenerator.NoiseFunctions.noise(x, y, z, woodIntData.noisesize, 0, woodIntData.noisebasis, woodIntData.isHard);
176 	            result = woodIntData.waveformFunction.execute((float) Math.sqrt(x * x + y * y + z * z) * 20.0f + result);
177 	        	break;
178         	default:
179         		result = 0;
180         }
181         return result;
182     }
183 
184     /**
185      * A class that collects the data for wood intensity calculations.
186      * @author Marcin Roguski (Kaelthas)
187      */
188     private static class WoodIntensityData {
189     	public final WaveForm waveformFunction;
190     	public final int noisebasis;
191     	public final float noisesize;
192     	public final float turbul;
193     	public final int noiseType;
194     	public final int woodType;
195     	public final boolean isHard;
196 
WoodIntensityData(Structure tex)197     	public WoodIntensityData(Structure tex) {
198     		int waveform = ((Number) tex.getFieldValue("noisebasis2")).intValue();//wave form: TEX_SIN=0, TEX_SAW=1, TEX_TRI=2
199     		if (waveform > TEX_TRI || waveform < TEX_SIN) {
200                 waveform = 0; // check to be sure noisebasis2 is initialized ahead of time
201             }
202     		waveformFunction = waveformFunctions[waveform];
203     		noisebasis = ((Number) tex.getFieldValue("noisebasis")).intValue();
204             woodType = ((Number) tex.getFieldValue("stype")).intValue();
205             noisesize = ((Number) tex.getFieldValue("noisesize")).floatValue();
206             turbul = ((Number) tex.getFieldValue("turbul")).floatValue();
207             noiseType = ((Number) tex.getFieldValue("noisetype")).intValue();
208             isHard = noiseType != TEX_NOISESOFT;
209 		}
210     }
211 
212     protected static interface WaveForm {
213 
execute(float x)214         float execute(float x);
215     }
216 }
217