ftp.nice.ch/pub/next/graphics/3d/geomview.1.4.1.s.tar.gz#/Geomview/src/lib/gprim/polylist/plload.c

This is plload.c in view mode; [Download] [Up]

/* Copyright (c) 1992 The Geometry Center; University of Minnesota
   1300 South Second Street;  Minneapolis, MN  55454, USA;
   
This file is part of geomview/OOGL. geomview/OOGL is free software;
you can redistribute it and/or modify it only under the terms given in
the file COPYING, which you should have received along with this file.
This and other related software may be obtained via anonymous ftp from
geom.umn.edu; email: software@geom.umn.edu. */

/* Authors: Charlie Gunn, Stuart Levy, Tamara Munzner, Mark Phillips */

 /*
  * Load a PolyList object from an "off"-format file.
  * 
  */

#include <string.h>
#include "polylistP.h"

#define	SIZEOF_CMAP	256
static ColorA	*colormap;

static int
LoadCmap(char *file)
{
    FILE *fp;
    colormap = OOGLNewNE(ColorA, 256, "PolyList colormap");
    if((file = findfile(NULL, file)) != NULL &&
			   (fp = fopen(file,"r")) != NULL) {
	fgetnf(fp, SIZEOF_CMAP*4, (float *)colormap, 0);
	fclose(fp);
    }
}

PolyList *
PolyListFLoad(FILE *file, char *fname)
{
    register PolyList *pl;
    int edges;
    register int i;
    register Vertex *v;
    int binary = 0;
    int headerseen = 0;
    int flags = 0;
    int makenorm = 0;
    int dimn = 3;
    static ColorA black = { 0,0,0,0 };

    if (file == NULL)
	return NULL;

    (void) fnextc(file, 0);
    i = fgetc(file);
    if(i == 'C') {
	flags = PL_HASVCOL;	/* Per-vertex colors */
	i = fgetc(file);
    }
    if(i == 'N') {
	flags |= PL_HASVN;	/* Per-vertex normals */
	i = fgetc(file);
    }
    if(i == '4') {
	dimn = 4;
	i = fgetc(file);
    }
    if(i == 'S') {		/* "smooth surface": we compute vtx normals */
	makenorm = 1;
	i = fgetc(file);
    }
    if(i == 'O' && !fexpectstr(file, "FF")) {	/* Skip blanks & comments */
	headerseen = 1;
	if(fnextc(file, 1) == 'B' && fexpectstr(file, "BINARY") == 0) {
	    binary = 1;
	    if(fnextc(file, 1) != '\n')	/* Expect \n after BINARY */
		return NULL;
	    (void) getc(file);		/* Toss \n */
	}
    } else if(i >= '0' && i <= '9') {
	ungetc(i, file);
	if(dimn == 4)
	    ungetc('4', file);
	flags = 0, dimn = 3;
    } else {
	return NULL;
    }


    pl = OOGLNewE(PolyList, "PolyListFLoad PolyList");
    GGeomInit(pl, PolyListMethods(), PLMAGIC, NULL);
    pl->p = NULL;
    pl->vl = NULL;

    pl->flags = flags;
    /* this is very ugly, but this whole routine is, too.  
       e.g., Why isn't GeomCreate being called here to guarantee that the
       returned structure is valid? */
    pl->geomflags = (dimn == 4) ? VERT_4D : 0;

    if(fgetni(file, 1, &pl->n_verts, binary) <= 0 ||
       fgetni(file, 1, &pl->n_polys, binary) <= 0 ||
       fgetni(file, 1, &edges, binary) <= 0) {
		if(headerseen)
		    OOGLSyntax(file, "PolyList %s: Bad vertex/face/edge counts", fname);
		goto bogus;
    }

    pl->vl = OOGLNewNE(Vertex, pl->n_verts, "PolyListFLoad vertices");

    for(v = pl->vl, i = 0; i < pl->n_verts; v++, i++) {
	if(fgetnf(file, dimn, (float *)&v->pt, binary) < dimn ||
	   flags & PL_HASVN && fgetnf(file, 3, (float *)&v->vn, binary) < 3 ||
	   flags & PL_HASVCOL && fgetnf(file, 4, (float *)&v->vcol, binary) < 4) {
		OOGLSyntax(file, "PolyList %s: Bad vertex %d (of %d)", fname, i, pl->n_verts);
		goto bogus;
	}
	if (dimn == 3)  v->pt.w = 1.0;
    }

    pl->p = OOGLNewNE(Poly, pl->n_polys, "PolyListFLoad polygons");
    for(i = 0; i < pl->n_polys; ) {
	register Poly *p;
	register int k,index,m;
	float rgba[4];

	p = &pl->p[i];
	if(fgetni(file, 1, &p->n_vertices, binary) <= 0 || p->n_vertices <= 0) {
	   OOGLSyntax(file, "PolyList %s: bad %d'th polygon (of %d)",
		fname, i, pl->n_polys);
	   goto bogus_face;
	}
	p->v = OOGLNewNE(Vertex *, p->n_vertices, "PolyListFLoad polygon");
	i++;

	for(k = 0; k < p->n_vertices; k++) {
	    int index;

	    if(fgetni(file, 1, &index, binary) <= 0 ||
	       index < 0 || index >= pl->n_verts) {
		    OOGLSyntax(file, "PolyList: %s: bad index %d on %d'th polygon (of %d)", 
			fname, index, i, p->n_vertices);
		    goto bogus_face;
	    }
	    p->v[k] = &pl->vl[index];
	}

	/* Pick up the color, if any.
	 * In ASCII format, take whatever's left before end-of-line
	 */
	p->pcol = black;
	if(binary) {
	    int ncol;

	    if(fgetni(file, 1, &ncol, 1) <= 0
	       || fgetnf(file, ncol, (float *)&p->pcol, 1) < ncol)
		goto bogus_face_color;
	    k = ncol;
	} else {
	    for(k = 0; k < 4 && fnextc(file, 1) != '\n'; k++) {
		if(fgetnf(file, 1, ((float *)(&p->pcol))+k, 0) < 1)
		    goto bogus_face_color;
	    }
	}

	if((flags & PL_HASVCOL) == 0) {
	    if(k > 0)
		pl->flags |= PL_HASPCOL;

	    if(k != 1 && (p->pcol.r>1||p->pcol.g>1||p->pcol.b>1||p->pcol.a>1)) {
		p->pcol.r /= 255, p->pcol.g /= 255,
		p->pcol.b /= 255, p->pcol.a /= 255;
	    }

	    switch(k) {
	    case 0:
		p->pcol.r = 170/255.0;		/* Gray */
		p->pcol.g = p->pcol.r;
	    case 2:
		p->pcol.b = p->pcol.g;
	    case 3:
		p->pcol.a = 170/255.0;		/* Semitransparent */
		break;
	    case 1:				/* use colormap */
		if ( colormap == NULL )
			LoadCmap("cmap.fmap");
		index = p->pcol.r;
		if((unsigned)index >= SIZEOF_CMAP) index = 0;
		p->pcol = colormap[index];
	    }				/* case 4, all components supplied */
	}
    }

    if(makenorm && !(flags & PL_HASVN)) {
	pl->flags |= PL_HASVN;
	pl->flags &= ~PL_HASPN;		/* Leave vertex-normals only */
    }
    return pl;

  
  bogus_face_color:
    OOGLSyntax(file, "PolyList %s: bad face color on face %d (of %d)",
	fname, i, pl->n_polys);
  bogus_face:
    pl->n_polys = i;
  bogus:
    GeomDelete((Geom *)pl);
    return NULL;
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.