home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / lib / gprim / mesh / meshload.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-12  |  3.7 KB  |  161 lines

  1. /* Copyright (c) 1992 The Geometry Center; University of Minnesota
  2.    1300 South Second Street;  Minneapolis, MN  55454, USA;
  3.    
  4. This file is part of geomview/OOGL. geomview/OOGL is free software;
  5. you can redistribute it and/or modify it only under the terms given in
  6. the file COPYING, which you should have received along with this file.
  7. This and other related software may be obtained via anonymous ftp from
  8. geom.umn.edu; email: software@geom.umn.edu. */
  9.  
  10. /* Authors: Charlie Gunn, Stuart Levy, Tamara Munzner, Mark Phillips */
  11.  
  12. #include <ctype.h>
  13. #include "meshP.h"
  14.  
  15. static int
  16. getmeshvert(file, flag, u, v, p, n, c, t)
  17.     FILE    *file;
  18.     register int    flag;
  19.     int    u;
  20.     int    v;
  21.     register HPoint3    *p;
  22.     Point3    *n;
  23.     ColorA    *c;
  24.     Point3    *t;        /* actually u, the texture parameter */
  25. {
  26.     float    inputs[4];
  27.     Point3  p3;
  28.     register int binary = flag&MESH_BINARY;
  29.  
  30.     if (flag & MESH_Z) {
  31.         p->x = (float) u;
  32.         p->y = (float) v;
  33.         p->w = 1.0;
  34.         if (fgetnf(file, 1, &p->z, binary) <= 0)
  35.             return 0;
  36.     } else if (flag & MESH_4D) {
  37.         if (fgetnf(file, 4, (float *)p, binary) < 4)
  38.             return 0;
  39.     } else             {
  40.         if (fgetnf(file, 3, (float *)p, binary) < 3)
  41.             return 0;
  42.         p->w = 1.0;
  43.     }
  44.  
  45.     if (flag & MESH_N && fgetnf(file, 3, (float *)n, binary) < 3)
  46.         return 0;
  47.  
  48.     if (flag & MESH_C && fgetnf(file, 4, (float *)c, binary) < 4)
  49.         return 0;
  50.  
  51.     if (flag & MESH_U && fgetnf(file, 3, (float *)t, binary) < 3)
  52.         return 0;
  53.  
  54.     return 1;
  55. }
  56.  
  57. static char oldbinary;    /* Old binary format -- has 3-component colors */
  58.  
  59. static int
  60. getheader(file)
  61.     FILE    *file;
  62. {
  63.     register int c, i, flag;
  64.     static char keys[] = "CNZ4Uuv";
  65.     static short bit[] =
  66.         { MESH_C, MESH_N, MESH_Z, MESH_4D, MESH_U, MESH_UWRAP, MESH_VWRAP };
  67.  
  68.         /* Parse [C][N][Z][4][U][u][v]MESH[ BINARY]\n */
  69.     if(fnextc(file, 0) == '\0') {
  70.         /* Looks like first byte of magic number of old binary mesh */
  71.         oldbinary = 1;
  72.         return(MESH_BINARY);
  73.     }
  74.  
  75.     oldbinary = 0;
  76.     flag = 0;
  77.     c = fgetc(file);
  78.     for(i = 0; keys[i] != '\0'; i++) {
  79.         if(c == keys[i]) {
  80.         flag |= bit[i];
  81.         c = fgetc(file);
  82.         }
  83.     }
  84.     ungetc(c, file);
  85.     if(fexpectstr(file, "MESH"))
  86.         return(-1);
  87.  
  88.     if(fnextc(file, 1) == 'B') {
  89.         if(fexpectstr(file, "BINARY"))
  90.         return(-1);
  91.         flag |= MESH_BINARY;
  92.         if(fnextc(file, 1) == '\n')
  93.         (void) fgetc(file);    /* Toss \n */
  94.     }
  95.     return(flag);
  96. }
  97.  
  98.  
  99. Mesh *
  100. MeshFLoad(file, fname)
  101.     FILE *file;
  102.     char *fname;
  103. {
  104.     Mesh    m;
  105.     int    n;
  106.     Point3    *p3;
  107.     register int i, u, v;
  108.     int binary;
  109.  
  110.     if (!file)
  111.         return NULL;
  112.  
  113.     if((m.flag = getheader(file)) == -1)
  114.         return NULL;
  115.  
  116.     binary = m.flag & MESH_BINARY;
  117.  
  118.     if (fgetni(file, 1, &m.nu, binary) <= 0 ||
  119.         fgetni(file, 1, &m.nv, binary) <= 0) {
  120.         OOGLSyntax(file,"Reading MESH from \"%s\": expected mesh grid size", fname);
  121.         return NULL;
  122.     }
  123.     if(m.nu <= 0 || m.nv <= 0 || m.nu > 9999999 || m.nv > 9999999) {
  124.         OOGLSyntax(file,"Reading MESH from \"%s\": invalid mesh size %d %d",
  125.         fname,m.nu,m.nv);
  126.         return NULL;
  127.     }
  128.  
  129.     n = m.nu * m.nv;
  130.  
  131.     m.p = OOGLNewNE(HPoint3, n, "MeshFLoad: vertices");
  132.     m.n = NULL;
  133.     m.u = NULL;
  134.     m.c = NULL;
  135.     m.d = NULL;
  136.     m.nd = NULL;
  137.  
  138.     if (m.flag & MESH_N)
  139.         m.n = OOGLNewNE(Point3, n, "MeshFLoad: normals");
  140.     if (m.flag & MESH_C)
  141.         m.c = OOGLNewNE(ColorA, n, "MeshFLoad: colors");
  142.     if (m.flag & MESH_U)
  143.         m.u = OOGLNewNE(Point3, n, "MeshFLoad: texture coords");
  144.  
  145.     for (i = 0, v = 0; v < m.nv; v++) {
  146.         for (u = 0; u < m.nu; u++, i++) {
  147.         if(getmeshvert(file, m.flag, u, v,
  148.             &m.p[i], &m.n[i], &m.c[i], &m.u[i]) == 0) {
  149.             OOGLSyntax(file,
  150.         "Reading MESH from \"%s\": bad element (%d,%d) of (%d,%d)",
  151.                 fname, u,v, m.nu,m.nv);
  152.             return NULL;
  153.         }
  154.         }
  155.     }
  156.     return (Mesh *) GeomCCreate (NULL, MeshMethods(), CR_NOCOPY,
  157.         CR_4D, (m.flag & MESH_4D), CR_FLAG, m.flag, CR_NU, m.nu,
  158.         CR_NV, m.nv, CR_POINT4, m.p, CR_COLOR, m.c, CR_NORMAL, m.n,
  159.         CR_U, m.u, NULL);
  160. }
  161.