POV-Ray : Newsgroups : povray.general : looking for sample code if nested #if #else #etc : Re: looking for sample code if nested #if #else #etc Server Time
18 Apr 2024 11:56:54 EDT (-0400)
  Re: looking for sample code if nested #if #else #etc  
From: omniverse
Date: 28 Dec 2016 20:30:00
Message: <web.586466f9363945409c5d6c810@news.povray.org>
"Kenneth" <kdw### [at] gmailcom> wrote:
> dick balaska <dic### [at] buckosoftcom> wrote:
> > I could have sworn I saw in the include menu samples (or somewhere in
> > the distribution) a wab of code like:
> > #if (xfoo=1) xbar1 #else #if (xfoo=2) xbar2 #end #end
> > #if (yfoo=1) ybar1 #else #if (yfoo=2) ybar2 #end #end
> > #if (zfoo=1) zbar1 #else #if (zfoo=2) zbar2 #end #end
> >
> > ...
> >
> > Anyway, my mighty povclipse parser threw up on that. I said "huh, I'll
> > have to get back to that" but I didn't make note of where I saw it.
> >
>
> I just tried this on my Windows 7 (64-bit) machine, using POV-ray v3.7, and it
> works OK, no problems(!) I used only one of the three code lines...
>
>               #if (xfoo=1) xbar1 #else #if (xfoo=2) xbar2 #end #end
>
> .... but if it works for one, it should work for all three. (I don't *think* I've
> ever used such a construct, though-- an #if(...)/#end inside of an #else(...) --
> but it works on my system.)
>
> Try this:
>
> #version 3.7
> global_settings {assumed_gamma 1.0}
>
> camera {
>   perspective
>   location  <0, 1, -4>
>   look_at   <0, 0,  0>
>   right     x*image_width/image_height
>   angle 67
> }
>
> light_source {
>   0*x
>   color rgb <1,1,1>
>   translate <20, 40, -20>
> }
>
> #declare xfoo = 1; // 1 or 2
>
> #declare xbar1 =
> sphere {0,1 pigment {srgb <.1,.5,.9>} finish {ambient .1 diffuse .9}} // BLUE
>
> #declare xbar2 =
> sphere {0,1 pigment {srgb <.5,.5,.5>} finish {ambient .1 diffuse .9}} // GREY
>
> #if (xfoo=1) xbar1 #else #if (xfoo=2) xbar2 #end #end

I'm bad at writing SDL so I throw stuff together like this all the time and just
fix when an error occurs during parse time. Except that I know to use #elseif
instead of separate conditionals.

This gets interesting because I was able to get a pass on the following as well:

#declare xfoo = 0; // 0, 1 or 2

#declare xbar0 =
sphere {0,1 pigment {srgb <.9,.5,.1>} finish {ambient .1 diffuse .9}} // ORANGE

#declare xbar1 =
sphere {0,1 pigment {srgb <.1,.5,.9>} finish {ambient .1 diffuse .9}} // BLUE

#declare xbar2 =
sphere {0,1 pigment {srgb <.5,.5,.5>} finish {ambient .1 diffuse .9}} // GREY

#if (xfoo=1) xbar1 #else #if (xfoo=0) xbar0 #end #if (xfoo=2) xbar2 #end #end

//#if (xfoo=1) xbar1 #elseif (xfoo=2) xbar2 #end #if (xfoo=0) xbar0 #end

Commented line is a change to #elseif which also works okay. This sort of thing
is why I like the Ctrl+} (match brace) feature and would be great if same could
be done for #end.

Bob


Post a reply to this message

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