Vous êtes sur la page 1sur 16

POV-Ray

Ray Tracing with POV-Ray

Introduction
This brief tutorial is designed to introduce you to a computer graphics technique called ray-trac-
ing. Much more information - including free copies of POV-Ray can be found at the official website.

Windows: http://www.povray.org Mac:http://mac.povray.org

What is POV-Ray?
The Persistence of Vision Raytracer is a high-quality, totally free tool for creating stunning three-
dimensional graphics. It is available in official versions for Windows, Macintosh, Linux, SunOS, and
Amiga.

How does ray-tracing work?


Although there are several methods of ray-tracing, one of the most common (and the one the
POV-Ray software package uses) works something like this. First, an internal model of the scene is gen-
erated, with your computer screen included as the receiving eye in the model. Then, the software traces
imaginary light rays backwards from where their endpoint lies (a pixel on your computer screen) to their
initial point (some light source in the scene). This step is repeated, pixel by pixel, until the entire image
has been created. The reason the software traces the light rays backwards, instead of starting at the light
source, is for efficiency's sake -- if a light ray doesn't end up on your screen, then you, as the user, don't
care about it (because you'll never see it). By tracing the light rays backwards, beginning at the computer
screen, the software can assure that every light ray it calculates is one you care about, because it knows
that it will end up on your screen. In their journey, the light rays can be reflected by mirrors, refracted by
glass, or undergo various other contortions, all of which result in a single pixel of the final image.
Because the ray-tracing software must trace one ray of light for each pixel in the output image, and
because the light rays can undergo so many contortions, the process of ray-tracing can take a very long
time, depending on the size and complexity of the image and the processing power of your computer.

Exercise 1 - What kind of pictures can POV-Ray create?


Explore the "Hall_of_Fame" examples. You will need a JPEG viewer to be able to do this. These
examples were all created using POV-Ray by various artists. Many more examples can be found on the
official POV-Ray website. In each case notice the mathematical precision of the scene and the realistic
rendering of reflection and shadows. This is particularly clear in the file "bearings.jpg" Ray tracing scenes
appear so perfect that they often give a surrealistic impression.

293
POV-Ray

Figure 1 - Note the realistic reflection and shadows.

Exercise 2 - Rendering
The input to POV-Ray is a simple text file which describes three things - the camera, the lighting
and the objects in the scene. Open POV-Ray by double-clicking on its icon, or by finding it under the
Apple menu item. You will see a status window to report progress of various functions. Open a new unti-
tled document under the File menu. Rather than work from scratch let's learn the easy way by looking at
some more examples.

a. Go to the File menu and Open the file "exercise2.pov" which is contained in the folder POV-Ray
Examples. Then choose Start Rendering under the Render menu item.

b. Rendering can take quite a long time. The length of time grows extremely fast with the size of the
image. Most artists start off with a very small rendering window and only render at the larger sizes when
they are happy with all aspects of their work. Rendering is done by clicking Render... then Start Ren-
dering.

c. Render Quality
To control the quality of the image, select exercise2.pov Settings under the Edit menu. Under the Out-
put category are two boxes, one controlling the vertical size, and the other controlling the horizontal size.
Change the image size to 240 by 240. Now render the image.

294
POV-Ray

Exercise 3 - Practice Rendering


To build confidence with what we have learned so far, render the file exercise3.pov at various
sizes. It is also in the POV-Ray Examples folder. You should see a beautiful mathematical shape floating
over a marble surface.

Exercise 4 - Include Files "colors.inc"


Another way to keep things simple is to use files that you or others have already created. This is
done using the include statement.You will note that almost all of our examples include the file colors.inc.
a. Open the copy of this file found in the Examples folder. Take a few moments to scan through the names
of the defined colors. You will be able to use any of them at will. Notice this file consists primarily of #
declare statements that enable you to refer to various RGB colors by their name if you have included this
file.

How to describe color: RGB and RGBF Vectors


In a position vector, the individual elements can be any real number. In a RGB vector, the numbers should
be between 0.0 and 1.0. You can have values higher that 1.0, but they don't correspond to any physical
property. A value of 1.0 means 100% of that color. For example, the color black, which is actually the
absence of all color, is described by the color vector <0,0,0>. The color white, a complete combination of
all three primary colors, is specified by the color vector <1,1,1>.

295
POV-Ray
In addition to RGB vectors, you can specify a color in POV-Ray with an RGBF vector. As you might
guess, a RGBF vector is like a RGB vector, but with one extra element - the F, for filter. The filter value
specifies how transparent the pigment is, ranging from 0.0 (not transparent at all) to 1.0 (100%
transparent). RGB vectors have an implied filter value of 0. In other words, a color specified by a RGB
vector will be perfectly opaque. A filter value of 1.0 means that all light will be let through, but the light
will still be filtered. For example, the RGBF vector <1,0,0,1> acts like red cellophane, 100% of light is
passed through, but it is filtered by the red pigment.

b. Open the file spikey_glass.pov contained in the POV-Ray Examples folder.

i. Try to render it. You should get an error message stating that colors and textures are unknown. Let's fix
this now. Notice that the three include statements have been preceded by a double slash //.
//#include "colors.inc"
//#include "textures.inc"
//#include "functions.inc"
The double slash indicates comments which are ignored by POV-Ray. Remove the double slash before
each include statement, then try rendering the above image again.
ii. The glass object is bounded by the box{-1,1}. Other boxes are commented out. Render the file again
using the box{-0.5,0.5} to see less of the spikes. Just remove or add comments.
iii. Render the file again using the box{-2,2} to see more of the spikes.

c. Just for fun, try changing the texture to White_Marble or Red_Marble instead. Edit each of the three
planes that form the walls of the room by changing each occurrence of the texture Brown_Agate to
White_Marble or Red_Marble.

296
POV-Ray
Exercise 5 - Basic Scene
Now its time to start from scratch. We will draw a sphere hovering over a plane.

a. Create a new file by choosing New from the file menu. Include the standard colors and textures by
choosing Headers under the Templates menu and selecting Standard Includes. This should add the fol-
lowing code to your file.
//==== Standard POV-Ray Includes
#include "colors.inc" // Standard Color definitions
#include "textures.inc" // Standard Texture definitions

b. Next we need to specify the location of the camera.


Using the Templates menu, choose, Cameras, and then, A Typical Camera. This will add the following
code. Which specifies both where the camera is and where it is pointed.
camera {
location <0.0 , 2.0 ,-5.0>
look_at <0.0 , 0.0 ,0.0>
right x*image_width/image_height }
Thus the camera is the same as if you were looking at the center of the computer screen with your eyes
slightly above the center of the screen.

Vectors in POV-Ray
POV-Ray calls the number triples that define positions - position vectors. The term vector refers to any
group of numbers describing a certain thing - there are also color vectors and normal vectors for exam-
ple, in addition to position vectors. In POV-Ray, vectors are surrounded by angle brackets (< and >).

POV-Ray uses the adjacent 3D coordinate axes system. The point <0,0,0> corre-
sponds to the origin, or center of the graph. Notice this is a left-handed coordi-
nate system with the positive z-axis pointing into the computer screen.

c. Now we need some lights.


Using the under Templates menu, choose Light Sources and then select Point light.
This will add the code:

// create a regular point light source


light source {
0*x // light's position (translated below)
color red 1.0 green 1.0 blue 1.0 //light's color
translate <-20, 40, -20>
}

297
POV-Ray

Declared vectors: Note that POV-Ray has predefined the unit vectors
x = <1,0,0> y = <0,1,0> z = <0,0,1>
You can use either form interchangeably.
d. Now we are ready to describe the object in our scene. First let us draw the sphere. Under Templates,
choose Shapes and then Sphere. This results in the code.
// create a sphere shape
sphere {
<0, 1, 0> // center of sphere <X Y Z>
0.5 // radius of sphere
// scale <1,2,1>// <= Note: Spheres can become ellipses by uneven scaling
}
Save your file as sphere.pov and then render it. You should see nothing but a black background.
Why? The reason is that we need to specify the color of the sphere.
To do this, alter the code for the sphere as follows.
sphere {
<0, 1, 0> // center of sphere <X Y Z>
0.5 // radius of sphere
pigment {Green}
}
Now render and you should see a small sphere. The sphere is so small because it is far from where we put
our camera. Change the radius of the sphere to 2, move its center to the origin <0,0,0> and render again.
sphere {
<0,0,0> // center of sphere <X, Y, Z>
2 // radius of sphere
pigment {Green} }

e. Next we will place a horizontal plane at height -4 beneath the sphere. Under Templates, choose
Shapes and then plane to add the code for the plane.
// An infinite planar surface
// plane {<A, B, C>, D } where: A*x + B*y + C*z = D
plane {
y, // <X Y Z>; unit surface normal, vector points "away from surface
-1.0 // distance from the origin in the direction of the surface normal
hollow on // has an inside pigment?
}

298
POV-Ray
Before rendering, specify the pigment for the plane and its distance from the origin as follows:
plane{
y, // <X Y Z> unit surface normal, vector points "away from surface"
-4.0 // distance from the origin in the direction of the surface normal
hollow on // has an inside pigment?
pigment{ Red}
}
Now render. Notice how the sphere actually casts a shadow!

Move the camera further away to obtain a better view.


camera {
location <0.0,5.0, -5.0>
look at <0.0,0.0,0.0> }

f. Using a texture. Change the pigment for the floor to Brown_Agate inside the code for the plane as fol-
lows.
pigment {Brown_Agate}

g. Now scale the pattern of the Brown_ Agate as shown below. What is the difference?
pigment {Brown_Agate scale 2}

h. Change the Brown_Agate to White_Marble and the Green to BrightGold.

i. The gold sphere does not look all that bright. We need more lighting! Lift the center of the sphere to
<0,0,2> and add another light source at <-20, 40, +20>. Alter the radius of the sphere.

299
POV-Ray

Exercise 6 - Cones
To save you a lot of work, open the file basic.pov, which already contains specifications for the
camera, lighting and a background plane of White_Marble. We will add various objects to this file to
explore other POV-Ray options.
1. Add a cone using the Templates/Shape menu item. Be sure to add this code at the end of the file
basic.pov. Carefully examine the template code for the default cone. Alter the default cone so that its
base is centered at <0,0,0> and has radius 2. Let the top be centered at <0,2,0> and have radius one.
Add the pigment specification: pigment{ Red_Marble }

Warning: Be sure the pigment command appears after the open option as shown below.

cone {
<0,0,0>,2,
<0,2,0>,1
//open
pigment { Red_Marble }
}

Render the cone to obtain the image shown to the right. You may
wish to move the camera a little closer to obtain a better view of
the image.

2. Now remove the comment slashes in front of the word open and create a new rendering. What is the
difference?

cone {
<0,0,0>,2,
<0,2,0>,1
open
pigment { Red_Marble }
}

300
POV-Ray
Exercise 7 - Torus
Open a fresh copy of the file basic.pov, which already contains specifications for the camera,
lighting and a background plane of White_Marble. Add a torus using the Templates/Shape menu item.
Be sure to add this code at the end of the file basic.pov. Adjust the code to obtain a BrightGold torus hav-
ing inner radius 2 and outer radius 0.5 It will look a little better if we lift it up using the translate com-
mand as shown below.
torus {
2, .5
pigment { BrightGold}
translate <0,1,0> }

Exercise 8 - Using Text


Open a fresh copy of the file basic.pov which already contains specifications for the camera,
lighting and a background plane of White_Marble. To create interesting 3D effects using letters and
words, insert a text using the Templates/Shape menu item. Change the default text string to Dragons
and add a BrightGold pigment as shown below.
text {
ttf // font type
"crystal.ttf",
"Dragons", // the string to create
2, // the extrusion depth
0 // inter-character spacing
pigment{ BrightGold }
}

Render your text string.

Notice that the text is not centered. This is because the text begins at <0,0,0>. What we want to do is
translate the text somewhat to the left so it will be centered. Add the translate command inside the text
command.
text {
ttf // font type
"crystal.ttf",
"Dragons", // the string to create
2, // the extrusion depth
0 // inter-character spacing
pigment{ BrightGold }
translate <-1.5,0,0>
}

301
POV-Ray
Move the camera a little closer to obtain a better view of the image.

camera {
location <0.0 , 2.5,-2.5>
look_at <0.0 , 0.5 ,0.0>
}

Advanced Techniques
Exercise 9 - Blobs
Render the file blob7.pov

The new code shows how this blob is composed from three spheres that have been smeared
together. Adjust the threshold to various numbers and examine the effect.
blob{
threshold 0.6
sphere{ <.75,0,0>, 1, 1}
sphere{ <-.375, .64952,0>, 1,1}
sphere {-.375, -.64952,0>, 1,1}
scale 2
texture { Gold_Metal }
}

Blobs are described as spheres and cylinders covered with "goo" which stretches to smoothly join
them Ideal for modeling atoms and molecules, blobs are also powerful tools for creating many smooth
flowing "organic" shapes. A slightly more mathematical way of describing a blob would be to say that it
is one object made up of two or more component pieces. Each piece is really an invisible field of force,
which starts out at a particular strength and falls off smoothly to zero at a given radius. Wherever these
components overlap in space, their field strength gets added together (and yes, we can have negative
strength, which gets subtracted out of the total as well).

302
POV-Ray
Exercise 10 - Polynomial Surfaces
Render the file quadratic4.pov. This gives an example of a creative use of polynomial surfaces.

Exercise 11 - Rendering mathematical surfaces using the Isosurface extension.


Many mathematically defined shapes can be produced using the isosurface extension in POV-Ray.
Inside the function command, you can place any mathematical expression using * for multiplication and
^ for exponentiation. The clipped_by command tells within what box to draw the surface.
a. Open and render the file functions.pov, which draws a sphere of radius 1. Its equation is:
x2 + y2 + z2 -1

The new code is:


isosurface {
function { x*x + y*y + z*z -1} //<- Enter function here
max_gradient 6 // <- Enter maximum gradient here
contained_by { box {-2,2} } // <- Define bounding box
texture { Red_Marble scale 0.5 }
}
The equation immediately follows the word function. The contained_by box tells where the surface
stops.

303
POV-Ray
b. Adjust the function in this file to draw a heart.
The equation for the heart is: (2 x 2 + y 2 + z 2 1)3 (0.1x 2 + y 2 )z 3 = 0
1. Change the function specification to:
function {pow (2*x*x + y*y + z*z -1, 3) - (0.1*x*x + y*y)*pow(z,3)}
2. Change the maximum gradient to 3000.
max_gradient 3000 // <- Enter maximum gradient here
3. Change the texture code to pigment { Red } and then render again.

( )
c. Adjust the function in this file to draw some circular waves. The equation is: z cos 10 ( x + y ) = 0
2 2

1. Change the function to specification to:


function {z - cos(10 * sqrt ( x*x + y*y ))}
2. Change the maximum gradient to 10.
max_gradient 10 // <- Enter maximum gradient here
3. Render your image.

304
POV-Ray
Exercise 12 - Resistor Challenge
The following graphic was designed to show the application of POV-Ray for various design and
engineering projects. The resistor shown is constructed using only spheres and cylinders. Wed like to
thank Ian White for providing the code for the following graphic. The body of the resistor consists of two
spheres and a cylindrical shaft. The color bars on the resistor from left to right are: red, green, blue, gold.

1. The following is the code used to render the above image of the resistor.

//Step 0 - Include the standard colors and textures and some special metallic textures.
#include "colors.inc" #include "textures.inc" #include "metals.inc"

// Step 1 - Specify the camera.


camera { location <-8.0 , 4.0 ,-10.0> look_at <0.0 , 0.0 , 0.0>}

// Step 2a - Create two regular point light source.


light_source{ 0*x color White translate <-20, 40, -20>}
light_source { 0*x color White translate <-20,40,20> }
// Step 2b - Now add an area light.
light_source { 0*x // light's position (translated below)
color rgb 1.0 // light's color
area_light
<8, 0, 0> <0, 0, 8> // lights spread out across this distance (x * z)
4, 4 // total number of lights in grid (4x*4z = 16 lights)
adaptive 0 // 0,1,2,3...
jitter // adds random softening of light
circular // make the shape of the light circular
orient // orient light
translate <40, 80, -40> // <x y z> position of light }

305
POV-Ray
// Step 3 - Create the sphere on the right.
sphere{ <3, 0, 0> 1.25 texture{Gold_metal} }

// Step 4 - Create the sphere on the left.


sphere{ <-3, 0, 0> 1.25 texture{Gold_metal} }

// Step 5 - Create the shaft of the resistor.


cylinder{ <-3,0,0>, <3,0,0>, 1 texture{Gold_metal} }

// Step 6 - Create an interesting background.


plane{ y, -40 pigment{White_Marble scale 5} }

//Step 7 - Create the wire terminals of the resistor.


cylinder{ <-7,0,0>, <9,0,0>, 0.3 texture{T_Silver_1A} }

// Step 8 - Create the resistor color bands.


cylinder{ <-1.5,0,0>, <-1.2,0,0>, 1.01 pigment{Red} }
cylinder{ <-.6,0,0>, <-.3,0,0>, 1.01 pigment{Green} }
cylinder{ <.3,0,0>, <.6,0,0>, 1.01 pigment{Blue} }
cylinder{ <1.2,0,0>, <1.5,0,0>, 1.01 pigment{Gold} }

Finally, render your image to see the resistor.

2. Change the color bands to create a 1 Kilo-Ohm resistor.

You can use POV-Ray to illustrate many of your design ideas. Consider how you might apply
POV-Ray to your design project. To learn more about POV-Ray and to obtain your free copy - just go to
the official Web sites referred to at the beginning of the lab. Complete documentation is available on the
official POV-Ray website.

306
POV-Ray

POV-Ray Quiz A Grade =


Name = ID = Section =
____________________________________________________________________________________

1. The RGB vector for the color black is:

a.<0,0,0> b. <1,1,1> c. <0,0,0,0> d. <1,1,1,1> e.1

2. To simulate a material like blue cellophane one would use the RGBF vector:

a. <1,0,0,1> b. <0,0,1,1> c. <0,0,1,0>


d. <0,0,0,0> e. <1,0,0,0>

3. To use the predefined colors file one would type the command:
a. #include "colors.inc b. #include "textures.inc
c. "colors.inc" d. include "colors.inc"

4. Show your 1 Kilo-Ohm resistor from Exercise 12, part 2. (2 points)


Your TA will record this exercise when you have completed it.

307
POV-Ray

POV-Ray Quiz B Grade =


Name = ID = Section =
____________________________________________________________________________________

1. The RGB vector for the color white is:

a.<0,0,0> b. <1,1,1> c. <0,0,0,0> d. <1,1,1,1> e.1

2. To simulate a material like red cellophane one would use the RGBF vector:

a. <1,0,0,1> b. <0,0,1,1> c. <0,0,1,0>


d. <0,0,0,0> e. <1,0,0,0>

3. In the POV-Ray RGB declaration for the color BrightGold, the amount of blue is:

a. 0.65 b. 0.75 c. 0.85 d. 0.95 e. 0.10

4. Show your 1 Kilo-Ohm resistor from Exercise 12, part 2. (2 points)


Your TA will record this exercise when you have completed it.

308

Vous aimerez peut-être aussi