# HG changeset patch
# User mmuetzel <markus.muetzel@gmx.de>
# Date 1457977571 -3600
#      Mon Mar 14 18:46:11 2016 +0100
# Node ID 56ee61ad487adc2f845decaeb7d454c785eff8bd
# Parent  de155ca994d44fc6601c24fd482d8654f24ef910
fix for bug #42006 (set (h, "facelighting", "flat") renders dark surfaces/patches)

diff -r de155ca994d4 -r 56ee61ad487a libinterp/corefcn/gl-render.cc
--- a/libinterp/corefcn/gl-render.cc	Sun Mar 13 08:46:59 2016 -0700
+++ b/libinterp/corefcn/gl-render.cc	Mon Mar 14 18:46:11 2016 +0100
@@ -1801,6 +1801,21 @@
   int ea_mode = (props.edgealpha_is_double () ? 0 :
                  (props.edgealpha_is ("flat") ? 1 : 2));
 
+  // check whether there is a light object
+  // Is it save to assume that the parent of surface always is the axes object?
+  graphics_object parent_go = gh_manager::get_object (props.get_parent ());
+  Matrix children = parent_go.get_properties ().get_all_children ();
+  bool has_light = false;
+  for (octave_idx_type i = 0; i < children.numel (); i++)
+    {
+      graphics_object go = gh_manager::get_object (children(i));
+      if (go.get_properties ().is_visible () && go.isa ("light"))
+        {
+          has_light = true;
+          break;
+        }
+    }
+
   Matrix fcolor = (fc_mode == TEXTURE ? Matrix (1, 3, 1.0)
                                       : props.get_facecolor_rgb ());
   Matrix ecolor = props.get_edgecolor_rgb ();
@@ -1877,7 +1892,7 @@
                 }
             }
 
-          if (fl_mode > 0)
+          if ((fl_mode > 0) && has_light)
             glEnable (GL_LIGHTING);
           glShadeModel ((fc_mode == INTERP || fl_mode == GOURAUD) ? GL_SMOOTH
                                                                   : GL_FLAT);
@@ -2052,7 +2067,7 @@
           if (fc_mode == TEXTURE)
             glDisable (GL_TEXTURE_2D);
 
-          if (fl_mode > 0)
+          if ((fl_mode > 0) && has_light)
             glDisable (GL_LIGHTING);
         }
       else
@@ -2080,7 +2095,7 @@
                 }
             }
 
-          if (el_mode > 0)
+          if ((el_mode > 0) && has_light)
             glEnable (GL_LIGHTING);
           glShadeModel ((ec_mode == INTERP || el_mode == GOURAUD) ? GL_SMOOTH
                                                                   : GL_FLAT);
@@ -2285,7 +2300,7 @@
           set_linestyle ("-");
           set_linewidth (0.5);
 
-          if (el_mode > 0)
+          if ((el_mode > 0) && has_light)
             glDisable (GL_LIGHTING);
         }
       else
@@ -2414,6 +2429,21 @@
   int ea_mode = (props.edgealpha_is_double () ? 0 :
                  (props.edgealpha_is ("flat") ? 1 : 2));
 
+  // check whether there is a light object
+  // Is it save to assume that the parent of patch always is the axes object?
+  graphics_object parent_go = gh_manager::get_object (props.get_parent ());
+  Matrix children = parent_go.get_properties ().get_all_children ();
+  bool has_light = false;
+  for (octave_idx_type i = 0; i < children.numel (); i++)
+    {
+      graphics_object go = gh_manager::get_object (children(i));
+      if (go.get_properties ().is_visible () && go.isa ("light"))
+        {
+          has_light = true;
+          break;
+        }
+    }
+
   Matrix fcolor = props.get_facecolor_rgb ();
   Matrix ecolor = props.get_edgecolor_rgb ();
 
@@ -2546,7 +2576,7 @@
                 }
             }
 
-          if (fl_mode > 0)
+          if ((fl_mode > 0) && has_light)
             glEnable (GL_LIGHTING);
 
           // NOTE: Push filled part of patch backwards to avoid Z-fighting with
@@ -2605,7 +2635,7 @@
               tess.end_polygon ();
             }
 
-          if (fl_mode > 0)
+          if ((fl_mode > 0) && has_light)
             glDisable (GL_LIGHTING);
         }
       else
@@ -2636,7 +2666,7 @@
                 }
             }
 
-          if (el_mode > 0)
+          if ((el_mode > 0) && has_light)
             glEnable (GL_LIGHTING);
 
           set_linestyle (props.get_linestyle (), false);
@@ -2728,7 +2758,7 @@
           set_linestyle ("-");
           set_linewidth (0.5);
 
-          if (el_mode > 0)
+          if ((el_mode > 0) && has_light)
             glDisable (GL_LIGHTING);
         }
       else
