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.fractal;
31
32import com.jme3.terrain.noise.Basis;
33import com.jme3.terrain.noise.ShaderUtils;
34import com.jme3.terrain.noise.basis.ImprovedNoise;
35import com.jme3.terrain.noise.basis.Noise;
36
37/**
38 * FractalSum is the simplest form of fractal functions summing up a few octaves
39 * of the noise value with an ever decreasing (0 < roughness < 1) amplitude
40 *
41 * lacunarity = 2.0f is the classical octave distance
42 *
43 * Note: though noise basis functions are generally designed to return value
44 * between -1..1, there sum can easily be made to extend out of this range. To
45 * handle this is up to the user.
46 *
47 * @author Anthyon
48 *
49 */
50public class FractalSum extends Noise implements Fractal {
51
52	private Basis basis;
53	private float lacunarity;
54	private float amplitude;
55	private float roughness;
56	private float frequency;
57	private float octaves;
58	private int maxFreq;
59
60	public FractalSum() {
61		this.basis = new ImprovedNoise();
62		this.lacunarity = 2.124367f;
63		this.amplitude = 1.0f;
64		this.roughness = 0.6f;
65		this.frequency = 1f;
66		this.setOctaves(1);
67	}
68
69	@Override
70	public float value(final float x, final float y, final float z) {
71		float total = 0;
72
73		for (float f = this.frequency, a = this.amplitude; f < this.maxFreq; f *= this.lacunarity, a *= this.roughness) {
74			total += this.basis.value(this.scale * x * f, this.scale * y * f, this.scale * z * f) * a;
75		}
76
77		return ShaderUtils.clamp(total, -1, 1);
78	}
79
80	@Override
81	public Fractal addBasis(final Basis basis) {
82		this.basis = basis;
83		return this;
84	}
85
86	public float getOctaves() {
87		return this.octaves;
88	}
89
90	@Override
91	public Fractal setOctaves(final float octaves) {
92		this.octaves = octaves;
93		this.maxFreq = 1 << (int) octaves;
94		return this;
95	}
96
97	public float getFrequency() {
98		return this.frequency;
99	}
100
101	@Override
102	public Fractal setFrequency(final float frequency) {
103		this.frequency = frequency;
104		return this;
105	}
106
107	public float getRoughness() {
108		return this.roughness;
109	}
110
111	@Override
112	public Fractal setRoughness(final float roughness) {
113		this.roughness = roughness;
114		return this;
115	}
116
117	public float getAmplitude() {
118		return this.amplitude;
119	}
120
121	@Override
122	public Fractal setAmplitude(final float amplitude) {
123		this.amplitude = amplitude;
124		return this;
125	}
126
127	public float getLacunarity() {
128		return this.lacunarity;
129	}
130
131	@Override
132	public Fractal setLacunarity(final float lacunarity) {
133		this.lacunarity = lacunarity;
134		return this;
135	}
136
137	@Override
138	public void init() {
139
140	}
141
142}
143