1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
3 |
#endif |
4 |
/* |
5 |
* instance.c - routines for octree objects. |
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 |
|
23 |
SCENE * |
24 |
getscene(sname, flags) /* load octree sname */ |
25 |
char *sname; |
26 |
int flags; |
27 |
{ |
28 |
char *pathname; |
29 |
register SCENE *sc; |
30 |
|
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) { |
35 |
sc->nref++; |
36 |
return(sc); /* loaded */ |
37 |
} |
38 |
break; /* load the rest */ |
39 |
} |
40 |
if (sc == NULL) { |
41 |
sc = (SCENE *)malloc(sizeof(SCENE)); |
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, getlibpath(), R_OK)) == NULL) { |
56 |
sprintf(errmsg, "cannot find octree file \"%s\"", sname); |
57 |
error(USER, errmsg); |
58 |
} |
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 |
} |
68 |
|
69 |
|
70 |
INSTANCE * |
71 |
getinstance(o, flags) /* get instance structure */ |
72 |
register OBJREC *o; |
73 |
int flags; |
74 |
{ |
75 |
register INSTANCE *ins; |
76 |
|
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 |
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 (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 (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 |
} |