Tutorial FreeCAD POV ray

Introduction
This tutorial shows how to produce a rendered image in FreeCAD, using the POV-Ray renderer. It assumes that the user already created a part or assembly in FreeCAD, or has imported one into it. It uses the Raytracing Workbench to produce the file for rendering.

This tutorial is based on the forum post by schupin FreeCAD / pov ray tutorial, which also includes one file required to produce a rendering.



The files used in this tutorial are in another post in the same thread FreeCAD / pov ray tutorial (pending).

Basic setup
Follow the basic workflow outlined in the Raytracing Workbench documentation.

For direct rendering to work, the executable must be set in ; set it to its location in your system, for example,. Other options used by the renderer can be defined here as well, including the width +W and height +H of the image.

Setup the .pov file
1. Create an assembly using bodies from the Part or PartDesign Workbenches, or any other workbench that produces solid objects, for example, the Arch Workbench. Assign colors or materials to the individual bodies that make the assembly, approximately matching the color that you want in your render.



2. If your model is very detailed, make sure the of the body is set to a low value, between  and, or even smaller. The lower this value is, the more detailed the exported mesh will be, and thus the better the quality of the render will be.



3. Create a POV-ray project by clicking. If the viewport is set as orthographic, change it to perspective, as the renderer will work with a camera with perspective view. Using perspective view will allow you to see better what type of image will be actually rendered.

4. Select all the objects that you want to add to your scene, then select the created object, and click on.

beware of those objects that are not currently visible in the 3D viewport. If they are invisible but are included in the scene, they will still have an effect on light bounces, reflections, and shadows. On the other hand, if you really want to omit a body from being rendered, don't select it for inclusion into the POV-ray project.

all objects in the POV-ray project will have a name based on their internal FreeCAD name. It's important to note which is the POV-ray name, as further options, for example, the material properties, will be assigned to these POV-ray names.

5. In the 3D viewport, zoom, pan and rotate the view to setup the scene that you want to render, that is, make sure the objects are centered in the viewport as much as possible; then select the created object, and press.

6. The POV-ray file is now ready; it contains all the selected objects and the camera information. Select the created object, and then press  to save the  file.

7. The created file can now be rendered directly from FreeCAD. Select the created object, then press, and give the file a name. When the popup image appears on screen, click on it so that it is sent to FreeCAD in its own window tab.



8.1. With the file already created, it is possible to run the the  executable from the command line.

The command line options set the horizontal and vertical sizes of the final image.

The command line options and  are useful to trigger antialiasing to produce a smoother image.

8.2. By double clicking the object it's possible to see that it is using the  template; this template creates a basic  file which will produce a simple and dark image.

To improve the appearance of the image, use a better template. Double click the object and choose the  template. Then export a new file, and run again the renderer. The image should look brighter and generally better.



Once again double click the object and now choose the  template. Then export a new file, and run again the renderer. The image should take a bit longer to produce, but the resulting image should have better quality.

If the rendered images are good enough, then there is nothing more to do. However, in order to control precisely the appearance of the materials and produce even better results, the file needs to be edited manually.

In the following sections, we edit the basic file produced with the standard template.

Editing the .pov file
9. The file generated by FreeCAD is a simple text file that can be opened with any editor. It loosely resembles a C++ source code file: directives start with a hash and are terminated by a semi-colon. Control structures are enclosed in curly braces, and indentation is arbitrary whitespace. Lines that start with a double slash represent comments that are ignored by the renderer; block comments can be defined with a pair of, like in C.

The file may look complicated at first but 90% of its content is just mesh data that doesn't require many changes, as these meshes represent the solid bodies that we want to render.

The file is structured as follows:
 * Includes
 * Global settings
 * Sky sphere
 * Planes
 * Finishes and textures
 * Camera
 * Mesh and body information
 * Light source

The camera information will not be touched, neither most information in the meshes. The main modifications will be made on the other sections.

As the meshes won't be heavily modified, the file can be re-organized so this information is at the end of the file.

Basic re-organization
10. Open the file with a text editor, go to the end of the file, select and cut the  section, and paste it before the first  line or similar.

The resulting file should have the and  sections next to each other, for example

Prepare lights
13. By default, the project file defines one light with a position and color.

The position of the light is defined by a vector. The of the light should be in the range  to  for it to have normal brightness; alternatively it could be a named color such as.

Like other objects, the light can be modified with many options. The option creates a rectangular source, which is more realistic as it results in diffuse illumination that creates soft shadows. The keyword helps reduce the computation time of the light paths; the larger the value the more accurate the result will be; to avoid long rendering times you should use the smallest integer that gives an acceptable result ( or  is usually enough); to obtain the best result remove the keyword completely (long rendering time). The keyword helps improve the shadows by randomly shifting the position of the lights. The keywords and  turn the area light into a spherical source, which will produce better shadows when there are rounded objects in the scene. Including and  is helpful to attenuate the value of the light with distance, just like it happens with a real light source.

Set up the light coming from the right and above.

If the light source is supposed to be in the scene, it may be useful to see a reference on the screen where this source should be. To this effect, create a sphere of a small radius and assume this sphere represents the light source; position the sphere where you want, then move the light very close to these coordinates, and test the lighting of the scene; when you are satisfied with the position of the light, simply delete the sphere.

The section is used to create a realistic sky background. It is commonly defined as a and a  of at least two colors in order to produce a smooth transition from the horizon to the zenith of the scene.



Prepare the body textures
11. The textures of each body need to be adjusted. This is the most time-consuming job of this process.

In the file each body is described in this way
 * Face1, Face2, Face3, Face4, ...
 * Body (union of faces)
 * Object

A body mesh is defined by faces, and each face is defined by a series of triangular elements that themselves are defined by, , and. This information doesn't need to be modified at all. Then, each body is defined as the union of the specified faces. Again, this information doesn't need modification.

Finally, each to be rendered is defined as one of the specified bodies, which has a particular, which itself is defined by properties like  and.

By searching the file for the keyword, it's possible to go directly to the desired part in the file, and change its  appropriately.

As indicated in the comment, the definition of the value is at the top of the file, in this case before the camera information. The value can be declared in many ways, as a combination of different properties, as shown in the commented and uncommented lines.

In general, a is a container that describes a material; it includes information like the  (color or graphic),  (how the color changes with the curvature of the surface),  (interaction of the surface with the light),  (agate, brick, dents, leopard, radial, ripples, tiling, waves, wood, etc.), and other properties. There are many options that can be combined together to produce a texture. This mixing is not trivial, but there are many examples online to obtain the desired appearance of the material.

Material libraries
POV-ray comes with an extensive library of materials that can be used by name. By default, the project template makes available some materials by using statements at the beginning of the file. These materials can be further modified as desired.

The library defines basic colors by name,, , , , , , , , and. It also defines several other shades as well as functions to transform colors. The library contains copper, silver, chrome, and brass textures, and  contains the gold textures.

The standard libraries are located in the installation directory of POV-ray, for example

New textures
For example, to create a mirror texture, the is given a high value of.

Alternatively, for metals, a predefined finish can be used.

Then it can be assigned to the specific object.

The library defines the  texture (yellow pine, ragged grain). It can be used as the basis of a more complex texture, with some additional scaling and translation.

Then it is assigned to the specific object.

The library defines  as a finish for transparent acrylic; it also defines  as an interior material which, together with the  option, is used to calculate as close as possible the effects of light passing through a transparent material. In this case, the section is used, containing external  and internal  information of the material.

Then it is assigned to the specific object.



Prepare planes
12. If not provided by the original 3D model, planes can be added to simulate a floor or table top on which the objects are standing. More planes can be defined to serve as walls or other types of boundaries.

By default, a single plane is created. It is placed 1 millimeter below the model, so that it appears as a floor. The plane is assigned a basic texture that is black and slightly reflective.

Beware that when adding planes, in POV-ray the X axis is defined as horizontal (left-right), the Y axis is defined as vertical (up-down), and the Z axis is defined as depth (front-rear).

For a simple gray floor, that is barely reflective use



The plane can be given a more complex texture with the help of normals and material libraries.

Define a normal map that will be used to give the plane the appearance of a parquet floor.

Then define the plane. As use a wood  defined in, and modify it with a bit of  and scale it so that the wood grain looks random. Then add the created normal, together with another normal; this will result in the texture of the parquet with slight imperfections. Then as finish, make it a little bit reflective and glossy.



13. Add a second plane, this time perpendicular to the Z direction, to serve as a backwall. Displace it just a little bit behind the model to avoid covering the mirror. Include the library, add a generic granite texture, and scale it a bit. This will result in the appearance off a simple dry wall.

A third plane can be added behind the position of the camera so that the mirror has a limited reflection.



Prepare the global settings, radiosity
14. The global settings define ambient light.

The property inside the  controls the way POV-ray computes diffuse light interactions between different objects. It's essential to adjust this property to obtain good rendering results.

Because it can be time consuming to test different settings you can use a variable  and a  statement to quickly set low, medium or high quality render settings.



The library defines a macro to quickly set up the  to a predefined configuration.

The value can be one of the predefined constants:

The and  values are either  or.

Therefore, to test different settings, the statement could also be written like in the following.

The exact values used by these radiosity presets can be found in the file itself, which is found in the installation directory of POV-ray:

The Raytracing Workbench has three default templates:
 * , it doesn't use at all.
 * , it uses the preset.
 * , it uses the preset.

Final render
15. The edited file can be saved when all adjustments have been done.

The final structure is as follows:
 * Includes, with additional libraries
 * Global settings, with radiosity parameters
 * Sky sphere, with lighter color
 * Planes, positioned and with textures
 * Finishes and textures, with custom definitions
 * Camera, not changed
 * Light source, with additional properties
 * Mesh and body information, using the textures defined previously

the sections of the file can be in any order, although it is probably easier to work with the file if the mesh information is at the end.

13. The final rendering can be done by clicking or calling executable from the command line.



Final notes
POV-ray is a relatively old piece of software; its main advantage is that it is a tested solution whose properties are set only by a text file. Also, the renderer is available from the command line in many operating systems, and it runs in old hardware.

The user is advised to read more documentation and tutorials on POV-ray in order to set up the right settings for his or her needs.