CS424 Notes, 2 April 2012
- Scene Description Languages
- A scene description language is a language for describing scenes (duh). That is, it's a language that you can use to build a scene graph "declaratively," simply by stating a list of things in the scene and their properties.
- VRML (Virtual Reality Markup/Modeling Langauge) was an early scene description language that is still used sometimes as a file format. It has evolved into X3D, a standardized XML language for describing 3D scenes.
- As an example, and for a change of pace, we will look at the scene description language that is used by the POV-Ray ray tracing program. See www.povray.org for full information about POV-Ray.
- Here is a short example of a POV-Ray scene description.
#include "textures.inc" camera { location <-3,5,5> look_at <0,0,0> } light_source { <-5,15,10>, color <1,1,1> } light_source { <5,0,20>, color <1,1,1> } sky_sphere { pigment { Bright_Blue_Sky } } cone { <-2,-1,0>, 1, <-2,1,0>, 0.3 texture { New_Brass } } sphere { <0,0,0>, 1.2 texture { Polished_Chrome } } box { <2,-1,-1>, <4,1,1> texture { White_Marble scale 0.5 } }
You can probably guess what most of this means, but click here to see the picture that this makes And here is a longer example and the image that it produced.
- Rendering POV-Ray
- The scene description for a POV-Ray image should be put into a file whose name ends with ".pov". The
following command will create an 800-by-600 PNG file named scene.png from a POV-Ray file
named scene.pov:
povray +a +w800 +h600 scene.pov
The "+a" is essential since it turns on anti-aliasing. This command opens a window that shows the image as it is being computed -- but that does funny things on my Linux computer at home. To turn off the display feature, usepovray -d +a +w800 +h600 scene.pov
- The scene description for a POV-Ray image should be put into a file whose name ends with ".pov". The
following command will create an 800-by-600 PNG file named scene.png from a POV-Ray file
named scene.pov:
- Some Syntax
- A .pov file can include other files, using the #includes directive. There is a set of standard include files, which can be found on our Linux system in /usr/share/povray/include. Files include textures.inc, which defines various texture identifies such as Polished_Chrome, and colors.inc, which defines common color names such as White, Red, and LightSteelBlue.
- Vectors are written with angle brackets < and >; for example: <2.3,-7,3.1>. There are predefined identifiers x, y, and z that stand for the vectors <1,0,0>, <0,1,0>, and <0,0,1>, respectively. You can multiply a vector by a constant; for example, x*3 is the vector <3,0,0>. In a place where a vector is expected, you can give a single number and it will be interpreted as a vector in which all three components are equal to that number; for example, "scale 0.5" is interpreted to mean "scale <0.5,0.5,0.5>".
- Commas outside of vectors are generally optional. The word "color" when you are specifying a color is optional in some places, but not in others where there are other possibilities besides a color. (These are examples of "syntactic sugar.")
- A scene should have one camera, which is defined as shown in the example with a location vector and a look_at vector. (By default, the camera assumes an aspect ratio of 4/3. If the picture that you want to render has a different aspect, the image will be distorted unless you change the default. To do that, add right <a,0,0> to the camera definition, where a is the aspect ratio of he displayed image.)
- A scene must have at least one light source for anything to be visible. A light_source is a point light. It must have a location and a color. POV-Ray supports other kinds of light, including area lights.
- A sky_sphere establishes the background of a scene. It requires a texture, which is given here as a pigment. A pigment is a color, pattern of colors, or image. Every texture has a pigment; if you use a pigment where a texture is expected, it is automatically wrapped in a texture. In the example, Bright_Blue_Sky is a pigment that is defined in the include file textures.inc.
- Aside from that, there are may predefined objects that you can put into a scene. These include basic objects like box, sphere, and cone. There are more complicated objects such as lathed objects and objects defined by mathematical functions. Each kind of object has certain required parameters, which come first in the object definition; for example, a sphere has a center and a radius. The parameters can be followed by other things that affect the object, such as textures and transformations.
- You can use identifiers to name numbers, strings, vectors, objects, textures, etc. This is done with the
#declare directive. For example:
#declare Count = 0; #declare DarkRed = color <0.5,0,0>; #declare Platform = box { <-10,0,-10>, <10,-1,10> };
To use an object identifier such as Platform, use the "object" key word:object { Platform scale <2,0,2> }
- POV-Ray supports a number of programming-language constructs, such as while loops and if statements.
It supports most math expressions and functions, and you can use them on the right side of a
#declare. So, for example, you can make a row of spheres of different colors by saying
#declare xcoord = -3; #while (xcoord <= 3) sphere { <xcoord,0,0>, 1 pigment { #declare R = (xcoord+3)/6; color < R, 1-R, 0 > } } #declare xcoord = xcoord + 1; #end
- Transformations
- http://www.povray.org/documentation/view/3.6.1/49/ has documentation for transforms.
- You can use transformations both in objects and in textures. Usually, the transforms are the last items in the object or texture.
- Transforms are applied in the order in which they are stated. You can have more than one transform of a given type. For example, you can translate, then scale, then translate.
- Transforms include
- translate <dx,dy,dz> -- translate by dx, dy, dz
- scale <sx,sy,sz> -- scale by sx, sy, sz. If you try to scale by a factor of zero, it will be treated as a one. Remember that scale s means scale <s,s,s>
- rotate <a,b,c> -- Rotates a degrees about the x-axis, then b degrees about the y-axis, then c degrees about the z-axis. So rotate <0,30,0> rotates by 30 degrees about the y-axis. POV-Ray uses left-handed rotations.
- Note, by the way, that POV_Ray's coordinate system is also left-handed. That is, if you have the x-axis pointing to the right and the y-axis pointing up, then the z-axis points into the screen.
- Objects
- http://www.povray.org/documentation/view/3.6.1/273/ is the documentation page for POV-Ray objects.
- Some common objects are
- box { <a,b,c>, <d,e,f> ... } -- a rectangular box with opposite corners at <a,b,c> and <e,f,g>. The edges are parallel to the axes. (The ... refers to the texture and transforms that are to be applied to the object.)
- sphere { <a,b,c>, r ... } -- a sphere of radius r and center at <a,b,c>
- cylinder { <a,b,c>, <d,e,f>, r ... } -- A right circular cylinder with radius r and an axis that extends from <a,b,c> to <e,f,g>.
- cone { <a,b,c>, r1, <d,e,f>, r2 ... } -- A right circular cone or truncated cone whose axis extends from <a,b,c> to <e,f,g>. The radius of the circle at the first point is r1, and the radius of the circle at the second point is r2. For a normal cone, use r2 = 0. If both radii are non-zero, you get a truncated cone.
- torus { r1, r2 ... } -- A torus lying in the xz-plane centered at <0,0,0>. r1 is the "major radius," the distance from 0 to the center of the tube of the torus. r2 is the "minor radius," the radius of the tube. r2 should be less than r1.
- plane {<a,b,c>, d ... } -- An infinite plane that is perpendicular to the vector <a,b,c>. (The vector is a normal vector to the plane.) d is the distance of the plane from the origin, given as multiples of the length of the vector. If you use a unit vector, then d is simply the distance from the origin. For example, plane { y, -1 } is a plane perpendicular to the y-axis, one unit below the origin. (Remember that x, y, and z are predefined identifiers.)
- text { ttf Font-file String-of-text t ... } -- Renders a string of text starting at the origin, with the positive x-axis as baseline, lying in the xy-plane and extruded in the positive z direction to a thickness of t. String-of-text is the actual string. Font-file is a path to a true-type font (with file name ending in tff). You can find true-type fonts in the directory /usr/share/fonts/truetype. (I haven't actually tried this!)
- Textures
- http://www.povray.org/documentation/view/3.6.1/331/ is the documentation page for POV-Ray textures.
- Textures are rather complicated. The documentation says that the typical plain texture has the form
texture { pigment { MyPigment } normal { MyNormal } finish { MyFinish } scale SoBig rotate SoMuch translate SoFar }
All parts are optional. (The transforms at the end are applied as texture transforms, which, you should recall, have the inverse effect to what you might expect. For example, a scale factor greater than 1 makes the texture smaller on the object.) - http://www.povray.org/documentation/view/3.6.1/332/ discusses
all kinds of pigments. A pigment defines the basic color of the object. It can be a plain color, such as color <1,0.5,0>.
It can be a pattern, such as a gradient or a checkerboard pattern. It can also be an image. The standard include files define
a variety of pigments. Here, the only patterns that I want to mention are checkerboard, hexagon, and brick. They have the form
- pigment {checker color1, color2 } -- A checkerboard pattern made up of unit squares of the two specified colors. The colors have default values of black and white.
- pigment {brick color1, color2 } -- A brick wall-type pattern where the bricks have color color1 and the mortar between the bricks is color2. From the documentation: "If no colors are specified a default deep red and dark gray are used. The default size of the brick and mortar together is <8,3,4.5> units. The default thickness of the mortar is 0.5 units."
- pigment {hexagon color1, color2, color3 } -- A three-colored hexagonal tiling pattern. The pattern is in planes parallel to the xz-plane. The default colors are red, green, and blue. this example uses an infinite plane with a hexagon pigment.
-
Note that checker and brick are 3D patterns. A checker pattern is actually made up of unit cubes, and a 3D object
will be "carved" out of that pattern. Similarly, a brick pattern is made up of solid 3D bricks. The hexagon pigment is made up of
infinite hexagonal rods. The colors in these patterns can actually themselves be pigments. The documentation gives this
example:
pigment { hexagon pigment { Jade }, pigment { White_Marble }, pigment { Black_Marble } }
- The normal component of a texture is a bump map. That is, it changes the surface normal in some
pattern from point-to-point to simulate variations in surface orientation. It can be used with a variety
of patterns, but I will only mention the example
normal { bumps 0.2 }
where the numerical parameter gives the size of the bumps. - The finish affects reflection of light from the surface. It is particularly important for specular
reflection, which by default is turned off. To give a specular reflection, you could use, for example
finish { phong 0.8 }
which gives a strong specular reflection. The phong value goes from 0.0 to 1.0 and represents the proportion of incoming light that is reflected specularly. The default is 0.0, which give zero specular reflection. By default, the color of the reflected light is the same as the color of the incoming light. For a metallic appearance, you want the color of the reflection to be affected by the color of the surface. To get that effect, add the keyword metallic. The size of the specular highlight is determined by the phong_size, a number between 1 and 128 (called "shininess" in OpenGL). The default phong_size is 40. For example, for a bright metallic finish with sharp highlights:finish { phong 0.8 phong_size 100 metallic }
- For the most part, I suggest that you use the predefined textures from textures.inc or other include files. However, since you can apply more than one texture to an object, you can always add a finish or normal to a standard texture. (If you use a "naked" finish or normal in an object, it is automatically wrapped inside a texture for you.)
- Interior
- In addition to textures and transforms, objects can have an interior. This only applied to transparent/translucent objects, such as objects that use the glass textures from textures.inc. (Color vectors can have a fourth component, representing an alpha value, to create translucent pigments.) The only property of interiors that I will mention is ior, which stands for index of refraction. Indices of refraction determine how light rays bend as the pass from one medium to another. For air, the ior is 1.0. Glass typically has an ior of about 1.5. Diamond has an ior of 2.4.
- To add refraction to a transparent object, add a modifier of the following form
to the object definition:
interior { ior 1.5 }
(There is an example in the next section.)
- Constructive Solid Geometry
- An interesting way to construct new objects in POV-Ray is to take the union, intersection,
or difference of other objects. This technique is called constructive solid geometry (CSG).
The syntax for the operations is
- union { object1, object2, ..., objectN ... }
- intersection { object1, object2, ..., objectN ... }
- difference { object1, object2, ..., objectN ... }
- Click here for an example image that was produced
with POV-Ray using CSG. In this example, the lens was made as an intersection of two
spheres:
intersection { sphere { <0,0,-3> 3.5 } sphere { <0,0,3> 3.5 } texture { NBglass } interior { ior 1.5 } translate <0, 2, -6> }
The marble crate was made by subtracting three rectangular boxes from a cubical box using the difference operation.
- An interesting way to construct new objects in POV-Ray is to take the union, intersection,
or difference of other objects. This technique is called constructive solid geometry (CSG).
The syntax for the operations is