POV-Ray : Newsgroups : povray.advanced-users : macro inside a loop : Re: macro inside a loop Server Time
29 Jul 2024 16:25:38 EDT (-0400)
  Re: macro inside a loop  
From: Mike Williams
Date: 19 Oct 2001 03:32:40
Message: <B$i7SBABN9z7Ew+p@econym.demon.co.uk>
Wasn't it Arie L. Stavchansky who wrote:
>I have the following:
>
>//macro to make a tube of rings of spheres
>#macro sphereTube(height, tubeRadius, elements, bottomSurface, topSurface,
>place)
>
> #declare i = 10;
> #declare shift = <0, 0, 0>;
>
> #while (i < height)
>
>  objectRing(
>
>   tubeRadius, //radius of ring
>   elements, //number of objects in ring
>   bottomSurface, //start texture of object
>   topSurface, //end texture of object
>   place + shift //place away from origin
>
>  )
>
>  #declare shift = shift + <0, ballRadius(tubeRadius, elements)*2*i, 0>;
>  #declare i = i+1;
>
> #end
>
>#end
>
>The problem is that the macro inside the loop (which functions properly) is
>not being called more than once.  In fact, even the message I am given says
>there are only a certain amount of frame level objects, as opposed to a
>certain amount of frame level objects times the height (which is the loop
>condition).
>
>If you replace the macro inside the loop with a simple prmitive, such as a
>sphere, then you will see the many spheres.  Why is the macro not being
>called multiple times?
>
>Thanks for your help,
>Arie
>
>

Since it works with simple primitives, I'd guess that one of the macros
you call "ballRadius" or "objectRing" is clobbering the value of your
counter variable "i".

The way to fix this is to change your #declares into #locals. This
allows each macro to have its own copy of the variable which then won't
interfere with similarly names variables in the calling macro or in the
top level of the scene file.

When you use "#declare i" you are changing the value of the global
variable called "i". So, if objectRing() uses "i" as its counter, and
counts up to "elements", when control returns out from objectRing() into
sphereTube(), the value of "i" has been changed from 10 to elements. If
elements+1 is greater than height, the while clause of sphereTube() is
satisfied, and the macro terminates.

If this is the case, then you're lucky that height is less than
elements+1, otherwise it could easily have gone into an infinite loop.

-- 
Mike Williams
Gentleman of Leisure


Post a reply to this message

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