naev 0.12.5
perlin.c
Go to the documentation of this file.
1/*
2 * libtcod 1.3.1
3 * Copyright (c) 2007,2008 J.C.Wilk
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 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * The name of J.C.Wilk may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY J.C.WILK ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL J.C.WILK BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
42
44#include <stdlib.h>
46
47#include "perlin.h"
48
49#include "rng.h"
50
51#define SIMPLEX_SCALE 0.5f
52
57 int ndim;
58 unsigned char map[256];
59};
60
61#define SWAP( a, b, t ) \
62 ( t ) = ( a ); \
63 ( a ) = ( b ); \
64 ( b ) = ( t )
65#define FLOOR( a ) \
66 ( (int)( a ) - ( ( a ) < 0 && ( a ) != (int)( a ) ) )
67
71perlin_data_t *noise_new( void )
72{
73 perlin_data_t *pdata;
74 int i, j;
75 unsigned char tmp;
76
77 /* Create the data. */
78 pdata = calloc( 1, sizeof( perlin_data_t ) );
79
80 /* Create the buffer and map. */
81 for ( i = 0; i < 256; i++ )
82 pdata->map[i] = (unsigned char)i;
83
84 while ( --i ) {
85 j = RNG( 0, 255 );
86 SWAP( pdata->map[i], pdata->map[j], tmp );
87 }
88
89 return pdata;
90}
91
92#define NOISE_SIMPLEX_GRADIENT_1D( n, h, x ) \
93 { \
94 float grad; \
95 h &= 0xF; \
96 grad = 1.0f + ( h & 7 ); \
97 if ( h & 8 ) \
98 grad = -grad; \
99 n = grad * x; \
100 }
101
108float noise_simplex1( perlin_data_t *pdata, float f[1] )
109{
110 int i0 = (int)FLOOR( f[0] * SIMPLEX_SCALE );
111 int i1 = i0 + 1;
112 float x0 = f[0] * SIMPLEX_SCALE - i0;
113 float x1 = x0 - 1.0f;
114 float t0 = 1.0f - x0 * x0;
115 float t1 = 1.0f - x1 * x1;
116 float n0, n1;
117
118 t0 = t0 * t0;
119 t1 = t1 * t1;
120 i0 = pdata->map[i0 & 0xFF];
121 NOISE_SIMPLEX_GRADIENT_1D( n0, i0, x0 );
122 n0 *= t0 * t0;
123 i1 = pdata->map[i1 & 0xFF];
124 NOISE_SIMPLEX_GRADIENT_1D( n1, i1, x1 );
125 n1 *= t1 * t1;
126
127 return 0.25f * ( n0 + n1 );
128}
129
135void noise_delete( perlin_data_t *pdata )
136{
137 free( pdata );
138}
perlin_data_t * noise_new(void)
Creates a new perlin noise generator.
Definition perlin.c:71
#define FLOOR(a)
Definition perlin.c:65
void noise_delete(perlin_data_t *pdata)
Frees some noise data.
Definition perlin.c:135
#define SWAP(a, b, t)
Definition perlin.c:61
float noise_simplex1(perlin_data_t *pdata, float f[1])
Gets 1D simplex noise for a position.
Definition perlin.c:108
Structure used for generating noise.
Definition perlin.c:56
unsigned char map[256]
Definition perlin.c:58