[BACK]Return to pfphd.c CVS log [TXT][DIR] Up to [Development] / performer / src / lib / libpfdb / libpfphd

File: [Development] / performer / src / lib / libpfdb / libpfphd / pfphd.c (download)

Revision 1.1, Tue Nov 21 21:39:34 2000 UTC (16 years, 11 months ago) by flynnt
Branch: MAIN
CVS Tags: HEAD

Initial check-in based on OpenGL Performer 2.4 tree.
-flynnt

/*
 * Copyright 1993, 1995, Silicon Graphics, Inc.
 * ALL RIGHTS RESERVED
 *
 * This source code ("Source Code") was originally derived from a
 * code base owned by Silicon Graphics, Inc. ("SGI")
 * 
 * LICENSE: SGI grants the user ("Licensee") permission to reproduce,
 * distribute, and create derivative works from this Source Code,
 * provided that: (1) the user reproduces this entire notice within
 * both source and binary format redistributions and any accompanying
 * materials such as documentation in printed or electronic format;
 * (2) the Source Code is not to be used, or ported or modified for
 * use, except in conjunction with OpenGL Performer; and (3) the
 * names of Silicon Graphics, Inc.  and SGI may not be used in any
 * advertising or publicity relating to the Source Code without the
 * prior written permission of SGI.  No further license or permission
 * may be inferred or deemed or construed to exist with regard to the
 * Source Code or the code base of which it forms a part. All rights
 * not expressly granted are reserved.
 * 
 * This Source Code is provided to Licensee AS IS, without any
 * warranty of any kind, either express, implied, or statutory,
 * including, but not limited to, any warranty that the Source Code
 * will conform to specifications, any implied warranties of
 * merchantability, fitness for a particular purpose, and freedom
 * from infringement, and any warranty that the documentation will
 * conform to the program, or any warranty that the Source Code will
 * be error free.
 * 
 * IN NO EVENT WILL SGI BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT
 * LIMITED TO DIRECT, INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES,
 * ARISING OUT OF, RESULTING FROM, OR IN ANY WAY CONNECTED WITH THE
 * SOURCE CODE, WHETHER OR NOT BASED UPON WARRANTY, CONTRACT, TORT OR
 * OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM,
 * OR AROSE OUT OF USE OR RESULTS FROM USE OF, OR LACK OF ABILITY TO
 * USE, THE SOURCE CODE.
 * 
 * Contact information:  Silicon Graphics, Inc., 
 * 1600 Amphitheatre Pkwy, Mountain View, CA  94043, 
 * or:  http://www.sgi.com
 */

/*
 * pfphd.c: $Revision: 1.1 $ $Date: 2000/11/21 21:39:34 $
 */

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

#ifdef	_POSIX_SOURCE
extern int strcasecmp (const char *s1, const char *s2);
#endif

#include <Performer/pf.h>

#include <Performer/pfdu.h>
#include <Performer/pfutil.h>

/* length of longest input line in ".phd" file */
#define	BUFFER_SIZE	256

/* case insensitive string equality test */
#define	SAME(_a, _b)	(strcasecmp(_a,_b) == 0)

/*
 * pfdLoadFile_phd -- Load SGI ".phd" polyhedron files into IRIS Performer
 */

extern pfNode *
pfdLoadFile_phd (char *fileName)
{
    FILE	*phdFile;
    long	 seedValue;
    char	 buffer[BUFFER_SIZE];
    char	 token[BUFFER_SIZE];
    char	 objectName[BUFFER_SIZE] = "";
    char	*next		= NULL;
    pfNode	*node		= NULL;
    int		 width		= 0;
    int		 numSkip	= 0;
    int		 numOther	= 0;
    int		 numVerts	= 0;
    int		 maxVerts	= 256;
    int		 totalTris	= 0;
    int		 totalVerts	= 0;
    pfdGeom	*prim		= NULL;
    pfMaterial  *m      	= NULL;

    double	 startTime	= pfGetTime();
    double	 elapsedTime	= 0.0;

    int		 t		= 0;

    /* restore builder to initial state */
    pfdResetBldrGeometry();
    pfdResetBldrState();

    /* open ".phd" file */
    if ((phdFile = pfdOpenFile(fileName)) == NULL)
	return NULL;

    /* allocate primitive buffer */
    prim = pfdNewGeom(maxVerts);

    /* set color sequence to a random value */
    pfuRandomize(*(int *)&startTime);
    seedValue = pfuRandomLong();

    /* establish material definition for model */
    m = (pfMaterial*)pfdGetTemplateObject(pfGetMtlClassType());
    pfMtlColor(m, PFMTL_AMBIENT,  0.2f, 0.2f, 0.2f);
    pfMtlColor(m, PFMTL_DIFFUSE,  0.8f, 0.8f, 0.8f);
    pfMtlColor(m, PFMTL_SPECULAR, 1.0f, 1.0f, 1.0f);
    pfMtlShininess(m, 30.0f);
    pfMtlColorMode(m, PFMTL_FRONT, PFMTL_CMODE_AD);
    pfdBldrStateAttr(PFSTATE_FRONTMTL, m);
    pfdBldrStateMode(PFSTATE_ENLIGHTING, PF_ON);
    pfdBldrStateMode(PFSTATE_CULLFACE, PFCF_BACK);

    /* read ".phd" file */
    while (fgets(buffer, BUFFER_SIZE, phdFile) != NULL)
    {
	/* find first non-"space" character in line */
	for (next = buffer; *next != '\0' && isspace(*next); next++)
	    /* EMPTY */ {};
	
	/* skip blank lines and comments */
	if (*next == '\0' || *next == '#' || *next == '!' || *next == '$')
	    continue;
	
	/* extract token */
	sscanf(next, "%s%n", token, &width);
	next += width;

	/* identify token */
	if (SAME(token, "object.begin"))
	{
	    /* extract object name */
	    next = strchr(next, '"');
	    if (next != NULL)
	       next++;
	    strcpy(objectName, next);
	    next = strchr(objectName, '"');
	    if (next != NULL)
	       *next = '\0';
	}
	else
	if (SAME(token, "object.end"))
	{
	    ++numSkip;
	}
	else
	if (SAME(token, "vertex"))
	{
	    /* enlarge primitive buffer if necessary */
	    if (numVerts >= maxVerts)
	    {
		maxVerts *= 2;
		pfdResizeGeom(prim, maxVerts);
	    }

	    /* read coordinate from string */
	    sscanf(next, "%f %f %f", 
		&prim->coords[numVerts][PF_X], 
		&prim->coords[numVerts][PF_Y], 
		&prim->coords[numVerts][PF_Z]);

	    /* advance vertex count */
	    ++numVerts;
	}
	else
	if (SAME(token, "polygon.begin"))
	{
	    /* reset facet vertex count */
	    numVerts = 0;
	    ++numSkip;
	}
	else
	if (SAME(token, "polygon.end"))
	{
	    /* specify facet information */
	    prim->numVerts = numVerts;
	    prim->primtype = PFGS_POLYS;
	    for (t = 0 ; t < PF_MAX_TEXTURES ; t ++)
		prim->tbind[t] = PFGS_OFF;
	    prim->nbind = PFGS_OFF;
	    prim->cbind = PFGS_PER_PRIM;

	    /* pick a random not-too-dark color based on vertex count */
	    pfuRandomize(seedValue + prim->numVerts);
	    pfuRandomColor(prim->colors[0], 0.2f, 0.7f);

	    /* add this facet to builder */
	    pfdAddBldrGeom(prim, 1);

	    /* keep total counts */
	    totalTris += prim->numVerts - 2;
	    totalVerts += prim->numVerts;
	}
	else
	if (SAME(token, "file"))
	{
	    ++numSkip;
	}
	else
	{
	    pfNotify(PFNFY_WARN, PFNFY_PRINT, "pfdLoadFile_phd: unrecognized: %s", buffer);
	    ++numOther;
	}
    }

    /* close ".phd" file */
    fclose(phdFile);

    /* release primitive buffer */
    pfdDelGeom(prim);

    /* get a complete scene graph representing file's primitives */
    node = pfdBuild();

    /* use indicated name for top-level pfNode */
    if (node != NULL && objectName[0] != '\0')
	pfNodeName(node, objectName);

    /* print statistics */
    pfNotify(PFNFY_NOTICE, PFNFY_PRINT, "pfdLoadFile_lsa: %s", fileName);
    pfNotify(PFNFY_INFO,   PFNFY_MORE,  "  Input Data:");
    if (objectName[0] != '\0')
	pfNotify(PFNFY_INFO,   PFNFY_MORE,  
	    "    Object name:        %s", objectName);
    if (numSkip != 0)
	pfNotify(PFNFY_INFO,   PFNFY_MORE,  
	    "    Commands skipped:   %8ld", numSkip);
    if (numOther != 0)
	pfNotify(PFNFY_INFO,   PFNFY_MORE,  
	    "    Commands unknown:   %8ld", numOther);
    if (totalVerts != 0)
	pfNotify(PFNFY_INFO,   PFNFY_MORE,  
	    "    Input vertices:     %8ld", totalVerts);
    if (totalTris != 0)
	pfNotify(PFNFY_INFO,   PFNFY_MORE,  
	    "    Input polygons:     %8ld", totalTris);

    return node;
}