naev 0.11.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
45#include <math.h>
46#include <stdlib.h>
47#include "SDL.h"
48#include "SDL_thread.h"
49
50#include "naev.h"
53#include "perlin.h"
54
55#include "log.h"
56#include "nfile.h"
57#include "nstring.h"
58#include "rng.h"
59
60#define SIMPLEX_SCALE 0.5f
61
66 int ndim;
67 unsigned char map[256];
68};
69
70#define SWAP(a, b, t) (t) = (a); (a) = (b); (b) = (t)
71#define FLOOR(a) ((int)(a) - ((a) < 0 && (a) != (int)(a)))
76perlin_data_t* noise_new (void)
77{
78 perlin_data_t *pdata;
79 int i, j;
80 unsigned char tmp;
81
82 /* Create the data. */
83 pdata = calloc(sizeof(perlin_data_t),1);
84
85 /* Create the buffer and map. */
86 for (i=0; i<256; i++)
87 pdata->map[i] = (unsigned char)i;
88
89 while (--i) {
90 j = RNG(0, 255);
91 SWAP(pdata->map[i], pdata->map[j], tmp);
92 }
93
94 return pdata;
95}
96
97#define NOISE_SIMPLEX_GRADIENT_1D(n,h,x) { float grad; h &= 0xF; grad=1.0f+(h & 7); if ( h & 8 ) grad = -grad; n = grad * x; }
98
105float noise_simplex1( perlin_data_t* pdata, float f[1] )
106{
107 int i0 = (int)FLOOR( f[0]*SIMPLEX_SCALE );
108 int i1 = i0+1;
109 float x0 = f[0]*SIMPLEX_SCALE - i0;
110 float x1 = x0 - 1.0f;
111 float t0 = 1.0f - x0*x0;
112 float t1 = 1.0f - x1*x1;
113 float n0,n1;
114
115 t0 = t0*t0;
116 t1 = t1*t1;
117 i0 = pdata->map[i0&0xFF];
118 NOISE_SIMPLEX_GRADIENT_1D( n0, i0, x0 );
119 n0 *= t0*t0;
120 i1 = pdata->map[i1&0xFF];
121 NOISE_SIMPLEX_GRADIENT_1D( n1, i1, x1 );
122 n1 *= t1*t1;
123
124 return 0.25f * (n0+n1);
125}
126
132void noise_delete( perlin_data_t* pdata )
133{
134 free(pdata);
135}
Header file with generic functions and naev-specifics.
perlin_data_t * noise_new(void)
Creates a new perlin noise generator.
Definition perlin.c:76
#define FLOOR(a)
Definition perlin.c:71
void noise_delete(perlin_data_t *pdata)
Frees some noise data.
Definition perlin.c:132
#define SWAP(a, b, t)
Definition perlin.c:70
float noise_simplex1(perlin_data_t *pdata, float f[1])
Gets 1D simplex noise for a position.
Definition perlin.c:105
Structure used for generating noise.
Definition perlin.c:65
unsigned char map[256]
Definition perlin.c:67