// ../xa/tst_ut_GL.c


#ifdef globTag
 void tst_ut_GL(){}
#endif


#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>



#include <GL/gl.h>

#include "../ut/ut_geo.h"              // Point-def
#include "../ut/ut_memTab.h"           // MemTab_..
#include "../ut/ut_itmsh.h"            // MSH_EDGLN_.. typedef_MemTab..
#include "../ut/msh2d.h"               // TessStru


// ../gr/ut_GL.c
static GLuint DL_base_LnAtt = 150;

/*
TODO:
- GL_view_ipatch_2 und GL_view_inpt  zusammenlegen (gemeinsame pa) ?

=====================================================
GL_Draw_ini__         init DL-obj (glNewList)

GL_view_inpt          display curve of indexed points

=====================================================

GL_view_ipatch_2      display indexed-Opengl-patch (type,indexTable,points)
GL_view_ini__         create new dispList-record OR draw into open dispList
GL_view_nsymB         display bitmap symbols SYM_TRI_S SYM_STAR_S ..

GL_view_npt           display points (GL_POINTS)
GL_view_cv            display polygon
GL_view_dbo    

GL_Disp_nsymB         display bitmap symbols SYM_TRI_S SYM_STAR_S ..

GL_Disp_ntria         ??

*/



/*
//======================================================================
  int GL_view_cv (Point *pta, int ptNr, int att, char *opts, long dbi) {
//======================================================================
// GL_view_cv            display polygon
//   att     see INF_COL_CV (eg Typ_Att_hili1)
// see GL_att_cv GL_Disp_cv
    

  long    dli;


  //----------------------------------------------------------------
  GL_view_ini__ (dbi);

  GL_att_cv (att);

  GL_Disp_cv (ptNr, pta);



  //----------------------------------------------------------------
  L_done:
  if(dbi) {
    GL_EndList ();     // glEndList
  }



  return 0;

}

 
//================================================================
  int GL_view_dbo (int typ, long dbi, int att) {
//================================================================
// Draw data-base-obj (typ & dbi)
// see APT_DrawSur

  int  oNr, dbTyp;
  void *oDat;

  // // get DB-obj-data
  // dbTyp = DB_GetObjDat (&oDat, &oNr, typ, dbi);
  // if(dbTyp <= 0) return -1;

  APT_Draw__ (att, typ, dbi);

  return 0;

}
*/


//====================================================================
  int GL_view_inpt (int iNr, int *ia, Point *pa, int iClo, int iatt) {
//====================================================================
// display curve of indexed points
//   iClo         0=open-polygon, 1=closed-contour
//   iatt         see INF_COL_CV
 
  printf("GL_view_inpt2 iNr=%d iatt=%d\n",iNr,iatt);

  int      i1;
  long     dli;
  char     s1[40];
  GLuint   dlInd;

/*
  if(dbi) {
    if(dbi > 0) {
      DL_SetInd (dbi);
    }
    dli = DL_StoreObj (Typ_GL_Sur, dbi, Typ_Att_Fac1);
    GL_Surf_Ini (&dli, NULL);  // init cv/surf, glNewList < GL_fix_DL_ind
  }
*/

  // see GL_Draw_Ini
  dli = DL_StoreObj (Typ_GL_Sur, -1L, iatt);
  dlInd = GL_fix_DL_ind (&dli);
  glNewList (dlInd, GL_COMPILE);


  // GL_att_cv (iatt);
  glDisable (GL_LIGHTING);
  glCallList (DL_base_LnAtt + iatt);


  //----------------------------------------------------------------
  // activate and specify pointer to vertex array
  glEnableClientState (GL_VERTEX_ARRAY);
  glVertexPointer (3, GL_DOUBLE, 0, pa);

  // GL_INT not supported !
  glDrawElements (GL_LINE_STRIP, iNr, GL_UNSIGNED_INT, ia);

  glDisableClientState (GL_VERTEX_ARRAY);


  //----------------------------------------------------------------
  // display closing segment
  if(iClo) {
    glBegin (GL_LINES);
      glVertex3dv ((double*)&pa[ia[iNr-1]]);
      glVertex3dv ((double*)&pa[ia[0]]);
    glEnd ();
  }

/*
  //----------------------------------------------------------------
  GL_att_cv (Typ_Att_top1);          // INF_COL_CV
  for(i1=0; i1<iNr; ++i1) {
    // sprintf(s1,"%d",ia[i1]);
    sprintf(s1,"%d",i1);
    GL_Disp_txtA (&pa[ia[i1]], s1);  // Typ_Att_Symb = 7 = yellow thick3
    // GL_Disp_symB2
  }
*/



  //----------------------------------------------------------------
  glEndList ();

  // if(dbi) {
    // GL_EndList ();     // glEndList
  // }


  return 0;

}


/* replaced by GL_Disp_nifac ..
//========================================================================
  int GL_view_ipatch_2 (int gTyp, int iNr, int *ia, Point *pa, Vec3f *va,
                        Vector *vcz, long dbi) {
//========================================================================
// GL_Disp_ipatch           display indexed-Opengl-patch (type,indexTable,points)
// Input:
//   gTyp     type of triangles in ia; eg GL_TRIANGLES|GL_TRIANGLE_STRIP|..
//            bitVal-16 set: face is planar; else not.
//   ia[iNr]  indices into pa
//   pa       all points
//   va       all normalvectors; parallel to pa; not for planar patches
//            (one normalvector for one point !)
//   vcz      normalvector for planar patches
//
// see also GL_Disp_ipatch GL_Disp_nfac
 

  long     dli;
  GLuint   dlInd;

  printf("GL_view_ipatch_2 gTyp=%d iNr=%d dbi=%ld\n",gTyp,iNr,dbi);
  {int i1, i2;for(i1=0;i1<iNr; ++i1) { i2=ia[i1];
   printf(" ia[%d]=%d %f %f %f\n",i1,i2,pa[i2].x,pa[i2].y,pa[i2].z);}}


  dli = DL_StoreObj (Typ_GL_Sur, -1L, Typ_Att_Fac1);
  dlInd = GL_fix_DL_ind (&dli);
  glNewList (dlInd, GL_COMPILE);

  // see GL_att_su
  glEnable (GL_LIGHTING);
  // glColorMaterial (GL_FRONT,  GL_AMBIENT);
  // glCallList (150 + Typ_Att_Fac1);
  // glColor3fv (GL_col_tab[13]);
  glColor3f (0.7f,0.7f,0.9f);





  //----------------------------------------------------------------
  if(va) {
    glEnableClientState (GL_NORMAL_ARRAY);
    glNormalPointer (GL_FLOAT, 0, va);       // same number as vertices !
  } else {
    glNormal3dv ((double*)vcz);
  }


  // activate and specify pointer to vertex array
  glEnableClientState (GL_VERTEX_ARRAY);
  glVertexPointer (3, GL_DOUBLE, 0, pa);

  
  // GL_INT not supported !
  glDrawElements (gTyp, iNr, GL_UNSIGNED_INT, ia);


  glDisableClientState (GL_VERTEX_ARRAY);

  if(va) glDisableClientState (GL_NORMAL_ARRAY);


  //----------------------------------------------------------------
  glEndList ();
  // if(dbi) {
    // GL_EndList ();     // glEndList
  // }


  return 0;

}


//================================================================
  int GL_Disp_ntria (Triangle *ta, int tNr) {
//================================================================
// TODO  ein indexarray erstellen; via glDrawElements
// display triangles;
// see ../xa/tst_glDrawElements1.c
 
  int     i1;
  Point   *p1;

  printf("GL_Disp_ntria %d\n",tNr);
  UT3D_stru_dump (Typ_Tria, &ta[0], "tria-0\n");
  UT3D_stru_dump (Typ_Tria, &ta[1], "tria-1\n");


  for(i1=0; i1<tNr; ++i1) {
      p1 = ta[i1].pa[0];
      UT3D_stru_dump (Typ_PT, p1, "P1-%d",i1);
  }


   // // create per-vertex-normals
   // // nvcDyn1 = (GLfloat*) alloca (sizeof(GLfloat) * 36 * 3);
   // // nvcDyn1 = (GLfloat*) malloc (sizeof(GLfloat) * 36 * 3);
   // // comp_normals (nvcDyn1, 36, (Point*)vertices1);
// 
   // // glEnableClientState(GL_NORMAL_ARRAY);  // normals per vertex !
   // glEnableClientState(GL_VERTEX_ARRAY);
// 
   // // glNormalPointer(GL_FLOAT, 0, normals1);
// 
   // glVertexPointer(3, GL_DOUBLE, 0, ta);
// 
   // glDrawArrays(GL_TRIANGLES, 0, tNr * 3);
// 
   // glDisableClientState(GL_VERTEX_ARRAY);
   // // glDisableClientState(GL_NORMAL_ARRAY);


  return 0;

}
*/
// EOF
