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