home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 3 / Amiga Tools 3.iso / grafik / raytracing / rayshade-4.0.6.3 / libray / libsurf / surface.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-09  |  3.4 KB  |  160 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.  * surface.c,v 4.1 1994/08/09 08:01:39 explorer Exp
  17.  *
  18.  * surface.c,v
  19.  * Revision 4.1  1994/08/09  08:01:39  explorer
  20.  * Bump version to 4.1
  21.  *
  22.  * Revision 1.1.1.1  1994/08/08  04:52:13  explorer
  23.  * Initial import.  This is a prerelease of 4.0.6enh3, or 4.1 possibly.
  24.  *
  25.  * Revision 4.0  91/07/17  14:40:55  kolb
  26.  * Initial version.
  27.  * 
  28.  */
  29. #include "atmosphere.h"
  30. #include "surface.h"
  31.  
  32. #define blend(a, b, p, q)    (a * p + b * q)
  33.  
  34. Color    Black = {0., 0., 0.},
  35.     White = {1., 1., 1.};
  36.  
  37. /*
  38.  * Create and return pointer to surface with given properties.
  39.  */
  40. Surface *
  41. SurfaceCreate()
  42. {
  43.     Surface *stmp;
  44.  
  45.     stmp = (Surface *)Malloc(sizeof(Surface));
  46.  
  47.     stmp->amb = stmp->diff = stmp->spec =
  48.         stmp->translu = Black;
  49.  
  50.     stmp->body = White;
  51.  
  52.     stmp->srexp = stmp->stexp = DEFAULT_PHONGPOW;
  53.     stmp->statten = 1.;    /* No attenuation by default */
  54.  
  55.     stmp->reflect = stmp->transp = stmp->translucency = 0.;
  56.  
  57.     stmp->noshadow = FALSE;
  58.  
  59.     stmp->index = DEFAULT_INDEX;
  60.  
  61.     stmp->name = (char *)NULL;
  62.     stmp->next = (Surface *)NULL;
  63.  
  64.     return stmp;
  65. }
  66.  
  67. Surface *
  68. SurfaceCopy(surf)
  69. Surface *surf;
  70. {
  71.     Surface *res;
  72.  
  73.     if (!surf)
  74.         return (Surface *)NULL;
  75.  
  76.     res = SurfaceCreate();
  77.     *res = *surf;
  78.     res->next = (Surface *)NULL;
  79.     res->name = (char *)NULL;
  80.     return res;
  81. }
  82.  
  83. /*
  84.  * Compute combination of two surfaces. Resulting surface is copied into surf1.
  85.  */
  86. void
  87. SurfaceBlend(surf1, surf2, p, q)
  88. Surface *surf1, *surf2;
  89. Float p, q;
  90. {
  91.     /*
  92.       * P is weight of surf1.  q is weight of surf2.
  93.      * Result is placed in surf1.
  94.      */
  95.     if (q < EPSILON)
  96.         return;    /* keep surf1 as-is */
  97.  
  98.     ColorBlend(&surf1->amb, &surf2->amb, p, q);
  99.     ColorBlend(&surf1->diff, &surf2->diff, p, q);
  100.     ColorBlend(&surf1->spec, &surf2->spec, p, q);
  101.     ColorBlend(&surf1->translu, &surf2->translu, p, q);
  102.     ColorBlend(&surf1->body, &surf2->body, p, q);
  103.  
  104.     surf1->srexp = blend(surf1->srexp, surf2->srexp, p, q);
  105.     surf1->stexp = blend(surf1->stexp, surf2->stexp, p, q);
  106.  
  107.     surf1->reflect = blend(surf1->reflect, surf2->reflect, p, q);
  108.     surf1->transp  = blend(surf1->transp,  surf2->transp,  p, q);
  109.     surf1->translucency = blend(surf1->translucency, surf2->translucency,
  110.             p, q);
  111.     /*
  112.      * Questionable...
  113.      */
  114.     surf1->statten = blend(surf1->statten, surf2->statten, p, q);
  115.     surf1->index = blend(surf1->index, surf2->index, p, q);
  116.  
  117.     if (p < EPSILON) {
  118.         surf1->noshadow = surf2->noshadow;
  119.     } else {
  120.         /* else there's a blend of some kind... */
  121.         surf1->noshadow = (surf1->noshadow && surf2->noshadow);
  122.     }
  123. }
  124.  
  125. /*
  126.  * Blend two colors.  Result is placed in color1.
  127.  */
  128. void
  129. ColorBlend(color1, color2, p, q)
  130. Color *color1, *color2;
  131. Float p, q;
  132. {
  133.     color1->r = blend(color1->r, color2->r, p, q);
  134.     color1->g = blend(color1->g, color2->g, p, q);
  135.     color1->b = blend(color1->b, color2->b, p, q);
  136. }
  137.  
  138. SurfList *
  139. SurfPop(list)
  140. SurfList *list;
  141. {
  142.     SurfList *stmp = list->next;
  143.  
  144.     free((voidstar)list);
  145.     return stmp;
  146. }
  147.  
  148. SurfList *
  149. SurfPush(surf, list)
  150. Surface *surf;
  151. SurfList *list;
  152. {
  153.     SurfList *stmp;
  154.  
  155.     stmp = (SurfList *)Malloc(sizeof(SurfList));
  156.     stmp->surf = surf;
  157.     stmp->next = list;
  158.     return stmp;
  159. }
  160.