<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/249184</link>
    <title>MATLAB Central Newsreader - problem with surface lighting (OpenGL)</title>
    <description>Feed for thread: problem with surface lighting (OpenGL)</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Thu, 16 Apr 2009 12:14:01 -0400</pubDate>
      <title>problem with surface lighting (OpenGL)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/249184#643147</link>
      <author>Andres </author>
      <description>Dear all,&lt;br&gt;
please compare the surface lighting of the following four plots:&lt;br&gt;
&lt;br&gt;
% -----------------------------------&lt;br&gt;
[Y,Z,X] = cylinder([1.2 1 1 0.8],72);   % &quot;[Y,Z,X]&quot;!&lt;br&gt;
X(3:end,:) = X(3:end,:)+0.5;&lt;br&gt;
&lt;br&gt;
figure(1)&lt;br&gt;
surf(X,Y,Z,'EdgeAlpha',0.2,'FaceColor','c')&lt;br&gt;
axis equal off&lt;br&gt;
light('Position',[0 0 1],'Style','infinite');&lt;br&gt;
&lt;br&gt;
idxShift = mod(1:73,4)&amp;gt;1;&lt;br&gt;
X(3,idxShift) = X(3,idxShift)+0.2;&lt;br&gt;
&lt;br&gt;
figure(2)&lt;br&gt;
surf(X,Y,Z,'EdgeAlpha',0.2,'FaceColor','c')&lt;br&gt;
axis equal off&lt;br&gt;
light('Position',[0 0 1],'Style','infinite');&lt;br&gt;
&lt;br&gt;
figure(3)&lt;br&gt;
surf(X(1:3,:),Y(1:3,:),Z(1:3,:),'EdgeAlpha',0.2,'FaceColor','c')&lt;br&gt;
axis equal off&lt;br&gt;
light('Position',[0 0 1],'Style','infinite');&lt;br&gt;
&lt;br&gt;
figure(4)&lt;br&gt;
surf(X(1:3,:),Y(1:3,:),Z(1:3,:),'EdgeAlpha',0.2,'FaceColor','c')&lt;br&gt;
hold on&lt;br&gt;
surf(X(3:4,:),Y(3:4,:),Z(3:4,:),'EdgeAlpha',0.2,'FaceColor','c')&lt;br&gt;
axis equal off&lt;br&gt;
light('Position',[0 0 1],'Style','infinite');&lt;br&gt;
% -----------------------------------&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
I am not happy with the surface of figure 2 - which represents the one I need, of course :-(&lt;br&gt;
The inner faces are irreguarly lit and look zebra-like, unlike those in the other figures (in my real application the effect is even worse).&lt;br&gt;
Figure 4 comes close to what I want and expect, except for the double edge lines, but anyhow breaking up the mesh is not an option.&lt;br&gt;
The renderer 'zbuffer' always gives a good face lighting, btw, but I need to control the transparency of the surface and the edges.&lt;br&gt;
&lt;br&gt;
So is there any good explanation for why fig. 2 looks so different from fig. 4?&lt;br&gt;
And hopefully even a workaround?&lt;br&gt;
Thanks for your thoughts&lt;br&gt;
Andres</description>
    </item>
    <item>
      <pubDate>Fri, 17 Apr 2009 10:03:01 -0400</pubDate>
      <title>Re: problem with surface lighting (OpenGL)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/249184#643423</link>
      <author>Oliver Woodford</author>
      <description>It looks to me like the face normals are being incorrectly calculated here. I would submit it as a bug report and see what happens.</description>
    </item>
    <item>
      <pubDate>Fri, 17 Apr 2009 12:23:02 -0400</pubDate>
      <title>Re: problem with surface lighting (OpenGL)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/249184#643459</link>
      <author>Andres </author>
      <description>&quot;Oliver Woodford&quot; &amp;lt;o.j.woodford.98@cantab.net&amp;gt; wrote in message &amp;lt;gs9k4l$3c4$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; It looks to me like the face normals are being incorrectly calculated here. I would submit it as a bug report and see what happens.&lt;br&gt;
&lt;br&gt;
Thanks for your answer, Oliver, so I'm not the only one observing the zebra. It's remarkable that the face normal calculation should depend on the existence or shape of neighbouring faces.&lt;br&gt;
In the meantime I've sent in a service request. To strengthen the effect, I've increased the number of faces:&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
[Y,Z,X] = cylinder([1.2 1 1 0.8],144);   % &quot;[Y,Z,X]&quot;!&lt;br&gt;
X(3:end,:) = X(3:end,:)+0.5;&lt;br&gt;
&lt;br&gt;
figure(1)&lt;br&gt;
surf(X,Y,Z,'EdgeAlpha',0.2,'FaceColor','c')&lt;br&gt;
axis equal off&lt;br&gt;
light('Position',[0 0 1],'Style','infinite');&lt;br&gt;
&lt;br&gt;
idxShift = mod(1:size(X,2),4)&amp;gt;1;&lt;br&gt;
X(3,idxShift) = X(3,idxShift)+0.2;&lt;br&gt;
&lt;br&gt;
figure(2)&lt;br&gt;
surf(X,Y,Z,'EdgeAlpha',0.2,'FaceColor','c')&lt;br&gt;
axis equal off&lt;br&gt;
light('Position',[0 0 1],'Style','infinite');&lt;br&gt;
&lt;br&gt;
figure(3)&lt;br&gt;
surf(X(1:3,:),Y(1:3,:),Z(1:3,:),'EdgeAlpha',0.2,'FaceColor','c')&lt;br&gt;
axis equal off&lt;br&gt;
light('Position',[0 0 1],'Style','infinite');&lt;br&gt;
&lt;br&gt;
figure(4)&lt;br&gt;
surf(X(1:3,:),Y(1:3,:),Z(1:3,:),'EdgeAlpha',0.2,'FaceColor','c')&lt;br&gt;
hold on&lt;br&gt;
surf(X(3:4,:),Y(3:4,:),Z(3:4,:),'EdgeAlpha',0.2,'FaceColor','c')&lt;br&gt;
axis equal off&lt;br&gt;
light('Position',[0 0 1],'Style','infinite');</description>
    </item>
    <item>
      <pubDate>Fri, 17 Apr 2009 14:04:52 -0400</pubDate>
      <title>Re: problem with surface lighting (OpenGL)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/249184#643481</link>
      <author>Michael Garrity</author>
      <description>&amp;gt;&quot;Andres&quot; &amp;lt;rantore@werb.deNoRs&amp;gt; wrote in message news:gs77e9$fbj$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; Dear all,&lt;br&gt;
&amp;gt; please compare the surface lighting of the following four plots:&lt;br&gt;
&amp;gt;&lt;br&gt;
&lt;br&gt;
Sorry, there's a bug in the opengl renderer that's causing it to&lt;br&gt;
use the wrong normals. It uses the vertex normals instead of&lt;br&gt;
the face normals in this case. If you switch your renderer to&lt;br&gt;
zbuffer instead of opengl, you'll get the correct lighting in this&lt;br&gt;
case.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Mike Garrity&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;The MathWorks</description>
    </item>
    <item>
      <pubDate>Sun, 19 Apr 2009 11:17:02 -0400</pubDate>
      <title>Re: problem with surface lighting (OpenGL)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/249184#643779</link>
      <author>Chaos </author>
      <description>&quot;Andres&quot; &amp;lt;rantore@werb.deNoRs&amp;gt; wrote in message&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So is there any good explanation for why fig. 2 looks so different from fig. 4?&lt;br&gt;
&amp;gt; And hopefully even a workaround?&lt;br&gt;
&amp;gt; Thanks for your thoughts&lt;br&gt;
&amp;gt; Andres&lt;br&gt;
\&lt;br&gt;
&lt;br&gt;
i have found so many bugs in the opengl implementation, it's a running joke in these parts.  we have the latest screaming ATI cards and have to dumb them down to other renderers.&lt;br&gt;
&lt;br&gt;
there is a kluge of a work around we've found, if you setup how things 'should' look even though on screen they look wrong, modify your graphic call to turn off the &quot;Visibility&quot; and write directly to disk in a tif or whatever.  we got lucky and the images on disk are correct even when display is wrong. i did a complete manual mode and it works.&lt;br&gt;
&lt;br&gt;
no thanks to anyone on this forum.&lt;br&gt;
&lt;br&gt;
plot_set=figure('Units','pixels','Position',iPos,...&lt;br&gt;
&amp;nbsp;&amp;nbsp;'OuterPosition',oPos,'InvertHardCopy', 'off',...&lt;br&gt;
&amp;nbsp;&amp;nbsp;'ActivePositionProperty','OuterPosition','Visible','off',...&lt;br&gt;
&amp;nbsp;&amp;nbsp;'Renderer','OpenGl','RendererMode','manual','doubleBuffer','off',...&lt;br&gt;
&amp;nbsp;&amp;nbsp;'MenuBar','none','NextPlot','replacechildren','ToolBar','none',...&lt;br&gt;
&amp;nbsp;&amp;nbsp;'Color',[0.2 0.6 0.9],'DefaultAxesXColor',[.85  .85  0],'DefaultAxesYColor',[.85  .85  0],...&lt;br&gt;
&amp;nbsp;&amp;nbsp;'PaperOrientation', 'portrait','PaperPositionMode','auto',...&lt;br&gt;
&amp;nbsp;&amp;nbsp;'DefaultAxesXMinorGrid','on','DefaultAxesYMinorGrid','off','DefaultAxesTickDir','in',...&lt;br&gt;
&amp;nbsp;&amp;nbsp;'DefaultAxesXMinorTick','on','DefaultAxesMinorGridLineStyle',':','DefaultLineLineStyle','-');</description>
    </item>
    <item>
      <pubDate>Mon, 20 Apr 2009 19:20:02 -0400</pubDate>
      <title>Re: problem with surface lighting (OpenGL)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/249184#644111</link>
      <author>Andres </author>
      <description>Thank you, Chaos, for your support, but I had no success with your figure settings and direct printing.&lt;br&gt;
And thanks to Mike for your bug confirmation. Your post gave me the idea to compute the face normals by myself and simply use them with the 'VertexNormals' parameter in surf.&lt;br&gt;
I've written a small function, 'facenorm':&lt;br&gt;
&lt;br&gt;
% ---&lt;br&gt;
function N = facenorm(X,Y,Z)&lt;br&gt;
&lt;br&gt;
% FACENORM compute alternate surface normals to be used with surface plots&lt;br&gt;
%&lt;br&gt;
% ...&lt;br&gt;
&lt;br&gt;
% input checking left to surf et.al. ...&lt;br&gt;
&lt;br&gt;
coord = cat(3,X,Y,Z);&lt;br&gt;
&lt;br&gt;
% edge vectors of a face&lt;br&gt;
d11 = diff(coord(:,1:end-1,:),1,1);&lt;br&gt;
d12 = diff(coord(1:end-1,:,:),1,2);&lt;br&gt;
&lt;br&gt;
% opposite edge vectors&lt;br&gt;
d21 = diff(coord(end:-1:1,end:-1:2,:),1,1);&lt;br&gt;
d22 = diff(coord(end:-1:2,end:-1:1,:),1,2);&lt;br&gt;
&lt;br&gt;
% normals of the opposing edges&lt;br&gt;
n1 = cross(d12,d11,3);&lt;br&gt;
n2 = cross(d22,d21,3);&lt;br&gt;
&lt;br&gt;
% add the normals to get an average normal of the face&lt;br&gt;
n = n1 + n2(end:-1:1,end:-1:1,:);&lt;br&gt;
&lt;br&gt;
% prepare for surf and normalize&lt;br&gt;
N = NaN(size(n)+[1 1 0]);&lt;br&gt;
N(2:end,2:end,:) = n;&lt;br&gt;
mag = sqrt(sum(N.^2,3));&lt;br&gt;
mag(mag==0)=eps;&lt;br&gt;
N = N./repmat(mag,[1 1 3]);&lt;br&gt;
&lt;br&gt;
% ---&lt;br&gt;
&lt;br&gt;
See what it does:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;% generate some demo data&lt;br&gt;
&amp;nbsp;&amp;nbsp;[Y,Z,X] = cylinder([0.8 1 1 0.8],144);&lt;br&gt;
&amp;nbsp;&amp;nbsp;idxShift = mod(1:size(X,2),4)&amp;gt;1;&lt;br&gt;
&amp;nbsp;&amp;nbsp;X(2,idxShift) = X(2,idxShift)-0.2;&lt;br&gt;
&amp;nbsp;&amp;nbsp;X(3,idxShift) = X(3,idxShift)+0.2;&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;% standard surf plot&lt;br&gt;
&amp;nbsp;&amp;nbsp;figure(1)&lt;br&gt;
&amp;nbsp;&amp;nbsp;surf(X,Y,Z,'EdgeAlpha',0.2,'FaceColor','c')&lt;br&gt;
&amp;nbsp;&amp;nbsp;axis equal&lt;br&gt;
&amp;nbsp;&amp;nbsp;light('Position',[0 0 1],'Style','infinite')&lt;br&gt;
&amp;nbsp;&amp;nbsp;view(0,60)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;% same plot with alternate surface normals &lt;br&gt;
&amp;nbsp;&amp;nbsp;% (giving a symmetrical lighting as it should)&lt;br&gt;
&amp;nbsp;&amp;nbsp;figure(10)&lt;br&gt;
&amp;nbsp;&amp;nbsp;surf(X,Y,Z,'EdgeAlpha',0.2,'FaceColor','c',...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'VertexNormals',facenorm(X,Y,Z))&lt;br&gt;
&amp;nbsp;&amp;nbsp;axis equal&lt;br&gt;
&amp;nbsp;&amp;nbsp;light('Position',[0 0 1],'Style','infinite')&lt;br&gt;
&amp;nbsp;&amp;nbsp;view(0,60)&lt;br&gt;
&lt;br&gt;
To me, the second plot looks quite promising, but I'm not sure whether it will work in general, or what drawbacks there are. If anyone likes to look at it, I'd be glad (e.g. I heavily suspect it can be simplified), but hopefully it does the trick for me as it is.</description>
    </item>
    <item>
      <pubDate>Sat, 25 Apr 2009 19:21:01 -0400</pubDate>
      <title>Re: problem with surface lighting (OpenGL)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/249184#645387</link>
      <author>Andres </author>
      <description>I've put the workaround to&lt;br&gt;
&lt;a href=&quot;http://www.mathworks.de/matlabcentral/fileexchange/23882&quot;&gt;http://www.mathworks.de/matlabcentral/fileexchange/23882&lt;/a&gt;</description>
    </item>
  </channel>
</rss>

