summaryrefslogtreecommitdiff
path: root/tags/START/glagen/3d/perlin.cc
blob: 5cb4aee5292e4440c67b9c79883def0ef0614839 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//=============================================================================
//
// Glagen : a planet sized landscape generator
// Copyright (C) 2002  Julien Guertault, Hugues Hiegel, Meng-Tih Lam
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
//=============================================================================
//
// Glagen : GPL LAndscape GENerator
//
// perlin.cc for Glagen : made by Zavie (Julien Guertault)
//
// www.glagen.org
//
//=============================================================================

// Original implementation fo Perlin noice.
// It can be found on :
// http://mrl.nyu.edu/~perlin/doc/oscar.html#noise
//
// The 4th dimension noise has been added for our needs

// coherent noise function over 1, 2 or 3 dimensions
// (copyright Ken Perlin)

#include <stdlib.h>
#include <math.h>
#include "data_glagen.hh"

#define	B 0x100
#define	BM 0xff

#define	N 0x1000
#define	NP 12   /* 2^N */
#define	NM 0xfff

static int	p[B + B + 2];
static float	g4[B + B + 2][4];
static float	g3[B + B + 2][3];
static float	g2[B + B + 2][2];
static float	g1[B + B + 2];

#define s_curve(t) ( t * t * (3. - 2. * t) )

#define lerp(t, a, b) ( a + t * (b - a) )

#define setup(i,b0,b1,r0,r1)\
	t = vec[i] + N;\
	b0 = ((int)t) & BM;\
	b1 = (b0+1) & BM;\
	r0 = t - (int)t;\
	r1 = r0 - 1.;

float		GLG_Perlin_noise_1D (float arg)
{
  int		bx0, bx1;
  float	rx0, rx1;
  float	sx;
  float	t, u, v;
  float	vec[1];

  vec[0] = arg;

  setup(0, bx0,bx1, rx0,rx1);

  sx = s_curve(rx0);

  u = rx0 * g1[ p[ bx0 ] ];
  v = rx1 * g1[ p[ bx1 ] ];

  return lerp(sx, u, v);
}

float		GLG_Perlin_noise_2D (float vec[2])
{
  int		bx0, bx1;
  int		by0, by1;
  int		b00, b10, b01, b11;
  float		rx0, rx1;
  float		ry0, ry1;
  float		*q, sx, sy;
  float		a, b;
  float		t, u, v;
  register int	i, j;

  setup(0, bx0,bx1, rx0,rx1);
  setup(1, by0,by1, ry0,ry1);

  i = p[ bx0 ];
  j = p[ bx1 ];

  b00 = p[ i + by0 ];
  b10 = p[ j + by0 ];
  b01 = p[ i + by1 ];
  b11 = p[ j + by1 ];

  sx = s_curve(rx0);
  sy = s_curve(ry0);

#define at2(rx,ry) ( rx * q[0] + ry * q[1] )

  q = g2[ b00 ] ; u = at2(rx0,ry0);
  q = g2[ b10 ] ; v = at2(rx1,ry0);
  a = lerp(sx, u, v);

  q = g2[ b01 ] ; u = at2(rx0,ry1);
  q = g2[ b11 ] ; v = at2(rx1,ry1);
  b = lerp(sx, u, v);

  return lerp(sy, a, b);
}

float		GLG_Perlin_noise_3D (float vec[3])
{
  int		bx0, bx1;
  int		by0, by1;
  int		bz0, bz1;
  int		b00, b10, b01, b11;
  float		rx0, rx1;
  float		ry0, ry1;
  float		rz0, rz1;
  float		*q, sy, sz;
  float		a, b, c, d;
  float		t, u, v;
  register int	i, j;

  setup(0, bx0,bx1, rx0,rx1);
  setup(1, by0,by1, ry0,ry1);
  setup(2, bz0,bz1, rz0,rz1);

  i = p[ bx0 ];
  j = p[ bx1 ];

  b00 = p[ i + by0 ];
  b10 = p[ j + by0 ];
  b01 = p[ i + by1 ];
  b11 = p[ j + by1 ];

  t  = s_curve(rx0);
  sy = s_curve(ry0);
  sz = s_curve(rz0);

#define at3(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )

  q = g3[ b00 + bz0 ] ; u = at3(rx0,ry0,rz0);
  q = g3[ b10 + bz0 ] ; v = at3(rx1,ry0,rz0);
  a = lerp(t, u, v);

  q = g3[ b01 + bz0 ] ; u = at3(rx0,ry1,rz0);
  q = g3[ b11 + bz0 ] ; v = at3(rx1,ry1,rz0);
  b = lerp(t, u, v);

  c = lerp(sy, a, b);

  q = g3[ b00 + bz1 ] ; u = at3(rx0,ry0,rz1);
  q = g3[ b10 + bz1 ] ; v = at3(rx1,ry0,rz1);
  a = lerp(t, u, v);

  q = g3[ b01 + bz1 ] ; u = at3(rx0,ry1,rz1);
  q = g3[ b11 + bz1 ] ; v = at3(rx1,ry1,rz1);
  b = lerp(t, u, v);

  d = lerp(sy, a, b);

  return lerp(sz, c, d);
}

float		GLG_Perlin_noise_4D (float vec[4])
{
  int		bx0, bx1;
  int		by0, by1;
  int		bz0, bz1;
  int		bt0, bt1;
  register int	a0, a1;
  int		b00, b10, b01, b11;
  int		c000, c001, c010, c011, c100, c101, c110, c111;
  float		rx0, rx1;
  float		ry0, ry1;
  float		rz0, rz1;
  float		rt0, rt1;
  float		*q, sx, sy, sz, st;
  float		a, b, c, d, e, f;
  float		t, u, v;

  setup(0, bx0,bx1, rx0,rx1);
  setup(1, by0,by1, ry0,ry1);
  setup(2, bz0,bz1, rz0,rz1);
  setup(3, bt0,bt1, rt0,rt1);

  a0 = p[ bx0 ];
  a1 = p[ bx1 ];

  b00 = p[ a0 + by0 ];
  b10 = p[ a1 + by0 ];
  b01 = p[ a0 + by1 ];
  b11 = p[ a1 + by1 ];

  c000 = p[ b00 + bz0 ];
  c100 = p[ b10 + bz0 ];
  c010 = p[ b01 + bz0 ];
  c110 = p[ b11 + bz0 ];
  c001 = p[ b00 + bz1 ];
  c101 = p[ b10 + bz1 ];
  c011 = p[ b01 + bz1 ];
  c111 = p[ b11 + bz1 ];

  sx = s_curve(rx0);
  sy = s_curve(ry0);
  sz = s_curve(rz0);
  st = s_curve(rt0);

#define at4(rx, ry, rz, rt) ( rx * q[0] + ry * q[1] + rz * q[2] + rt * q[3])

  q = g4[c000 + bt0]; u = at4(rx0, ry0, rz0, rt0);
  q = g4[c100 + bt0]; v = at4(rx1, ry0, rz0, rt0);
  a = lerp(sx, u, v);

  q = g4[c010 + bt0]; u = at4(rx0, ry1, rz0, rt0);
  q = g4[c110 + bt0]; v = at4(rx1, ry1, rz0, rt0);
  b = lerp(sx, u, v);

  c = lerp (sy, a, b);

  q = g4[c001 + bt0]; u = at4(rx0, ry0, rz1, rt0);
  q = g4[c101 + bt0]; v = at4(rx1, ry0, rz1, rt0);
  a = lerp(sx, u, v);

  q = g4[c011 + bt0]; u = at4(rx0, ry1, rz1, rt0);
  q = g4[c111 + bt0]; v = at4(rx1, ry1, rz1, rt0);
  b = lerp(sx, u, v);

  d = lerp (sy, a, b);


  e = lerp (sz, c, d);


  q = g4[c000 + bt1]; u = at4(rx0, ry0, rz0, rt1);
  q = g4[c100 + bt1]; v = at4(rx1, ry0, rz0, rt1);
  a = lerp(sx, u, v);

  q = g4[c010 + bt1]; u = at4(rx0, ry1, rz0, rt1);
  q = g4[c110 + bt1]; v = at4(rx1, ry1, rz0, rt1);
  b = lerp(sx, u, v);

  c = lerp (sy, a, b);

  q = g4[c001 + bt1]; u = at4(rx0, ry0, rz1, rt1);
  q = g4[c101 + bt1]; v = at4(rx1, ry0, rz1, rt1);
  a = lerp(sx, u, v);

  q = g4[c011 + bt1]; u = at4(rx0, ry1, rz1, rt1);
  q = g4[c111 + bt1]; v = at4(rx1, ry1, rz1, rt1);
  b = lerp(sx, u, v);

  d = lerp (sy, a, b);


  f = lerp (sz, c, d);


  return lerp (st, e, f);
}

static void	normalize2 (float v[2])
{
  float		s;

  s = sqrtf(v[0] * v[0] + v[1] * v[1]);
  v[0] = v[0] / s;
  v[1] = v[1] / s;
}

static void	normalize3 (float v[3])
{
  float		s;

  s = sqrtf(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
  v[0] = v[0] / s;
  v[1] = v[1] / s;
  v[2] = v[2] / s;
}

static void	normalize4 (float v[4])
{
  float		s;

  s = sqrtf(v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3]);
  v[0] = v[0] / s;
  v[1] = v[1] / s;
  v[2] = v[2] / s;
  v[3] = v[3] / s;
}

void		Perlin_init ()
{
  int		i, j, k;

  for (i = 0 ; i < B ; i++)
    {
      p[i] = i;

      g1[i] = (float)((rand() % (B + B)) - B) / B;

      for (j = 0 ; j < 2 ; j++)
	g2[i][j] = (float)((rand() % (B + B)) - B) / B;
      normalize2(g2[i]);

      for (j = 0 ; j < 3 ; j++)
	g3[i][j] = (float)((rand() % (B + B)) - B) / B;
      normalize3(g3[i]);

      for (j = 0 ; j < 4 ; j++)
	g4[i][j] = (float)((rand() % (B + B)) - B) / B;
      normalize4(g4[i]);
    }

  while (--i)
    {
      k = p[i];
      p[i] = p[j = rand() % B];
      p[j] = k;
    }

  for (i = 0 ; i < B + 2 ; i++)
    {
      p[B + i] = p[i];
      g1[B + i] = g1[i];
      for (j = 0 ; j < 2 ; j++)
	g2[B + i][j] = g2[i][j];
      for (j = 0 ; j < 3 ; j++)
	g3[B + i][j] = g3[i][j];
    }
}