Raytracing Fundamentals By the time we are in kindergarten almost all of us can draw simple shapes like circles, squares, and lines. We would then be able to possibly combine these shapes to create things such as stick figures. And perhaps some of us would continue on and draw more realisticly. Finally a selected few of us might become true artists, using shading and imitating light. Even the most gifted artists spend a considerable amount of time concentrating on how light and shadow effects their artwork. When light and shadow are interpreted well by an artist, the results of his work can be very realistic. Throughout the years man has been facinated with art, but the skill of actually being a great artist was limited to only a few gifted people. The advent of the computer has changed all of this. In the 1960's when computers were invented, the makers of these machines saw them as number crunchers that could not deal with other media. Even though this was true, I am sure people realized that there were other possibities, as Havelock Ellis once said "It is here in mathamatics that the artist has the full scope of his imagination" As time went on computers gained artistic capabilities, at the same time they kept their mathmatical ones. This lead to raytracing, which is the use of a computer to imitate the behavior of light. The computer and raytracing software use the laws of physics and then interprets them into mathmatical equations to best resemble the path that light takes. Doing this takes a considerable amount of time because the computer makes thousands upon thousands of floating point calculations, and on even today's fastest computers a complex scene with many light sources may take a good while. But this does not limit us, because we still can create the same effects on a personal computer, it may take a while longer but the results are definitly worth the wait. Raytraced scenes are noted for their extremely realistic apearance and are often mistaked for photographs. Almost anything is possible with raytracing, from creating a perfect model of something as simple as a cola bottle, to something as complicated as a preview of the inside of a building. Animation is also possible with raytracing, and is used today alot in commercials and movies. With all these possibilities and power of raytracing one might excpect raytracing software to be too expensive for the average joe, but the truth is your excpectation is totally wrong. With an IBM PC or compatible you can create profesional Raytraced images with extremely sofisticated software that is absolutly FREE! The reason is that people in the PC world finally got fed up with the high prices of software, and started a marketing technique that has revolutionized home computing. It is a concept where the author of some type of software decides to distribute his software as free. This concept is called "ShareWare", and it will make your life a whole lot easier if you are a IBM and compatible PC user. The lack of many shareware programs for the Macintosh makes it very hard to find in the mac format, but you might find something, just keep your eyes open. ----------------------------------------------------------------------------- You know what a Raytracer does and how much the cost is, but where do you get this software? Well there are a few ways, for one you might be able to download them from a online service such as America Online, and you will definitly be able to find them on the internet. But if you do not have access to either you can call a BBS that has these programs. Look in file areas marked as GRAPHIC UTILITYS or maybe even RAYTRACING. Here is a small list of some of the raytracers avaliable: PovRay................The best and most popular Shareware Raytracer avaliable PolyRay Vivid DKB-Trace I run a Bulletin Board myself and you may call and download some of these raytracers along with other thing, my BBS is called FREELOAD BBS, and the number is (415) 348-8806. Look in my GRAPHICS UTILITYS file area. Now that you know what raytracers do, and where to get them we can now move on and see how these wonderfull programs actually work. ----------------------------------------------------------------------------- Creating a Raytraced Scene With PovRay I will be using PovRay in my examples, for it is the most popular and widely used Raytracer. Now we come to using the raytracer. We will be writing code to create scenes that will then be compiled by the PovRay into a TGA image file. One of the most important things you must understand about Raytracing is the 3-D coordinate System. Its Syntax is . The X axis moves Horizontily Left and right, the Y axis moves verticly up and down, and the Z acces moves forward to backward 3 demensionaly. Every RayTraced scene has an origin, which is usually at <0 0 0>, meaning 0 units to the left or right, 0 units up or down, and 0 units forward or backward. So a location of <2 3 1> would mean 2 units to the right of the origin, 3 units above the origin and 1 unit in front of the origin. And <-2 -3 -1> would mean 2 units to the left of the origin, 3 units below, and 1 unit in behind it. Ok we have a way to measure our scenes, and a way to find locations. Now we need an Object to be in our scene. The basic geometric shapes (such as the sphere, box, cone, disk, and triangle) and a few not-so-basic shapes (like the torus or donut, polynomial, height field, Bezier patch, and triangular patch) are called primitives. These shapes can be used as buliding blocks for other more complicated shapes. Another thing we need in our scene is a view point, or a "Camera". A camera is treated as a special object that both has a location and a place to look at or be pointed at, but can NEVER be seen, because it will be taking the picture. The following example declares a camera in your scene: camera { location <0 1 -2> look_at <0 1 2> } This will produce a camera that is located 1 unit above the origin and 2 units behind it. It would be looking at a location 1 unit above the origin and 2 units in front of the origin. All this is great, but there is one thing missing, light. Without light you cannot see anything, so now we introduce what is known as the light source. The light source is treated as an object, meaning you give it a location. Look at the following example of stating a light source: object { light_source { <3 3 -3> color white } } This would creat a light source that produces White Light, 3 units to the right of the origin, 3 units above the origin, and 3 units in front of the origin of our scene. Next we will actually be creating scenes. You will need a standard ascii based text editor if you want to actually follow along and create the scenes, EDIT is an editor that comes with dos, and will work fine. Otherwise if you don't want to participate you may just simply continue reading. With all this knowledge we can finally create something! This first scene will be a simple scene with a sphere sitting on an endless plane. The following is the actual text file with all the code: NOTE: two /'s represent a comment in a scene so whenever you see //, it means that is just a comment. ----------------------------------------------------------------------------- // A shiny red ball on a green floor camera { location <0 1 -2> look_at <0 1 2> } object { sphere { <0 1 2> 1 } texture { color red 1 phong 1} } object { plane { <0 1 0> 0 } texture { color green 1 } } object { light_source { <3 3 -3> color red 1 green 1 blue 1 } } ------------------------------------------------------------------------------ Now we will tear this apart piece by piece and explain everything. I will start with the camera. camera { location <0 1 -2> look_at <0 1 2> } This statement represents a camera that is located 1 unit above and 2 units behind the sphere. The camera is looking at the location <0 1 2> which is the location of the sphere object { sphere { <0 1 2> 1 } texture { color red 1 phong 1} } This statement declares the sphere. This sphere is 1 unit above the origin and 2 units in front of it. The texture statement declares the color, and in this case Phong which is the size of the highlight on the sphere. So in this scene the sphere is 100 percent red (red 1) and has a phong size of 1 (phong 1). } object { plane { <0 1 0> 0 } texture { color green 1 } } This code declares the plane that the sphere is sitting on. Which you can simply think of as the floor. As you can see it is green with no phong. object { light_source { <3 3 -3> color red 1 green 1 blue 1 } } And the final touch of the scene is the light source, remember without it all would be useless because we would not be able to see anything in the dark. It is located 3 units to the right 3 units above and 3 units behind the origin. The mixture of red green and blue make white, the natural color of light. We are now done with creating a small and simple universe. This is one of the simplist scenes posible, so do not even begin to think this is anywhere close to the limit, when really the possibilities are absolutly endless! We will now move on to slightly more complicated scenes ivolving different shapes and combining these shapes to create more interesting shapes. ------------------------------------------------------------------------------ How to make Povray render or compile your scenes We know how to describe a scene for Povray, but how do we turn all those statements into a picture? Well we need to render them. If you have ever done any programing, you may think of rendering as compiling. If you have not already, please save the text file you created with your text editor, the name may be anything with the .POV extension. Now the following command line will render a scene: POVRAY +Imyfile +W640 +H480 +Omyfile.tga First we typed in POVRAY, which is the execution file. Then we added the switch +I which is the Input file or your text file, it is immediatly proceeded by the name of the text file which includes the povray decription text file. Then we used the +W switch which determines the number of pixels wide the scene file should be, and the +H switch determines the number of pixels high the scene should be. Note that you should stick with these basic sizes: 320 by 200, 320 by 240, 320 by 400, 320 by 480, 360 by 480, 640 by 480, 640 by 400, 800 by 600, 1024 by 768, or 1280 by 1024. If you use other dimensions you might get a distorted picture. Also note that the higher the set of numbers the finer the picture quality will be. Finally we come to the +O switch, this is the filename you want your picture to be called. The format must be .TGA. But you can change this format with many different shareware programs, one that I can think of off the top of my head is GRAPHIC WORK SHOP, and you can find it on my bulletin board. (415) 348-8806 ---------------------------------------------------------------------------- Combining Shapes As you may have suspected, we could have possibly included another sphere into our first scene and had two spheres. Now imagine that we had one sphere at <0 2 2> and another at <1 2 2>. What do you think would happen? Would they overlap or would povray give an error message? Well the answer is they would overlap, Povray will never butt in because of the way you place your objects. We could possibly use three or four spheres to create something like a snow man. We might do it by placing spheres closely on top of each other, and changing the sizes of each sphere. The size of the sphere is defined in its declaration: object { sphere { <0 2 2> 1 } } The 1 in { <0 2 2> 1 } is the size of the sphere, in this scene the sphere is 1 unit large, you may size it up or down by changing the number to be larger, or smaller. For example: { <0 2 2> .4 } would make the sphere smaller because I used a decimal less then 1, whereas { < 0 2 2> 1.5 } would make the number larger, because it is greater than 1. We have covered the sphere, now we will talk about the box. The syntax for the box is: box { } Corner1 is the x,y,z location of one of the box's corners while corner2 is the x,y,z location of the other corners location. Each of the x,y,z values of corner1 must be smaller than the coresponding x,y,z location for corner2. The following is an example of box: object { box { <-1 0 -1> <1 2 1> } texture { color red 1 phong 1 } } This box's bottom left back corner is located 1 unit left, and 1 unit behind the origin, while the oposite corner (The top right front corner) is located 1 unit to the right, 2 units above and 1 unit infront of the origin. This makes the box 2 units wide, 2 units high, and 2 units long, a perfect cube. Finally the color of the square is solid red with a phong size of 1. Note that on a box the phong is not as brilliant as on a sphere, the fact is spheres are nice and rounded, while box's are flat edged, this causes less highlight. Now that we know how to use a box, and that a scene can have multiple objects to create interesting shapes, we will put this knowledge to work in a scene that will house a table. The following is the actual code to create this table scene, we will then pick this code apart and explain it piece by piece. ----------------------------------------------------------------------------- // A union of several boxes combined to make a table camera { location <5 6 -8> look_at <0 5 0> } // The union of boxes making the table object { union{ box { <-2 0 1.5> <-1.5 4 2> } // Back left leg box { <-2 0 -2> <-1.5 4 -1.5> } // Front left leg box { <1.5 0 1.5> <2 4 2> } // Back right leg box { <1.5 0 -2> <2 4 -1.5> } // Front right leg box { <-2.5 4 -2.5> <2.5 4.2 2.5> } // Table Top } texture { color red 1 } } // A white light source behind and to the right of the camera object { light_source { <10 15 -20> color red 1 green 1 blue 1 } } ---------------------------------------------------------------------------- Before we pick this code apart I want to explain the union. This scene could have been made using boxes and squares individualy. For example we could actually type the following in for EACH of the boxes: object { box { <-2 0 1.5> <-1.5 4 2> } // Back left leg texture { color red 1 } } This is how we would think to do it. But there is a better way, why not just combine all the boxes into one mass and then color the whole mass, this is what the statement "union" is used for. The syntax for union is: object { union { shape_1 shape_2 shape_3 ... } } Union saves alot of key strokes, and gives the advantage of having just one shape to work with. Now onto explaing the table scene! camera { location <5 6 -8> look_at <0 5 0> } This as you know is the location and viewpoint of the camera. Pretty much self explanitory. object { union{ box { <-2 0 1.5> <-1.5 4 2> } // Back left leg box { <-2 0 -2> <-1.5 4 -1.5> } // Front left leg box { <1.5 0 1.5> <2 4 2> } // Back right leg box { <1.5 0 -2> <2 4 -1.5> } // Front right leg box { <-2.5 4 -2.5> <2.5 4.2 2.5> } // Table Top } texture { color red 1 } } This is the union of boxes that form the table, I am not going to go into how each box is located because it would take far to long, but it can be simply figured out. Also note that I did not just make this table straight from my head, it would be virtualy impossible. Instead I got some graph paper and used every two squares as a unit, and figured out the locations needed to make a table, this is the recommended aproach. Now notice the TEXTURE statement, using it for the whole union as red 1, makes the whole table red. object { light_source { <10 15 -20> color red 1 green 1 blue 1 } } And this is simply the location of the light source that gives off white light. ----------------------------------------------------------------------------- We have covered alot, but there is still MUCH more to be covered! We cannot take the time to do it here. But if you are interested in learning more there are many ways to go about it. For one a good book will teach you alot, here are a couple I recomend: * RayTracing Creations by the Waite Group - Excelent book that will teach you almost everything you need to know about raytracing, this one is highly recomended. * Making Movies on Your PC - This book goes beyond Raytracing still images, into the world of Raytracing animations. If you want to keep up to date and stay in touch with all the Raytracing attics, you should go online! Here are a few places to try: * Compuserve Information Service - Goto the GRAPHDEV forum * You Can Call Me Ray BBS, or YCCMR (708) 358-5611 Located in Palatine, IL. A great bulletin board for graphic freeks. * The Graphics Alternative BBS or TGA (510) 524-2780 Located in El Cerito, CA. A good graphics BBS, with about everything you ever wanted on the subject * FreeLoad BBS, (415) 348-8806 Located in San Mateo CA, I run this board. Look in the Graphics Utilitys File area. ------------------------------------------------------------------------------ Ways to reach me electronicly if you have any comments are: - Through my BBS, FreeLoad BBS (415) 348-8806 (Leave a comment to the Sysop, me) - Through fidonet at 1:125/225 - Through the internet at JOHN.MCGRAW@NERDSHACK.COM I hope you enjoyed this Text! And Have Fun! -John McGraw