| 1 |
– |
/* Copyright (c) 1988 Regents of the University of California */ |
| 2 |
– |
|
| 1 |
|
#ifndef lint |
| 2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
| 2 |
> |
static const char RCSid[] = "$Id$"; |
| 3 |
|
#endif |
| 6 |
– |
|
| 4 |
|
/* |
| 5 |
|
* instance.c - routines for octree objects. |
| 9 |
– |
* |
| 10 |
– |
* 11/10/88 |
| 6 |
|
*/ |
| 7 |
|
|
| 8 |
+ |
#include "copyright.h" |
| 9 |
+ |
|
| 10 |
|
#include "standard.h" |
| 11 |
|
|
| 12 |
+ |
#include "octree.h" |
| 13 |
+ |
|
| 14 |
|
#include "object.h" |
| 15 |
|
|
| 16 |
|
#include "instance.h" |
| 17 |
|
|
| 18 |
+ |
#define IO_ILLEGAL (IO_FILES|IO_INFO) |
| 19 |
+ |
|
| 20 |
|
static SCENE *slist = NULL; /* list of loaded octrees */ |
| 21 |
|
|
| 22 |
|
|
| 25 |
|
char *sname; |
| 26 |
|
int flags; |
| 27 |
|
{ |
| 27 |
– |
extern char *libpath; |
| 28 |
|
char *pathname; |
| 29 |
|
register SCENE *sc; |
| 30 |
|
|
| 31 |
< |
flags &= ~IO_FILES; /* not allowed */ |
| 31 |
> |
flags &= ~IO_ILLEGAL; /* not allowed */ |
| 32 |
|
for (sc = slist; sc != NULL; sc = sc->next) |
| 33 |
|
if (!strcmp(sname, sc->name)) { |
| 34 |
< |
if ((sc->ldflags & flags) == flags) |
| 34 |
> |
if ((sc->ldflags & flags) == flags) { |
| 35 |
> |
sc->nref++; |
| 36 |
|
return(sc); /* loaded */ |
| 37 |
+ |
} |
| 38 |
|
break; /* load the rest */ |
| 39 |
|
} |
| 40 |
|
if (sc == NULL) { |
| 42 |
|
if (sc == NULL) |
| 43 |
|
error(SYSTEM, "out of memory in getscene"); |
| 44 |
|
sc->name = savestr(sname); |
| 45 |
+ |
sc->nref = 1; |
| 46 |
|
sc->ldflags = 0; |
| 47 |
+ |
sc->scube.cutree = EMPTY; |
| 48 |
+ |
sc->scube.cuorg[0] = sc->scube.cuorg[1] = |
| 49 |
+ |
sc->scube.cuorg[2] = 0.; |
| 50 |
+ |
sc->scube.cusize = 0.; |
| 51 |
+ |
sc->firstobj = sc->nobjs = 0; |
| 52 |
|
sc->next = slist; |
| 53 |
|
slist = sc; |
| 54 |
|
} |
| 55 |
< |
if ((pathname = getpath(sname, libpath, R_OK)) == NULL) { |
| 55 |
> |
if ((pathname = getpath(sname, getrlibpath(), R_OK)) == NULL) { |
| 56 |
|
sprintf(errmsg, "cannot find octree file \"%s\"", sname); |
| 57 |
|
error(USER, errmsg); |
| 58 |
|
} |
| 59 |
< |
readoct(pathname, flags & ~sc->ldflags, &sc->scube, NULL); |
| 59 |
> |
flags &= ~sc->ldflags; /* skip what's already loaded */ |
| 60 |
> |
if (flags & IO_SCENE) |
| 61 |
> |
sc->firstobj = nobjects; |
| 62 |
> |
readoct(pathname, flags, &sc->scube, NULL); |
| 63 |
> |
if (flags & IO_SCENE) |
| 64 |
> |
sc->nobjs = nobjects - sc->firstobj; |
| 65 |
|
sc->ldflags |= flags; |
| 66 |
|
return(sc); |
| 67 |
|
} |
| 72 |
|
register OBJREC *o; |
| 73 |
|
int flags; |
| 74 |
|
{ |
| 75 |
< |
register INSTANCE *in; |
| 75 |
> |
register INSTANCE *ins; |
| 76 |
|
|
| 77 |
< |
if ((in = (INSTANCE *)o->os) == NULL) { |
| 78 |
< |
if ((in = (INSTANCE *)malloc(sizeof(INSTANCE))) == NULL) |
| 77 |
> |
flags &= ~IO_ILLEGAL; /* not allowed */ |
| 78 |
> |
if ((ins = (INSTANCE *)o->os) == NULL) { |
| 79 |
> |
if ((ins = (INSTANCE *)malloc(sizeof(INSTANCE))) == NULL) |
| 80 |
|
error(SYSTEM, "out of memory in getinstance"); |
| 81 |
|
if (o->oargs.nsargs < 1) |
| 82 |
|
objerror(o, USER, "bad # of arguments"); |
| 83 |
< |
in->f.sca = 1.0; |
| 70 |
< |
setident4(in->f.xfm); |
| 71 |
< |
if (xf(in->f.xfm, &in->f.sca, o->oargs.nsargs-1, |
| 83 |
> |
if (fullxf(&ins->x, o->oargs.nsargs-1, |
| 84 |
|
o->oargs.sarg+1) != o->oargs.nsargs-1) |
| 85 |
|
objerror(o, USER, "bad transform"); |
| 86 |
< |
if (in->f.sca < 0.0) |
| 87 |
< |
in->f.sca = -in->f.sca; |
| 88 |
< |
in->b.sca = 1.0; |
| 89 |
< |
setident4(in->b.xfm); |
| 90 |
< |
invxf(in->b.xfm, &in->b.sca,o->oargs.nsargs-1,o->oargs.sarg+1); |
| 91 |
< |
if (in->b.sca < 0.0) |
| 80 |
< |
in->b.sca = -in->b.sca; |
| 81 |
< |
in->obj = NULL; |
| 82 |
< |
o->os = (char *)in; |
| 86 |
> |
if (ins->x.f.sca < 0.0) { |
| 87 |
> |
ins->x.f.sca = -ins->x.f.sca; |
| 88 |
> |
ins->x.b.sca = -ins->x.b.sca; |
| 89 |
> |
} |
| 90 |
> |
ins->obj = NULL; |
| 91 |
> |
o->os = (char *)ins; |
| 92 |
|
} |
| 93 |
< |
if (in->obj == NULL || (in->obj->ldflags & flags) != flags) |
| 94 |
< |
in->obj = getscene(o->oargs.sarg[0], flags); |
| 95 |
< |
return(in); |
| 93 |
> |
if (ins->obj == NULL || (ins->obj->ldflags & flags) != flags) |
| 94 |
> |
ins->obj = getscene(o->oargs.sarg[0], flags); |
| 95 |
> |
return(ins); |
| 96 |
> |
} |
| 97 |
> |
|
| 98 |
> |
|
| 99 |
> |
void |
| 100 |
> |
freescene(sc) /* release a scene reference */ |
| 101 |
> |
SCENE *sc; |
| 102 |
> |
{ |
| 103 |
> |
SCENE shead; |
| 104 |
> |
register SCENE *scp; |
| 105 |
> |
|
| 106 |
> |
if (sc == NULL) |
| 107 |
> |
return; |
| 108 |
> |
if (sc->nref <= 0) |
| 109 |
> |
error(CONSISTENCY, "unreferenced scene in freescene"); |
| 110 |
> |
sc->nref--; |
| 111 |
> |
if (sc->nref) /* still in use? */ |
| 112 |
> |
return; |
| 113 |
> |
shead.next = slist; /* else remove from our list */ |
| 114 |
> |
for (scp = &shead; scp->next != NULL; scp = scp->next) |
| 115 |
> |
if (scp->next == sc) { |
| 116 |
> |
scp->next = sc->next; |
| 117 |
> |
sc->next = NULL; |
| 118 |
> |
break; |
| 119 |
> |
} |
| 120 |
> |
if (sc->next != NULL) /* can't be in list anymore */ |
| 121 |
> |
error(CONSISTENCY, "unlisted scene in freescene"); |
| 122 |
> |
slist = shead.next; |
| 123 |
> |
freestr(sc->name); /* free memory */ |
| 124 |
> |
octfree(sc->scube.cutree); |
| 125 |
> |
freeobjects(sc->firstobj, sc->nobjs); |
| 126 |
> |
free((void *)sc); |
| 127 |
> |
} |
| 128 |
> |
|
| 129 |
> |
|
| 130 |
> |
void |
| 131 |
> |
freeinstance(o) /* free memory associated with instance */ |
| 132 |
> |
OBJREC *o; |
| 133 |
> |
{ |
| 134 |
> |
if (o->os == NULL) |
| 135 |
> |
return; |
| 136 |
> |
freescene((*(INSTANCE *)o->os).obj); |
| 137 |
> |
free((void *)o->os); |
| 138 |
> |
o->os = NULL; |
| 139 |
|
} |