prev index next |
Functions
and Looping
|
POV-Ray Tips,
Tricks, and Techniques |
Loops and
Multiple CSG Subtractions?
|
Bill Watts has a detailed question: | I have a problem looping multiple cutout objects using
the #while loop statement.
The relevant code is as follows: #declare radius=2
#declare x=sin(Count+phase_offset)*radius
#declare dish=
#declare dishcutout=
/*
difference
#declare Count=Count+piunit
When I run the loop, I only get one cutout in the 6 o'clock position from the base object. If I run the loop with the unremed cylinder concurrently with the "dishcutout" cylinder, which if you notice is an exact copy with the exception of the pigment statement, of the "dishcutout" cylinder definition, I get the six correctly placed and oriented cylinders. Is there some kind of limitation in the difference statement that would preclude using the loop function for making multiple CSG subtractions? I seem to remember someone who made gears by CSG subraction. I believe it was an old raytracing contest winner or runner up. It was a rendering of an internally exposed mechanical clock. I have done it by defining them individually. I'm just trying to
simplify and condense the code.
|
|
Chris Colefax goes to work:
|
From the code you included it seems it would have been simpler
defining them individually! I have posted the correct code below,
which fixes a number of major problems in the original file:
1) With a while loop, everthing between the #while and #end statements is 'executed' each time the loop runs; in your case, not only were you redeclaring X and Y, you were also redeclaring the Dish and DishCutout objects, as well as creating six copies of the final difference object! Because each copy included only one DishCutout and they all occupied the same space, you were only seeing one of them in the final render. 2) A common mistake; you were using trigonometric equations to position objects in a circle. What many people forget is that this is exactly what POV-Ray does with the rotate command, and it's a lot easier to use and understand. So here is the (much simpler) code that should do what you want: it declares the necessary variables and object definitions just once, and then starts the difference object. After invoking the base Dish object, the while loop is used to place 6 copies of the Cutout object, using simple translations and rotations to move them into position. Then the while loop is finished, the object is textured, and there you have one copy of your desired object (of course, you could add a '#declare MyDish=' statement before the difference and then reuse the object in mulitple positions, if you wanted). // -- START POV CODE -------------------- #declare Radius = 2
difference {
// -- FINISH POV CODE ---------------------
|
|