home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / lib / geomutil / plutil / plcombine.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-03  |  4.7 KB  |  155 lines

  1. /*
  2.  * plcombine.c
  3.  * author:  Celeste Fowler
  4.  * date:  June 12, 1992
  5.  */
  6. #include <stdlib.h>
  7. #include "polylistP.h"
  8. #include "hpoint3.h"
  9. #include "point3.h"
  10. #include "plutil.h"
  11.  
  12. static char msg[] = "plcombine.c";
  13.  
  14. /*
  15.  * PolyList combiner.
  16.  * A minor sideshow.  If a and b differ in some fundamental way in terms
  17.  * of per vertex / per face colors / normals, the colors / normals will
  18.  * be eliminated in the result.  If one polylist is three-d and the other
  19.  * is four-d, the result will be three-d
  20.  */
  21. Geom *PLCombine(Geom *a1, Geom *b1) 
  22. {
  23.   PolyList *a = (PolyList *)a1, *b = (PolyList *)b1;
  24.   int i, j, k;
  25.   HPoint3 *point4;
  26.   ColorA *color, *polycolor;
  27.   Point3 *normal, *polynormal;
  28.   int *vert, nvertices, *nvert;
  29.   Vertex *maxverta = NULL, *maxvertb = NULL;
  30.   Geom *new;
  31.   int flags, fourd = 0;
  32.  
  33.   if (a == NULL) return(GeomCopy((Geom *)b));
  34.   if (b == NULL) return(GeomCopy((Geom *)a));
  35.  
  36.   if (strcmp(GeomName(a1), "polylist") || strcmp(GeomName(b1), "polylist"))
  37.     OOGLError(0, "Arguements to PLCombine() not of polylist type.");
  38.  
  39.   nvertices = a->n_verts + b->n_verts;
  40.  
  41.  
  42.   /* Copy points, normals, colors. */
  43.   point4 = (HPoint3 *)OOG_NewE(nvertices * sizeof(HPoint3), msg);
  44.   color = (ColorA *)OOG_NewE(nvertices * sizeof(ColorA), msg);
  45.   normal = (Point3 *)OOG_NewE(nvertices * sizeof(Point3), msg);
  46.   for (i = 0; i < a->n_verts; i++) {
  47.     HPt3Copy(&a->vl[i].pt, &point4[i]);
  48.     bcopy(&a->vl[i].vcol, &color[i], sizeof(ColorA));
  49.     Pt3Copy(&a->vl[i].vn, &normal[i]);
  50.   }
  51.   for (i = 0; i < b->n_verts; i++) {
  52.     HPt3Copy(&b->vl[i].pt, &point4[a->n_verts + i]);
  53.     bcopy(&b->vl[i].vcol, &color[a->n_verts + i], sizeof(ColorA));
  54.     Pt3Copy(&b->vl[i].vn, &normal[a->n_verts + i]);
  55.   }
  56.  
  57.   /* Create polygons */
  58.   nvert = (int *)OOG_NewE((a->n_polys + b->n_polys) * sizeof(int), msg);
  59.   polycolor = 
  60.     (ColorA *)OOG_NewE((a->n_polys + b->n_polys) * sizeof(ColorA), msg);
  61.   polynormal =
  62.     (Point3 *)OOG_NewE((a->n_polys + b->n_polys) * sizeof(Point3), msg);
  63.   for (i = j = 0; i < a->n_polys; i++) j += a->p[i].n_vertices;
  64.   for (i = 0; i < b->n_polys; i++) j += b->p[i].n_vertices;
  65.   vert = (int *)OOG_NewE(j * sizeof(int), msg);
  66.   for (i = k = 0; i < a->n_polys; i++) {
  67.     nvert[i] = a->p[i].n_vertices;
  68.     bcopy(&a->p[i].pcol, &polycolor[i], sizeof(ColorA));
  69.     Pt3Copy(&a->p[i].pn, &polynormal[i]);
  70.     for (j = 0; j < a->p[i].n_vertices; j++) 
  71.       vert[k++] = a->p[i].v[j] - a->vl;
  72.   }
  73.   for (i = 0; i < b->n_polys; i++) {
  74.     nvert[a->n_polys + i] = b->p[i].n_vertices;
  75.     bcopy(&b->p[i].pcol, &polycolor[a->n_polys + i], sizeof(ColorA));
  76.     Pt3Copy(&b->p[i].pn, &polynormal[a->n_polys + i]); 
  77.     for (j = 0; j < b->p[i].n_vertices; j++)
  78.       vert[k++] = a->n_verts + b->p[i].v[j] - b->vl;
  79.   }
  80.  
  81.   flags = a->flags;
  82.   fourd = (a->geomflags & VERT_4D) ? 1 : 0;
  83.   
  84.   if ((a->flags & PL_HASVCOL) && !(b->flags & PL_HASVCOL)) {
  85.     if (b->flags & PL_HASPCOL) {
  86.       for (i = 0; i < a->n_polys; i++) 
  87.     bcopy(&a->p[i].v[0]->vcol, &polycolor[i], sizeof(ColorA));
  88.       flags ^= PL_HASVCOL;
  89.       flags |= PL_HASPCOL;
  90.     }
  91.     else for (i = 0; i < b->n_verts; i++) {
  92.       color[a->n_verts + i].r = color[a->n_verts + i].g =
  93.     color[a->n_verts + i].b = 0.15;
  94.       color[a->n_verts + i].a = 1.0;
  95.     } 
  96.   }
  97.   if ((a->flags & PL_HASPCOL) && !(b->flags & PL_HASPCOL)) {
  98.     if (b->flags & PL_HASVCOL) {
  99.       for (i = 0; i < b->n_polys; i++) 
  100.     bcopy(&b->p[i].v[0]->vcol, &polycolor[a->n_polys + i], 
  101.           sizeof(ColorA));
  102.     }
  103.     else for (i = 0; i < b->n_polys; i++) {
  104.       polycolor[a->n_polys + i].r = polycolor[a->n_polys + i].g = 
  105.     polycolor[a->n_polys + i].b = 0.15;
  106.       polycolor[a->n_polys + i].a = 1.0;
  107.     }
  108.   }
  109.   if (!(flags & PL_HASVCOL) && !(flags & PL_HASPCOL)) {
  110.     if (b->flags & PL_HASPCOL) {
  111.       for (i = 0; i < a->n_polys; i++) {
  112.     polycolor[i].r = polycolor[i].g = polycolor[i].b = 0.15;
  113.     polycolor[i].a = 1.0;
  114.       }
  115.       flags |= PL_HASPCOL;
  116.     }
  117.     if (b->flags & PL_HASVCOL) {
  118.       for (i = 0; i < a->n_verts; i++) {
  119.     color[i].r = color[i].g = color[i].b = 0.15;
  120.     color[i].a = 1.0;
  121.       }
  122.       flags |= PL_HASVCOL;
  123.     }
  124.   }
  125.  
  126.   if ((a->flags & PL_HASVN) && !(b->flags & PL_HASVN)) flags ^= PL_HASVN;
  127.   if ((a->flags & PL_HASPN) && !(b->flags & PL_HASPN)) flags ^= PL_HASPN;
  128.   if ((a->geomflags & VERT_4D) && !(b->geomflags & VERT_4D)) 
  129.     fourd = 0;
  130.  
  131.   new = GeomCreate("polylist", 
  132.            CR_NPOLY, a->n_polys + b->n_polys,
  133.            CR_POINT4, point4,
  134.            CR_NORMAL, normal,
  135.            CR_COLOR, color,
  136.            CR_NVERT, nvert,
  137.            CR_VERT, vert,
  138.            CR_POLYNORMAL, polynormal,
  139.            CR_POLYCOLOR, polycolor,
  140.            CR_FLAG, flags,
  141.            CR_4D, fourd,
  142.            CR_END);
  143.  
  144.   OOGLFree(point4);
  145.   OOGLFree(color);
  146.   OOGLFree(normal);
  147.   OOGLFree(nvert);
  148.   OOGLFree(polycolor);
  149.   OOGLFree(polynormal);
  150.   OOGLFree(vert);
  151.  
  152.   return(new);
  153.  
  154. }
  155.