home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Graphics / graphics-16000.iso / msdos / raytrace / rayshade / src / surface.c < prev    next >
C/C++ Source or Header  |  1992-04-28  |  3KB  |  154 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. Color    Black = {0., 0., 0.},
  29.     White = {1., 1., 1.};
  30.  
  31. /*
  32.  * Create and return pointer to surface with given properties.
  33.  */
  34. Surface *
  35. SurfaceCreate()
  36. {
  37.     Surface *stmp;
  38.  
  39.     stmp = (Surface *)RayMalloc(sizeof(Surface));
  40.  
  41.     stmp->amb = stmp->diff = stmp->spec =
  42.         stmp->translu = Black;
  43.  
  44.     stmp->body = White;
  45.  
  46.     stmp->srexp = stmp->stexp = DEFAULT_PHONGPOW;
  47.     stmp->statten = 1.;    /* No attenuation by default */
  48.  
  49.     stmp->reflect = stmp->transp = 0.;
  50.  
  51.     stmp->noshadow = FALSE;
  52.  
  53.     stmp->index = DEFAULT_INDEX;
  54.  
  55.     stmp->name = (char *)NULL;
  56.     stmp->next = (Surface *)NULL;
  57.  
  58.     return stmp;
  59. }
  60.  
  61. Surface *
  62. SurfaceCopy(surf)
  63. Surface *surf;
  64. {
  65.     Surface *res;
  66.  
  67.     if (!surf)
  68.         return (Surface *)NULL;
  69.  
  70.     res = SurfaceCreate();
  71.     *res = *surf;
  72.     res->next = (Surface *)NULL;
  73.     res->name = (char *)NULL;
  74.     return res;
  75. }
  76.  
  77. /*
  78.  * Compute combination of two surfaces. Resulting surface is copied into surf1.
  79.  */
  80. void
  81. SurfaceBlend(surf1, surf2, p, q)
  82. Surface *surf1, *surf2;
  83. Float p, q;
  84. {
  85.     /*
  86.       * P is weight of surf1.  q is weight of surf2.
  87.      * Result is placed in surf1.
  88.      */
  89.     if (q < EPSILON)
  90.         return;    /* keep surf1 as-is */
  91.  
  92.     ColorBlend(&surf1->amb, &surf2->amb, p, q);
  93.     ColorBlend(&surf1->diff, &surf2->diff, p, q);
  94.     ColorBlend(&surf1->spec, &surf2->spec, p, q);
  95.     ColorBlend(&surf1->translu, &surf2->translu, p, q);
  96.     ColorBlend(&surf1->body, &surf2->body, p, q);
  97.  
  98.     surf1->srexp = blend(surf1->srexp, surf2->srexp, p, q);
  99.     surf1->stexp = blend(surf1->stexp, surf2->stexp, p, q);
  100.  
  101.     surf1->reflect = blend(surf1->reflect, surf2->reflect, p, q);
  102.     surf1->transp  = blend(surf1->transp,  surf2->transp,  p, q);
  103.     surf1->translucency = blend(surf1->translucency, surf2->translucency,
  104.             p, q);
  105.     /*
  106.      * Questionable...
  107.      */
  108.     surf1->statten = blend(surf1->statten, surf2->statten, p, q);
  109.     surf1->index = blend(surf1->index, surf2->index, p, q);
  110.  
  111.     if (p < EPSILON) {
  112.         surf1->noshadow = surf2->noshadow;
  113.     } else {
  114.         /* else there's a blend of some kind... */
  115.         surf1->noshadow = (surf1->noshadow && surf2->noshadow);
  116.     }
  117. }
  118.  
  119. /*
  120.  * Blend two colors.  Result is placed in color1.
  121.  */
  122. void
  123. ColorBlend(color1, color2, p, q)
  124. Color *color1, *color2;
  125. Float p, q;
  126. {
  127.     color1->r = blend(color1->r, color2->r, p, q);
  128.     color1->g = blend(color1->g, color2->g, p, q);
  129.     color1->b = blend(color1->b, color2->b, p, q);
  130. }
  131.  
  132. SurfList *
  133. SurfPop(list)
  134. SurfList *list;
  135. {
  136.     SurfList *stmp = list->next;
  137.  
  138.     free((voidstar)list);
  139.     return stmp;
  140. }
  141.  
  142. SurfList *
  143. SurfPush(surf, list)
  144. Surface *surf;
  145. SurfList *list;
  146. {
  147.     SurfList *stmp;
  148.  
  149.     stmp = (SurfList *)RayMalloc(sizeof(SurfList));
  150.     stmp->surf = surf;
  151.     stmp->next = list;
  152.     return stmp;
  153. }
  154.