| Home < Solutions < Using an image as a background |
| The POV-Ray Cyclopedia |
| www.spiritone.com/~english/cyclopedia/ |
It was requested at one point for POV-Ray to be able to use an image in the background. Usually POV-Ray can make it's own background look interesting, but there may be a specialized need for a background that is more than a simple color
| Part Zero | |
| Getting Started | |
Luckily, this feature can be done with POV-Ray 3.1 with a little work based on the orient macros of John Van Sickle. In fact, several camera effects can be faked by adding a small box in front of the camera and positioning the box right in front of the camera. For a background image, you place the box with the image very far away and then place it appropriately. |
1. The plain background |
| Part One | |
| Getting Started | |
Since this is pretty straightforward, I'll explain the code in detail and then explain the minimum requirements of what you need to do to use it.
#declare CamLook = <0,0,3>; // Camera's Look_at
#declare CamLoc = <0,0.5,-6>; //where the camera's location is
#declare cam_z = 1.5; //the amount of camera zoom you want
#declare back_dist = 200; // how far away the background is
#declare cam_a = 4/3; // camera aspect ratio
#declare cam_s = <0,1,0>; // camera sky vectoy
#declare cam_d = vnormalize(CamLook-CamLoc); // camera direction vector
#declare cam_r = vnormalize(vcross(cam_s,cam_d)); // camera right vector
#declare cam_u = vnormalize(vcross(cam_d,cam_r)); // camera up vector
#declare cam_dir = cam_d * cam_z; // direction vector scaled
#declare cam_right = cam_r * cam_a; // right vector scaled
#declare fz = vlength(cam_dir);
#declare fx = vlength(cam_right)/2;
#declare fy = vlength(cam_u)/2;
#macro OrientZ(p1,p2,cs)
#local nz = vnormalize(p2-p1);
#local nx = vnormalize(vcross(cs,nz));
#local ny = vcross(nz,nx);
matrix <nx.x,nx.y,nx.z, ny.x,ny.y,ny.z, nz.x,nz.y,nz.z, p1.x,p1.y,p1.z>
#end
camera {
location CamLoc
up cam_u
right cam_r * cam_a
direction (cam_d * cam_z)
}
box { <0,0,0> <1,1,0.1>
pigment { image_map { png "background.png"
map_type 0
interpolate 2 } }
finish { ambient 0.5 }
translate <-0.5,-0.5,0>
scale 2*<fx,fy,0.5>
translate fz*z
scale back_dist
OrientZ(CamLoc,CamLook,cam_s) }
You can include this code directly into your scene file as is, just making the necessary changes to the image map code to point to your particular image. Now the only things you need to control are the following: #local CamLook = <0,0,3>; // Camera's Look_at #local CamLoc = <0,0.5,-6>; //where the camera's location is #local cam_z = 1.5; //the amount of camera zoom you want #local back_dist = 200; // how far away the background is #local cam_a = 4/3; // camera aspect ratio #local cam_s = <0,1,0>; // camera sky vectoy cam_z is the value you would normally use as the camera's direction value, is the value you would normally use as the camera's right value, and cam_s is the value you would use in the up or sky vector. The only real difference is back_dist. It should be large enough so that it doesn't block anything in your scene. In image 2, back_dist is only 6, but for a more complex scene this needs to be larger to make sure that the box is behind the elements of your scene. Of course, making it really small would place it really close to the camera, and block most of the scene, but if you used an image with semitranparency, you can create some nice effects. |
2. The background rendered behind a sphere |
Send feedback, or head back to the Cyclopedia.