Sunflow and Processing: the basics

Sunflow is an open source ray tracing renderer which can produce some astonishing results in the right hands. Someone far cleverer than me wrote a Java wrapper for it (the catchily titled SunflowAPIAPI), and another did a tutorial about getting it talking nicely to Processing, which I relied on heavily in getting this working. There is also a Processing library by the same author (the even catchier P5SunflowAPIAPI) but thus far I’ve not been able to get it to do what I want.

Amnon’s post goes into a bit of detail about getting Sunflow APIAPI reading complex geometry from Processing using ToxicLibs- this was my first time using ToxicLibs but it was relatively straightforward. I wrote a simple class to generate some semi-random geometry using ToxicLibs’ TriangleMesh and a couple of lines of code in that prepare it to be passed to Sunflow. In the main sketch I put all the Sunflow calls (setting up the lights, shaders, camera, etc.) in one function which can be triggered by a keypress. This means the sketch is mostly the same as it would be without Sunflow, and can use the OpenGL renderer to view the scene before raytracing- the sketch and the rendering are almost totally separated. I’m not sure if that is possible with the P5SunflowAPIAPI library, or with more complex geometry.

So, to my results…

These images use a white point light int the centre of the image and a square mesh light way up high. Rendering time was approximately half an hour for each image- at full size they’re 2100 x 2100 pixels.
SunflowTestRender2
This one uses a diffuse shader with a constant grey colour.

SunflowTestRender3
This is either a diffuse or shiny diffuse shader, with a red value changing from 0-255, ring by ring.

SunflowTestRender4
Finally, this one uses the glass shader, again with the red value ranging from 0-255.

The only real issue I’ve found so far, which Amnon alluded to in his post, is that the camera behaves differently in Sunflow and Processing. I’ve got the settings pretty close, but everything I render in Sunflow comes out flipped 180 degrees horizontally compared to its position in the Processing window. I have no idea why at this point, and any ideas as to how to correct this would be appreciated!

Overall, this is a great example of how open source tools can really work. The freedom for people to build on each others’ work and the willingness to share experience and expertise is really inspiring.

More to come on this soon, I expect.

edit: I’ve uploaded a zipped version of this sketch, updated slightly for Processing 1.5. See the comments for links.

7 thoughts on “Sunflow and Processing: the basics

  1. Looks great!

    ” I wrote a simple class to generate some semi-random geometry using ToxicLibs’ TriangleMesh and a couple of lines of code in that prepare it to be passed to Sunflow. ”

    Any chance of seeing your code? thanks!

    • Hi Felix,
      Thanks for the kind words. Rather than post the code here I’ve zipped the sketch (with all the Sunflow files etc.) and uploaded it here. Something in 1.5 changed the way Processing handles libraries and Java packages, so it won’t work with earlier versions. There’s also a slightly simpler sketch on the Processing forum although you’ll need to go through the process of setting up SunflowAPIAPI for the sketch as per Amnon’s post linked above.
      Hope that helps- let me know if I can be of more help.

  2. Your work is amazing. You’re a genius, thanks! I searched all over the web for a solid example of how to mix toxiclibs with sunflow. Your code is priceless!

    e

    • Thanks! You’re too generous though, most of the work was done for me, especially by Amnon Owed, not to mention the guys who wrote the libraries I relied on. Nonetheless, thanks for taking the time to comment and I’m glad it was of use to you.

  3. Pingback: Sunflow and Processing: the basics | Processing | Scoop.it