# HG changeset patch
# User Pantxo Diribarne <pantxo.diribarne@gmail.com>
# Date 1495974067 -7200
#      Sun May 28 14:21:07 2017 +0200
# Node ID 59f1f7728e7536cdb2022360eccd8c04dd79de94
# Parent  1093162037b44c9472d55386eb2b50879152505e
Draw 2D axes primitives in the children stack order (bug #50750)

* gl-render.h/cc: (opengl_renderer::draw_axes_grids): new method to group all
  grids rendering
  (opengl_renderer::draw_axes): for 2D axes disable DEPTH_TEST. Draw grids 
  before/after primitives depending on the "layer" property  
* gl2ps-print.cc (gl2ps_renderer::draw): if all axes are 2D disable primitive 
  sorting

diff -r 1093162037b4 -r 59f1f7728e75 libinterp/corefcn/gl-render.cc
--- a/libinterp/corefcn/gl-render.cc	Wed Mar 15 22:00:17 2017 +0100
+++ b/libinterp/corefcn/gl-render.cc	Sun May 28 14:21:07 2017 +0200
@@ -1912,6 +1912,28 @@
   }
 
   void
+  opengl_renderer::draw_axes_grids (const axes::properties& props)
+  {
+    // Disable line smoothing for axes
+    GLboolean antialias;
+    glGetBooleanv (GL_LINE_SMOOTH, &antialias);
+    if (antialias == GL_TRUE)
+      glDisable (GL_LINE_SMOOTH);
+
+    set_linecap ("square");
+    set_linewidth (props.get_linewidth ());
+    set_font (props);
+    set_interpreter (props.get_ticklabelinterpreter ());
+
+    draw_axes_x_grid (props);
+    draw_axes_y_grid (props);
+    draw_axes_z_grid (props);
+
+    if (antialias == GL_TRUE)
+      glEnable (GL_LINE_SMOOTH);
+  }
+
+  void
   opengl_renderer::draw_all_lights (const base_properties& props,
                                     std::list<graphics_object>& obj_list)
   {
@@ -2006,6 +2028,7 @@
         if (! go.isa ("text") || go.get ("units").string_value () == "data")
           {
             set_clipping (go.get_properties ().is_clipping ());
+
             draw (go);
 
             it = obj_list.erase (it);
@@ -2026,8 +2049,6 @@
         draw (go);
       }
 
-    glEnable (GL_DEPTH_TEST);
-
     set_clipping (false);
 
     // FIXME: finalize rendering (transparency processing)
@@ -2049,7 +2070,6 @@
   opengl_renderer::draw_axes (const axes::properties& props)
   {
 #if defined (HAVE_OPENGL)
-
     // Legends are not drawn when "visible" is "off".
     if (! props.is_visible () && props.get_tag () == "legend")
       return;
@@ -2072,23 +2092,17 @@
 
     setup_opengl_transformation (props);
 
-    // Disable line smoothing for axes
-    GLboolean antialias;
-    glGetBooleanv (GL_LINE_SMOOTH, &antialias);
-    if (antialias == GL_TRUE)
-      glDisable (GL_LINE_SMOOTH);
-
-    set_font (props);
-    set_interpreter (props.get_ticklabelinterpreter ());
-    set_linecap ("square");
-    set_linewidth (props.get_linewidth ());
-
-    // draw axes object
+    // For 2D axes, draw from back to front without depth sorting
+    bool is2D = props.get_is2D ();
+    if (is2D)
+      glDisable (GL_DEPTH_TEST);
+    else
+      glEnable (GL_DEPTH_TEST);
+
     draw_axes_planes (props);
 
-    draw_axes_x_grid (props);
-    draw_axes_y_grid (props);
-    draw_axes_z_grid (props);
+    if (! is2D || props.layer_is ("bottom"))
+      draw_axes_grids (props);
 
     if (props.get_tag () != "legend" || props.get_box () != "off")
       draw_axes_boxes (props);
@@ -2097,11 +2111,10 @@
 
     set_clipbox (x_min, x_max, y_min, y_max, z_min, z_max);
 
-    // Re-enable line smoothing for children
-    if (antialias == GL_TRUE)
-      glEnable (GL_LINE_SMOOTH);
-
     draw_axes_children (props);
+    
+    if (is2D && props.layer_is ("top"))
+      draw_axes_grids (props);
 
 #else
 
diff -r 1093162037b4 -r 59f1f7728e75 libinterp/corefcn/gl-render.h
--- a/libinterp/corefcn/gl-render.h	Wed Mar 15 22:00:17 2017 +0100
+++ b/libinterp/corefcn/gl-render.h	Sun May 28 14:21:07 2017 +0200
@@ -173,6 +173,7 @@
     void draw_axes_planes (const axes::properties& props);
     void draw_axes_boxes (const axes::properties& props);
 
+    void draw_axes_grids (const axes::properties& props);
     void draw_axes_x_grid (const axes::properties& props);
     void draw_axes_y_grid (const axes::properties& props);
     void draw_axes_z_grid (const axes::properties& props);
diff -r 1093162037b4 -r 59f1f7728e75 libinterp/corefcn/gl2ps-print.cc
--- a/libinterp/corefcn/gl2ps-print.cc	Wed Mar 15 22:00:17 2017 +0100
+++ b/libinterp/corefcn/gl2ps-print.cc	Sun May 28 14:21:07 2017 +0200
@@ -227,8 +227,10 @@
         GLint gl2ps_sort = GL2PS_BSP_SORT;
 
         // For 2D plots we can use a simpler Z-depth sorting algorithm
+        // FIXME: gl2ps does not provide a way to change the sorting algorythm
+        // on a viewport basis, we thus disable sorting only if all axes are 2D
         if (term.find ("is2D") != std::string::npos)
-          gl2ps_sort = GL2PS_SIMPLE_SORT;
+          gl2ps_sort = GL2PS_NO_SORT;
 
         // Use a temporary file in case an overflow happens
         FILE *tmpf = octave_tmpfile_wrapper ();
