Acme-MITHALDU-BleedingOpenGL

 view release on metacpan or  search on metacpan

pogl_glu.xs  view on Meta::CPAN


struct PGLUtess {
	GLUtesselator * triangulator;
#ifdef GLU_VERSION_1_2
	SV * begin_callback;
	SV * edgeFlag_callback;
	SV * vertex_callback;
	SV * end_callback;
	SV * error_callback;
	SV * combine_callback;
#endif
        bool do_colors;
        bool do_normals;
        bool use_vertex_data;
        GLdouble * vertex_data; /* used during non-GLU_TESS_VERTEX_DATA */
	SV * polygon_data;
	AV * vertex_datas;
	AV * tess_datas;
};

typedef struct PGLUtess PGLUtess;

#define delete_vertex_datas() \
		if (tess->vertex_datas) {                    \
                  AV * vds = tess->vertex_datas;             \
                  SV** svp;                                  \
                  I32 i;                                     \
                  for (i=0; i<=av_len(vds); i++) {           \
                    svp = av_fetch(vds, i, FALSE);           \
                    free(INT2PTR(GLdouble*, SvIV(*svp)));    \
                  }                                          \
                  SvREFCNT_dec(tess->vertex_datas);          \
                  tess->vertex_datas = 0;                    \
                }

#define delete_tess_datas() \
		if (tess->tess_datas) {                      \
                  AV * tds = tess->tess_datas;               \
                  SV** svp;                                  \
                  I32 i;                                     \
                  for (i=0; i<=av_len(tds); i++) {           \
                    svp = av_fetch(tds, i, FALSE);           \
                    free(INT2PTR(PGLUtess*, SvIV(*svp)));    \
                  }                                          \
                  SvREFCNT_dec(tess->tess_datas);            \
                  tess->tess_datas = 0;                      \
                }

#define delete_polygon_data()                             \
		if (tess->polygon_data) {                 \
			SvREFCNT_dec(tess->polygon_data); \
			tess->polygon_data = 0;           \
		}



#ifdef GLU_VERSION_1_2


/* Begin a named callback handler */
#define begin_tess_marshaller(name, type, params, croak_msg, default_handler) \
void CALLBACK _s_marshal_glu_t_callback_ ## name params			\
{                                                                       \
    dSP;                                                                \
    int i; int j = 3;                                                   \
    PGLUtess * t = (PGLUtess*)gl_polygon_data;                          \
    SV * handler = t-> type ## _callback;                               \
    if (!handler) croak(croak_msg);                                     \
    if (! SvROK(handler)) { /* default */                               \
      default_handler;                                                  \
      return;                                                           \
    }                                                                   \
    PUSHMARK(sp);

/* End a gluTess callback handler */
#define end_tess_marshaller()                                           \
	PUTBACK;                                                        \
	perl_call_sv(handler, G_DISCARD);                               \
}


/* Declare gluTess BEGIN */
begin_tess_marshaller(begin, begin, (GLenum type, void * gl_polygon_data), "Missing tess callback for begin", glBegin(type))
	XPUSHs(sv_2mortal(newSViv(type)));
end_tess_marshaller()

/* Declare gluTess BEGIN_DATA */
begin_tess_marshaller(begin_data, begin, (GLenum type, void * gl_polygon_data), "Missing tess callback for begin_data", glBegin(type))
	XPUSHs(sv_2mortal(newSViv(type)));
    if (t->polygon_data) XPUSHs((SV*)t->polygon_data);
end_tess_marshaller()

/* Declare gluTess END */
begin_tess_marshaller(end, end, (void * gl_polygon_data), "Missing tess callback for end", glEnd())
end_tess_marshaller()

/* Declare gluTess END_DATA */
begin_tess_marshaller(end_data, end, (void * gl_polygon_data), "Missing tess callback for end_data", glEnd())
    if (t->polygon_data) XPUSHs((SV*)t->polygon_data);
end_tess_marshaller()

/* Declare gluTess EDGEFLAG */
begin_tess_marshaller(edgeFlag, edgeFlag, (GLboolean flag, void * gl_polygon_data), "Missing tess callback for edgeFlag", glEdgeFlag(flag))
	XPUSHs(sv_2mortal(newSViv(flag)));
end_tess_marshaller()

/* Declare gluTess EDGEFLAG_DATA */
begin_tess_marshaller(edgeFlag_data, edgeFlag, (GLboolean flag, void * gl_polygon_data), "Missing tess callback for edgeFlag_data", glEdgeFlag(flag))
	XPUSHs(sv_2mortal(newSViv(flag)));
    if (t->polygon_data) XPUSHs((SV*)t->polygon_data);
end_tess_marshaller()

/* Declare gluTess VERTEX */
begin_tess_marshaller(vertex, vertex, (void * gl_polygon_data), "Missing tess callback for vertex", \
                      GLdouble * vd = t->vertex_data;                                   \
                      if (t->do_colors) { \
                         glColor4f(vd[j], vd[j+1], vd[j+2], vd[j+3]); \
                         j += 4; \
                      } \
                      if (t->do_normals) glNormal3f(vd[j], vd[j+1], vd[j+2]);           \
                      glVertex3f(vd[0], vd[1], vd[2]);                                  \
                      )
{
    GLdouble * vd = (GLdouble*) t->vertex_data;
    for (i = 0; i < 3; i++)
      XPUSHs(sv_2mortal(newSVnv(vd[i])));
    if (t->do_colors) {
      int J = j + 4;
      for ( ; j < J; j++)
        XPUSHs(sv_2mortal(newSVnv(vd[j])));

pogl_glu.xs  view on Meta::CPAN

	XPUSHs(sv_2mortal(newSViv(errno_)));
end_tess_marshaller()

/* Declare gluTess ERROR_DATA */
begin_tess_marshaller(error_data, error, (GLenum errno_, void * gl_polygon_data), "Missing tess callback for error_data", \
                      warn("Tesselation error: %s", gluErrorString(errno_)); \
                     )
	XPUSHs(sv_2mortal(newSViv(errno_)));
    if (t->polygon_data) XPUSHs((SV*)t->polygon_data);
end_tess_marshaller()

/* Declare gluTess COMBINE AND COMBINE_DATA */
void CALLBACK _s_marshal_glu_t_callback_combine (GLdouble coords[3], void * vertex_data[4],
                                                 GLfloat weight[4], void ** out_data,
                                                 void * gl_polygon_data)
{
        SV * handler;
	AV * vds;
        SV * item;
        I32 n;
	int i, j = 3;
	dSP;
        GLdouble *vd[4];
	PGLUtess *t = (PGLUtess*)gl_polygon_data;
        bool has_data = FALSE;
        int size = 3 + (t->do_colors ? 4 : 0) + (t->do_normals ? 3 : 0);
        GLdouble *vertex = malloc(sizeof(GLdouble) * size);
        if (vertex == NULL) croak("Couldn't allocate combination vertex during tesselation");
        vds = t->vertex_datas;
        if (!vds) croak("Missing vertex data storage");
	av_push(vds, newSViv(PTR2IV(vertex)));

	handler = t->combine_callback;
        if (!handler) croak("Missing tess callback for combine_data");

	if (t->use_vertex_data) {
            PGLUtess * opaque = malloc(sizeof(PGLUtess));
            if (!opaque) croak("Couldn't allocate storage for vertex opaque data");
	    opaque->triangulator     = t->triangulator;
            opaque->vertex_datas     = t->vertex_datas;
            opaque->vertex_callback  = t->vertex_callback;
            opaque->combine_callback = t->combine_callback;
            opaque->vertex_data      = vertex;
            opaque->polygon_data     = &PL_sv_undef;
            opaque->use_vertex_data  = TRUE;
            opaque->do_colors        = t->do_colors;
            opaque->do_normals       = t->do_normals;
            if (! t->tess_datas) t->tess_datas = newAV();
            av_push(t->tess_datas, newSViv(PTR2IV(opaque)));
            *out_data = opaque;
            for (i = 0; i < 4; i++) {
                PGLUtess* ot = (PGLUtess*)vertex_data[i];
                vd[i] = (GLdouble*)ot->vertex_data;
            }
	} else {
            *out_data = vertex;
            for (i = 0; i < 4; i++)
               vd[i] = (GLdouble*)vertex_data[i];
        }

        if (! SvROK(handler)) { /* default */
          vertex[0] = coords[0];
          vertex[1] = coords[1];
          vertex[2] = coords[2];
          if (t->do_colors) {
            int J = j + 4;
            for ( ; j < J; j++) {
              vertex[j] = 0;
              for (i = 0; i < 4; i++)
                if (weight[i]) vertex[j] += weight[i] * vd[i][j];
            }
          }
          if (t->do_normals) {
            int J = j + 3;
            for ( ; j < J; j++) {
              vertex[j] = 0;
              for (i = 0; i < 4; i++)
                if (weight[i]) vertex[j] += weight[i] * vd[i][j];
            }
          }
        } else {
          PUSHMARK(sp);
          for (i = 0; i < 3; i++)
            XPUSHs(sv_2mortal(newSVnv(coords[i])));
          for (i = 0; i < 4; i++) {
            AV* vec = (AV*)sv_2mortal((SV*)newAV());
            XPUSHs(newRV_inc((SV*)vec));
            for (j = 0 ; j < 3; j++)
                av_push(vec, sv_2mortal(newSVnv(weight[i] ? vd[i][j] : 0)));
            if (t->do_colors) {
              int J = j + 4;
              for ( ; j < J; j++)
                av_push(vec, sv_2mortal(newSVnv(weight[i] ? vd[i][j] : 0)));
            }
            if (t->do_normals) {
              int J = j + 3;
              for ( ; j < J; j++)
                av_push(vec, sv_2mortal(newSVnv(weight[i] ? vd[i][j] : 0)));
            }
            if (t->use_vertex_data) {
              PGLUtess* ot = (PGLUtess*)vertex_data[i];
              av_push(vec, ot->polygon_data ? ot->polygon_data : &PL_sv_undef);
            }
          }
          for (i = 0; i < 4; i++)
            XPUSHs(sv_2mortal(newSVnv(weight[i])));
          XPUSHs(t->polygon_data ? t->polygon_data : &PL_sv_undef); /* would be nice to have the option to only do this on COMBINE_DATA */

	  PUTBACK;

          n = perl_call_sv(handler, G_ARRAY);

          SPAGAIN;

          if (t->do_colors) {
              if (t->do_normals) {
                  if (n == 11 && t->use_vertex_data) has_data = TRUE;
                  else if (n != 10) {
                    if (t->use_vertex_data) croak("Callback expects (x,y,z, r,g,b,a, nx,ny,nz [,polygon_data])");
                    else  croak("Callback expects (x,y,z, r,g,b,a, nx,ny,nz)");
                  }



( run in 1.487 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )