1/**
2 * Copyright (c) 2011, Novyon Events
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * - Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 *
12 * - Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * @author Anthyon
29 */
30package com.jme3.terrain.noise;
31
32import java.awt.Color;
33import java.awt.Graphics2D;
34import java.awt.image.BufferedImage;
35import java.awt.image.DataBuffer;
36import java.awt.image.DataBufferInt;
37import java.awt.image.WritableRaster;
38import java.nio.ByteBuffer;
39import java.nio.ByteOrder;
40
41/**
42 * Helper class containing useful functions explained in the book:
43 * Texturing & Modeling - A Procedural Approach
44 *
45 * @author Anthyon
46 *
47 */
48public class ShaderUtils {
49
50	public static final float[] i2c(final int color) {
51		return new float[] { (color & 0x00ff0000) / 256f, (color & 0x0000ff00) / 256f, (color & 0x000000ff) / 256f,
52				(color & 0xff000000) / 256f };
53	}
54
55	public static final int c2i(final float[] color) {
56		return (color.length == 4 ? (int) (color[3] * 256) : 0xff000000) | ((int) (color[0] * 256) << 16) | ((int) (color[1] * 256) << 8)
57				| (int) (color[2] * 256);
58	}
59
60	public static final float mix(final float a, final float b, final float f) {
61		return (1 - f) * a + f * b;
62	}
63
64	public static final Color mix(final Color a, final Color b, final float f) {
65		return new Color((int) ShaderUtils.clamp(ShaderUtils.mix(a.getRed(), b.getRed(), f), 0, 255), (int) ShaderUtils.clamp(
66				ShaderUtils.mix(a.getGreen(), b.getGreen(), f), 0, 255), (int) ShaderUtils.clamp(
67				ShaderUtils.mix(a.getBlue(), b.getBlue(), f), 0, 255));
68	}
69
70	public static final int mix(final int a, final int b, final float f) {
71		return (int) ((1 - f) * a + f * b);
72	}
73
74	public static final float[] mix(final float[] c1, final float[] c2, final float f) {
75		return new float[] { ShaderUtils.mix(c1[0], c2[0], f), ShaderUtils.mix(c1[1], c2[1], f), ShaderUtils.mix(c1[2], c2[2], f) };
76	}
77
78	public static final float step(final float a, final float x) {
79		return x < a ? 0 : 1;
80	}
81
82	public static final float boxstep(final float a, final float b, final float x) {
83		return ShaderUtils.clamp((x - a) / (b - a), 0, 1);
84	}
85
86	public static final float pulse(final float a, final float b, final float x) {
87		return ShaderUtils.step(a, x) - ShaderUtils.step(b, x);
88	}
89
90	public static final float clamp(final float x, final float a, final float b) {
91		return x < a ? a : x > b ? b : x;
92	}
93
94	public static final float min(final float a, final float b) {
95		return a < b ? a : b;
96	}
97
98	public static final float max(final float a, final float b) {
99		return a > b ? a : b;
100	}
101
102	public static final float abs(final float x) {
103		return x < 0 ? -x : x;
104	}
105
106	public static final float smoothstep(final float a, final float b, final float x) {
107		if (x < a) {
108			return 0;
109		} else if (x > b) {
110			return 1;
111		}
112		float xx = (x - a) / (b - a);
113		return xx * xx * (3 - 2 * xx);
114	}
115
116	public static final float mod(final float a, final float b) {
117		int n = (int) (a / b);
118		float aa = a - n * b;
119		if (aa < 0) {
120			aa += b;
121		}
122		return aa;
123	}
124
125	public static final int floor(final float x) {
126		return x > 0 ? (int) x : (int) x - 1;
127	}
128
129	public static final float ceil(final float x) {
130		return (int) x + (x > 0 && x != (int) x ? 1 : 0);
131	}
132
133	public static final float spline(float x, final float[] knot) {
134		float CR00 = -0.5f;
135		float CR01 = 1.5f;
136		float CR02 = -1.5f;
137		float CR03 = 0.5f;
138		float CR10 = 1.0f;
139		float CR11 = -2.5f;
140		float CR12 = 2.0f;
141		float CR13 = -0.5f;
142		float CR20 = -0.5f;
143		float CR21 = 0.0f;
144		float CR22 = 0.5f;
145		float CR23 = 0.0f;
146		float CR30 = 0.0f;
147		float CR31 = 1.0f;
148		float CR32 = 0.0f;
149		float CR33 = 0.0f;
150
151		int span;
152		int nspans = knot.length - 3;
153		float c0, c1, c2, c3; /* coefficients of the cubic. */
154		if (nspans < 1) {/* illegal */
155			throw new RuntimeException("Spline has too few knots.");
156		}
157		/* Find the appropriate 4-point span of the spline. */
158		x = ShaderUtils.clamp(x, 0, 1) * nspans;
159		span = (int) x;
160		if (span >= knot.length - 3) {
161			span = knot.length - 3;
162		}
163		x -= span;
164		/* Evaluate the span cubic at x using Horner’s rule. */
165		c3 = CR00 * knot[span + 0] + CR01 * knot[span + 1] + CR02 * knot[span + 2] + CR03 * knot[span + 3];
166		c2 = CR10 * knot[span + 0] + CR11 * knot[span + 1] + CR12 * knot[span + 2] + CR13 * knot[span + 3];
167		c1 = CR20 * knot[span + 0] + CR21 * knot[span + 1] + CR22 * knot[span + 2] + CR23 * knot[span + 3];
168		c0 = CR30 * knot[span + 0] + CR31 * knot[span + 1] + CR32 * knot[span + 2] + CR33 * knot[span + 3];
169		return ((c3 * x + c2) * x + c1) * x + c0;
170	}
171
172	public static final float[] spline(final float x, final float[][] knots) {
173		float[] retval = new float[knots.length];
174		for (int i = 0; i < knots.length; i++) {
175			retval[i] = ShaderUtils.spline(x, knots[i]);
176		}
177		return retval;
178	}
179
180	public static final float gammaCorrection(final float gamma, final float x) {
181		return (float) Math.pow(x, 1 / gamma);
182	}
183
184	public static final float bias(final float b, final float x) {
185		return (float) Math.pow(x, Math.log(b) / Math.log(0.5));
186	}
187
188	public static final float gain(final float g, final float x) {
189		return x < 0.5 ? ShaderUtils.bias(1 - g, 2 * x) / 2 : 1 - ShaderUtils.bias(1 - g, 2 - 2 * x) / 2;
190	}
191
192	public static final float sinValue(final float s, final float minFreq, final float maxFreq, final float swidth) {
193		float value = 0;
194		float cutoff = ShaderUtils.clamp(0.5f / swidth, 0, maxFreq);
195		float f;
196		for (f = minFreq; f < 0.5 * cutoff; f *= 2) {
197			value += Math.sin(2 * Math.PI * f * s) / f;
198		}
199		float fade = ShaderUtils.clamp(2 * (cutoff - f) / cutoff, 0, 1);
200		value += fade * Math.sin(2 * Math.PI * f * s) / f;
201		return value;
202	}
203
204	public static final float length(final float x, final float y, final float z) {
205		return (float) Math.sqrt(x * x + y * y + z * z);
206	}
207
208	public static final float[] rotate(final float[] v, final float[][] m) {
209		float x = v[0] * m[0][0] + v[1] * m[0][1] + v[2] * m[0][2];
210		float y = v[0] * m[1][0] + v[1] * m[1][1] + v[2] * m[1][2];
211		float z = v[0] * m[2][0] + v[1] * m[2][1] + v[2] * m[2][2];
212		return new float[] { x, y, z };
213	}
214
215	public static final float[][] calcRotationMatrix(final float ax, final float ay, final float az) {
216		float[][] retval = new float[3][3];
217		float cax = (float) Math.cos(ax);
218		float sax = (float) Math.sin(ax);
219		float cay = (float) Math.cos(ay);
220		float say = (float) Math.sin(ay);
221		float caz = (float) Math.cos(az);
222		float saz = (float) Math.sin(az);
223
224		retval[0][0] = cay * caz;
225		retval[0][1] = -cay * saz;
226		retval[0][2] = say;
227		retval[1][0] = sax * say * caz + cax * saz;
228		retval[1][1] = -sax * say * saz + cax * caz;
229		retval[1][2] = -sax * cay;
230		retval[2][0] = -cax * say * caz + sax * saz;
231		retval[2][1] = cax * say * saz + sax * caz;
232		retval[2][2] = cax * cay;
233
234		return retval;
235	}
236
237	public static final float[] normalize(final float[] v) {
238		float l = ShaderUtils.length(v);
239		float[] r = new float[v.length];
240		int i = 0;
241		for (float vv : v) {
242			r[i++] = vv / l;
243		}
244		return r;
245	}
246
247	public static final float length(final float[] v) {
248		float s = 0;
249		for (float vv : v) {
250			s += vv * vv;
251		}
252		return (float) Math.sqrt(s);
253	}
254
255	public static final ByteBuffer getImageDataFromImage(BufferedImage bufferedImage) {
256		WritableRaster wr;
257		DataBuffer db;
258
259		BufferedImage bi = new BufferedImage(128, 64, BufferedImage.TYPE_INT_ARGB);
260		Graphics2D g = bi.createGraphics();
261		g.drawImage(bufferedImage, null, null);
262		bufferedImage = bi;
263		wr = bi.getRaster();
264		db = wr.getDataBuffer();
265
266		DataBufferInt dbi = (DataBufferInt) db;
267		int[] data = dbi.getData();
268
269		ByteBuffer byteBuffer = ByteBuffer.allocateDirect(data.length * 4);
270		byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
271		byteBuffer.asIntBuffer().put(data);
272		byteBuffer.flip();
273
274		return byteBuffer;
275	}
276
277	public static float frac(float f) {
278		return f - ShaderUtils.floor(f);
279	}
280
281	public static float[] floor(float[] fs) {
282		float[] retval = new float[fs.length];
283		for (int i = 0; i < fs.length; i++) {
284			retval[i] = ShaderUtils.floor(fs[i]);
285		}
286		return retval;
287	}
288}
289