home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 160.lha / silobj.doc < prev    next >
Text File  |  1988-04-27  |  9KB  |  389 lines

  1. /* :ts=8 */
  2.  
  3. /*
  4.     Data types used in Silver
  5. */
  6.  
  7. #define    FAST    register
  8. #define    CARD    unsigned
  9. #define    GLOBAL    extern
  10. #define    FRACT    long
  11.  
  12. typedef    UBYTE    COLOR[3];
  13.  
  14. typedef    struct    vectors {
  15.     FRACT    X;
  16.     FRACT    Y;
  17.     FRACT    Z;
  18. } VECTOR;
  19.  
  20. /*
  21.  *    A "FRACT" number is a 32 bit fixed point number with a 16 bit
  22.  *    fractional part.  (e.g.  7.25 is represented as 0x00074000)
  23.  *
  24.  *    Colors are stored as 3 UBYTE values on a scale of 0 to 255.
  25.  */
  26.  
  27. /*
  28.     This is the internal structure of Turbo Silver Objects
  29. */
  30.  
  31. #define    PRP_SURFACE    0
  32. #define    PRP_BRUSH    PRP_SURFACE + 1
  33. #define    PRP_STENCIL    PRP_BRUSH + 1
  34. #define    PRP_MATTER    PRP_STENCIL + 1
  35. #define    PRP_BLEND    PRP_MATTER + 1
  36. #define    PRP_SMOOTH    PRP_BLEND + 1
  37. #define    PRP_SHINY    PRP_SMOOTH + 1
  38. #define    PRP_EXTRA    PRP_SHINY + 1
  39.  
  40. #define    NUM_OBJ_PROPS    (PRP_EXTRA + 1)
  41.  
  42. #define    ABS_ROT    1
  43. #define ABS_SCL 2
  44. #define    LOC_ROT    4
  45. #define    LOC_SCL    8
  46.  
  47. typedef    struct    story    {
  48.     UBYTE    Path[18];            /*  18 bytes        */
  49.     VECTOR    Rotate;                /*  12 bytes        */
  50.     VECTOR    Scale;                /*  12 bytes        */
  51.     UWORD    info;                /*   2 bytes        */
  52. } STORY;                    /*  44 bytes total    */
  53.  
  54. typedef    struct    barfu    {
  55.     UBYTE    Shape;                /*   1 byte        */
  56.     UBYTE    Parent [3];            /*   3 bytes        */
  57.     ULONG    Number;                /*   4 bytes        */
  58.     VECTOR    Position;            /*  12 bytes        */
  59.     VECTOR    A;                /*  12 bytes        */
  60.     VECTOR    B;                /*  12 bytes        */
  61.     VECTOR    C;                /*  12 bytes        */
  62.     VECTOR    S;                /*  12 bytes        */
  63. } SUBOBJ, *SUBPTR;                /*  68 bytes total    */
  64.  
  65. typedef    struct    object    {
  66. struct    object    *Parent;            /*   4 bytes        */
  67. struct    object    *Child;                /*   4 bytes        */
  68. struct    object    *Older;                /*   4 bytes        */
  69. struct    object    *Younger;            /*   4 bytes        */
  70.     STORY    *Story;                /*   4 bytes        */
  71.     SUBOBJ    Object;                /*  68 bytes        */
  72.     UBYTE    Props [NUM_OBJ_PROPS];        /*   8 bytes        */
  73.     UBYTE    Name [18];            /*  18 bytes        */
  74.     UWORD    PCount;                /*   2 bytes        */
  75.     ULONG    TCount;                /*   4 bytes        */
  76.     VECTOR    *Points;            /*   4 bytes / 12 bytes    */
  77.     UWORD    *Connects;            /*   4 bytes /  6 bytes    */
  78.     SUBOBJ    *Subjects;            /*   4 bytes / 68 bytes    */
  79.     VECTOR    *Phongs;            /*   4 bytes / 12 bytes    */
  80.     COLOR    *Diffuse;            /*   4 bytes /  3 bytes    */
  81.     COLOR    *Reflect;            /*   4 bytes /  3 bytes    */
  82.     COLOR    *Transmit;            /*   4 bytes /  3 bytes    */
  83. } OBJECT, *OBJPTR;                /* 152 bytes total    */
  84.  
  85. #define    OSIZE    (long) sizeof (OBJECT)
  86. #define    SSIZE    (long) sizeof (SUBOBJ)
  87. #define    VSIZE    (long) sizeof (VECTOR)
  88.  
  89. #define    SPHERE        0L
  90. #define    STENCIL        1L
  91. #define    AXIS        2L
  92. #define    FACETS        3L
  93. #define    SURFACE        4L
  94.  
  95. /*
  96.  *    The 'Object' component of the 'struct object' structure
  97.  *    and the 'Props[]' component, contains the necessary info
  98.  *    for everything but faceted objects (i.e. spheres, surfaces,
  99.  *    stencils and axes).  The 'Object.Shape' field is a number
  100.  *    (from the #defines above) specifying which type of object
  101.  *    is described.  The 'Object.Parent' and 'Object.Number' fields
  102.  *    don't matter in file data.  The 'Object.A', 'B', and 'C' vectors
  103.  *    are the unit vectors describing the object coordinate system
  104.  *    ('A' is the 'x' axis, 'B' is the 'y' axis, 'C' is the 'z').
  105.  *
  106.  *    The 'Props[]' array is:
  107.  *
  108.  *    Props[PRP_SURFACE]    - bits 0-3 are the "surface type":
  109.  *                    0 = Matte, 4 = genlock, 5 = brush
  110.  *                - bit 6 is clear for phong shaded, else set
  111.  *    Props[PRP_BRUSH  ]    - the brush number
  112.  *    Props[PRP_STENCIL]    - the stencil number
  113.  *    Props[PRP_MATTER ]    - type of matter:  0 = Air, 1 = Water,
  114.  *                    2 = Glass, 3 = Crystal, 4 = Custom
  115.  *    Props[PRP_BLEND  ]    - blending factor 0-255
  116.  *    Props[PRP_SMOOTH ]    - roughness factor 0-255
  117.  *    Props[PRP_SHINY  ]    - non-zero for shiny objects
  118.  *    Props[PRP_EXTRA  ]    - custom index of refraction 0-255
  119.  *                    (if Props[PRP_MATTER] == 4).
  120.  *                  0 correnponds to 1.00, and 255 to 3.55
  121.  *
  122.  *      The 'Diffuse', 'Reflect', and 'Transmit' fields should really
  123.  *    be unions.  If the object is not a "custom" (points/faces) object,
  124.  *    they are really a pad byte and 3 color bytes (RGB).
  125.  *
  126.  *      If the object is a "custom" - faceted - object, the 'PCount', and
  127.  *    'TCount' parameters give the number of points and faces respectively.
  128.  *    The point list is pointed to by 'Points', and the face list is
  129.  *    pointed to by 'Connects'.  The point list is a list of 'VECTOR's,
  130.  *    and the face list is a list of UWORD point number triples.  The
  131.  *    Colors for each face are stored in 3 COLOR lists pointed to by
  132.  *    'Diffuse', 'Reflect', and 'Transmit'.  There is one COLOR
  133.  *    structure for each face, in order corresponding to the face list.
  134.  *
  135.  *    The routines below indicate what information actually gets read
  136.  *    or written to the file, and how.
  137. */
  138.  
  139. /*
  140.  *    These routines write the Silver object trees to the file:
  141.  *
  142.  *    after opening the file ('ofile'):
  143.  *
  144.  *        fprintf (ofile, "SILVER II\nOBJT\n");
  145.  *        WrtNode (TheObject, ofile);
  146.  *        fclose (ofile);
  147.  *
  148.  *    - where 'TheObject' points to a 'struct object'.
  149. */
  150.  
  151. static    ULONG    story_header [] = { 0x53545259, (ULONG) sizeof (STORY) };
  152.  
  153. void    write_node (p, fp)
  154.  
  155. OBJECT    *p;
  156. FILE    *fp;
  157.  
  158. {
  159.     while (p) {
  160.         WrtNode (p, fp);
  161.         p = p->Younger;
  162.     }
  163.     fwrite ("\0", 1, 1, fp);
  164. }
  165.  
  166. void    WrtNode (p, fp)
  167.  
  168. OBJECT    *p;
  169. FILE    *fp;
  170.  
  171. {
  172. FAST    ULONG    i;
  173. FAST    UWORD    *w;
  174. FAST    COLOR    *c;
  175.  
  176.     if ((long) p <= 0)
  177.         return;
  178.  
  179.     fwrite ("\001", 1, 1, fp);
  180.  
  181.     fwrite (p->Name, 18, 1, fp);
  182.     fwrite (&p->Object, (int) SSIZE, 1, fp);
  183.     fwrite (p->Props, NUM_OBJ_PROPS, 1, fp);
  184.  
  185.     if (p->Story) {
  186.         fwrite (story_header, 8, 1, fp);
  187.         fwrite (p->Story, sizeof (STORY), 1, fp);
  188.     }
  189.  
  190.     fwrite (&p->PCount, 2, 1, fp);
  191.     fwrite (&p->TCount, 4, 1, fp);
  192.  
  193.     if (p->PCount) {
  194.         fwrite ("PNTS", 4, 1, fp);
  195.         fwrite (p->Points, (int) VSIZE, p->PCount, fp);
  196.     }
  197.  
  198.     if (p->TCount) {
  199.         fwrite ("CNRS", 4, 1, fp);
  200.         i = p->TCount * 3L;
  201.         w = p->Connects;
  202.         while (i--)
  203.             fwrite (w++, 2, 1, fp);
  204.  
  205.         i = p->TCount;
  206.         c = p->Diffuse;
  207.         while (i--)
  208.             fwrite (c++, 3, 1, fp);
  209.  
  210.         i = p->TCount;
  211.         c = p->Reflect;
  212.         while (i--)
  213.             fwrite (c++, 3, 1, fp);
  214.  
  215.         i = p->TCount;
  216.         c = p->Transmit;
  217.         while (i--)
  218.             fwrite (c++, 3, 1, fp);
  219.     } else {
  220.         fwrite (&p->Diffuse, 4, 1, fp);
  221.         fwrite (&p->Reflect, 4, 1, fp);
  222.         fwrite (&p->Transmit, 4, 1, fp);
  223.     }
  224.  
  225.     write_node (p->Child, fp);
  226. }
  227.  
  228. /*
  229.  *    These read the Silver object trees from the file:
  230.  *
  231.  *    After opening the file ('ofile'):
  232.  *
  233.  *        char    s[256];
  234.  *        OBJECT    *temp;
  235.  *
  236.  *        fgets (s, 256, ofile);
  237.  *        if (strncmp (s, "SILVER II", 9))
  238.  *            goto oerr;
  239.  *
  240.  *        fgets (s, 256, ofile);
  241.  *        if (strncmp (s, "OBJT", 4))
  242.  *            goto oerr;
  243.  *
  244.  *        temp = LodNode (ofile);
  245.  *
  246. */
  247.  
  248. OBJECT    *load_node (fp)
  249.  
  250. FILE    *fp;
  251.  
  252. {
  253.     OBJECT    *head, *prev, *curr;
  254.  
  255.     head = 0L;
  256.     prev = 0L;
  257.  
  258.     for (;;) {
  259.         curr = LodNode (fp);
  260.         if (!curr)
  261.             break;
  262.  
  263.         if (prev)
  264.             prev->Younger = curr;
  265.         else
  266.             head = curr;
  267.         curr->Older = prev;
  268.         prev = curr;
  269.     }
  270.  
  271.     return head;
  272. }
  273.  
  274. OBJECT    *LodNode (fp)
  275.  
  276. FILE    *fp;
  277.  
  278. {
  279.     OBJECT    *temp, *curr;
  280.     UBYTE    s;
  281.     ULONG    id;
  282. FAST    ULONG    i;
  283. FAST    UWORD    *w;
  284. FAST    COLOR    *c;
  285.  
  286.     if (feof (fp) || ferror (fp))
  287.         return NULL;
  288.  
  289.     if (!(s = getc (fp)))
  290.         return NULL;
  291.  
  292.     if (!(temp = add_node ((int) FACETS)))
  293.         return NULL;
  294.  
  295.     fread (temp->Name, 18, 1, fp);
  296.     temp->Name[17] = '\0';
  297.     StringUp (temp->Name);
  298.     fread (&temp->Object, (int) SSIZE, 1, fp);
  299.     fread (temp->Props, NUM_OBJ_PROPS, 1, fp);
  300.  
  301.     fread (&id, 4, 1, fp);
  302.     if (id == 0x53545259) {
  303.         fread (&id, 4, 1, fp);
  304.         if (temp->Story = AllocMem (id, 0L))
  305.             fread (temp->Story, (int) id, 1, fp);
  306.         else
  307.             fseek (fp, id, 1);
  308.     } else
  309.         fseek (fp, -4L, 1);
  310.  
  311.     fread (&temp->PCount, 2, 1, fp);
  312.     fread (&temp->TCount, 4, 1, fp);
  313.  
  314.     if (temp->PCount) {
  315.         fread (&id, 4, 1, fp);
  316.         if (id != 0x504e5453) {
  317.             DelNode (temp);
  318.             return NULL;
  319.         }
  320.  
  321.         if (temp->Points = AllocMem (temp->PCount * VSIZE, 0L))
  322.             fread (temp->Points, (int) VSIZE, temp->PCount, fp);
  323.         else
  324.             fseek (fp, temp->PCount * VSIZE, 1);
  325.     }
  326.  
  327.     if (temp->TCount) {
  328.         fread (&id, 4, 1, fp);
  329.         if (id != 0x434e5253) {
  330.             DelNode (temp);
  331.             return NULL;
  332.         }
  333.         if (temp->Connects = AllocMem (temp->TCount * 6L, 0L)) {
  334.             i = temp->TCount * 3;
  335.             w = temp->Connects;
  336.             while (i--)
  337.                 fread (w++, 2, 1, fp);
  338.         } else
  339.             fseek (fp, temp->TCount * 6L, 1);
  340.     }
  341.  
  342.     if (temp->TCount)
  343.         if (temp->Diffuse = AllocMem (temp->TCount * 3L, 0L)) {
  344.             i = temp->TCount;
  345.             c = temp->Diffuse;
  346.             while (i--)
  347.                 fread (c++, 3, 1, fp);
  348.         } else
  349.             fseek (fp, temp->TCount * 3L, 1);
  350.     else
  351.         fread (&temp->Diffuse, 4, 1, fp);
  352.  
  353.     if (temp->TCount)
  354.         if (temp->Reflect = AllocMem (temp->TCount * 3L, 0L)) {
  355.             i = temp->TCount;
  356.             c = temp->Reflect;
  357.             while (i--)
  358.                 fread (c++, 3, 1, fp);
  359.         } else
  360.             fseek (fp, temp->TCount * 3L, 1);
  361.     else
  362.         fread (&temp->Reflect, 4, 1, fp);
  363.  
  364.     if (temp->TCount)
  365.         if (temp->Transmit = AllocMem (temp->TCount * 3L, 0L)) {
  366.             i = temp->TCount;
  367.             c = temp->Transmit;
  368.             while (i--)
  369.                 fread (c++, 3, 1, fp);
  370.         } else
  371.             fseek (fp, temp->TCount * 3L, 1);
  372.     else
  373.         fread (&temp->Transmit, 4, 1, fp);
  374.  
  375.     SetSubParent (temp);
  376.  
  377.     temp->Child = load_node (fp);
  378.  
  379.     curr = temp->Child;
  380.  
  381.     while (curr) {
  382.         curr->Parent = temp;
  383.         curr = curr->Younger;
  384.     }
  385.  
  386.     return temp;
  387. }
  388.  
  389.