1 /* === S Y N F I G ========================================================= */
2 /*! \file mod_particle/random.cpp
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
38 /* === M A C R O S ========================================================= */
40 /* === G L O B A L S ======================================================= */
42 /* === P R O C E D U R E S ================================================= */
44 /* === M E T H O D S ======================================================= */
47 Random::set_seed(int x)
52 for(i=0;i<POOL_SIZE;i++)
55 x_mask=rand()+rand()*RAND_MAX;
56 y_mask=rand()+rand()*RAND_MAX;
57 t_mask=rand()+rand()*RAND_MAX;
61 Random::operator()(const int salt,const int x,const int y,const int t)const
63 const int salt_hash(pool_[salt&(POOL_SIZE-1)]);
65 int index(((x^x_mask)+(y^y_mask)*234672+(t^t_mask)*8439573)^salt_hash);
67 index+=index*(index/POOL_SIZE);
69 return (float(pool_[index&(POOL_SIZE-1)])/float(RAND_MAX))*2.0f-1.0f;
73 Random::operator()(int smooth,int subseed,float xf,float yf,float tf)const
75 int x((int)floor(xf));
76 int y((int)floor(yf));
77 int t((int)floor(tf));
83 #define f(j,i,k) ((*this)(subseed,i,j,k))
84 //Using catmull rom interpolation because it doesn't blur at all
85 //bezier curve with intermediate ctrl pts: 0.5/3(p(i+1) - p(i-1)) and similar
86 float xfa [4], tfa[4];
88 //precalculate indices (all clamped) and offset
89 const int xa[] = {x-1,x,x+1,x+2};
91 const int ya[] = {y-1,y,y+1,y+2};
93 const int ta[] = {t-1,t,t+1,t+2};
99 //figure polynomials for each point
102 0.5*dx*(dx*(dx*(-1) + 2) - 1), //-t + 2t^2 -t^3
103 0.5*(dx*(dx*(3*dx - 5)) + 2), //2 - 5t^2 + 3t^3
104 0.5*dx*(dx*(-3*dx + 4) + 1), //t + 4t^2 - 3t^3
105 0.5*dx*dx*(dx-1) //-t^2 + t^3
110 0.5*dy*(dy*(dy*(-1) + 2) - 1), //-t + 2t^2 -t^3
111 0.5*(dy*(dy*(3*dy - 5)) + 2), //2 - 5t^2 + 3t^3
112 0.5*dy*(dy*(-3*dy + 4) + 1), //t + 4t^2 - 3t^3
113 0.5*dy*dy*(dy-1) //-t^2 + t^3
118 0.5*dt*(dt*(dt*(-1) + 2) - 1), //-t + 2t^2 -t^3
119 0.5*(dt*(dt*(3*dt - 5)) + 2), //2 - 5t^2 + 3t^3
120 0.5*dt*(dt*(-3*dt + 4) + 1), //t + 4t^2 - 3t^3
121 0.5*dt*dt*(dt-1) //-t^2 + t^3
124 //evaluate polynomial for each row
125 for(int i = 0; i < 4; ++i)
127 for(int j = 0; j < 4; ++j)
129 tfa[j] = f(ya[i],xa[j],ta[0])*ttf[0] + f(ya[i],xa[j],ta[1])*ttf[1] + f(ya[i],xa[j],ta[2])*ttf[2] + f(ya[i],xa[j],ta[3])*ttf[3];
131 xfa[i] = tfa[0]*txf[0] + tfa[1]*txf[1] + tfa[2]*txf[2] + tfa[3]*txf[3];
134 //return the cumulative column evaluation
135 return xfa[0]*tyf[0] + xfa[1]*tyf[1] + xfa[2]*tyf[2] + xfa[3]*tyf[3];
141 case 5: // Fast Spline (non-animated)
143 #define P(x) (((x)>0)?((x)*(x)*(x)):0.0f)
144 #define R(x) ( P(x+2) - 4.0f*P(x+1) + 6.0f*P(x) - 4.0f*P(x-1) )*(1.0f/6.0f)
145 #define F(i,j) ((*this)(subseed,i+x,j+y)*(R((i)-a)*R(b-(j))))
146 #define FT(i,j,k) ((*this)(subseed,i+x,j+y,k+t)*(R((i)-a)*R(b-(j))*R((k)-c)))
147 #define Z(i,j) ret+=F(i,j)
148 #define ZT(i,j,k) ret+=FT(i,j,k)
149 #define X(i,j) // placeholder... To make box more symmetric
150 #define XT(i,j,k) // placeholder... To make box more symmetric
152 float a(xf-x), b(yf-y);
156 Z(-1,-1); Z(-1, 0); Z(-1, 1); Z(-1, 2);
157 Z( 0,-1); X( 0, 0); Z( 0, 1); Z( 0, 2);
158 Z( 1,-1); Z( 1, 0); Z( 1, 1); Z( 1, 2);
159 Z( 2,-1); Z( 2, 0); Z( 2, 1); Z( 2, 2);
164 case 3: // Spline (animated)
166 float a(xf-x), b(yf-y), c(tf-t);
169 float ret(FT(0,0,0));
170 ZT(-1,-1,-1); ZT(-1, 0,-1); ZT(-1, 1,-1); ZT(-1, 2,-1);
171 ZT( 0,-1,-1); ZT( 0, 0,-1); ZT( 0, 1,-1); ZT( 0, 2,-1);
172 ZT( 1,-1,-1); ZT( 1, 0,-1); ZT( 1, 1,-1); ZT( 1, 2,-1);
173 ZT( 2,-1,-1); ZT( 2, 0,-1); ZT( 2, 1,-1); ZT( 2, 2,-1);
175 ZT(-1,-1, 0); ZT(-1, 0, 0); ZT(-1, 1, 0); ZT(-1, 2, 0);
176 ZT( 0,-1, 0); XT( 0, 0, 0); ZT( 0, 1, 0); ZT( 0, 2, 0);
177 ZT( 1,-1, 0); ZT( 1, 0, 0); ZT( 1, 1, 0); ZT( 1, 2, 0);
178 ZT( 2,-1, 0); ZT( 2, 0, 0); ZT( 2, 1, 0); ZT( 2, 2, 0);
180 ZT(-1,-1, 1); ZT(-1, 0, 1); ZT(-1, 1, 1); ZT(-1, 2, 1);
181 ZT( 0,-1, 1); ZT( 0, 0, 1); ZT( 0, 1, 1); ZT( 0, 2, 1);
182 ZT( 1,-1, 1); ZT( 1, 0, 1); ZT( 1, 1, 1); ZT( 1, 2, 1);
183 ZT( 2,-1, 1); ZT( 2, 0, 1); ZT( 2, 1, 1); ZT( 2, 2, 1);
185 ZT(-1,-1, 2); ZT(-1, 0, 2); ZT(-1, 1, 2); ZT(-1, 2, 2);
186 ZT( 0,-1, 2); ZT( 0, 0, 2); ZT( 0, 1, 2); ZT( 0, 2, 2);
187 ZT( 1,-1, 2); ZT( 1, 0, 2); ZT( 1, 1, 2); ZT( 1, 2, 2);
188 ZT( 2,-1, 2); ZT( 2, 0, 2); ZT( 2, 1, 2); ZT( 2, 2, 2);
203 ret+=(*this)(subseed,i+x,j+y,h+t)*(R(i-dx)*R(j-dy)*R(h-dt));
217 int x((int)floor(xf));
218 int y((int)floor(yf));
221 a=(1.0f-cos(a*3.1415927))*0.5f;
222 b=(1.0f-cos(b*3.1415927))*0.5f;
227 (*this)(subseed,x,y,t)*(c*d)+
228 (*this)(subseed,x2,y,t)*(a*d)+
229 (*this)(subseed,x,y2,t)*(c*b)+
230 (*this)(subseed,x2,y2,t)*(a*b);
238 a=(1.0f-cos(a*3.1415927))*0.5f;
239 b=(1.0f-cos(b*3.1415927))*0.5f;
241 // We don't perform this on the time axis, otherwise we won't
243 //c=(1.0f-cos(c*3.1415927))*0.5f;
249 int x2=x+1,y2=y+1,t2=t+1;
252 (*this)(subseed,x,y,t)*(d*e*f)+
253 (*this)(subseed,x2,y,t)*(a*e*f)+
254 (*this)(subseed,x,y2,t)*(d*b*f)+
255 (*this)(subseed,x2,y2,t)*(a*b*f)+
256 (*this)(subseed,x,y,t2)*(d*e*c)+
257 (*this)(subseed,x2,y,t2)*(a*e*c)+
258 (*this)(subseed,x,y2,t2)*(d*b*c)+
259 (*this)(subseed,x2,y2,t2)*(a*b*c);
264 int x((int)floor(xf));
265 int y((int)floor(yf));
272 (*this)(subseed,x,y,t)*(c*d)+
273 (*this)(subseed,x2,y,t)*(a*d)+
274 (*this)(subseed,x,y2,t)*(c*b)+
275 (*this)(subseed,x2,y2,t)*(a*b);
288 int x2=x+1,y2=y+1,t2=t+1;
291 (*this)(subseed,x,y,t)*(d*e*f)+
292 (*this)(subseed,x2,y,t)*(a*e*f)+
293 (*this)(subseed,x,y2,t)*(d*b*f)+
294 (*this)(subseed,x2,y2,t)*(a*b*f)+
295 (*this)(subseed,x,y,t2)*(d*e*c)+
296 (*this)(subseed,x2,y,t2)*(a*e*c)+
297 (*this)(subseed,x,y2,t2)*(d*b*c)+
298 (*this)(subseed,x2,y2,t2)*(a*b*c);
302 return (*this)(subseed,x,y,t);