home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / lib / mg / rib / mgribshade.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-17  |  4.7 KB  |  141 lines

  1. #include "mgP.h"
  2. #include "mgribP.h"
  3. #include "mgribshade.h"
  4. #include "mgribtoken.h"
  5.  
  6. /*
  7.  * Notes:    Tossed mgrib_material, just use mgrib_appearance
  8.  *        since shaders depend on both appearance and material 
  9.  *        settings. (4/1/93 TMM)
  10.  */
  11. void
  12. mgrib_appearance( struct mgastk *astk, int ap_mask, int mat_mask)
  13. {
  14.     Appearance *ap = &(astk->ap);
  15.     Material *mat = &astk->mat;
  16.  
  17.     mrti(mr_section, "Interpreting Material", mr_NULL);
  18.  
  19.     if(mat_mask & MTF_DIFFUSE)
  20.         mrti(mr_color, mr_parray, 3, &mat->diffuse, mr_NULL);
  21.  
  22.     if( (ap_mask & APF_TRANSP || mat_mask & MTF_ALPHA) && 
  23.        ap->valid & APF_TRANSP && ap->flag & APF_TRANSP) {
  24.         /* presumably, we want this here as well as per vertex opacity
  25.          * specification
  26.      * 
  27.      * Actually, if the material alpha override bit is set, we
  28.      * don't output the per vertex opacity information, which
  29.      * would take precedence. 4/16/93 TMM
  30.          */
  31.         float alpha = astk->mat.alpha;
  32.     mrti(mr_opacity, mr_array, 3, alpha, alpha, alpha, mr_NULL);
  33.     }
  34.  
  35.     if(ap_mask & APF_SHADING || 
  36.        (mat_mask & MTF_SPECULAR|MTF_SHININESS|MTF_Kd|MTF_Ka|MTF_Ks)) {
  37.  
  38.         float roughness = (mat->shininess)? 1.0/mat->shininess : 1.0;
  39.     if(ap->shading == APF_CONSTANT) {
  40.         mrti(mr_surface, mr_constant, mr_NULL);
  41.     } else if(ap->shading == APF_FLAT) {
  42.         int shader;
  43.         /* determain shader */
  44.         if(_mgribc->shader==MG_RIBSTDSHADE) {
  45.         shader = mr_plastic;
  46.         } else {
  47.             if(_mgc->space & TM_HYPERBOLIC) shader = mr_hplastic;
  48.         else shader = mr_plastic;
  49.         }
  50.         /* define surface */
  51.         mrti(mr_shadinginterpolation, mr_constant,
  52.             mr_surface, shader, mr_Ka, mr_float, mat->ka,
  53.             mr_Kd, mr_float, mat->kd, mr_Ks, mr_float, mat->ks,
  54.             mr_specularcolor, mr_parray, 3, &(mat->specular),
  55.             mr_roughness, mr_float, roughness, mr_NULL);
  56.     } else if(ap->shading == APF_SMOOTH) {
  57.         mrti(mr_shadinginterpolation, mr_string, "smooth",
  58.             mr_surface, mr_plastic, mr_Ka, mr_float, mat->ka,
  59.             mr_Kd, mr_float, mat->kd, mr_Ks, mr_float, mat->ks,
  60.             mr_specularcolor, mr_parray, 3, &(mat->specular),
  61.             mr_roughness, mr_float, roughness, mr_NULL);
  62.     }
  63.     }    
  64. }
  65.  
  66. void
  67. mgrib_lighting(struct mgastk *astk, int mask)
  68. {
  69.   LtLight *light;
  70.   LmLighting *li = &astk->lighting;
  71.  
  72.   if (li->valid & mask & LMF_AMBIENT) {
  73.       mrti(mr_section, "Interpreting Lights", mr_NULL);
  74.       /* output the ambient light */
  75.       mrti(mr_comment, "Ambient Light", mr_NULL);
  76.       mrti(mr_lightsource, mr_ambientlight, mr_int, 0,
  77.        mr_lightcolor, mr_parray, 3, &(li->ambient), mr_NULL);
  78.     }    
  79.   /* We must be in global coordinate space to place lights correctly. */
  80.   mrti(mr_transformbegin, mr_identity, mr_NULL);
  81.   mgrib_lights( li->lights, astk );
  82.   mrti(mr_transformend, mr_NULL);
  83. }
  84.  
  85. /* In GL, we want unique light numbers. In RenderMan we want to re-use
  86. light numbers, so we always start with light "1".  In GL, we define
  87. then bind lights. Defining is expensive and we avoid it as much as
  88. possible. In RenderMan, it's a one-step process. Also, once a light is
  89. defined it cannot be deleted. Thus we might need to explicitly turn
  90. off lights if we are replacing more lights with less lights. So we
  91. keep track of how many lights we've used so far, and turn off the
  92. extras when we need to.
  93. 1/13/92 TMM */
  94.  
  95. void mgrib_lights( LtLight *light, struct mgastk *astk ) 
  96.   int i, lightsused; 
  97.   static int prevused = 0;
  98.  
  99.   lightsused = 0;
  100.   while (light) {
  101.     ++lightsused;
  102.  
  103.     if (light->Private == 0) {
  104.       /* this is a new light */
  105.       light->Private = lightsused;
  106.       light->changed = 1;
  107.     }
  108.     if (light->changed) {
  109.       if (light->position.w == 0.0) {
  110.     /* directional light */
  111.     /* We used to output the lights as "to" instead of as "from".
  112.        This is like negating them, which is why we then did
  113.        explicitly negate them to get the right result!
  114.        So, leave the lights alone now. Do NOT negate them.
  115.        It's NOT about handedness of coordinate systems.
  116.          */
  117.     mrti(mr_comment, "Directional Light",
  118.          mr_lightsource, mr_distantlight, mr_int, light->Private,
  119.          mr_intensity, mr_float, light->intensity,
  120.          mr_lightcolor, mr_parray, 3, &(light->color),
  121.          mr_string, "to", mr_array, 3, 0.,0.,0., /* aim at origin */
  122.          mr_string, "from", mr_parray, 3, &(light->globalposition),
  123.          mr_NULL);
  124.       } else {
  125.     /* point light */
  126.     mrti(mr_lightsource, mr_string, "pointlight", mr_int, light->Private,
  127.          mr_intensity, mr_float, light->intensity,
  128.          mr_lightcolor, mr_parray, 3, &(light->color),
  129.          mr_string, "from", mr_parray, 3, &(light->globalposition),
  130.          mr_NULL);
  131.       }
  132.       light->changed = 0;
  133.     }
  134.     light = light->next;
  135.   }
  136.   for (i = lightsused +1; i <= prevused; i++)
  137.     mrti(mr_illuminate, mr_int, i, mr_int, 0, mr_NULL);
  138.   if (prevused < lightsused) prevused = lightsused;
  139. }
  140.