home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / COLOR.DI$ / DIFFUSE.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  1.1 KB  |  36 lines

  1. function r = diffuse(nx,ny,nz,s)
  2. %DIFFUSE Diffuse reflectance.
  3. %    R = DIFFUSE(Nx,Ny,Nz,S) returns the reflectance for a surface with
  4. %    normal vector components [Nx,Ny,Nz].  S is a three vector that
  5. %    defines the direction to the light source. S can also be a two vector
  6. %    S = [Az,El] specifying the direction with azimuth and elevation.
  7. %
  8. %    Lambert's Law: R = cos(theta) where theta is the angle between the
  9. %    surface normal and light source.
  10. %
  11. %    See also SPECULAR, SURFNORM, SURFL.
  12.  
  13. %    Clay M. Thompson 5-1-91
  14. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  15.  
  16. [m,n] = size(nx);
  17. if size(ny)~=[m,n], error('Ny must be the same size as Nx.'); end
  18. if size(nz)~=[m,n], error('Nz must be the same size as Nx.'); end
  19.  
  20. % Generate component matrices for source.
  21. if length(s) == 2 % convert to 3-vector
  22.     az = s(1)*pi/180;
  23.     el = s(2)*pi/180;
  24.     s = [cos(az)*cos(el) sin(az)*cos(el) sin(el)];
  25. end
  26. s = s/norm(s); % Normalize
  27. sx = s(1)*ones(m,n);
  28. sy = s(2)*ones(m,n);
  29. sz = s(3)*ones(m,n);
  30.  
  31. % Normalize normal vectors.
  32. mag = sqrt(nx.*nx+ny.*ny+nz.*nz);
  33. d = (mag==0); mag(d) = eps*ones(size(mag(d)));
  34.  
  35. r = max(0,(sx.*nx + sy.*ny + sz.*nz)./mag);
  36.