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

  1. #include "mgP.h"
  2. #include "mgriP.h"
  3. #include "mgrishade.h"
  4.  
  5. /*-----------------------------------------------------------------------
  6.  * Function:    mgri_material
  7.  * Description: bind a material. define it if it's not yet defined.
  8.  * Args:        *mat: Material to bind.
  9.  *              mask: Bitmask telling which material fields are valid.
  10.  *                    Passed into mgri_materialdef.
  11.  * Returns:
  12.  * Author:      gunn, wisdom
  13.  * Date:        Tue Jul 21 14:34:09 CDT 1992
  14.  */
  15.  
  16. void
  17. mgri_material(struct mgastk *astk, int mask)
  18. {
  19.     Material *mat = &astk->mat;
  20.     float roughness = (mat->shininess)? 1.0/mat->shininess : 1.0;
  21.  
  22.     if (mat == NULL) return;
  23.  
  24.     if(mask & ((MTF_EMISSION|MTF_AMBIENT|MTF_SPECULAR
  25.                 |MTF_SHININESS|MTF_Kd|MTF_Ka|MTF_Ks/*|MTF_ALPHA*/))) {
  26.     if(astk->useshader || !IS_SHADED(astk->ap.shading)) {
  27.         RiSurface(RI_CONSTANT, RI_NULL);
  28.     } else {
  29.         RiSurface("plastic", RI_KA, &mat->ka, RI_KD,&mat->kd,
  30.         RI_KS, &mat->ks, RI_SPECULARCOLOR, &mat->specular,
  31.         RI_ROUGHNESS, &roughness, RI_NULL);
  32.     }
  33.     }
  34.  
  35.     // WHAT'S THE PURPOSE OF KEEPING THIS AROUND? THIS FUNCTION
  36.     // SHOULD BE RIPPED OUT, OR?
  37.     if(mask & MTF_DIFFUSE)
  38.     RiColor((float *)&mat->diffuse);
  39.  
  40. }
  41.  
  42.  
  43. mgri_appearance( struct mgastk *astk, int mask )
  44. {
  45.     Appearance *ap = &(astk->ap);
  46.     Material *mat = &(astk->mat);
  47.     
  48.     if(mask & APF_TRANSP) {
  49.         float alpha = astk->mat.alpha;
  50.     /* Quick RenderMan does not (yet) support opacity */
  51.     }
  52.  
  53.     if(ap->flag & APF_FACEDRAW && _mgric->drawsfaces == 0) {
  54.         _mgric->drawsfaces = 1;
  55.     }
  56.     
  57.     if(_mgric->drawsfaces) RiGeometricRepresentation("primitive");
  58.         
  59.     /*
  60.      * We set surface properties here. This is not done in mgri_material
  61.      * because that function doesn't know if the surface should be constant
  62.      * flat or smooth. This needn't be done lower down in mgridraw because
  63.      * the surface properties will override surface normal information.
  64.      */
  65.     if(mask & APF_SHADING) {
  66.     RiShadingInterpolation( IS_SMOOTH(ap->shading) ? RI_SMOOTH : RI_CONSTANT );
  67.     astk->useshader = (astk->shader != NULL) && IS_SHADED(ap->shading);
  68.     mgri_material(astk, ~0);
  69.     }
  70.  
  71.   /*
  72.    * Following are handled at draw time
  73.    *
  74.    *            APF_FACEDRAW
  75.    *            APF_EDGEDRAW
  76.    *
  77.    * WE WILL NEED TO HANDLE THE CASE OF APF_EVERT. Normally, this
  78.    * would be done in a shader. QuickRenderMan does not utilize
  79.    * programmable shaders. How can we handle this?
  80.    */
  81.     
  82. }
  83.  
  84. void mgri_lighting(struct mgastk *astk, int mask)
  85. {
  86.   LtLight *light;
  87.   LmLighting *li = &astk->lighting;
  88.   char lightlabel[8];
  89.  
  90.   sprintf(lightlabel,"0");
  91.   RiLightSource(lightlabel, RI_AMBIENTLIGHT, RI_LIGHTCOLOR,
  92.       &(li->ambient), RI_NULL);
  93.  
  94.   /* We must be in global coordinate space to place lights correctly */
  95.   RiTransformBegin();
  96.   RiIdentity(); /* wont work if we use on to twisted identity in worldbegin? */
  97.               /* or is it the case that since camera transform is 'locked  */
  98.         /* into place' after world begin, that an identity at this   */
  99.         /* point gives us the same transform as we have at wrld bgn? */
  100.   RiConcatTransform(cam2ri); /* cam2ri^-1 = cam2ri, we invert back for qrman */
  101.   mgri_lights( li->lights, astk );
  102.   RiTransformEnd();
  103. }
  104.  
  105. void
  106. mgri_lights( LtLight *light, struct mgastk *astk )
  107. {
  108.   int i, lightsused=0;
  109.   int baselight = -1;
  110.   char lightlabel[8];
  111.   static int prevused = 0;
  112.   
  113.   while (light) {
  114.     ++lightsused;
  115.     /* Don't use the private or changed fields: we don't care. 1/14/92 TMM */
  116.  
  117.     if (light->Private == 0) {
  118.       /* this is a new light */
  119.       light->Private = lightsused;
  120.       light->changed = 1;  /* RenderMan doesn't care, since it has 
  121.                   to spit out the light no matter what... */
  122.     }
  123.     
  124.     /* string label for qrman light definitions */
  125.     sprintf(lightlabel,"%d",light->Private);
  126.     
  127.     if (light->changed) {
  128.       if (light->position.w == 0.0) {
  129.         RtPoint lightTo = {0.0, 0.0, 0.0};
  130.         RtPoint lightFrom =
  131.         {light->position.x, light->position.y, -light->position.z};
  132.         /* we are accomodating for a right handed coordinate */
  133.         /* system? - lights exist outside the world space     */
  134.     /* directional light */
  135.         
  136.     RiLightSource(lightlabel, RI_DISTANTLIGHT, RI_INTENSITY,
  137.         &(light->intensity), RI_LIGHTCOLOR, &(light->color),
  138.         RI_FROM, lightFrom, RI_TO, lightTo, RI_NULL);   
  139.         } else {
  140.         /* point light */
  141.         RtPoint frompoint;
  142.         mgri_normalize(&light->position, frompoint);
  143.         frompoint[2] *= -1; /* flip z coord to compensate for z-flip  */
  144.         RiLightSource(lightlabel, RI_POINTLIGHT, RI_INTENSITY,
  145.         &light->intensity, RI_LIGHTCOLOR, &(light->color),
  146.         RI_FROM, &frompoint,RI_NULL);
  147.       }
  148.       light->changed = 0;
  149.     }
  150.     light = light->next;
  151.   }
  152.   /* turn off extra lights */
  153.   for (i = lightsused +1; i <= prevused; i++) {
  154.     sprintf(lightlabel,"%d",i);   /* This may not work - I may need to */
  155.                       /* collect the tokens returned from  */
  156.                   /* from RiLightSource in a static    */
  157.                   /* array or something like that -smw */
  158.     RiIlluminate(lightlabel, RI_FALSE);
  159.   }
  160.   if (prevused < lightsused) prevused = lightsused;
  161. }
  162.  
  163. mgri_setshader(mgshadefunc shader)
  164. {
  165.   _mgc->astk->shader = shader;
  166.   mgri_appearance(_mgc->astk, APF_SHADING);
  167. }
  168.