POV-Ray : Newsgroups : povray.advanced-users : Optimizing scenes with nested loops. : Re: Optimizing scenes with nested loops. Server Time
28 Jul 2024 12:33:29 EDT (-0400)
  Re: Optimizing scenes with nested loops.  
From: Mike Williams
Date: 13 Aug 2005 01:50:19
Message: <mfea4CA4nY$CFwvC@econym.demon.co.uk>
Wasn't it Josh who wrote:
>I am working on some timple functions to make my own grass (I want the
>challenge of making my own, I know there are some out there already).  The
>way I'm doing it right now gives me the results I want (it looks like I
>want it to look), but the parsing process is very slow due to the nested
>loops that I use.  I am using splines to calculate the path I want.  With
>my spline, I simply draw triangles to make up the grass.  There is a loops
>the goes up the spline, while drawing triangles.  This loop is contained in
>another loop whose roll it is to create multiple blades.  Heck, here is the
>source code:

I notice that all your blades are actually identical within each frame
of the animation, so it's possible to have two separate loops, one that
#declares a blade as a union of triangles, and the other that places
lots of copies of it.

I then noticed that it's possible to create the blade as a mesh, instead
of a union, since it consists only of triangles.

On my old slow computer it now parses in 7 seconds, using 111 Mb peak
memory.

Here's the resulting code:


#version 3.6;
global_settings {assumed_gamma 1.0}
camera {location  <0,10,-30> look_at <0,0,0> angle 50}
background {rgb 1}
light_source {<-30, 100, -30> color rgb 1}
#include "math.inc"

#declare counter=0;
#declare num_blades=12000;

#declare s_point1=<0,0,0>;
#declare s_point2=<0,0,0>;
#declare s_step=.1;
#declare blade_width = .025;

#declare R2 = seed(1);

#declare sway=cosd(clock)*.025;
#declare s = spline {quadratic_spline  0, <0,0,0> .5, <0, .5,0> 1,
<0,1,.5>}

// Create a single grass blade as a mesh
#declare Blade = mesh {
  #declare ctr=0;
  #while (ctr < 1)
    #declare s_point1=s(ctr);
    #declare s_point2=s(ctr+s_step);
    #declare T = texture {
       pigment {color rgb <0,ctr,0>}
       finish {diffuse .5}
    }
    triangle {<s_point1.x-blade_width,s_point1.y,s_point1.z>
       <s_point1.x+blade_width,s_point1.y,s_point1.z>
       <s_point1.x-blade_width,s_point2.y,s_point2.z>
     texture {T}
    }
    triangle {<s_point1.x+blade_width,s_point1.y,s_point1.z>
        <s_point1.x+blade_width,s_point2.y,s_point2.z>
        <s_point1.x-blade_width,s_point2.y,s_point2.z>
     texture {T}
    }
    #declare ctr = ctr + s_step;
  #end
}

// place lots of blades
union {
  #while( counter < num_blades)
    #declare num1=((rand(R2)-.5)*30) ;//-11 ;
    #declare num2=0;
    #declare num3=(rand(R2)-.5)*30;//- 11  ;
    #declare rot = rand(R2)*360;

    object {Blade
      translate <num1,0,num3>
      rotate y*rot
    }
    #declare counter = counter + 1;
  #end
}

-- 
Mike Williams
Gentleman of Leisure


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.