Scene Definition Language
The Ray Tracer kernel (the CIDRay Facility) supports two primary mechanisms
for creating ray traced scenes. The first is to write a C++ program which
creates and renders a scene. The second is to describe the scene in a text
file, which the CIDRay facility will parse and use to create the scene for you.
Creating a scene via a custom written C++ program is extremely flexible and
powerful, having all of the power of the C++ language (and that of CIDLib)
available; however, it is also a very tedious method for doing day to day ray
tracing and there is a long turn around time (a compile and link) before a
variation can be tested. If you wish to take this route, then see the appendix
on demo programs and check out some of the ray tracer demo programs.
Describing a scene via a scene file is just the opposite. It is very easy
to modify the text description and try a number of small variations of a scene;
but, you are limited to the capabilities of the `language' provided by the
ray tracer's language. This language is called the Scene Definition Language
(SDL) as it defines the objects in a scene, and all of their attributes, in a
way that the parser can understand but which is still quite expressive and
self documenting, for human consumption. This section describes the SDL
syntax and tips for making good use of it. Keep in mind that this appendix is
a rather dry recounting of the syntax. It is assumed that you are working with
the demo scene files that come with the CIDLib distribution. These are well
commented and will provide the kind of practical, hands on experience that
this document cannot provide.
Current Limitations
The biggest limitation of the SDL at this time is that it does not support
function calls and loops, so there is not really any `code' at this time.
Eventually having function calls and loops (for, while, etc...) wil make the
SDL a `real language' and support programatic scene construction that is now
only available by using the ray tracer's C++ interface.
Compilation
Though it is primarily declarative, the SDL is still a compiled language.
However, since it lacks loops and function calls, it is almost totally a
compilation of `data', i.e. the conversion of the objects, textures, and
colors in the scene into CIDLib ray tracer C++ objects. So basically the
compilation is just the creation of a large data structure which is then
traced by a CIDLib program (via the CIDRay.Dll facility.) Eventually, when
loops and functions are added, it will be more of a `real' compilation with
tokenized code. However, that code will still only be run during the
building of the scene and used to programatically create objects, textures,
and colors. For instance, you might create one spoke template object (see
Templates below) and then use a for{} loop to make multiple copies of it and
translate them around to make them extend from a central hub out to a bicycle
tire. But, once the scene is created, there is no need to retain the `code'
part of the scene, even if the scene is saved away and the reloaded later to
complete. So still it will remain primarily a declarative language.
The Scene Object
Even if you just had an empty scene file, there is always a magic object
named `Scene'. This object represents the current scene as a whole and allows
you to affect scene wide attributes such as eye position, default background
color, sampling mode, add objects to the scene, etc... In general it will
only be used in the main scene file, but it is certainly possible for
imported files to change the scene or add objects to the scene if that is
the desired functionality. In this example, some of the scene attributes are
set up:
// Set up the main scene attributes
Scene.Quality(5);
Scene.MaxDepth(5);
Scene.Sample(Normal);
Scene.Color(0.9, 0.9, 0.9);
Scene.EyePos(6, 4, -6);
Scene.LookAt(1.5, 0.5, 0);
Generally the scene setup is done as the first set of operations in the scene
so that they are quickly located and changed. However they can really be done
anywhere. Following is a list of the possible methods that you can call on
the Scene object and what they do.
Scene Object Methods
Here is a list of the possible methods that can be called against the Scene
object. Each one is discussed in more detail following this list.
- AddObject
- AddLight
- Color
- EyePos
- FogColor
- Lens
- LookAt
- MaxDepth
- Quality
- RightDir
- Sample
- UpDir
Here is a short discussion of the Scene methods. They are discussed in more
detail in the ray tracer tutorial.
- AddObject
- This method will add a new object to the scene. The declaration of an
object does not add it to the scene, it just creates a `template' object
which can be inserted into the scene or used to create other objects.
Insertion of an object into the scene actually inserts a copy of the object,
leaving the original for continued use.
- AddLight
- Adds a light source object, but otherwise is just like AddObject().
- Color
- Sets the default background color. If a light ray does not strike any
object directly or by reflection, it will be given this color. It is
convenient for creating a `sky' color. It will never display shadows from
objects in front of it. Defaults to black.
- EyePos
- Sets the position of the eye in the scene.
- FogColor
- Sets the fog color for the scene. This is added to the color of every pixel
of the scene, causing a kind of `washout' effect much like fog. Defaults to
black, which means no fog at all.
- Lens
- Sets the lens type. This value can be `FishEye', `Normal', or `TelePhoto'.
Defaults to Normal.
- LookAt
- Controls where the eye looks at in the scene.
- MaxDepth
- Sets the maximum level of reflection/refraction that will be done by the
tracer. Higher levels can make the scene more realistic but can really
crank up the time taken to render it. Defaults to 2.
- Quality
- The quality of the rendering. There are 6 levels of rendering that control
how realistic the scene will look. The value is given as a vlue from 0 to 5.
Level 3 is often a good quality for a quick and dirty look at the general
look of the scene. Defaults to 5.
- RightDir
- Sets the right direction vector of the scene. This can be used to control
horz/vertical pixel ratios. Most PCs have square pixels these days to it
defaults to {1, 0, 0}.
- Sample
- Sets the sampling mode. It can be `Normal', `Stochastic', or `Statistical'.
Defaults to Normal.
- UpDir
- Sets the up direction vector of the scene. This can be used to roll the
eye perspective around the axis from the eye position to the lookat
position. Defaults to {0, 1, 0}.
Scene Gotchas
This section covers some gotchas to be on the look out for. The flexibility
of a scene description language also means that it can allow you to shoot
yourself in the foot.
- The methods that set up the eye attributes are interrelated, particularly
in respect to the LookAt() method. So you could set up all of the other eye
attributes first then call LookAt(). Otherwise you might get some
interesting to weird effects. The reason for this is that the LookAt() method
uses all of the other attributes in order to do its work.
- The host ray tracer application will generally choose to let the user
override some of the scene attributes from the command line, so the
attributes set here would just provide the defaults for the scene. But this
is up to the particular application.
Scene Objects vs. Template Objects
Just declaring an object does not put it in the scene. You must pass the
object to the the Scene object's AddObject() or AddLight() method in order
to put the object in the scene. The reason for this is that most scenes are
built from `template' objects that are set up and then copied multiple
times and from standard libraries of objects that are imported into a main
scene. If just declaring an object put it in the scene, then all of these
imported and template objects would litter the scene.
When an object is `inserted' into the scene, a copy of the source object
is really put into the scene and the original is left unchanged to be used
again and again. It can be modified without affecting the copy that was put
into the scene. This allows a template object to be created, inserted into
the scene, modified slightly and put in the scene again, and so on. In this
example, the first declaration of a sphere does not put anything in the
scene. But, when AddObject is called, a copy of it is made and put into
the scene with a new name.
//
// Create a sphere. This does nothing to the scene, just makes a
// box shape named MyBox available. Scale it up to twice its
// default size.
//
Object BaseShapes::Box MyBox;
MyBox.Scale(2, 2, 2);
//
// Add it to the scene. A copy of the box is put into the scene
// with a new name of MySceneBox. If we did not care to reference
// it again later then we could have left off the name.
//
Scene.AddObject(MyBox MyNewBox);
An important thing to keep in mind is that this means you can create objects
on the fly just to insert into a scene. You call AddObject() and define an
object right there in place, using the initialization block to initialize it.
So in the above example we could have just done this:
// Do the same thing but all at once
Scene.AddObject
(
BaseShapes::Box MyNewBox
{
Scale(2, 2, 2);
}
);
The reason for doing it the first way above would be that we wanted to set up
a particular style of box and replicate it multiple times. If we later wanted
all of the boxesto be bigger we could just scale the original template object
and all of the copies of it would inherite this new size.
Objects
After you have set up the overall attributes of a scene, you will then want
to create objects to put into the scene. The ray tracer defines a small set
of basic, intrinsic object types from which all other, more complex, objects
are built. There are a couple of ways to put together multiple basic objects
to create more complex object types. As discussed in the previous section,
you can create quite complex objects in separate files and import them into
many scenes, in order to save time and to achieve consistency. The intrinsic
object types are listed below and then discussed separately.
Intrinsic Object Types
All of the predefined object types are within the BaseShapes namespace. So,
for instance, the Box type is named BaseShapes ::Box and Sphere is named
BaseShapes::Sphere. Like the other intrinsic namespaces, they are
automatically imported into all SDL files.
- Box
- Light
- MetaObj
- Plane
- Quadric
- SolidGeom
- Sphere
Here is a short discussion of each of the intrinsic object types and their
default attributes. They are discussed in more detail in the ray tracer
tutorial.
- Box
- A box is, suprise, a 4 sided polygon. When initially created a box is 1
unit on each side and centered on the origin. It can be scaled
non-uniformly to create slabs, boards, square columns, etc...
- Light
- A light is a special shape but it is still treated basically like a shape
by the ray tracer. If it is visible in the scene, then it looks like a
sphere. Initially it, like a sphere, has a 1/2 unit radius and is centered
on the origin.
- MetaObj
- A Metaobject is a compound object that just contains other objects. It is
used in order to group a set of objects together so that they can be
manipulated as a whole. If you rotate the metaobject, it rotates all of the
objects inside it (called its children.) If you apply a texture to it, it
applies that texture to its children.
Meta objects can be nested indefinitely. A metaobject has no size or
ppearance itself.
- Plane
- A plane is an infinitely thin 2 dimensional object. It is defined as a
surface normal vector and a distance from the origin in the opposite
direction of the normal.
- Quadric
- A quadric is a very powerful object described by a quadratic equation. It
can be used to create spheres, paraboloids, cylinders, and cones. By default
a quadric creates a unit sphere on the origin. Note that quadric spheres can
be scaled non-uniformly, making it more powerful than the regular sphere
object. But it also takes much more CPU time to render a quadric sphere.
Parabaloids Cones, and cylinders are unlimited in their primary axis if not
capped off by clipping planes. The quadric object supports specialized
clipping planes for that purpose.
- SolidGeom
- This type of object supports Constructive Solid Geometry, or CSG. CSG is a
very powerful, though slightly CPU consumptive, mechanism for performing
boolean operations on multiple objects. This object type is derived from
MetaObj and inherits all of its capabilities to manage multiple objects.
The first object inserted is the main object and subsequent objects are
applied to the main object via one of the supported boolean operations,
intersection, union, or difference.
- Sphere
- Sphere is a very quick and simple spherical object that is defined as a
center point and a radius. Quadric spheres can be scaled non-uniformly but
regular spheres are always scaled uniformily. This is why they can be
rendered so much more efficiently. The profusion of spheres in ray traced
scenes makes the two types of spheres worth it.
Basic Object Attributes
All objects have attributes that control them. Note that object attributes do
not control how the object `looks', textures do that. Textures will be
discussed below. Object attributes control things like the size, the position,
and the visibility.
All objects support a couple of simple attributes. They are:
- Visibility
- Bounding Object
The visibility attribute just controls whether the object will be seen
(assuming its even in the field of view.) This will be much more useful
in the future, when movies are supported, since it will allow objects to
be visible or invisible at certain times. This attribute is set via the
Visible() method.
The Bounding Object attribute allows another object to be used to bound
this object. Bounding objects are used for efficiency. You can put a very
complex, composite object inside a very simple bounding object such as sphere
or box. When the ray tracer is testing a ray for intersections with the
complex object, it will first test against the bounding object. If the
bounding object is not intersected, then it is assumed that the bounded
object would not have been hit. If the bounding object is intersected, then
the tracer goes on to test the bounded object itself. The bounding object is
set via the Bounding() method. If a bounding object already exists, it is
replaced with the new one.
Basically, if a very complex object is actually quite small in the scene,
having a bounding object (even a relatively complex one like a box) can make
a massive amount of difference in the rendering time. If the complex object
covers a majority of the scene, then the bounding object can actually make
the process longer since it will be hit during the rendering of most of the
scene's pixels anyway.
Object Specific Attributes
Beyond the basic object attributes, each type of object will have attributes
that define it in some way. For instance, a sphere is defined by a center
point and a radius. A box is defined by 6 outward pointing planes, etc...
The ray tracer tutorial covers these.
Textures
This section describes the types of textures available in the CIDLib Ray
Tracer. These are the base textures, which correspond to actual CIDLib C++
classes. From these base textures, an almost infinite number of actual
visual effects can be created, since they can be layered and since most of
them have large number of parameters (attributes) that can be tweaked to
affect the actual effect.
There are two basic types of textures. There are those which affect the
color of an object's surface and those that affect (perturb) the surface
normal of the object. Textures like Waves and Wrinkles are surface
perturbation textures. Textures like Granite or Stripes are color textures.
A texture can actually do both of these things if it chooses to do so. The
basic texture is Surface, which just applies a color to an object. All
other textures are derived from this basic texture. This texture also has
a lot of attributes which affect how a light ray will interact with the
object's surface. So just with this one texture, a huge number of effects
can be achieved.
Intrinsic Textures
An object in a scene has no `appearence' but is only a mathematical
description of that object in `object space'. In order to give objects an
appearence, `textures' are applied to it. A texture can be as simple as a
color and as complex as a 3 dimensional, CRC based noise function or a
bitmapped image. Multiple textures can be layered on top of one another
(each with an optional `transparency' that lets underlying textures show
through) to create very complex effects. All of the predefined textures are
part of the BaseTextures namespace. So, for instance, the Waves texture is
named BaseTextures::Waves and the Granite texture is named
BaseTextures::Granite.
- Agate
- Banded
- Bumps
- Checkers
- Surface
- Dents
- Granite
- Marble
- Spots
- Stripes
- Waves
- Wood
- Wrinkles
Declaring Textures
A texture can be declared in 2 different ways. One is to actually create a
texture object and the other is to create an `immediate' texture. The
immediate texture is used when you don't want to create a named texture
(because perhaps there will never be a need to reuse it), so you just embed
the texture definition in place.
Here is an example of a named texture being created:
//
// A texture (named MyTexture) with all default attributes
// but a color of white.
//
Texture BaseTextures::Surface MyTexture
{
Color(StdColors::White);
}
Here is an example of the same texture being used in the immediate mode. A
plane object is created and the texture is applied to it. It is inserted
straight into the object and does not exist at all in the outer world. It is
not named in this case, but it could have been.
BaseShapes::Plane MyPlane
{
AppendTexture
(
BaseTextures::Surface
{
Color(StdColors::White);
}
);
}
Common Texture Attributes
These are the attributes of the basic Surface texture, and therefore all
textures since they are all derived from Surface.
- Ambient1
- The amount of the object's color contributed by light that is bouncing off
it. From 0.0 to 1.0.
- Diffuse1
- The amount of the object's color contributed by its own color. From 0.0
to 1.0.
- Brilliance
- Tightens or loosens the `sharpness' of diffuse. 0 is very dull (wide
diffusion) and higher numbers it tighter and tighter. There is no real limit
on the value, but around 10.0 is the highest practical value. The effect is
to make something very rough or smooth.
- Metallic
- Controls whether specular reflections and/or Phong reflections use the
color the reflected light or the color of the object itself. Using the
surface color causes more of a metallic effect.
- Phong
- Controls the intensity of specular highlights of the object. 0.0 means
none, and 1.0 means very noticeable specular highlights.
- PhongSize
- Controls how tight or loose the specular reflections are. It is very
similar to Brilliance, but for specular reflections instead of diffuse color.
The values allowed by the SDL are 0.0 to 500.0.
- Reflection
- The amount of light hitting the object that is reflected off of it. 0.0
creates a totatly non-reflective surface and 1.0 creates a mirror like effect.
- Refraction
- The amount of light hitting the object that is refracted through the
object. 0.0 creates a non-refractive surface and 1.0 creates a glass like
effect.
Not Implemented at this time!
- RefractionIndex
- Controls the speed of light through the `substance' of the texture. The
practical effect is the bending of light that occurs when light passes through
things like glass or water. Regular old air is 1.0 and I assume a perfect
vacumn is 0.0. Water is about 1.3 and glass about 1.5.
Not implemented at this time!
- Roughness
- Roughness is a multiplying effect for the tightness of specular highlights.
The actual roughness is 1/Roughness. So larger values create smaller actual
values. When used to scale the intensity of specular reflections, the larger
roughness values will thus make the specular reflections less intense and
focused. The limit allowed by the SDL is 0.0 to 1000.0.
A large roughness creates a matte or chalky surface, because its rough
surface reflects light in a non-coherent manner. A small roughness makes
the surface more glassy and keeps specular highlights tight.
- Specular
- Controls the intensity of specular highlights. 0.0 means none at all and
1.0 means very intense specular highlights.
The sum of Ambient and Diffuse can never be more than 1.0. So, if you set
either of them and the result would violate this rule, the other is bumped
down to the highest legal it can be. If the sum would not be invalid, the
other is left unaffected.
Noise Textures
A large number of textures are created by using a CRC based, 3D `noise
function'. The noise function is used in a number of ways to create complex,
often random looking textures such as marble and wood. These types of
textures are generally characterized by a major color with random swirls or
blotches of one or more other colors through them. All of the noise textures
have these attributes:
- Bump
- Controls how extreme the surface perturbation wil be for those textures
that use the noise function to adjust the surface normal. From 0.0 to 1.0.
- Frequency
- Controls how frequent (i.e. close together) the surface perturbation
features are. As the number goes up, they get closer together. From 0.0 to
1.0.
- Phase
- The phase is a way to offset a noise texture such that it can be moved
over time, in multiple frames of a sequence of frames. This will make the
texture seem to move over the object in increments of the frequency. A value
of 0 does not effect the texture and 1 takes it all the way back around to
the start again. From 0.0 to 1.0.
- Turbulence
- Controls the randomness of the effects of the noise function. From 0.0
to 1.0.
Banded Textures
Banded textures are derived from the noise texture class. They add an extra
spin to the noise texture by defining bands of color that are then switched
between by the value of the noise function. This allows for disjoint bands
of colors so that there are sudden shifts in the color for sometimes small
amounts of difference in the noise value. Marble, granite, and wood textures
are of this sort because they have thin bands of color mixed into a base color.
Banded textures just add one extra attribute, which is a set of color
bands. They are added by using:
- AppendBand
- Adds a new color band to the end of the current list of color bands for
this texture.
- PrependBand
- Adds a new color band to the beginning of the current list of color bands
for this texture. The ordering of textures is important when layering
multiple textures onto an object. If a texture is layered over another
texture and is given a transparent or semi-transparent color, the texture
underneath it will show through. This allows for quite complex and subtle
textures to be created. When the first nontransparent texture is hit,
starting from the beginning of the list, no more textures are processed for
the object.
Intrinsic Vectors
A set of standard vectors are implicitly imported into every file. These are
provided in order to save work and to give more symbolic representation to
declarations. You can create your own vectors of course, but these are often
convenient for on the fly declarations that require these often used vectors.
- Up
- Down
- Left
- Right
- Forward
- Backward
These vectors are part of the BaseVectors namespace, so the Left vector would
be referenced as BaseVectors::Left. Note that Forward and backward are from the
perspective of the standard `back on the z' viewpoint. So forward points in the
positive z direction and backward points in the negative z direction.
General Syntax
The SDL used by CIDLib is somewhat C++ like, and uses C++ type comments,
braces, and semicolons. It is, like C++, basically totally free format as
to the formatting of its text, as long as the correct syntax is used. So it
does not care if you write a single, thousand character line or use a very
elaborate and formal formatting scheme. Its up to you. This section covers
the general philosply of the SDL and the basic rules of its syntax.
Special Characters
A set of special characters are used to demark the parts of the scene and
the individual commands that make up those parts. So they have special
meaning and cannot be used in the names of any objects.
- } (closing brace)
- { (open brace)
- > (closing bracket)
- < (open bracket)
- ( (open parenthesis)
- ) (close parenthesis)
- , (comma)
- ; (semi colon)
- // (comment)
- :: (double colon)
Name Spaces
Because of the tediousness of creating new, highly detailed objects, it is
very convenient to write files of standard textures, colors, and objects
from which you can draw on when you create new scenes. For instance, you
could have a file of standard kitchenware objects or a file of standard
furniture objects or architectural elements. You need to be able to include
these standard files into your main scene files by names that are meaningful.
This ability to build up libraries of standard shapes and textures allows
you to leverage your hard work over and over; however, it also means that
now you potentially have lots of objects coming into your main scene file
whose content you created long ago or which were created by another person.
This opens up the possibility of having multiple objects or textures or
colors with the same name. In order to avoid this, the ray tracer uses name
spaces to divide the possible universe of object names into discrete chunks
that can be brought into the scene as needed. A namespace is a separate file
that can be imported into your main scene file. All objects declared inside
this namespace are referenced via the namespace name. The root part of the
imported file is the name of the namespace and it must have an extension of
RtInc. So the sample below imports the standard colors namespace and uses
some colors from it. StdColors is a namespace file that is distributed with
the CIDLib system. It is not magical in any way and you don't have to use
it. Its just distributed as a convenience. It defines a lot of commonly used
colors so as to insure that they have standardized names and to keep you
from having to figure them out yourself.
// Import the standard colors namespace
Import StdColors;
// Create a color of my own with initial value of black
Color MyColor(StdColors::Black);
// Could have also done this, but its less obvious
Color MyColor(0, 0, 0);
Within a file, all objects that are not referred to via a namespace are
assumed to have been declared with the current namespace. So, within the
StdColors file, it can refer to other objects declared there without the
namespace prefix because the current namespace is implied if not explicitly
given. So in this snippet, from the namespace file MySpace, you can see how
the name space is implied.
// -----------------------
// File: MySpace.RtInc
// -----------------------
// Declare a dark grey scale color
Color MyColor(0.2, 0.2, 0.2);
// Create another color using the first. MyColor has implied namespace
Color MyDerivedColor(MyColor);
// We could have also done this and explicitly used the namespace
Color MyOtherDerivedColor(MySpace::MyColor);
The main scene file always has an implied name space of "Main", but it is
usually never used since, as just discussed, you only need to use the name
space name when you refer to objects outside of the current name space.
There are a few namespaces which are always implicitly imported. These
name spaces are used to import the intrinsic shapes and textures that are
defined by CIDRay itself, and from which all other shapes and textures are
derived directly or indirectly. These name spaces are:
- BaseShapes
- BaseTextures
- BaseVectors
BaseShapes brings in the fundamental shapes defined by CIDRay. These shapes
correspond to actual C++ classes in the ray tracer engine. BaseTextures does
the same for the intrinsic textures defined by the ray tracer. BaseVectors
provides symbolic names for the very often used vectors that point straight
up, straight down, left, right, etc... There are no actual files that
represent these name spaces, they are magically created and imported by the
scene parser.
Comments
Comments in the SDL are exactly like C++ comments. They start with the
double forward slash // and anything after that comment character is
considered comment text. You can put comments anywhere you want in the file,
with the obvious limitation of being sure not to comment out your own code
accidentally.
// This is an SDL comment
SphereTexture.Reflection(0.5); // This is a comment also
Methods
In all of the examples above, you have seen objects declared and then
something that looks like function calls used with them. The SDL uses a C++
like way of attaching function calls to the types of objects that they apply
to. If you are familar with object oriented programming techniques, this is
a pretty instantly recognizable scheme. If not, you can either just go with it
and look at the sample scenes to see how it works, or bone up a little on
object oriented concepts via some other source.
There are two ways that you can call methods. In this example, you will
see both schemes. When you declare an object, you can use an initialization
block to set up all of the desired attributes at the same time. Within the
initialization block, the name of the object associated with the method calls
is implied, so you can save a lot of typing.
//
// Declare a texture and initialize it in an initialization
// block. Within the block, this object is the implied target
// of the method calls.
//
Texture BaseTextures::Color SphereTexture
{
Color(BaseColors::White);
Brilliance(10.0);
Reflection(0.6);
PhongSize(100.0);
Phong(1.0);
}
//
// The same commands could also be done outside the block like
// this but the object has to be named in order to do so!
// Otherwise there is no way to refer to it.
//
SphereTexture.Reflection(0.3);
The other scheme for call methods on an object is to call it via the name
of the object. It is done in the C++ style where the name is followed by a
period and then the method and its parameters. As always, if you want to
call a method on an object from another namespace, you must use the object's
namespace name to reference it. So, to scale MyObject from the imported name
space MySpace, you would do this:
// Import MySpace
Import MySpace;
// Scale my object by 2 in the y
MySpace::MyObject.Scale(1, 2, 1);