src/shlnonrm.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: shlnonrm.c,v 1.2 2006/08/17 19:28:17 mhender Exp $";
00018 
00019 #include <shInternal.h>
00020 #include <math.h>
00021 
00022 static int shlw=0;
00023 
00029 void shSetLineWidth(int w)
00030  {
00031   shlw=w;
00032   return;
00033  }
00034 
00035 static void shtmpsetp(int,int,int,int,int,float,int);
00036 
00052 void shlnonrm(float *x0,float *y0,float *z0,float *n0,float *x1,float *y1,float *z1,float *n1,int *ixoff,int *iyoff,int *idoff)
00053  {
00054   int rr=0;
00055   int gg=0;
00056   int bb=0;
00057   int i0,j0,i1,j1;
00058   int imask=0;
00059 
00060   float nn[3]={0.,0.,0.};
00061   float zbuf=0.;
00062   float dx,dy;
00063   int ipix,jpix;
00064 
00065   float s0=0.;
00066   float t0=0.;
00067   float d0;
00068   float s1=0.;
00069   float t1=0.;
00070   float d1;
00071   float xx,yy,zz;
00072   float xxx=0.;
00073   float yyy=0.;
00074   float zzz=0.;
00075   float xc,yc;
00076   int clipped;
00077   int ip;
00078   float direc;
00079 
00080 /*   Project into perspective coordinates. */
00081 
00082   shpers(x0,y0,z0,&s0,&t0,&d0);
00083   shpers(x1,y1,z1,&s1,&t1,&d1);
00084 
00085   if((*idoff)<0)
00086    {
00087     d0=1.005*d0;
00088     d1=1.005*d1;
00089    }
00090   if((*idoff)>0)
00091    {
00092     d0=.995*d0;
00093     d1=.995*d1;
00094    }
00095 
00096 /*   Transform to pixel coordinates. */
00097 
00098   i0=s0*shMax+.5;
00099   j0=t0*shMax+.5;
00100 
00101   i1=s1*shMax+.5;
00102   j1=t1*shMax+.5;
00103 
00104   if(i0==i1&&j0==j1)
00105    {
00106     if(i0>0&&i0<shIMax&&j0>0&&j0<shJMax)
00107      {
00108       shgetz(&i0,&j0,&zbuf);
00109       if(zbuf>d0)
00110        {
00111         if(shMask)
00112          {
00113           sh3dmask(x0,y0,z0,n0,&imask);
00114           if(imask==1)
00115            {
00116             shcolor(x0,y0,z0,n0,&sh_rl,&sh_gl,&sh_bl,&sh_rl,&sh_gl,&sh_bl,&rr,&gg,&bb);
00117             if(shlw==0)shsetp(&i0,&j0,&rr,&gg,&bb,&d0);
00118              else shtmpsetp(i0,j0,rr,gg,bb,d0,shlw);
00119            }
00120          }else{
00121           shcolor(x0,y0,z0,n0,&sh_rl,&sh_gl,&sh_bl,&sh_rl,&sh_gl,&sh_bl,&rr,&gg,&bb);
00122           if(shlw==0)shsetp(&i0,&j0,&rr,&gg,&bb,&d0);
00123            else shtmpsetp(i0,j0,rr,gg,bb,d0,shlw);
00124          }
00125        }
00126      }
00127    return;
00128   }
00129 
00130 /*  Trace and shade the line. */
00131 
00132   xc=i0;
00133   yc=j0;
00134   dx=i1-xc;
00135   dy=j1-yc;
00136   while(fabs(dx)>.5||fabs(dy)>.5)
00137    {
00138     if(fabs(i1-i0)>fabs(j1-j0))
00139      {
00140       xx=s0+(float)(xc-i0)*(s1-s0)/(float)(i1-i0);
00141       yy=t0+(float)(xc-i0)*(t1-t0)/(float)(i1-i0);
00142       zz=d0+(float)(xc-i0)*(d1-d0)/(float)(i1-i0);
00143       nn[0]=n0[0]+(float)(xc-i0)*(n1[0]-n0[0])/(float)(i1-i0);
00144       nn[1]=n0[1]+(float)(xc-i0)*(n1[1]-n0[1])/(float)(i1-i0);
00145       nn[2]=n0[2]+(float)(xc-i0)*(n1[2]-n0[2])/(float)(i1-i0);
00146      }else{
00147       xx=s0+(float)(yc-j0)*(s1-s0)/(float)(j1-j0);
00148       yy=t0+(float)(yc-j0)*(t1-t0)/(float)(j1-j0);
00149       zz=d0+(float)(yc-j0)*(d1-d0)/(float)(j1-j0);
00150       nn[0]=n0[0]+(float)(yc-j0)*(n1[0]-n0[0])/(float)(j1-j0);
00151       nn[1]=n0[1]+(float)(yc-j0)*(n1[1]-n0[1])/(float)(j1-j0);
00152       nn[2]=n0[2]+(float)(yc-j0)*(n1[2]-n0[2])/(float)(j1-j0);
00153      }
00154 
00155 /*    clip */
00156 
00157     clipped=0;
00158     shunpers(&xx,&yy,&zz,&xxx,&yyy,&zzz);
00159     if(sh_nplns>0)
00160      {
00161       for(ip=0;ip<sh_nplns;ip++)
00162        {
00163         direc=sh_plnn[  3*ip]*(xxx-sh_plno[  3*ip])+sh_plnn[1+3*ip]*(yyy-sh_plno[1+3*ip])+sh_plnn[2+3*ip]*(zzz-sh_plno[2+3*ip]);
00164         if(sh_oper[ip]==0)
00165           clipped=clipped||(sh_ipln[ip]*direc<0);
00166          else
00167           clipped=clipped&&(sh_ipln[ip]*direc<0);
00168        }
00169      }
00170     if(!clipped)
00171      {
00172       ipix=xc+.5;
00173       jpix=yc+.5;
00174       if(ipix>0&&ipix<shIMax&&jpix>0&&jpix<shJMax)
00175        {
00176         shgetz(&ipix,&jpix,&zbuf);
00177         if(zbuf>zz-.005)
00178          {
00179           if(shMask)
00180            {
00181             sh3dmask(&xxx,&yyy,&zzz,nn,&imask);
00182             if(imask==1)
00183              {
00184               shcolor(&xxx,&yyy,&zzz,nn,&sh_rl,&sh_gl,&sh_bl,&sh_rl,&sh_gl,&sh_bl,&rr,&gg,&bb);
00185               if(shlw==0)shsetp(&ipix,&jpix,&rr,&gg,&bb,&zz);
00186                else shtmpsetp(ipix,jpix,rr,gg,bb,zz,shlw);
00187              }
00188            }else{
00189             shcolor(&xxx,&yyy,&zzz,nn,&sh_rl,&sh_gl,&sh_bl,&sh_rl,&sh_gl,&sh_bl,&rr,&gg,&bb);
00190             if(shlw==0)shsetp(&ipix,&jpix,&rr,&gg,&bb,&zz);
00191              else shtmpsetp(ipix,jpix,rr,gg,bb,zz,shlw);
00192            }
00193          }
00194        }
00195      }
00196 
00197     dx=i1-xc;
00198     dy=j1-yc;
00199     if(fabs(dx)>.5||fabs(dy)>.5)
00200      {
00201       if(fabs(dx)>fabs(dy))
00202        {
00203         if(dx>0)
00204          {
00205           xc=xc+1;
00206           yc=yc+dy/dx;
00207          }else{
00208           xc=xc-1;
00209           yc=yc-dy/dx;
00210          }
00211        }else{
00212         if(dy>0)
00213          {
00214           yc=yc+1;
00215           xc=xc+dx/dy;
00216          }else{
00217           yc=yc-1;
00218           xc=xc-dx/dy;
00219          }
00220        }
00221      }
00222    }
00223 
00224   return;
00225  }
00226 
00227 void shtmpsetp(int i0,int j0,int r,int g,int b,float z,int w)
00228  {
00229   int iii,jjj,ii,jj;
00230   float zbuf;
00231 
00232   for(iii=-w;iii<w+1;iii++)
00233    {
00234     for(jjj=-w;jjj<w+1;jjj++)
00235      {
00236       ii=i0+iii;jj=j0+jjj;
00237       shgetz(&ii,&jj,&zbuf);
00238       if(sqrt(1.*(iii*iii+jjj*jjj)/w/w)<=1)
00239        {
00240         if(i0>0&&i0<shIMax&&j0>0&&j0<shJMax)
00241          {
00242           if(zbuf>z-.005)shsetp(&ii,&jj,&r,&g,&b,&z);
00243          }
00244        }
00245      }
00246    }
00247 
00248   return;
00249  }

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