# HG changeset patch
# User mmuetzel <markus.muetzel@gmx.de>
# Date 1461074207 -7200
#      Tue Apr 19 15:56:47 2016 +0200
# Node ID 3358d48c26329f9c54ade0fe225d7a352fe51f28
# Parent  37a44d8af461fbc66b89333ae46390dbeed23031
Add debugging messages for drawing patches (bug #47677)

diff -r 37a44d8af461 -r 3358d48c2632 libinterp/corefcn/gl-render.cc
--- a/libinterp/corefcn/gl-render.cc	Sun Apr 10 10:22:18 2016 +1000
+++ b/libinterp/corefcn/gl-render.cc	Tue Apr 19 15:56:47 2016 +0200
@@ -251,110 +251,6 @@
 }
 
 class
-opengl_tesselator
-{
-public:
-#if defined (HAVE_FRAMEWORK_OPENGL) && defined (HAVE_GLUTESSCALLBACK_THREEDOTS)
-  typedef GLvoid (CALLBACK *fcn) (...);
-#else
-  typedef void (CALLBACK *fcn) (void);
-#endif
-
-public:
-
-  opengl_tesselator (void) : glu_tess (0), fill () { init (); }
-
-  virtual ~opengl_tesselator (void)
-  { if (glu_tess) gluDeleteTess (glu_tess); }
-
-  void begin_polygon (bool filled = true)
-  {
-    gluTessProperty (glu_tess, GLU_TESS_BOUNDARY_ONLY,
-                     (filled ? GL_FALSE : GL_TRUE));
-    fill = filled;
-    gluTessBeginPolygon (glu_tess, this);
-  }
-
-  void end_polygon (void) const
-  { gluTessEndPolygon (glu_tess); }
-
-  void begin_contour (void) const
-  { gluTessBeginContour (glu_tess); }
-
-  void end_contour (void) const
-  { gluTessEndContour (glu_tess); }
-
-  void add_vertex (double *loc, void *data) const
-  { gluTessVertex (glu_tess, loc, data); }
-
-protected:
-  virtual void begin (GLenum /*type*/) { }
-
-  virtual void end (void) { }
-
-  virtual void vertex (void * /*data*/) { }
-
-  virtual void combine (GLdouble [3] /*c*/, void * [4] /*data*/,
-                        GLfloat  [4] /*w*/, void ** /*out_data*/) { }
-
-  virtual void edge_flag (GLboolean /*flag*/) { }
-
-  virtual void error (GLenum err)
-  { ::error ("OpenGL tesselation error (%d)", err); }
-
-  virtual void init (void)
-  {
-    glu_tess = gluNewTess ();
-
-    gluTessCallback (glu_tess, GLU_TESS_BEGIN_DATA,
-                     reinterpret_cast<fcn> (tess_begin));
-    gluTessCallback (glu_tess, GLU_TESS_END_DATA,
-                     reinterpret_cast<fcn> (tess_end));
-    gluTessCallback (glu_tess, GLU_TESS_VERTEX_DATA,
-                     reinterpret_cast<fcn> (tess_vertex));
-    gluTessCallback (glu_tess, GLU_TESS_COMBINE_DATA,
-                     reinterpret_cast<fcn> (tess_combine));
-    gluTessCallback (glu_tess, GLU_TESS_EDGE_FLAG_DATA,
-                     reinterpret_cast<fcn> (tess_edge_flag));
-    gluTessCallback (glu_tess, GLU_TESS_ERROR_DATA,
-                     reinterpret_cast<fcn> (tess_error));
-  }
-
-  bool is_filled (void) const { return fill; }
-
-private:
-  static void CALLBACK tess_begin (GLenum type, void *t)
-  { reinterpret_cast<opengl_tesselator *> (t)->begin (type); }
-
-  static void CALLBACK tess_end (void *t)
-  { reinterpret_cast<opengl_tesselator *> (t)->end (); }
-
-  static void CALLBACK tess_vertex (void *v, void *t)
-  { reinterpret_cast<opengl_tesselator *> (t)->vertex (v); }
-
-  static void CALLBACK tess_combine (GLdouble c[3], void *v[4], GLfloat w[4],
-                                     void **out,  void *t)
-  { reinterpret_cast<opengl_tesselator *> (t)->combine (c, v, w, out); }
-
-  static void CALLBACK tess_edge_flag (GLboolean flag, void *t)
-  { reinterpret_cast<opengl_tesselator *> (t)->edge_flag (flag); }
-
-  static void CALLBACK tess_error (GLenum err, void *t)
-  { reinterpret_cast<opengl_tesselator *> (t)->error (err); }
-
-private:
-
-  // No copying!
-
-  opengl_tesselator (const opengl_tesselator&);
-
-  opengl_tesselator operator = (const opengl_tesselator&);
-
-  GLUtesselator *glu_tess;
-  bool fill;
-};
-
-class
 vertex_data
 {
 public:
@@ -430,6 +326,118 @@
 };
 
 class
+opengl_tesselator
+{
+public:
+#if defined (HAVE_FRAMEWORK_OPENGL) && defined (HAVE_GLUTESSCALLBACK_THREEDOTS)
+  typedef GLvoid (CALLBACK *fcn) (...);
+#else
+  typedef void (CALLBACK *fcn) (void);
+#endif
+
+public:
+
+  opengl_tesselator (void) : glu_tess (0), fill () { init (); }
+
+  virtual ~opengl_tesselator (void)
+  { if (glu_tess) gluDeleteTess (glu_tess); }
+
+  void begin_polygon (bool filled = true)
+  {
+    gluTessProperty (glu_tess, GLU_TESS_BOUNDARY_ONLY,
+                     (filled ? GL_FALSE : GL_TRUE));
+    fill = filled;
+    gluTessBeginPolygon (glu_tess, this);
+  }
+
+  void end_polygon (void) const
+  { gluTessEndPolygon (glu_tess); }
+
+  void begin_contour (void) const
+  { gluTessBeginContour (glu_tess); }
+
+  void end_contour (void) const
+  { gluTessEndContour (glu_tess); }
+
+  void add_vertex (double *loc, void *data) const
+  {
+    printf ("opengl_tesselator::add_vertex (%g, %g, %g)\n", loc[0], loc[1], loc[2]);
+    gluTessVertex (glu_tess, loc, data);
+  }
+
+protected:
+  virtual void begin (GLenum /*type*/) { }
+
+  virtual void end (void) { }
+
+  virtual void vertex (void * /*data*/) { }
+
+  virtual void combine (GLdouble [3] /*c*/, void * [4] /*data*/,
+                        GLfloat  [4] /*w*/, void ** /*out_data*/) { }
+
+  virtual void edge_flag (GLboolean /*flag*/) { }
+
+  virtual void error (GLenum err)
+  { ::error ("OpenGL tesselation error (%d)", err); }
+
+  virtual void init (void)
+  {
+    glu_tess = gluNewTess ();
+
+    gluTessCallback (glu_tess, GLU_TESS_BEGIN_DATA,
+                     reinterpret_cast<fcn> (tess_begin));
+    gluTessCallback (glu_tess, GLU_TESS_END_DATA,
+                     reinterpret_cast<fcn> (tess_end));
+    gluTessCallback (glu_tess, GLU_TESS_VERTEX_DATA,
+                     reinterpret_cast<fcn> (tess_vertex));
+    gluTessCallback (glu_tess, GLU_TESS_COMBINE_DATA,
+                     reinterpret_cast<fcn> (tess_combine));
+    gluTessCallback (glu_tess, GLU_TESS_EDGE_FLAG_DATA,
+                     reinterpret_cast<fcn> (tess_edge_flag));
+    gluTessCallback (glu_tess, GLU_TESS_ERROR_DATA,
+                     reinterpret_cast<fcn> (tess_error));
+  }
+
+  bool is_filled (void) const { return fill; }
+
+private:
+  static void CALLBACK tess_begin (GLenum type, void *t)
+  { reinterpret_cast<opengl_tesselator *> (t)->begin (type); }
+
+  static void CALLBACK tess_end (void *t)
+  { reinterpret_cast<opengl_tesselator *> (t)->end (); }
+
+  static void CALLBACK tess_vertex (void *v, void *t)
+  {
+    reinterpret_cast<opengl_tesselator *> (t)->vertex (v);
+//    vertex_data::vertex_data_rep *vv
+//      = reinterpret_cast<vertex_data::vertex_data_rep *> (v);
+//    printf ("opengl_tesselator::tess_vertex (%g, %g, %g)\n", vv->coords(0), vv->coords(1), vv->coords(2));
+  }
+
+  static void CALLBACK tess_combine (GLdouble c[3], void *v[4], GLfloat w[4],
+                                     void **out,  void *t)
+  { reinterpret_cast<opengl_tesselator *> (t)->combine (c, v, w, out); }
+
+  static void CALLBACK tess_edge_flag (GLboolean flag, void *t)
+  { reinterpret_cast<opengl_tesselator *> (t)->edge_flag (flag); }
+
+  static void CALLBACK tess_error (GLenum err, void *t)
+  { reinterpret_cast<opengl_tesselator *> (t)->error (err); }
+
+private:
+
+  // No copying!
+
+  opengl_tesselator (const opengl_tesselator&);
+
+  opengl_tesselator operator = (const opengl_tesselator&);
+
+  GLUtesselator *glu_tess;
+  bool fill;
+};
+
+class
 opengl_renderer::patch_tesselator : public opengl_tesselator
 {
 public:
@@ -467,7 +475,7 @@
   {
     vertex_data::vertex_data_rep *v
       = reinterpret_cast<vertex_data::vertex_data_rep *> (data);
-    //printf ("patch_tesselator::vertex (%g, %g, %g)\n", v->coords(0), v->coords(1), v->coords(2));
+    printf ("patch_tesselator::vertex (%g, %g, %g)\n", v->coords(0), v->coords(1), v->coords(2));
 
     // NOTE: OpenGL can re-order vertices.  For "flat" coloring of FaceColor
     // the first vertex must be identified in the draw_patch routine.
@@ -517,6 +525,9 @@
           vmax = i;
       }
 
+    for (int i = 0; i < vmax; i++)
+      printf ("patch_tesselator::combine vertices to combine (%g, %g, %g)\n", v[i]->coords(0), v[i]->coords(1), v[i]->coords(2));
+
     Matrix vv (1, 3, 0.0);
     Matrix cc;
     Matrix nn (1, 3, 0.0);
@@ -549,6 +560,9 @@
     tmp_vdata.push_back (new_v);
 
     *out_data = new_v.get_rep ();
+    vertex_data::vertex_data_rep *new_vv
+      = reinterpret_cast<vertex_data::vertex_data_rep *> (*out_data);
+    printf ("patch_tesselator::combine (%g, %g, %g)\n", new_vv->coords(0), new_vv->coords(1), new_vv->coords(2));
   }
 
 private:
@@ -2516,6 +2530,7 @@
             else
               aa = a(idx);
           }
+        printf ("opengl_renderer::draw_patch vdata[%d] (%g, %g, %g)\n", i+j*fr, vv(0), vv(1), vv(2));
 
         vdata[i+j*fr] = vertex_data (vv, cc, nn, aa, as, ds, ss, se);
       }
@@ -2530,6 +2545,7 @@
 
   if (! props.facecolor_is ("none"))
     {
+      printf ("opengl_renderer::draw_patch drawing faces\n");
       // FIXME: adapt to double-radio property
       if (props.get_facealpha_double () == 1)
         {
@@ -2571,6 +2587,7 @@
               for (int j = count_f(i)-1; j > 0; j--)
                 {
                   vertex_data::vertex_data_rep *vv = vdata[i+j*fr].get_rep ();
+                  printf ("opengl_renderer::draw_patch (%g, %g, %g)\n", vv->coords(0), vv->coords(1), vv->coords(2));
 
                   tess.add_vertex (vv->coords.fortran_vec (), vv);
                 }
@@ -2578,6 +2595,7 @@
               if (count_f(i) > 0)
                 {
                   vertex_data::vertex_data_rep *vv = vdata[i].get_rep ();
+                  printf ("opengl_renderer::draw_patch (%g, %g, %g)\n", vv->coords(0), vv->coords(1), vv->coords(2));
 
                   if (fc_mode == FLAT)
                     {
@@ -2605,6 +2623,7 @@
                   tess.add_vertex (vv->coords.fortran_vec (), vv);
                 }
 
+              printf ("opengl_renderer::draw_patch ending face\n");
               tess.end_contour ();
               tess.end_polygon ();
             }
@@ -2620,6 +2639,7 @@
 
   if (! props.edgecolor_is ("none"))
     {
+      printf ("opengl_renderer::draw_patch drawing contours\n");
       // FIXME: adapt to double-radio property
       if (props.get_edgealpha_double () == 1)
         {
@@ -2724,6 +2744,7 @@
                       tess.add_vertex (vv->coords.fortran_vec (), vv);
                     }
 
+              printf ("opengl_renderer::draw_patch ending contour\n");
                   tess.end_contour ();
                   tess.end_polygon ();
                 }
