1 /* === S I N F G =========================================================== */
5 ** $Id: noise.cpp,v 1.2 2005/01/13 06:48:39 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === H E A D E R S ======================================================= */
33 #include <sinfg/string.h>
34 #include <sinfg/time.h>
35 #include <sinfg/context.h>
36 #include <sinfg/paramdesc.h>
37 #include <sinfg/renddesc.h>
38 #include <sinfg/surface.h>
39 #include <sinfg/value.h>
40 #include <sinfg/valuenode.h>
44 /* === M A C R O S ========================================================= */
46 using namespace sinfg;
50 /* === G L O B A L S ======================================================= */
52 SINFG_LAYER_INIT(Noise);
53 SINFG_LAYER_SET_NAME(Noise,"noise");
54 SINFG_LAYER_SET_LOCAL_NAME(Noise,_("Noise Gradient"));
55 SINFG_LAYER_SET_CATEGORY(Noise,_("Gradients"));
56 SINFG_LAYER_SET_VERSION(Noise,"0.0");
57 SINFG_LAYER_SET_CVS_ID(Noise,"$Id: noise.cpp,v 1.2 2005/01/13 06:48:39 darco Exp $");
59 /* === P R O C E D U R E S ================================================= */
61 /* === M E T H O D S ======================================================= */
65 gradient(Color::black(), Color::white())
71 random.set_seed(time(NULL));
73 displacement=Vector(1,1);
74 do_displacement=false;
81 Noise::color_func(const Point &point, float pixel_size,Context context)const
85 float x(point[0]/size[0]*(1<<detail));
86 float y(point[1]/size[1]*(1<<detail));
89 if(super_sample&&pixel_size)
91 x2=(point[0]+pixel_size)/size[0]*(1<<detail);
92 y2=(point[1]+pixel_size)/size[1]*(1<<detail);
98 int smooth((!speed && Noise::smooth==3)?5:Noise::smooth);
107 for(i=0;i<detail;i++)
109 amount=random(smooth,0+(detail-i)*5,x,y,t)+amount*0.5;
110 if(amount<-1)amount=-1;if(amount>1)amount=1;
112 if(super_sample&&pixel_size)
114 amount2=random(smooth,0+(detail-i)*5,x2,y,t)+amount2*0.5;
115 if(amount2<-1)amount2=-1;if(amount2>1)amount2=1;
117 amount3=random(smooth,0+(detail-i)*5,x,y2,t)+amount3*0.5;
118 if(amount3<-1)amount3=-1;if(amount3>1)amount3=1;
122 amount2=abs(amount2);
123 amount3=abs(amount3);
132 alpha=random(smooth,3+(detail-i)*5,x,y,t)+alpha*0.5;
133 if(alpha<-1)alpha=-1;if(alpha>1)alpha=1;
149 amount=amount/2.0f+0.5f;
150 alpha=alpha/2.0f+0.5f;
152 if(super_sample&&pixel_size)
154 amount2=amount2/2.0f+0.5f;
155 amount3=amount3/2.0f+0.5f;
159 if(super_sample && pixel_size)
160 ret=gradient(amount,max(amount3,max(amount,amount2))-min(amount3,min(amount,amount2)));
162 ret=gradient(amount);
165 ret.set_a(ret.get_a()*(alpha));
171 Noise::calc_supersample(const sinfg::Point &x, float pw,float ph)const
177 Noise::set_time(sinfg::Context context, sinfg::Time t)const
184 Noise::set_time(sinfg::Context context, sinfg::Time t, const sinfg::Point &point)const
187 context.set_time(t,point);
191 Noise::hit_check(sinfg::Context context, const sinfg::Point &point)const
193 if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
194 return const_cast<Noise*>(this);
195 if(get_amount()==0.0)
196 return context.hit_check(point);
197 if(color_func(point,0,context).get_a()>0.5)
198 return const_cast<Noise*>(this);
203 Noise::set_param(const String & param, const ValueBase &value)
205 if(param=="seed" && value.same_as(int()))
207 random.set_seed(value.get(int()));
217 IMPORT(super_sample);
219 return Layer_Composite::set_param(param,value);
223 Noise::get_param(const String & param)const
226 return random.get_seed();
234 EXPORT(super_sample);
239 return Layer_Composite::get_param(param);
243 Noise::get_param_vocab()const
245 Layer::Vocab ret(Layer_Composite::get_param_vocab());
247 ret.push_back(ParamDesc("gradient")
248 .set_local_name(_("Gradient"))
250 ret.push_back(ParamDesc("seed")
251 .set_local_name(_("Random Seed"))
253 ret.push_back(ParamDesc("size")
254 .set_local_name(_("Size"))
256 ret.push_back(ParamDesc("smooth")
257 .set_local_name(_("Interpolation"))
258 .set_description(_("What type of interpolation to use"))
260 .add_enum_value(0,"nearest",_("Nearest Neighbor"))
261 .add_enum_value(1,"linear",_("Linear"))
262 .add_enum_value(2,"cosine",_("Cosine"))
263 .add_enum_value(3,"spline",_("Spline"))
264 .add_enum_value(4,"cubic",_("Cubic"))
266 ret.push_back(ParamDesc("detail")
267 .set_local_name(_("Detail"))
269 ret.push_back(ParamDesc("speed")
270 .set_local_name(_("Animation Speed"))
272 ret.push_back(ParamDesc("turbulent")
273 .set_local_name(_("Turbulent"))
275 ret.push_back(ParamDesc("do_alpha")
276 .set_local_name(_("Do Alpha"))
278 ret.push_back(ParamDesc("super_sample")
279 .set_local_name(_("Super Sampling"))
286 Noise::get_color(Context context, const Point &point)const
288 const Color color(color_func(point,0,context));
290 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
293 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
297 Noise::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
299 SuperCallback supercb(cb,0,9500,10000);
301 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
303 surface->set_wh(renddesc.get_w(),renddesc.get_h());
307 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
316 Surface::pen pen(surface->begin());
317 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
319 Point tl(renddesc.get_tl());
320 const int w(surface->get_w());
321 const int h(surface->get_h());
322 float supersampleradius((abs(pw)+abs(ph))*0.5f);
326 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
328 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
329 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
330 pen.put_value(color_func(pos,supersampleradius,context));
334 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
335 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
336 pen.put_value(Color::blend(color_func(pos,supersampleradius,context),pen.get_value(),get_amount(),get_blend_method()));
339 // Mark our progress as finished
340 if(cb && !cb->amount_complete(10000,10000))