home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / macraysh.sit / Code / Source / surface.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-22  |  3.3 KB  |  158 lines

  1. /*
  2.  * surface.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Craig E. Kolb
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * $Id: surface.c,v 4.0 91/07/17 14:40:55 kolb Exp Locker: kolb $
  17.  *
  18.  * $Log:    surface.c,v $
  19.  * Revision 4.0  91/07/17  14:40:55  kolb
  20.  * Initial version.
  21.  * 
  22.  */
  23. #include "atmosphere.h"
  24. #include "surface.h"
  25.  
  26. #define blend(a, b, p, q)    (a * p + b * q)
  27.  
  28. extern Surface DefaultSurface ;
  29.  
  30. Color    Black = {0., 0., 0.},
  31.     White = {1., 1., 1.};
  32.  
  33. /*
  34.  * Create and return pointer to surface with given properties.
  35.  */
  36. Surface *
  37. SurfaceCreate()
  38. {
  39.     Surface *stmp;
  40.  
  41.     stmp = (Surface *)Malloc(sizeof(Surface));
  42.  
  43.     /* Make sure we dont have any silly values in any fields */
  44.  
  45.     *stmp = DefaultSurface ;
  46.     
  47.     stmp->amb = stmp->diff = stmp->spec = stmp->translu = Black;
  48.     stmp->body = White;
  49.  
  50.     stmp->srexp = stmp->stexp = DEFAULT_PHONGPOW;
  51.     stmp->statten = 1.;    /* No attenuation by default */
  52.  
  53.     stmp->reflect = stmp->transp = 0.;
  54.  
  55.     stmp->noshadow = FALSE;
  56.  
  57.     stmp->index = DEFAULT_INDEX;
  58.  
  59.     stmp->name = (char *)NULL;
  60.     stmp->next = (Surface *)NULL;
  61.  
  62.     return stmp;
  63. }
  64.  
  65. Surface *
  66. SurfaceCopy(surf)
  67. Surface *surf;
  68. {
  69.     Surface *res;
  70.  
  71.     if (!surf)
  72.         return (Surface *)NULL;
  73.  
  74.     res = SurfaceCreate();
  75.     *res = *surf;
  76.     res->next = (Surface *)NULL;
  77.     res->name = (char *)NULL;
  78.     return res;
  79. }
  80.  
  81. /*
  82.  * Compute combination of two surfaces. Resulting surface is copied into surf1.
  83.  */
  84. void
  85. SurfaceBlend(surf1, surf2, p, q)
  86. Surface *surf1, *surf2;
  87. Float p, q;
  88. {
  89.     /*
  90.       * P is weight of surf1.  q is weight of surf2.
  91.      * Result is placed in surf1.
  92.      */
  93.     if (q < EPSILON)
  94.         return;    /* keep surf1 as-is */
  95.  
  96.     ColorBlend(&surf1->amb, &surf2->amb, p, q);
  97.     ColorBlend(&surf1->diff, &surf2->diff, p, q);
  98.     ColorBlend(&surf1->spec, &surf2->spec, p, q);
  99.     ColorBlend(&surf1->translu, &surf2->translu, p, q);
  100.     ColorBlend(&surf1->body, &surf2->body, p, q);
  101.  
  102.     surf1->srexp = blend(surf1->srexp, surf2->srexp, p, q);
  103.     surf1->stexp = blend(surf1->stexp, surf2->stexp, p, q);
  104.  
  105.     surf1->reflect = blend(surf1->reflect, surf2->reflect, p, q);
  106.     surf1->transp  = blend(surf1->transp,  surf2->transp,  p, q);
  107.     surf1->translucency = blend(surf1->translucency, surf2->translucency,
  108.             p, q);
  109.     /*
  110.      * Questionable...
  111.      */
  112.     surf1->statten = blend(surf1->statten, surf2->statten, p, q);
  113.     surf1->index = blend(surf1->index, surf2->index, p, q);
  114.  
  115.     if (p < EPSILON) {
  116.         surf1->noshadow = surf2->noshadow;
  117.     } else {
  118.         /* else there's a blend of some kind... */
  119.         surf1->noshadow = (surf1->noshadow && surf2->noshadow);
  120.     }
  121. }
  122.  
  123. /*
  124.  * Blend two colors.  Result is placed in color1.
  125.  */
  126. void
  127. ColorBlend(color1, color2, p, q)
  128. Color *color1, *color2;
  129. Float p, q;
  130. {
  131.     color1->r = blend(color1->r, color2->r, p, q);
  132.     color1->g = blend(color1->g, color2->g, p, q);
  133.     color1->b = blend(color1->b, color2->b, p, q);
  134. }
  135.  
  136. SurfList *
  137. SurfPop(list)
  138. SurfList *list;
  139. {
  140.     SurfList *stmp = list->next;
  141.  
  142.     Free((voidstar)list);
  143.     return stmp;
  144. }
  145.  
  146. SurfList *
  147. SurfPush(surf, list)
  148. Surface *surf;
  149. SurfList *list;
  150. {
  151.     SurfList *stmp;
  152.  
  153.     stmp = (SurfList *)Malloc(sizeof(SurfList));
  154.     stmp->surf = surf;
  155.     stmp->next = list;
  156.     return stmp;
  157. }
  158.