X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=synfig-core%2Ftrunk%2Fsrc%2Fmodules%2Fmod_particle%2Fplant.cpp;h=4f10df9eba00867c56a68f4bcde6e9eea281cb49;hb=e13130619534dd014f1e41cefe89f0270e3169d0;hp=78aa6f752a2e2d2e9255b5153bc316a1c9e8e639;hpb=34a3be45d6e93a8973d65e85fbede1662c2e3d0e;p=synfig.git diff --git a/synfig-core/trunk/src/modules/mod_particle/plant.cpp b/synfig-core/trunk/src/modules/mod_particle/plant.cpp index 78aa6f7..4f10df9 100644 --- a/synfig-core/trunk/src/modules/mod_particle/plant.cpp +++ b/synfig-core/trunk/src/modules/mod_particle/plant.cpp @@ -6,6 +6,7 @@ ** ** \legal ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley +** Copyright (c) 2007 Chris Moore ** ** This package is free software; you can redistribute it and/or ** modify it under the terms of the GNU General Public License as @@ -78,7 +79,7 @@ Plant::Plant(): split_angle(Angle::deg(10)), gravity(0,-0.1), velocity(0.3), - perp_velocity(0.3), + perp_velocity(0.0), step(0.01), sprouts(10) { @@ -178,6 +179,7 @@ Plant::sync()const { Mutex::Lock lock(mutex); if (!needs_sync_) return; + time_t start_time; time(&start_time); particle_list.clear(); bounding_rect=Rect::zero(); @@ -215,6 +217,7 @@ Plant::sync()const Real f; int i=0, branch_count = 0, steps = round_to_int(1.0/step); + if (steps < 1) steps = 1; for(f=0.0;f<1.0;f+=step,i++) { Point point(curve(f)); @@ -225,7 +228,7 @@ Plant::sync()const bounding_rect.expand(point); - Real stunt_growth(random_factor * random(Random::SMOOTH_COSINE,i,f+seg,0.0f,0.0f)/2.0+0.5); + Real stunt_growth(random_factor * (random(Random::SMOOTH_COSINE,i,f+seg,0.0f,0.0f)/2.0+0.5)); stunt_growth*=stunt_growth; if((((i+1)*sprouts + steps/2) / steps) > branch_count) { @@ -242,6 +245,10 @@ Plant::sync()const } } + time_t end_time; time(&end_time); + if (end_time-start_time > 4) + synfig::info("Plant::sync() constructed %d particles in %d seconds\n", + particle_list.size(), int(end_time-start_time)); needs_sync_=false; } @@ -267,7 +274,15 @@ Plant::set_param(const String & param, const ValueBase &value) IMPORT_PLUS(gradient,needs_sync_=true); IMPORT_PLUS(velocity,needs_sync_=true); IMPORT_PLUS(perp_velocity,needs_sync_=true); - IMPORT_PLUS(step,needs_sync_=true); + IMPORT_PLUS(step,{ + needs_sync_ = true; + if (step <= 0) + step=0.01; // user is probably clueless - give a good default + else if (step < 0.00001) + step=0.00001; // 100K should be enough for anyone + else if (step > 1) + step=1; + }); IMPORT_PLUS(splits,needs_sync_=true); IMPORT_PLUS(sprouts,needs_sync_=true); IMPORT_PLUS(random_factor,needs_sync_=true); @@ -427,6 +442,9 @@ Plant::accelerated_render(Context context,Surface *surface,int quality, const Re const int w(renddesc.get_w()); const int h(renddesc.get_h()); + const int surface_width(surface->get_w()); + const int surface_height(surface->get_h()); + // Width and Height of a pixel const Real pw = (br[0] - tl[0]) / w; const Real ph = (br[1] - tl[1]) / h; @@ -435,93 +453,186 @@ Plant::accelerated_render(Context context,Surface *surface,int quality, const Re sync(); std::vector::reverse_iterator iter; - const float size_factor(1); - float radius(size_factor*size*sqrt(1.0f/(abs(pw)*abs(ph)))), temp_radius; - if(radius>1.0f) + float radius(size*sqrt(1.0f/(abs(pw)*abs(ph)))); + + int x1,y1,x2,y2; + for(iter=particle_list.rbegin();iter!=particle_list.rend();++iter) { - radius*=1.0; // what does this do? - int x1,y1,x2,y2; - for(iter=particle_list.rbegin();iter!=particle_list.rend();++iter) + float scaled_radius(radius); + Color color(iter->color); + if(size_as_alpha) { - temp_radius = radius; - float radius(temp_radius); - Color color(iter->color); - if(size_as_alpha) - { - radius*=color.get_a(); - color.set_a(1); - } - - // calculate the box that this particle will be drawn as - x1=ceil_to_int((iter->point[0]-tl[0])/pw-(radius*0.5)); - y1=ceil_to_int((iter->point[1]-tl[1])/ph-(radius*0.5)); - x2=x1+round_to_int(radius); - y2=y1+round_to_int(radius); - - // if the box is entirely off the canvas, go to the next particle - if(x1>=surface->get_w() || y1>=surface->get_h() || x2<0 || y2<0) continue; - - // adjust the box so it's entirely on the canvas - if(x2>=surface->get_w()) x2=surface->get_w(); - if(y2>=surface->get_h()) y2=surface->get_h(); - if(x1<0) x1=0; - if(y1<0) y1=0; - - int w(min(round_to_int(radius),x2-x1)); - int h(min(round_to_int(radius),y2-y1)); - - if(w<=0 || h<=0) - continue; + scaled_radius*=color.get_a(); + color.set_a(1); + } - Surface::alpha_pen surface_pen(dest_surface.get_pen(x1,y1),1.0f); + // previously, radius was multiplied by sqrt(step)*12 only if + // the radius came out at less than 1 (pixel): + // if (radius<=1.0f) radius*=sqrt(step)*12.0f; + // seems a little arbitrary - does it help? + + // calculate the box that this particle will be drawn as + float x1f=(iter->point[0]-tl[0])/pw-(scaled_radius*0.5); + float x2f=(iter->point[0]-tl[0])/pw+(scaled_radius*0.5); + float y1f=(iter->point[1]-tl[1])/ph-(scaled_radius*0.5); + float y2f=(iter->point[1]-tl[1])/ph+(scaled_radius*0.5); + x1=ceil_to_int(x1f); + x2=ceil_to_int(x2f)-1; + y1=ceil_to_int(y1f); + y2=ceil_to_int(y2f)-1; + + // if the box is entirely off the canvas, go to the next particle + if(x1>surface_width || y1>surface_height || x2<0 || y2<0) continue; + + float x1e=x1-x1f, x2e=x2f-x2, y1e=y1-y1f, y2e=y2f-y2; + // printf("x1e %.4f x2e %.4f y1e %.4f y2e %.4f\n", x1e, x2e, y1e, y2e); + + // adjust the box so it's entirely on the canvas + if(x1<=0) { x1=0; x1e=0; } + if(y1<=0) { y1=0; y1e=0; } + if(x2>=surface_width) { x2=surface_width; x2e=0; } + if(y2>=surface_height) { y2=surface_height; y2e=0; } + + int w(x2-x1), h(y2-y1); + + Surface::alpha_pen surface_pen(dest_surface.get_pen(x1,y1),1.0f); + if(w>0 && h>0) dest_surface.fill(color,surface_pen,w,h); + + /* the rectangle doesn't cross any vertical pixel boundaries so we don't + * need to draw any top or bottom edges + */ + if(x2color); - if(size_as_alpha) + // case 3 - a single horizontal row of pixels + if(y2point[0]-tl[0])/pw-0.5f); - int y=floor_to_int((iter->point[1]-tl[1])/ph-0.5f); - - // if the point is off the canvas, go to the next particle - // fixme: we're losing a whole row and a whole column of pixels from each tile - // by doing this. even in the final rendered image there are visible - // horizontal stripes of damage: - // http://dooglus.rincevent.net/synfig/plant-corruption.png - if(x>=surface->get_w()-1 || y>=surface->get_h()-1 || x<0 || y<0) continue; - - // calculate how much of the point is at (x) and how much at (x+1) - float x1=((iter->point[0]-tl[0])/pw-0.5f-x)*radius, x0=radius-x1; - - // calculate how much of the point is at (y) and how much at (y+1) - float y1=((iter->point[1]-tl[1])/ph-0.5f-y)*radius, y0=radius-y1; - - Surface::alpha_pen surface_pen(dest_surface.get_pen(x,y),1.0f); - - // | x0 | x1 - // ---+-----+----- - // y0 | 1st | 2nd - // ---+-----+----- - // y1 | 4th | 3rd - - surface_pen.set_alpha(x0*y0); surface_pen.put_value(color); surface_pen.inc_x(); - surface_pen.set_alpha(x1*y0); surface_pen.put_value(color); surface_pen.inc_y(); - surface_pen.set_alpha(x1*y1); surface_pen.put_value(color); surface_pen.dec_x(); - surface_pen.set_alpha(x0*y1); surface_pen.put_value(color); } }