home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 284.lha / DkbTrace_v1.1 / trace.txt < prev    next >
Text File  |  1989-09-10  |  21KB  |  565 lines

  1. This program was written by:
  2.  
  3. David Buck
  4. 22C Sonnet Cres.
  5. Nepean, Ontario
  6. Canada, K2H 8W7
  7.  
  8. It has been released to the public domain for free distribution.  I would
  9. like to place the following conditions on the use of this program:
  10.  
  11.   1) that it should not be used as part of any commercial package without
  12.      my explicit written consent.
  13.   2) if you make any neat and interesting pictures, please send them to me.
  14.   3) If you make any changes to the source code, please let me know.  I'd 
  15.      like to see what you've done.
  16.   4) This text file should accompany should accompany the program
  17.  
  18. I can be reached on the following BBS'es
  19.  
  20. Ottawa Online    (613) 526-4141
  21. OMX              (613) 731-3419
  22.  
  23.  
  24. This program is a ray tracer written completely in C.  It supports arbitrary
  25. quadric surfaces (spheres, ellipsoids, cones, cylinders, planes, etc.),
  26. constructive solid geometry, and various shading models (reflection,
  27. refraction, marble, wood, etc.)
  28.  
  29. In order to create pictures with this program, you must describe the objects
  30. in the world.  This description is a text file usually called "Object.data".
  31. Normally, such files are difficult to write and to read.  In order to make
  32. this task easier, the program contains a two-pass parser to read the data
  33. file.  It allows the user to easily create complex worlds from simple
  34. components.  Since the parser allows include files, the user may put the
  35. object descriptions into different files and combine them all into one
  36. final image.
  37.  
  38. This manual is divided into two main sections.  The first section describes
  39. the command-line parameters for the program.  The second section describes
  40. the syntax and semantics of the description language.  Some sample worlds
  41. and their corresponding images are provided on the disk.
  42.  
  43. Section 1 - Command Line Parameters
  44.  
  45. This program is designed to be run from the CLI, although it can be run
  46. from the Workbench if desired.  From the CLI, the program accepts various
  47. parameters:
  48.  
  49.    -wxxx    width of the picture in pixels
  50.    -hxxx    height of the picture in pixels
  51.    +f        produce an output file (see below)
  52.    -f        don't produce an output file
  53.    +d        display the picture while tracing
  54.    -d        don't display the picture while tracing
  55.    +p        prompt for CR before completing
  56.    -p        finish without prompting for CR
  57.    -ifilename    set the input filename
  58.    -ofilename    set output filename
  59.    +axxx    anti-alias - xxx is a tolerance level 
  60.    -a        don't anti-alias
  61.    +v        verbose option - print out the scan line number.
  62.    -v        disable verbose option
  63.  
  64. If the +f option is used, the ray tracer will produce an output file of the
  65. picture.  This output file describes each pixel with 24 bits (8 bits for red,
  66. 8 for green, and 8 for blue).  A post processor called "DumpToIFF" can
  67. convert this format to hi-res HAM format (320 x 400) making reasonable
  68. choices for the colour registers.  For compatability, the format of the dump
  69. file is the same as the format for the QRT ray tracer.
  70.  
  71. If the +d option is used, then a hi-res HAM image of the picture will be 
  72. produced while the program performs the ray tracing.  This picture is not
  73. as good as the one created by "DumpToIFF" because it does not try to make
  74. good choices for the colour registers.
  75.  
  76. The +p option makes the program wait for a carriage return before exitting
  77. (and closing the graphics screen). This gives you time to look at the final
  78. picture before destroying it.
  79.  
  80. If your input file is not "Object.data", then you can use -i to set the
  81. filename.  The default output filename is "data.dis". If you want a
  82. different output file name, use the -o option.
  83.  
  84. The +a option enables adaptive anti-aliasing.  The number after the +a
  85. option determines the threshold for anti-aliasing.  If the colour of a
  86. pixel differs from its neighbor (to the left or below) by more than the
  87. threshold, then the pixel is subdivided and super-sampled.  The samples
  88. are jittered to introduce noise and make the pictures look better.  If
  89. the anti-aliasing threshold is 0.0, then every pixel is supersampled.
  90. If the threshold is 1.0, then no anti-aliasing is done.  Good values
  91. seem to be around 0.2 to 0.4.
  92.  
  93. You may specify the default parameters by creating a file called
  94. "trace.defaults" which contains the parameters in the above format.
  95.  
  96. Section 2 - The Object Description Language
  97.  
  98. The Object Description Language allows the user to describe the world in a
  99. readable and convenient way.
  100.  
  101. The language delimits comments by the left and right braces ({ and }).
  102. Nested comments are allowed, but no sane person uses them, right?
  103.  
  104. The language allows include files to be specified by placing the line:
  105.  
  106. INCLUDE "filename"
  107.  
  108. at any point in the input file.  (Include files may include other files).
  109.  
  110. Section 2.1 - The Basic Data Types
  111.  
  112. There are several basic types of data:
  113.  
  114. Float
  115. Floats are represented by an optional sign (+ or -), some digits, an
  116. optional decimal point, and more digits.  It does not support the "e"
  117. notation for exponents.  The following are valid floats:
  118.  
  119. 1.0  -2.0  -4  +34
  120.  
  121. Vector
  122. Vectors are arrays of three floats.  They are bracketed by angle brackets
  123. ( < and > ).
  124.  
  125. < 1.0  3.2  -5.4578 >
  126.  
  127. Colour
  128. A colour consists of a red component, a green component, and a blue 
  129. component.  All three components are floats in the range 0.0 to 1.0.  The
  130. syntax for Colours is the word "COLOUR" followed by any or all of the RED,
  131. GREEN, or BLUE components in any order.  For example:
  132.  
  133. COLOUR  RED 1.0  GREEN 1.0  BLUE 1.0
  134. COLOUR  BLUE 0.56
  135. COLOUR  GREEN 0.45 RED 0.3
  136.  
  137. For those people who spell "Colour" the American way as "Color", the program
  138. also accepts "COLOR" as an equivalent to "COLOUR".
  139.  
  140. COLOUR_MAP
  141. For wood and marble texturing, the user may specify arbitrary colours to
  142. use for the texture.  This is done by a colour map.  When the object is
  143. being textured. a number between 0.0 and 1.0 is generated which is then used
  144. to determine the colour of the point.  A Colour map can specify the mapping
  145. used to change these numbers into colours.  The syntax is as follows:
  146.  
  147. COLOUR_MAP
  148.     [start_value end_value colour1 colour2]
  149.     [start_value end_value colour1 colour2]
  150.     ...
  151. END_COLOUR_MAP
  152.  
  153. The value is located in the colour map and the final colour is calculated
  154. as a linear interpolated between the two colours in the located range.
  155.  
  156. Section 2.2 - The More Complex Data Types
  157.  
  158. The data types used to describe the objects in the world are a bit more
  159. difficult to describe.  To make this task easier, the program allows users
  160. to describe these types in two ways.  The first way is to define it from
  161. first principles specifying all of the required parameters.  The second way
  162. allows the user to define an object as a modification of another object.
  163. (The other object is usually defined from first principles but is much
  164. simpler). Here's how it works.
  165.  
  166. You can use the term DECLARE to declare a type of object with a certain
  167. description. The object is not included in the world but it can be used as
  168. a prototype for defining other objects.  An example would help.
  169.  
  170. Viewpoint
  171. The viewpoint tells the ray tracer the location and orientation of the
  172. camera.  The viewpoint is described by four vectors - Location, Direction,
  173. Up, and Right.  Location determines where the camera is located.  Direction
  174. determines the direction that the camera is pointed. Up determines the "up"
  175. direction of the camera. Right determines the direction to the right of the
  176. camera.
  177.  
  178. A first principles declaration of a viewpoint would look like this:
  179.  
  180. VIEWPOINT
  181.   LOCATION < 0.0  0.0  0.0>
  182.   DIRECTION < 0.0  0.0  1.0>
  183.   UP < 0.0  1.0  0.0 >
  184.   RIGHT < 1.0  0.0  0.0>
  185. END_VIEWPOINT
  186.  
  187. This format becomes cumbersome, however, because the vectors must be
  188. calculated by hand.  This is especially difficult when the vectors are not
  189. lined up with the X, Y, and Z axes as they are in the above example.  To
  190. make it easier to define the viewpoint, you can define one viewpoint, then
  191. modify the description.  For example,
  192.  
  193. VIEWPOINT
  194.   LOCATION < 0.0  0.0  0.0>
  195.   DIRECTION < 0.0  0.0  1.0>
  196.   UP < 0.0  1.0  0.0 >
  197.   RIGHT < 1.0  0.0  0.0 >
  198.   TRANSLATE < 5.0  3.0  4.0 >
  199.   ROTATE < 30.0  60.0  30.0 >
  200. END_VIEWPOINT
  201.  
  202. In this example, the viewpoint is created, then translated to another point
  203. in space and rotated by 30 degrees about the X axis, 60 degrees about the
  204. Y axis, and 30 degrees about the Z axis.
  205.  
  206. Shapes
  207. Shapes describe the shape of an object without mentioning any surface
  208. characteristics like colour, lighting and reflectivity.  The most common
  209. shape used by this system is called a Quadric Surface. Quadric Surfaces
  210. can produce shapes like spheres, cones, and cylinders.  The easiest way to
  211. define these shapes is to include the file "BasicShapes.data" into your
  212. program and to transform these shapes (using TRANSLATE, ROTATE, and SCALE)
  213. into the ones you want.  To be complete, however, I will describe the
  214. mathematical principles behind quadric surfaces.  Those who are not
  215. interested in the mathematical details can skip to the next section.
  216.  
  217. A quadric surface is a surface in three dimensions which satisfies the
  218. following equation:
  219.  
  220.   A y**2  + B y**2  + C z**2
  221. + D xy    + E xz    + F yz
  222. + G x     + H y     + I z    + J = 0
  223.  
  224. Different values of A,B,C,...J will give different shapes.  So, if you take
  225. any three dimensional point and use its x, y, and z coordinates in the
  226. above equation, the answer will be 0 if the point is on the surface of the
  227. object.  The answer will be negative if the point is inside the object and
  228. positive if the point is outside the object.  Here are some examples:
  229.  
  230. X**2 + Y**2 + Z**2 - 1 = 0     Sphere
  231. X**2 + Y**2 - 1 = 0            Cylinder along the Z axis
  232. X**2 + Y**2 + Z = 0            Cone along the Z axis
  233.  
  234. General quadric surfaces can be defined as follows:
  235.  
  236. QUADRIC
  237.    < A B C >
  238.    < D E F >
  239.    < G H I >
  240.    J
  241. END_QUADRIC
  242.  
  243. Section 2.3 - Quadric surfaces the easy way
  244.  
  245. The file "BasicShapes.data" already includes the definitions of many
  246. quadric surfaces.  They are centered about the origin (0,0,0) and have a
  247. radius of 1.  To use them, you can define shapes as follows:
  248.  
  249. INCLUDE "BasicShapes.data"
  250.  
  251. QUADRIC
  252.     Cylinder_X
  253.     SCALE < 50.0  50.0  50.0 >
  254.     ROTATE < 30.0  10.0  45.0 >
  255.     TRANSLATE < 2.0  5.0  6.0 >
  256. END_QUADRIC
  257.  
  258. You may have as many transformation lines (scale, rotate, and translate) as
  259. you like in any order.  Usually, however, it's easiest to do a scale first,
  260. one or more rotations, then finally a translation.  Otherwise, the results
  261. may not be what you expect. (The transformations always transform the
  262. object about the origin.  If you have a sphere at the origin and you
  263. translate it then rotate it, the rotation will spin the sphere around the
  264. origin like planets about the sun).
  265.  
  266. Section  2.4 - Constructive Solid Geometry
  267.  
  268. This ray tracer supports Constructive Solid Geometry in order to make the
  269. object definition abilities more powerful.  Constructive Solid Geometry
  270. allows you to define shapes which are the union, intersection, or
  271. difference of other shapes.  Unions superimpose the two shapes.  This has
  272. the same effect as defining two separate objects, but is simpler to create.
  273. Intersections define the space where the two surfaces meet.  Differences
  274. allow you to cut one object out of another.
  275.  
  276. Intersections, Unions, and Differences can consist of several shapes.  They
  277. are defined as follows:
  278.  
  279. OBJECT
  280.      INTERSECTION
  281.          QUADRIC
  282.            ...
  283.          END_QUADRIC
  284.  
  285.          QUADRIC
  286.            ...
  287.          END_QUADRIC
  288.  
  289.          QUADRIC
  290.          ...
  291.          END_QUADRIC
  292.      END_INTERSECTION
  293. ...
  294. END_OBJECT
  295.  
  296. UNION or DIFFERENCE may be used instead of INTERSECTION.
  297.  
  298. The order of the shapes doesn't matter except for the DIFFERENCE shapes.
  299. For differences, the last shape is visible and the remaining shapes are
  300. cut out of the last.
  301.  
  302. Constructive solid geometry shapes may be translated, rotated, or scaled
  303. in the same way as a Quadric surface.  The quadric surfaces making up the
  304. CSG object may be translated, rotated, and scaled as well.
  305.  
  306. Section  2.5 - Objects
  307.  
  308. There is more to defining an object than just its shape.  You must tell the
  309. ray tracer about the properties of the surface like colour, reflectivity,
  310. refractivity, the index of refraction, and so on. To do this, you must
  311. define Objects.  A typical object definition looks something like this:
  312.  
  313. OBJECT
  314.    QUADRIC
  315.        Sphere
  316.        TRANSLATE < 40.0 40.0 60.0 >
  317.    END_QUADRIC
  318.  
  319.    AMBIENT  0.3
  320.    DIFFUSE   0.7
  321.    REFLECTION  0.3
  322.    REFRACTION  0.3
  323.    IOR 1.05
  324.    COLOUR GREEN 1.0 BLUE 1.0
  325. END_OBJECT
  326.  
  327. The following keywords may be used when defining objects:
  328.  
  329. AMBIENT value
  330.  - Ambient light is light that is scattered everywhere in the room.  An
  331. object lit only by ambient light will appear to have the same brightness
  332. over the entire surface.  The default value is 0.3. The value can range
  333. from 0.0 to 1.0.
  334.  
  335. DIFFUSE value
  336.    - Diffuse light is light coming from a light source that is scattered in
  337. all directions.  An object lit only by diffuse light looks like a rubber
  338. ball with a spot light shining on it.  The value can range from 0.0 to 1.0.
  339.  
  340. REFLECTION value
  341.     - By setting the reflection value to be non-zero, you can give the
  342. object a mirrored finish.  It will reflect all other objects in the room.
  343. The value can range from 0.0 to 1.0.
  344.  
  345. REFRACTION value
  346.     - By setting the refraction value to be non-zero, the object is made
  347. transparent.  Light will be refracted through the object like a lens.
  348. The value can be set between 0.0 and 1.0.
  349.  
  350. IOR value
  351.     - If the object is refracting light, then the IOR or Index of Refraction
  352. should be set.  This determines how dense the object is.  A value of 1.0
  353. will give no refraction.  Water is 1.33, glass is 1.5, and diamond is 2.4.
  354.  
  355. BRILLIANCE value
  356.     - Objects can be made to appear more metallic by increasing their
  357. brilliance.  The default value is 1.0.  Higher values from 3.0 to about
  358. 10.0 can give a more metallic appearance.
  359.  
  360. COLOUR value
  361.     - The colour of an object can be set by using this option.  The value
  362. is a colour or a colour constant.  For example,
  363.  
  364.     COLOUR RED 1.0  BLUE 0.4
  365.  
  366.      or
  367.     DECLARE Yellow = COLOUR RED 1.0 GREEN 1.0
  368.     ...
  369.     COLOUR Yellow
  370.  
  371.  
  372. TRANSLATE vector
  373. ROTATE vector
  374. SCALE vector
  375.     - Objects can be translated, rotated, and scaled just like surfaces.
  376. This feature is included for consistency.
  377.  
  378. LIGHT_SOURCE
  379.     - If the LIGHT_SOURCE keyword is used in the definition of an object,
  380. then the object is included in the list of light sources.  It can light
  381. objects and produce shadows.  (You should also specify the COLOUR of the
  382. light source).
  383.  
  384. TEXTURE
  385.      - The texture feature is an experiment into functionally based
  386. modelling.  This feature allows you to assign more interesting colouring
  387. schemes to objects.  For example, you can make some object look like wood
  388. or marble.  The syntax is as follows:
  389.  
  390.      TEXTURE
  391.          WOOD
  392.          TURBULENCE 0.2
  393.          TRANSLATE < 1.0 2.0 3.0 >
  394.          ROTATE < 0.0 10.0 40.0 >
  395.          SCALE < 10.0 10.0 10.0 >
  396.       END_TEXTURE
  397.  
  398. The transformations are optional.  They allow you to transform the texture
  399. independent of the object itself.  If you are doing animation, then the
  400. transformations should be the same as the object transformations so that
  401. the texture follows the object.
  402.  
  403. Instead of using WOOD, you may use MARBLE, BOZO, or CHECKER.  The WOOD and
  404. MARBLE textures are perturbed by a turbulence function.  This makes them look
  405. more random and irregular than they would normally appear.  The amount of
  406. turbulence can be changed by the TURBULENCE keyword followed by a number.
  407. Values from 0.1 to 0.3 seem to give the best results.  The default is 0.3.
  408.  
  409. CHECKER texturing gives a checker-board appearance.  This option works best
  410. on planes.  When using the CHECKER texturing, you must specify two colours
  411. immediately following the word CHECKER.  These colours are the colours of
  412. alternate squares in the checker pattern.
  413.  
  414. For WOOD, MARBLE, and BOZO textures, you may change the colouring scheme
  415. by using a colour map.  This map allows you to convert numbers from 0.0 to
  416. 1.0 (which are generated by the ray tracer) into ranges of colours. For
  417. example, the default BOZO colouring can be specified by:
  418.  
  419.     TEXTURE
  420.         BOZO
  421.         COLOUR_MAP
  422.            [0.0 0.4 COLOUR White COLOUR White]
  423.            [0.4 0.6 COLOUR Green COLOUR Green]
  424.            [0.6 0.8 COLOUR Blue COLOUR Blue]
  425.            [0.8 1.0 COLOUR Red COLOUR Red]
  426.         END_COLOUR_MAP
  427.     END_TEXTURE
  428.  
  429. Another option which can be used in conjunction with the above textures is
  430. RIPPLES.  This option makes the surface look like ripples of water.  The
  431. RIPPLES option requires a value to determine how deep the ripples are:
  432.  
  433.      TEXTURE
  434.          WOOD
  435.          RIPPLES 0.3
  436.          TRANSLATE < 1.0 2.0 3.0 >
  437.          ROTATE < 0.0 10.0 40.0 >
  438.          SCALE < 10.0 10.0 10.0 >
  439.       END_TEXTURE
  440.  
  441. (In this case, the WOOD, MARBLE, or BOZO keywords are optional).  If a
  442. different colouring is specified (WOOD, MARBLE, or BOZO), then the COLOUR
  443. parameter is ignored (except for light sources where it gives the light
  444. colour).
  445.  
  446. Another option that you may want to experiment with is called WAVES.  This
  447. works in a similar way to RIPPLES except that it makes waves with different
  448. frequencies.  The effect is to make waves look more like deep ocean waves.
  449. (I haven't done much testing on WAVES, so I can't guarantee that it works).
  450.  
  451. Both WAVES and RIPPLES respond to a texturing option called PHASE. The PHASE
  452. option allows you to create animations in which the water seems to move.
  453. This is done by making the PHASE increment slowly between frames.  The
  454. range from 0.0 to 1.0 gives one complete cycle of a wave.
  455.  
  456.  
  457. Section 2.6 - Composite Objects
  458.  
  459. Often it's useful to combine several objects together to act as a whole. 
  460. A car, for example, consists of wheels, doors, a roof, etc.  A composite
  461. object allows you to combine all of these pieces into one object.  This has
  462. two advantages.  It makes it easier to move the object as a whole and it
  463. allows you to speed up the ray tracing by defining bounding shapes that
  464. contain the objects.  (Rays are first tested to see if they intersect the
  465. bounding shape.  If not, the entire composite object is ignored). 
  466. Composite objects are defined as follows:
  467.  
  468. COMPOSITE
  469.     OBJECT
  470.        ...
  471.     END_OBJECT
  472.  
  473.     OBJECT
  474.        ...
  475.     END_OBJECT
  476.     ...
  477.     SCALE < 2.0 2.0 2.0 >
  478.     ROTATE < 30.0 45.0 160.0 >
  479.     TRANSLATE < 100.0 20.0 40.0 >
  480. END_COMPOSITE
  481.  
  482. Composite objects can contain other composite objects as well as regular
  483. objects.  Composite objects cannot be light sources (although any number
  484. of their components can).
  485.  
  486. Section 3 - Showing the final pictures
  487.  
  488. When the ray tracer draws the picture on the screen, it does not make good
  489. choices for the colour registers.  A post-processor has been provided which
  490. scans the picture and chooses the best possible colour registers.  It then
  491. redisplays the picture.  DumpToIFF can optionally save this picture in IFF
  492. format.  The syntax for the post-processor is:
  493.  
  494. show  filename
  495.  
  496. where the filename is the one given in the -o parameter of the ray tracer.
  497. If you didn't specify the -o option, then use:
  498.  
  499. show data.dis
  500.  
  501. If you want to save to an IFF file, then put the name of the IFF file after
  502. the name of the data file:
  503.  
  504. show data.dis picture
  505.  
  506. This will create a file called "picture" which contains the IFF image.
  507.  
  508.  
  509. Section 4 - Handy Hints
  510.  
  511.   - To see a quick version of your picture, use -w64 -h80 as command line
  512.      parameters.  This displays the picture in a small rectangle so that
  513.      you can see how it will look.
  514.  
  515.   - When translating light sources, translate the OBJECT not the QUADRIC
  516.      surface.  The light source uses the center of the object as the origin
  517.      of the light.
  518.  
  519.   - When animating objects with solid textures, the textures must move with
  520.      the object.
  521.  
  522.   - You can declare constants for most of the data types in the program
  523.      including floats and vectors.  By combining this with INCLUDE files,
  524.      you can easily separate the parameters for an animation into a separate
  525.      file.
  526.  
  527.   - The amount of ambient light plus diffuse light should be less than or
  528.      equal to 1.0.  The program accepts any value, but may produce strange
  529.      results.
  530.  
  531.   - When using ripples, don't make the ripples too deep or you may get strange
  532.      results.
  533.  
  534.   - Wood textures usually look better when they are scaled to diffent values
  535.      in x, y, and z, and rotated to a different angle.
  536.  
  537.   - You can sort of dither a colour by placing a floating point number
  538.      into the texture record:
  539.  
  540.        TEXTURE
  541.           0.05
  542.        END_TEXTURE
  543.  
  544.      This adds a small random value to the intensity of the diffuse light
  545.      on the object.  Don't make the number too big or you may get strange
  546.      results.
  547.  
  548.   - You can compensate for non-square aspect ratios on the monitors by making
  549.       the RIGHT vector in the VIEWPOINT longer or shorter.  A good value for
  550.       the Amiga is about 1.333.
  551.  
  552.   - By making the DIRECTION vector in the VIEWPOINT longer, you can achieve
  553.       the effect of a zoom lens.
  554.  
  555. Section 5 - Concluding remarks
  556.  
  557. I'm sure that there are bugs in the code somewhere, but I've done my best
  558. to remove all the bugs I could find.  I also think that the object
  559. description language needs to be re-worked.  Its current syntax is a bit
  560. cumbersome.  The system could also use a good graphical interface  :-).
  561.  
  562. I would like to thank Rick Mallett from Carleton University for his help
  563. in testing this program, for his comments and suggestions, and for creating
  564. the data file for ROMAN.DATA - awesome!
  565.