home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 19 / CD_ASCQ_19_010295.iso / vrac / rtbeginr.zip / RAYTRACE.TXT < prev    next >
Text File  |  1994-11-07  |  18KB  |  441 lines

  1.                              Raytracing Fundamentals
  2.  
  3.  
  4.    By the time we are in kindergarten almost all of us can draw simple shapes 
  5. like circles, squares, and lines. We would then be able to possibly combine 
  6. these shapes to create things such as stick figures. And perhaps some of us 
  7. would continue on and draw more realisticly. Finally a selected few of us 
  8. might become true artists, using shading and imitating light. Even the most 
  9. gifted artists spend a considerable amount of time concentrating on how light 
  10. and shadow effects their artwork. When light and shadow are interpreted well 
  11. by an artist, the results of his work can be very realistic. Throughout the 
  12. years man has been facinated with art, but the skill of actually being a great 
  13. artist was limited to only a few gifted people. The advent of the computer has 
  14. changed all of this.
  15.  
  16.    In the 1960's when computers were invented, the makers of these machines 
  17. saw them as number crunchers that could not deal with other media. Even though 
  18. this was true, I am sure people realized that there were other possibities, as 
  19. Havelock Ellis once said "It is here in mathamatics that the artist has the 
  20. full scope of his imagination"
  21.  
  22.   As time went on computers gained artistic capabilities, at the same time 
  23. they kept their mathmatical ones. This lead to raytracing, which is the use of 
  24. a computer to imitate the behavior of light. The computer and raytracing 
  25. software use the laws of physics and then interprets them into mathmatical 
  26. equations to best resemble the path that light takes. Doing this takes a 
  27. considerable amount of time because the computer makes thousands upon 
  28. thousands of floating point calculations, and on even today's fastest 
  29. computers a complex scene with many light sources may take a good while. But 
  30. this does not limit us, because we still can create the same effects on a 
  31. personal computer, it may take a while longer but the results are definitly 
  32. worth the wait.
  33.  
  34.    Raytraced scenes are noted for their extremely realistic apearance and are 
  35. often mistaked for photographs. Almost anything is possible with raytracing, 
  36. from creating a perfect model of something as simple as a cola bottle, to 
  37. something as complicated as a preview of the inside of a building. Animation 
  38. is also possible with raytracing, and is used today alot in commercials and 
  39. movies.
  40.  
  41.    With all these possibilities and power of raytracing one might excpect 
  42. raytracing software to be too expensive for the average joe, but the truth is 
  43. your excpectation is totally wrong. With an IBM PC or compatible you can 
  44. create profesional Raytraced images with extremely sofisticated software that 
  45. is absolutly FREE!  The reason is that people in the PC world finally got fed 
  46. up with the high prices of software, and started a marketing technique that 
  47. has revolutionized home computing. It is a concept where the author of some 
  48. type of software decides to distribute his software as free. This concept is 
  49. called "ShareWare", and it will make your life a whole lot easier if you are 
  50. a IBM and compatible PC user. The lack of many shareware programs for the
  51. Macintosh makes it very hard to find in the mac format, but you might find 
  52. something, just keep your eyes open.
  53.  
  54.  
  55. -----------------------------------------------------------------------------
  56.    
  57.  
  58.    You know what a Raytracer does and how much the cost is, but where do you 
  59. get this software? Well there are a few ways, for one you might be able to 
  60. download them from a online service such as America Online, and you will 
  61. definitly be able to find them on the internet. But if you do not have access 
  62. to either you can call a BBS that has these programs. Look in file areas 
  63. marked as GRAPHIC UTILITYS or maybe even RAYTRACING. Here is a small list of 
  64. some of the raytracers avaliable:
  65.  
  66. PovRay................The best and most popular Shareware Raytracer avaliable
  67. PolyRay
  68. Vivid
  69. DKB-Trace
  70.  
  71.    I run a Bulletin Board myself and you may call and download some of these 
  72. raytracers along with other thing, my BBS is called FREELOAD BBS, and the 
  73. number is (415) 348-8806. Look in my GRAPHICS UTILITYS file area.
  74.  
  75.    Now that you know what raytracers do, and where to get them we can now move 
  76. on and see how these wonderfull programs actually work.
  77.  
  78.  
  79.  
  80. -----------------------------------------------------------------------------
  81.  
  82.  
  83.                       Creating a Raytraced Scene With PovRay
  84.  
  85.  
  86.  
  87.    I will be using PovRay in my examples, for it is the most popular and 
  88. widely used Raytracer. Now we come to using the raytracer. We will be writing 
  89. code to create scenes that will then be compiled by the PovRay into a TGA 
  90. image file. One of the most important things you must understand about 
  91. Raytracing is the 3-D coordinate System. Its Syntax is <X Y Z>. The X axis
  92. moves Horizontily Left and right, the Y axis moves verticly up and down, and 
  93. the Z acces moves forward to backward 3 demensionaly.
  94.  
  95.    Every RayTraced scene has an origin, which is usually at <0 0 0>, meaning 
  96. 0 units to the left or right, 0 units up or down, and 0 units forward or 
  97. backward.  So a location of <2 3 1> would mean 2 units to the right of the 
  98. origin, 3 units above the origin and 1 unit in front of the origin. And
  99. <-2 -3 -1> would mean 2 units to the left of the origin, 3 units below, 
  100. and 1 unit in behind it.
  101.  
  102.    Ok we have a way to measure our scenes, and a way to find locations. Now 
  103. we need an Object to be in our scene. The basic geometric shapes (such as the 
  104. sphere, box, cone, disk, and triangle) and a few not-so-basic shapes (like 
  105. the torus or donut, polynomial, height field, Bezier patch, and triangular 
  106. patch) are called primitives. These shapes can be used as buliding blocks
  107. for other more complicated shapes.
  108.  
  109.    Another thing we need in our scene is a view point, or a "Camera". A camera 
  110. is treated as a special object that both has a location and a place to look at 
  111. or be pointed at, but can NEVER be seen, because it will be taking the 
  112. picture. The following example declares a camera in your scene:
  113.  
  114. camera {
  115.   location <0 1 -2>
  116.   look_at <0 1 2>
  117. }
  118.  
  119. This will produce a camera that is located 1 unit above the origin and 2 units 
  120. behind it. It would be looking at a location 1 unit above the origin and 2 
  121. units in front of the origin.
  122.  
  123.    All this is great, but there is one thing missing, light. Without light you 
  124. cannot see anything,  so now we introduce what is known as the light source. 
  125. The light source is treated as an object, meaning you give it a location. 
  126. Look at the following example of stating a light source:
  127.  
  128. object {
  129.   light_source { <3 3 -3> color white }
  130. }
  131.  
  132. This would creat a light source that produces White Light, 3 units to the 
  133. right of the origin, 3 units above the origin, and 3 units in front of the 
  134. origin of our scene.
  135.  
  136.   Next we will actually be creating scenes. You will need a standard ascii 
  137. based text editor if you want to actually follow along and create the scenes, 
  138. EDIT is an editor that comes with dos, and will work fine. Otherwise if you 
  139. don't want to participate you may just simply continue reading.
  140.  
  141.  
  142.   With all this knowledge we can finally create something!  This first scene 
  143. will be a simple scene with a sphere sitting on an endless plane. The 
  144. following is the actual text file with all the code: 
  145. NOTE: two /'s represent a comment in a scene so whenever you see //, it means 
  146. that is just a comment.
  147.  
  148. -----------------------------------------------------------------------------
  149. // A shiny red ball on a green floor
  150. camera {
  151.   location <0 1 -2>
  152.   look_at <0 1 2>
  153. }
  154. object {
  155.   sphere { <0 1 2> 1 }
  156.   texture { color red 1 phong 1}
  157. }
  158. object {
  159.   plane { <0 1 0> 0 }
  160.   texture { color green 1 }
  161. }
  162. object {
  163.   light_source { <3 3 -3> color red 1 green 1 blue 1 }
  164. }
  165. ------------------------------------------------------------------------------
  166.  
  167. Now we will tear this apart piece by piece and explain everything. I will 
  168. start with the camera.
  169.  
  170. camera {
  171.   location <0 1 -2>
  172.   look_at <0 1 2>
  173. }
  174.  
  175. This statement represents a camera that is located 1 unit above and 2 units 
  176. behind the sphere. The camera is looking at the location <0 1 2> which is 
  177. the location of the sphere
  178.  
  179. object {
  180.   sphere { <0 1 2> 1 }
  181.   texture { color red 1 phong 1}
  182. }
  183.  
  184. This statement declares the sphere. This sphere is 1 unit above the origin and 
  185. 2 units in front of it. The texture statement declares the color, and in this 
  186. case Phong which is the size of the highlight on the sphere. So in this scene 
  187. the sphere is 100 percent red (red 1) and has a phong size of 1 (phong 1).
  188.  
  189. }
  190. object {
  191.   plane { <0 1 0> 0 }
  192.   texture { color green 1 }
  193. }
  194.  
  195. This code declares the plane that the sphere is sitting on. Which you can 
  196. simply think of as the floor. As you can see it is green with no phong.
  197.  
  198.  
  199.  
  200. object {
  201.   light_source { <3 3 -3> color red 1 green 1 blue 1 }
  202. }
  203.  
  204. And the final touch of the scene is the light source, remember without it all 
  205. would be useless because we would not be able to see anything in the dark. It 
  206. is located 3 units to the right 3 units above and 3 units behind the origin. 
  207. The mixture of red green and blue make white, the natural color of light.
  208.  
  209.  
  210.    We are now done with creating a small and simple universe. This is one of 
  211. the simplist scenes posible, so do not even begin to think this is anywhere 
  212. close to the limit, when really the possibilities are absolutly endless!  We 
  213. will now move on to slightly more complicated scenes ivolving different shapes 
  214. and combining these shapes to create more interesting shapes.
  215.  
  216.  
  217. ------------------------------------------------------------------------------
  218.  
  219.                    How to make Povray render or compile your scenes
  220.  
  221.    We know how to describe a scene for Povray, but how do we turn all those 
  222. statements into a picture? Well we need to render them. If you have ever done 
  223. any programing, you may think of rendering as compiling. If you have not 
  224. already, please save the text file you created with your text editor, the 
  225. name may be anything with the .POV extension. Now the following command line 
  226. will render a scene:
  227.  
  228. POVRAY +Imyfile +W640 +H480 +Omyfile.tga
  229.  
  230. First we typed in POVRAY, which is the execution file. Then we added the 
  231. switch +I  which is the Input file or your text file, it is immediatly 
  232. proceeded by the name of the text file which includes the povray decription 
  233. text file. Then we used the +W switch which determines the number of pixels 
  234. wide the scene file should be, and the +H switch determines the number of 
  235. pixels high the scene should be. Note that you should stick with these basic 
  236. sizes:
  237.  
  238. 320 by 200, 320 by 240, 320 by 400, 320 by 480, 360 by 480, 640 by 480, 
  239. 640 by 400, 800 by 600, 1024 by 768, or 1280 by 1024.
  240.  
  241. If you use other dimensions you might get a distorted picture. Also note that 
  242. the higher the set of numbers the finer the picture quality will be.
  243.  
  244. Finally we come to the +O switch, this is the filename you want your picture 
  245. to be called. The format must be .TGA. But you can change this format with 
  246. many different shareware programs, one that I can think of off the top of my 
  247. head is GRAPHIC WORK SHOP, and you can find it on my bulletin board. 
  248. (415) 348-8806
  249.  
  250.  
  251.  
  252. ----------------------------------------------------------------------------
  253.  
  254.  
  255.                            Combining Shapes
  256.  
  257.  
  258.    As you may have suspected, we could have possibly included another sphere 
  259. into our first scene and had two spheres. Now imagine that we had one sphere 
  260. at <0 2 2> and another at <1 2 2>. What do you think would happen? Would they 
  261. overlap or would povray give an error message? Well the answer is they would 
  262. overlap, Povray will never butt in because of the way you place your objects. 
  263. We could possibly use three or four spheres to create something like a snow 
  264. man. We might do it by placing spheres closely on top of each other, and 
  265. changing the sizes of each sphere. The size of the sphere is defined in its 
  266. declaration:
  267.  
  268. object {
  269.   sphere { <0 2 2> 1 }
  270. }
  271.  
  272.    The 1 in { <0 2 2> 1 } is the size of the sphere, in this scene the sphere 
  273. is 1 unit large, you may size it up or down by changing the number to be 
  274. larger, or smaller. For example: { <0 2 2> .4 } would make the sphere smaller 
  275. because I used a decimal less then 1, whereas { < 0 2 2> 1.5 } would make the 
  276. number larger, because it is greater than 1.
  277.  
  278.    We have covered the sphere, now we will talk about the box. The syntax for 
  279. the box is:
  280.  
  281. box { <corner1> <corner2> }
  282.  
  283.    Corner1 is the x,y,z location of one of the box's corners while corner2 is 
  284. the x,y,z location of the other corners location. Each of the x,y,z values of 
  285. corner1 must be smaller than the coresponding x,y,z location for corner2. The 
  286. following is an example of box:
  287.  
  288. object {
  289.   box { <-1 0 -1> <1 2 1> }
  290.   texture { color red 1 phong 1 }
  291. }
  292.  
  293.   This box's bottom left back corner is located 1 unit left, and 1 unit behind 
  294. the origin, while the oposite corner (The top right front corner) is located 
  295. 1 unit to the right, 2 units above and 1 unit infront of the origin. This 
  296. makes the box 2 units wide, 2 units high, and 2 units long, a perfect cube.  
  297. Finally the color of the square is solid red with a phong size of 1. Note that 
  298. on a box the phong is not as brilliant as on a sphere, the fact is spheres are 
  299. nice and rounded, while box's are flat edged, this causes less highlight.
  300.  
  301.  
  302.  
  303.    Now that we know how to use a box, and that a scene can have multiple 
  304. objects to create interesting shapes, we will put this knowledge to work in a 
  305. scene that will house a table. The following is the actual code to create this 
  306. table scene, we will then pick this code apart and explain it piece by piece.
  307.  
  308. -----------------------------------------------------------------------------
  309. // A union of several boxes combined to make a table
  310. camera {
  311.   location <5 6 -8>
  312.   look_at <0 5 0>
  313. }
  314. // The union of boxes making the table
  315. object {
  316.   union{
  317.      box { <-2 0 1.5> <-1.5 4 2> }     // Back left leg
  318.      box { <-2 0 -2> <-1.5 4 -1.5> }    // Front left leg
  319.      box { <1.5 0 1.5> <2 4 2> }       // Back right leg
  320.      box { <1.5 0 -2> <2 4 -1.5> }     // Front right leg
  321.      box { <-2.5 4 -2.5> <2.5 4.2 2.5> }  // Table Top
  322.   }
  323.   texture {
  324.      color red 1
  325.    }
  326. }
  327. // A white light source behind and to the right of the camera
  328. object {
  329.   light_source { <10 15 -20> color red 1 green 1 blue 1 }
  330. }
  331. ----------------------------------------------------------------------------
  332.  
  333. Before we pick this code apart I want to explain the union. This scene could 
  334. have been made using boxes and squares individualy. For example we could 
  335. actually type the following in for EACH of the boxes:
  336.  
  337. object {
  338.   box { <-2 0 1.5> <-1.5 4 2> }     // Back left leg
  339.   texture { color red 1 }
  340. }
  341.  
  342. This is how we would think to do it. But there is a better way, why not just 
  343. combine all the boxes into one mass and then color the whole mass, this is 
  344. what the statement "union" is used for. The syntax for union is:
  345.  
  346. object {
  347.   union {
  348.      shape_1
  349.      shape_2
  350.      shape_3 ...
  351.    }
  352. }
  353.  
  354. Union saves alot of key strokes, and gives the advantage of having just one 
  355. shape to work with. Now onto explaing the table scene!
  356.  
  357.  
  358.  
  359. camera {
  360.   location <5 6 -8>
  361.   look_at <0 5 0>
  362. }
  363.  
  364. This as you know is the location and viewpoint of the camera. Pretty much 
  365. self explanitory.
  366.  
  367. object {
  368.   union{
  369.      box { <-2 0 1.5> <-1.5 4 2> }     // Back left leg
  370.      box { <-2 0 -2> <-1.5 4 -1.5> }    // Front left leg
  371.      box { <1.5 0 1.5> <2 4 2> }       // Back right leg
  372.      box { <1.5 0 -2> <2 4 -1.5> }     // Front right leg
  373.      box { <-2.5 4 -2.5> <2.5 4.2 2.5> }  // Table Top
  374.   }
  375. texture {
  376.      color red 1
  377.    }
  378. }
  379.  
  380. This is the union of boxes that form the table, I am not going to go into how 
  381. each box is located because it would take far to long, but it can be simply 
  382. figured out. Also note that I did not just make this table straight from my 
  383. head, it would be virtualy impossible. Instead I got some graph paper and used 
  384. every two squares as a unit, and figured out the locations needed to make a
  385. table, this is the recommended aproach.  Now notice the TEXTURE statement, 
  386. using it for the whole union as red 1, makes the whole table red.
  387.  
  388. object {
  389.   light_source { <10 15 -20> color red 1 green 1 blue 1 }
  390. }
  391.  
  392. And this is simply the location of the light source that gives off white 
  393. light.
  394.  
  395. -----------------------------------------------------------------------------
  396.  
  397.  
  398.  We have covered alot, but there is still MUCH more to be covered! We cannot 
  399. take the time to do it here. But if you are interested in learning more there 
  400. are many ways to go about it. For one a good book will teach you alot, here 
  401. are a couple I recomend:
  402.  
  403. * RayTracing Creations by the Waite Group - Excelent book that will teach you 
  404. almost everything you need to know about raytracing, this one is highly 
  405. recomended.
  406.  
  407. *  Making Movies on Your PC - This book goes beyond Raytracing still images, 
  408. into the world of Raytracing animations.
  409.  
  410.   If you want to keep up to date and stay in touch with all the Raytracing 
  411. attics, you should go online! Here are a few places to try:
  412.  
  413. * Compuserve Information Service - Goto the GRAPHDEV forum
  414.  
  415. * You Can Call Me Ray BBS, or YCCMR (708) 358-5611  Located in Palatine, 
  416. IL. A great bulletin board for graphic freeks.
  417.  
  418. * The Graphics Alternative BBS or TGA (510) 524-2780 Located in El Cerito, CA.
  419. A good graphics BBS, with about everything you ever wanted on the subject
  420.  
  421. * FreeLoad BBS, (415) 348-8806 Located in San Mateo CA, I run this board. Look 
  422. in the Graphics Utilitys File area.
  423.  
  424. ------------------------------------------------------------------------------
  425.  
  426. Ways to reach me electronicly if you have any comments are:
  427.  
  428. - Through my BBS, FreeLoad BBS (415) 348-8806 (Leave a comment to the Sysop, 
  429. me)
  430.  
  431. - Through fidonet at 1:125/225
  432.  
  433. - Through the internet at   JOHN.MCGRAW@NERDSHACK.COM
  434.  
  435.  
  436.  
  437. I hope you enjoyed this Text!  And Have Fun! 
  438.  
  439. -John McGraw
  440.  
  441.