1 ## Copyright (C) 2009-2012 Kai Habel
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} surfl (@var{x}, @var{y}, @var{z})
21 ## @deftypefnx {Function File} {} surfl (@var{z})
22 ## @deftypefnx {Function File} {} surfl (@var{x}, @var{y}, @var{z}, @var{L})
23 ## @deftypefnx {Function File} {} surfl (@var{x}, @var{y}, @var{z}, @var{L}, @var{P})
24 ## @deftypefnx {Function File} {} surfl (@dots{}, "light")
25 ## Plot a lighted surface given matrices @var{x}, and @var{y} from
26 ## @code{meshgrid} and
27 ## a matrix @var{z} corresponding to the @var{x} and @var{y} coordinates of
28 ## the mesh. If @var{x} and @var{y} are vectors, then a typical vertex
29 ## is (@var{x}(j), @var{y}(i), @var{z}(i,j)). Thus, columns of @var{z}
30 ## correspond to different @var{x} values and rows of @var{z} correspond
31 ## to different @var{y} values.
33 ## The light direction can be specified using @var{L}. It can be
34 ## given as 2-element vector [azimuth, elevation] in degrees or as 3-element
35 ## vector [lx, ly, lz].
36 ## The default value is rotated 45° counter-clockwise from the current view.
38 ## The material properties of the surface can specified using a 4-element vector
39 ## @var{P} = [@var{AM} @var{D} @var{SP} @var{exp}] which defaults to
40 ## @var{p} = [0.55 0.6 0.4 10].
42 ## @item "AM" strength of ambient light
44 ## @item "D" strength of diffuse reflection
46 ## @item "SP" strength of specular reflection
48 ## @item "EXP" specular exponent
51 ## The default lighting mode "cdata", changes the cdata property to give the
53 ## of a lighted surface. Please note: the alternative "light" mode, which
55 ## object to illuminate the surface is not implemented (yet).
61 ## colormap (bone (64));
66 ## @seealso{surf, diffuse, specular, surface}
69 ## Author: Kai Habel <kai.habel@gmx.de>
71 function retval = surfl (varargin)
73 [h, varargin] = __plt_get_axis_arg__ ("surfl", varargin{:});
80 ## Check for lighting type.
82 if (ischar (varargin{end}))
84 if (strncmp (tolower (lstr), "light", 5))
85 warning ("light method not supported (yet), using cdata method instead");
86 ## This can be implemented when light objects are supported.
88 elseif (strncmp (tolower (lstr), "cdata", 5))
91 error ("surfl: unknown lighting method");
96 ## Check for reflection properties argument.
98 ## r = [ambient light strength,
99 ## diffuse reflection strength,
100 ## specular reflection strength,
102 if (length (varargin{end}) == 4 && isnumeric (varargin{end}))
107 r = [0.55, 0.6, 0.4, 10];
110 ## Check for light vector (lv) argument.
112 if (isnumeric (varargin{end}))
113 len = numel (varargin{end});
114 lastarg = varargin{end};
120 [lv(1), lv(2), lv(3)] = sph2cart ((lastarg(1) - 90) * pi/180, lastarg(2) * pi/180, 1.0);
126 tmp = surface (varargin{:});
128 set (h, "view", [-37.5, 30],
129 "xgrid", "on", "ygrid", "on", "zgrid", "on", "clim", [0 1]);
132 ## Get view vector (vv).
135 [vv(1), vv(2), vv(3)] = sph2cart ((az - 90) * pi/180.0, el * pi/180.0, 1.0);
139 ## Calculate light vector (lv) from view vector.
140 Phi = 45.0 / 180.0 * pi;
141 R = [cos(Phi), -sin(Phi), 0;
142 sin(Phi), cos(Phi), 0;
147 vn = get (tmp, "vertexnormals");
148 dar = get (h, "plotboxaspectratio");
154 vn = vn ./ repmat (sqrt (sumsq (vn, 3)), [1, 1, 3]);
155 [nr, nc] = size(get(tmp, "zdata"));
157 ## Ambient, diffuse, and specular term.
158 cdata = (r(1) * ones (nr, nc)
159 + r(2) * diffuse (vn(:,:,1), vn(:,:,2), vn(:,:,3), lv)
160 + r(3) * specular (vn(:,:,1), vn(:,:,2), vn(:,:,3), lv, vv, r(4)));
162 set (tmp, "cdata", cdata ./ sum (r(1:3)));
164 unwind_protect_cleanup
186 %! surfl(X,Y,Z,[az+225,el],[0.2 0.6 0.4 25]);