%% Light a Surface Using Surface Normals
% Use the surface normals of a curved surface to light a flat surface.
% First, display a flat surface.

surf(ones(49),'EdgeColor','none');

%
% Display the curved surface to use as a lighting source.

surf(peaks);

%
% Now, draw the flat surface again, this time with lighting from the curved surface. To do this, first calculate the surface normals of the curved surface.

[nx, ny, nz] = surfnorm(peaks);

%
% Combine the x, y, and z surface normal components into a single 49-by-49-by-3 array.

b = reshape([nx ny nz], 49,49,3);

%
% Create a flat surface again, this time supplying this array as a value for the VertexNormals property. MATLAB® uses the VertexNormals property to calculate the surface lighting. Set the lighting algorithm to gouraud and add a light using camlight.

surf(ones(49),'VertexNormals',b,'EdgeColor','none');
lighting gouraud
camlight

% Copyright 2015 The MathWorks, Inc.