Sunday, August 21, 2016

POV-Ray animation

This post continues my introduction to generating images and movies using POV-Ray.

In the last post, I showed how to render a simple scene. Now I am going to show how to animate it.

POV-Ray includes a lot of options for animation and this page provides some great tutorials and examples. However, I have always found it easier to generate animations by automatically generating different *.pov files and then rendering them as part of my workflow.

The last part is easy because you can call POV-Ray directly from the command line. (See here for many options.)

For example, once you are in the directory that contains the main POV-Ray executable, you can produce a PNG file that is 340 by 280 pixels from the scene described in c:\temp\out.pov using this command.

pvengine +W340 +H280 c:\temp\out.pov /exit

Here's some MATLAB code that will spin the camera around the previous image.

function povray_animation

r = -25;
no_of_points = 50;
x = r*cosd(linspace(0,360,no_of_points))

for i=1:no_of_points
    
    pov_file = fopen(fullfile(cd,sprintf('out%.0f.pov',i)),'w');
    fprintf(pov_file,'#include "colors.inc"\n');
    fprintf(pov_file,'background {color White}\n');
    fprintf(pov_file,'camera {location <%f,25,25>\n',x(i));
    fprintf(pov_file,'\tsky <0,0,1>\n');
    fprintf(pov_file,'\tlook_at <10,0,0>}\n');
    fprintf(pov_file,'light_source {<%f,25,25> color White}\n',x(i));
    fprintf(pov_file, ...
        'cylinder {<0,0,0>,<10,0,0>,1 texture {pigment {color Red}}}\n');
    fprintf(pov_file, ...
        'cylinder {<0,0,0>,<0,10,0>,1 texture {pigment {color Green}}}\n');
    fprintf(pov_file, ...
        'cylinder {<0,0,0>,<0,0,10>,1 texture {pigment {color Blue}}}\n');
    fprintf(pov_file, ...
        'sphere {<10,10,10>,3 texture {pigment {color OrangeRed}}}\n');
    fclose(pov_file);
    
    cd_string = 'cd c:\program files\pov-ray\v3.7\bin';
    command_string = sprintf('%s\npvengine +W340 +H280 %s +A /exit', ...
        cd_string, ...
        fullfile(cd,sprintf('out%.0f.pov',i)));
    batch_file = fopen('pov.bat','w');
    fprintf(batch_file,'%s\n',command_string);
    fclose(batch_file);
    system('pov.bat');
end

It's now easy to stitch the *.png files together to make a movie.




1 comment:

  1. Thank you so much for writing a lot of this good information! I am looking forward to reading more.
    Motion Graphics Video

    ReplyDelete