src/shcolor.c

00001 
00006 /*
00007  *  PROGRAM NAME:  sh
00008  *
00009  *  (c) COPYRIGHT INTERNATIONAL BUSINESS MACHINES
00010  *  CORPORATION 12/1/2001.  ALL RIGHTS RESERVED.
00011  *
00012  *  Please refer to the LICENSE file in the top directory
00013  *
00014  *      author: Mike Henderson mhender@watson.ibm.com
00015  */
00016 
00017 static char *id="@(#) $Id: shcolor.c,v 1.2 2006/08/17 19:28:17 mhender Exp $";
00018 
00019 #include <shInternal.h>
00020 #include <math.h>
00021 
00039 void shcolor(float *x,float *y,float *z,float *n,int *rf,int *gf,int *bf,int *rb,int *gb,int *bb,int *r,int *g,int *b)
00040  {
00041 
00042 /*  Shade and render a point. */
00043 
00044    int rd[2]={0,0};
00045    int gd[2]={0,0};
00046    int bd[2]={0,0};
00047 
00048 /* Geometric Information */
00049 
00050 /*    EYE[2]    position of the viewer                            */
00051 /*    NLIT      number of light sources                           */
00052 /*    LIT(3,M)  position of light source m                        */
00053 /*    RS(M)     color of light source m                           */
00054 /*    GS(M)                                                       */
00055 /*    BS(M)                                                       */
00056 /*    AM        intensity of ambient illumination                 */
00057 /*    AD        intensity of diffuse illumination                 */
00058 /*    AS        intensity of specular illumination                */
00059 /*    ND        exponent  of specular illumination                */
00060 
00061 /* Clipping Planes                            */
00062 
00063 /*    NPLNS     Number of clipping planes */
00064 /*    PLNO(3,M) point on plane M */
00065 /*    PLNN(3,M) normal to plane M */
00066 /*    IPLN(M)   side of plane M to clip (IPLN*PLNN<0 ==> clipped) */
00067 /*    oper(M)   whether clipping is ored or anded. */
00068 
00069   float e[3]={0.,0.,0.};
00070   float l[3]={0.,0.,0.};
00071   float h[3]={0.,0.,0.};
00072 
00073   float an;
00074   float ah;
00075   float ae;
00076   float sd;
00077   float ssr;
00078   float ssg;
00079   float ssb;
00080   int   k;
00081   float xxx=0.;
00082   float yyy=0.;
00083   float zzz=0.;
00084   float al;
00085   int icol;
00086 
00087   an=sqrt(n[0]*n[0]+n[1]*n[1]+n[2]*n[2]);
00088   if(an<1.e-7)
00089    {
00090     *r=*rf;
00091     *g=*gf;
00092     *b=*bf;
00093     return;
00094    }
00095 
00096 /*             e is the vector from the pixel to the eye */
00097 
00098   e[0]=shv_eye[0]-*x;
00099   e[1]=shv_eye[1]-*y;
00100   e[2]=shv_eye[2]-*z;
00101   ae=1./sqrt(e[0]*e[0]+e[1]*e[1]+e[2]*e[2]);
00102   e[0]=e[0]*ae;
00103   e[1]=e[1]*ae;
00104   e[2]=e[2]*ae;
00105 
00106   sd =0.;
00107   ssr=0.;
00108   ssg=0.;
00109   ssb=0.;
00110   for(k=0;k<sh_nlit;k++)
00111    {
00112 
00113 /*             l is the vector from the pixel to light source k */
00114 
00115     if(sh_type[k]==0)
00116      {
00117       l[0]=sh_lit[  3*k]-xxx;
00118       l[1]=sh_lit[1+3*k]-yyy;
00119       l[2]=sh_lit[2+3*k]-zzz;
00120      }else{
00121       l[0]=sh_lit[  3*k];
00122       l[1]=sh_lit[1+3*k];
00123       l[2]=sh_lit[2+3*k];
00124      }
00125     al=1./sqrt(l[0]*l[0]+l[1]*l[1]+l[2]*l[2]);
00126     l[0]=l[0]*al;
00127     l[1]=l[1]*al;
00128     l[2]=l[2]*al;
00129 /*  printf("light %d, l=(%f,%f,%f) n=(%f,%f,%f\n",k,l[0],l[1],l[2],n[0],n[1],n[2]);*/
00130 
00131 /*             h will control the specular reflection */
00132 
00133     h[0]=e[0]+l[0];
00134     h[1]=e[1]+l[1];
00135     h[2]=e[2]+l[2];
00136     ah=1./sqrt(h[0]*h[0]+h[1]*h[1]+h[2]*h[2]);
00137     h[0]=h[0]*ah;
00138     h[1]=h[1]*ah;
00139     h[2]=h[2]*ah;
00140 /*  printf("h=(%f,%f,%f)\n",h[0],h[1],h[2]);*/
00141 
00142 /*  printf("sd=%f sscol=(%f,%f,%f)\n",fabs(n[0]*l[0]+n[1]*l[1]+n[2]*l[2]),sh_rs[k]*pow(fabs(n[0]*h[0]+n[1]*h[1]+n[2]*h[2]),sh_nd),sh_gs[k]*pow(fabs(n[0]*h[0]+n[1]*h[1]+n[2]*h[2]),sh_nd),sh_bs[k]*pow(fabs(n[0]*h[0]+n[1]*h[1]+n[2]*h[2]),sh_nd));*/
00143 
00144     sd =sd +fabs(n[0]*l[0]+n[1]*l[1]+n[2]*l[2]);
00145     ssr=ssr+sh_rs[k]*pow(fabs(n[0]*h[0]+n[1]*h[1]+n[2]*h[2]),sh_nd);
00146     ssg=ssg+sh_gs[k]*pow(fabs(n[0]*h[0]+n[1]*h[1]+n[2]*h[2]),sh_nd);
00147     ssb=ssb+sh_bs[k]*pow(fabs(n[0]*h[0]+n[1]*h[1]+n[2]*h[2]),sh_nd);
00148    }
00149 /*printf("total sd=%f sscol=(%f,%f,%f)\n",sd,ssr,ssg,ssb);*/
00150 
00151   icol=0;
00152   if(n[0]*(shv_eye[0]-*x)+n[1]*(shv_eye[1]-*y)+n[2]*(shv_eye[2]-*z)<0.)icol=1;
00153 /*printf("side %d\n",icol);*/
00154 
00155   rd[0]=*rf;
00156   gd[0]=*gf;
00157   bd[0]=*bf;
00158   rd[1]=*rb;
00159   gd[1]=*gb;
00160   bd[1]=*bb;
00161 
00162   *r=sh_am*rd[icol]+sh_ad*rd[icol]*sd/sh_nlit+sh_as*ssr/sh_nlit;
00163   *g=sh_am*gd[icol]+sh_ad*gd[icol]*sd/sh_nlit+sh_as*ssg/sh_nlit;
00164   *b=sh_am*bd[icol]+sh_ad*bd[icol]*sd/sh_nlit+sh_as*ssb/sh_nlit;
00165 
00166   if(*r>255)*r=255;
00167   if(*g>255)*g=255;
00168   if(*b>255)*b=255;
00169   if(*r<0)*r=0;
00170   if(*g<0)*g=0;
00171   if(*b<0)*b=0;
00172 
00173   return;
00174  }

Generated on Thu Aug 17 15:29:43 2006 for sh by  doxygen 1.4.6