ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/noise.h
(Generate patch)

Comparing deliantra/server/include/noise.h (file contents):
Revision 1.2 by root, Sun Aug 22 20:11:13 2010 UTC vs.
Revision 1.3 by root, Fri Apr 22 02:03:11 2011 UTC

23#ifndef NOISE_H_ 23#ifndef NOISE_H_
24#define NOISE_H_ 24#define NOISE_H_
25 25
26#include "global.h" 26#include "global.h"
27 27
28template<int N, typename T = float>
29struct gen_vec_base
30{
31 enum { D = N };
32 T v[N];
33
34 T &operator [](int n)
35 {
36 return v[n];
37 }
38
39 template<typename U>
40 const gen_vec_base<N,T> &operator =(gen_vec_base<N,U> v)
41 {
42 for (int i = 0; i < N; ++i)
43 this->v[i] = v.v[i];
44
45 return *this;
46 }
47};
48
49template<int N, typename T>
50gen_vec_base<N,T> floor (gen_vec_base<N,T> v)
51{
52 for (int i = 0; i < N; ++i)
53 v[i] = floor (v[i]);
54
55 return v;
56}
57
58template<int N, typename T, typename U>
59gen_vec_base<N,T> operator +(gen_vec_base<N,T> a, gen_vec_base<N,U> b)
60{
61 gen_vec_base<N,T> c;
62
63 for (int i = 0; i < N; ++i)
64 c[i] = a[i] + b[i];
65
66 return c;
67}
68
69template<int N, typename T, typename U>
70gen_vec_base<N,T> operator -(gen_vec_base<N,T> a, gen_vec_base<N,U> b)
71{
72 gen_vec_base<N,T> c;
73
74 for (int i = 0; i < N; ++i)
75 c[i] = a[i] - b[i];
76
77 return c;
78}
79
80template<int N, typename T, typename U>
81T operator *(gen_vec_base<N,T> a, gen_vec_base<N,U> b)
82{
83 T v = 0;
84
85 for (int i = 0; i < N; ++i)
86 v += a[i] * b[i];
87
88 return v;
89}
90
91template<int N, typename T = float>
92struct gen_vec;
93
94template<typename T>
95struct gen_vec<2,T> : gen_vec_base<2,T>
96{
97 using gen_vec_base<2,T>::operator =;
98
99 gen_vec ()
100 {
101 }
102
103 gen_vec (T x, T y)
104 {
105 this->v[0] = x;
106 this->v[1] = y;
107 }
108
109 template<typename U>
110 gen_vec (gen_vec_base<2,U> v)
111 {
112 *this = v;
113 }
114};
115
116template<typename T>
117struct gen_vec<3,T> : gen_vec_base<3,T>
118{
119 gen_vec ()
120 {
121 }
122
123 gen_vec (T x, T y, T z)
124 {
125 this->v[0] = x;
126 this->v[1] = y;
127 this->v[2] = z;
128 }
129
130 template<typename U>
131 gen_vec (gen_vec_base<3,U> v)
132 {
133 *this = v;
134 }
135};
136
137typedef gen_vec<2> vec2d;
138typedef gen_vec<3> vec3d;
139
140template<int N, typename T = float>
141struct space
142{
143 enum { D = N };
144 typedef gen_vec<N,T> vec;
145};
146
147typedef space<2> space2d;
148typedef space<3> space3d;
149
150/////////////////////////////////////////////////////////////////////////////
151
28template<int N, typename T = uint8_t> 152template<int N, typename T = uint8_t>
29struct permutation 153struct permutation
30{ 154{
31 T pmap [N]; 155 T pmap [N];
32 156
45 { 169 {
46 return pmap [v & (N - 1)]; 170 return pmap [v & (N - 1)];
47 } 171 }
48}; 172};
49 173
174// modelled after 2d/3d kensler noise without projection
50struct noise_gen 175struct noise2d
51{ 176{
52 permutation<256, uint8_t> rvmap[2]; 177 permutation<256, uint8_t> rvmap[2];
53 float rvec [256][2]; // random unit vectors 178 float rvec [256][2]; // random unit vectors
54 179
55 noise_gen (uint32_t seed); 180 noise2d (uint32_t seed);
56 float noise (float x, float y); 181 float noise (float x, float y);
182};
183
184struct noise3d
185{
186 permutation<256, uint8_t> rvmap[3];
187 float rvec [256][3]; // random unit vectors
188
189 noise3d (uint32_t seed);
190 float noise (float x, float y, float z);
191
192 // noise projected on a surface with normal n
193 float noise (float x, float y, float z, float nx, float ny, float nz);
57}; 194};
58 195
59struct rotate 196struct rotate
60{ 197{
61 float s, c; 198 float s, c;
75 y = yr; 212 y = yr;
76 } 213 }
77}; 214};
78 215
79struct frac_gen 216struct frac_gen
80: noise_gen 217: noise2d
81{ 218{
82 enum { MAX_OCTAVES = 64 }; 219 enum { MAX_OCTAVES = 64 };
83 220
84 float h, lac, ex [MAX_OCTAVES]; 221 float h, lac, ex [MAX_OCTAVES];
85 float fbm_mul [MAX_OCTAVES]; 222 float fbm_mul [MAX_OCTAVES];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines