1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
|
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "rwbase.h"
#include "rwerror.h"
#include "rwplg.h"
#include "rwpipeline.h"
#include "rwobjects.h"
#include "rwengine.h"
#define PLUGIN_ID ID_FRAMELIST
namespace rw {
int32 Frame::numAllocated;
PluginList Frame::s_plglist(sizeof(Frame));
static void *frameOpen(void *object, int32 offset, int32 size) { engine->frameDirtyList.init(); return object; }
static void *frameClose(void *object, int32 offset, int32 size) { return object; }
void
Frame::registerModule(void)
{
Engine::registerPlugin(0, ID_FRAMEMODULE, frameOpen, frameClose);
}
Frame*
Frame::create(void)
{
Frame *f = (Frame*)rwMalloc(s_plglist.size, MEMDUR_EVENT | ID_FRAMELIST);
if(f == nil){
RWERROR((ERR_ALLOC, s_plglist.size));
return nil;
}
numAllocated++;
f->object.init(Frame::ID, 0);
f->objectList.init();
f->child = nil;
f->next = nil;
f->root = f;
f->matrix.setIdentity();
f->ltm.setIdentity();
s_plglist.construct(f);
return f;
}
Frame*
Frame::cloneHierarchy(void)
{
Frame *frame = this->cloneAndLink();
this->purgeClone();
return frame;
}
void
Frame::destroy(void)
{
FORLIST(lnk, this->objectList)
ObjectWithFrame::fromFrame(lnk)->setFrame(nil);
s_plglist.destruct(this);
if(this->getParent())
this->removeChild();
if(this->object.privateFlags & Frame::HIERARCHYSYNC)
this->inDirtyList.remove();
for(Frame *f = this->child; f; f = f->next)
f->object.parent = nil;
rwFree(this);
numAllocated--;
}
void
Frame::destroyHierarchy(void)
{
Frame *next;
for(Frame *child = this->child; child; child = next){
next = child->next;
child->destroyHierarchy();
}
assert(this->objectList.isEmpty());
s_plglist.destruct(this);
if(this->object.privateFlags & Frame::HIERARCHYSYNC)
this->inDirtyList.remove();
rwFree(this);
}
Frame*
Frame::addChild(Frame *child, bool32 append)
{
Frame *c;
if(child->getParent())
child->removeChild();
if(append){
if(this->child == nil)
this->child = child;
else{
for(c = this->child; c->next; c = c->next);
c->next = child;
}
child->next = nil;
}else{
child->next = this->child;
this->child = child;
}
child->object.parent = this;
child->root = this->root;
for(c = child->child; c; c = c->next)
c->setHierarchyRoot(this);
// If the child was a root, remove from dirty list
if(child->object.privateFlags & Frame::HIERARCHYSYNC){
child->inDirtyList.remove();
child->object.privateFlags &= ~Frame::HIERARCHYSYNC;
}
this->updateObjects();
return this;
}
Frame*
Frame::removeChild(void)
{
Frame *parent = this->getParent();
Frame *child = parent->child;
if(child == this)
parent->child = this->next;
else{
while(child->next != this)
child = child->next;
child->next = this->next;
}
this->object.parent = this->next = nil;
// give the hierarchy a new root
this->setHierarchyRoot(this);
this->updateObjects();
return this;
}
Frame*
Frame::forAllChildren(Callback cb, void *data)
{
Frame *next;
for(Frame *f = this->child; f; f = next){
next = f->next;
if(cb(f, data) == nil)
return this;
}
return this;
}
static Frame*
countCB(Frame *f, void *count)
{
(*(int32*)count)++;
f->forAllChildren(countCB, count);
return f;
}
int32
Frame::count(void)
{
int32 count = 1;
this->forAllChildren(countCB, (void*)&count);
return count;
}
/*
* Synching is a bit complicated. If anything in the hierarchy is not synched,
* the root of the hierarchy is marked with the HIERARCHYSYNC flags.
* Every unsynched frame is marked with the SUBTREESYNC flags.
* If the LTM is not synched, the LTM flags are set.
* If attached objects need synching, the OBJ flags are set.
*/
/* Synch just LTM matrices in a hierarchy */
static void
syncLTMRecurse(Frame *frame, uint8 hierarchyFlags)
{
for(; frame; frame = frame->next){
// If frame is dirty or any parent was dirty, update LTM
hierarchyFlags |= frame->object.privateFlags;
if(hierarchyFlags & Frame::SUBTREESYNCLTM){
Matrix::mult(&frame->ltm, &frame->matrix,
&frame->getParent()->ltm);
frame->object.privateFlags &= ~Frame::SUBTREESYNCLTM;
}
// And synch all children
syncLTMRecurse(frame->child, hierarchyFlags);
}
}
/* Synch just objects in a hierarchy */
static void
syncObjRecurse(Frame *frame)
{
for(; frame; frame = frame->next){
// Synch attached objects
FORLIST(lnk, frame->objectList)
ObjectWithFrame::fromFrame(lnk)->sync();
frame->object.privateFlags &= ~Frame::SUBTREESYNCOBJ;
// And synch all children
syncObjRecurse(frame->child);
}
}
/* Synch LTM and objects */
static void
syncRecurse(Frame *frame, uint8 hierarchyFlags)
{
for(; frame; frame = frame->next){
// If frame is dirty or any parent was dirty, update LTM
hierarchyFlags |= frame->object.privateFlags;
if(hierarchyFlags & Frame::SUBTREESYNCLTM)
Matrix::mult(&frame->ltm, &frame->matrix,
&frame->getParent()->ltm);
// Synch attached objects
FORLIST(lnk, frame->objectList)
ObjectWithFrame::fromFrame(lnk)->sync();
frame->object.privateFlags &= ~Frame::SUBTREESYNC;
// And synch all children
syncRecurse(frame->child, hierarchyFlags);
}
}
/* Sync the LTMs of the hierarchy of which 'this' is the root */
void
Frame::syncHierarchyLTM(void)
{
// Sync root's LTM
if(this->object.privateFlags & Frame::SUBTREESYNCLTM)
this->ltm = this->matrix;
// ...and children
syncLTMRecurse(this->child, this->object.privateFlags);
// all clean now
this->object.privateFlags &= ~Frame::SYNCLTM;
}
Matrix*
Frame::getLTM(void)
{
if(this->root->object.privateFlags & Frame::HIERARCHYSYNCLTM)
this->root->syncHierarchyLTM();
return &this->ltm;
}
/* Synch all dirty frames; LTMs and objects */
void
Frame::syncDirty(void)
{
Frame *frame;
FORLIST(lnk, engine->frameDirtyList){
frame = LLLinkGetData(lnk, Frame, inDirtyList);
if(frame->object.privateFlags & Frame::HIERARCHYSYNCLTM){
// Sync root's LTM
if(frame->object.privateFlags & Frame::SUBTREESYNCLTM)
frame->ltm = frame->matrix;
// Synch attached objects
FORLIST(lnk, frame->objectList)
ObjectWithFrame::fromFrame(lnk)->sync();
// ...and children
syncRecurse(frame->child, frame->object.privateFlags);
}else{
// LTMs are clean, just synch objects
FORLIST(lnk, frame->objectList)
ObjectWithFrame::fromFrame(lnk)->sync();
syncObjRecurse(frame->child);
}
// all clean now
frame->object.privateFlags &= ~(Frame::SYNCLTM | Frame::SYNCOBJ);
}
engine->frameDirtyList.init();
}
void
Frame::rotate(const V3d *axis, float32 angle, CombineOp op)
{
this->matrix.rotate(axis, angle, op);
updateObjects();
}
void
Frame::rotate(const Quat *q, CombineOp op)
{
this->matrix.rotate(*q, op);
updateObjects();
}
void
Frame::translate(const V3d *trans, CombineOp op)
{
this->matrix.translate(trans, op);
updateObjects();
}
void
Frame::scale(const V3d *scl, CombineOp op)
{
this->matrix.scale(scl, op);
updateObjects();
}
void
Frame::transform(const Matrix *mat, CombineOp op)
{
this->matrix.transform(mat, op);
updateObjects();
}
void
Frame::updateObjects(void)
{
// Mark root as dirty and insert into dirty list if necessary
if((this->root->object.privateFlags & HIERARCHYSYNC) == 0)
engine->frameDirtyList.add(&this->root->inDirtyList);
this->root->object.privateFlags |= HIERARCHYSYNC;
// Mark subtree as dirty as well
this->object.privateFlags |= SUBTREESYNC;
}
void
Frame::setHierarchyRoot(Frame *root)
{
this->root = root;
for(Frame *child = this->child; child; child = child->next)
child->setHierarchyRoot(root);
}
static Frame*
cloneRecurse(Frame *old, Frame *newroot)
{
Frame *frame = Frame::create();
if(newroot == nil)
newroot = frame;
frame->object.copy(&old->object);
frame->matrix = old->matrix;
frame->root = newroot;
old->root = frame; // Remember cloned frame
for(Frame *child = old->child; child; child = child->next){
Frame *clonedchild = cloneRecurse(child, newroot);
clonedchild->next = frame->child;
frame->child = clonedchild;
clonedchild->object.parent = frame;
}
Frame::s_plglist.copy(frame, old);
return frame;
}
// Clone a frame hierarchy. Link cloned frames into Frame::root of the originals.
Frame*
Frame::cloneAndLink(void)
{
Frame *newhier = cloneRecurse(this, nil);
if(newhier){
// frame is not in dirty list so important to get this flag right
newhier->object.privateFlags &= ~HIERARCHYSYNC;
newhier->updateObjects();
}
return newhier;
}
// Remove links to cloned frames from hierarchy.
void
Frame::purgeClone(void)
{
Frame *parent = this->getParent();
this->setHierarchyRoot(parent ? parent->root : this);
}
struct FrameStreamData
{
V3d right, up, at, pos;
int32 parent;
int32 matflag;
};
FrameList_*
FrameList_::streamRead(Stream *stream)
{
FrameStreamData buf;
this->numFrames = 0;
this->frames = nil;
if(!findChunk(stream, ID_STRUCT, nil, nil)){
RWERROR((ERR_CHUNK, "STRUCT"));
return nil;
}
this->numFrames = stream->readI32();
this->frames = (Frame**)rwMalloc(this->numFrames*sizeof(Frame*), MEMDUR_EVENT | ID_FRAMELIST);
if(this->frames == nil){
RWERROR((ERR_ALLOC, this->numFrames*sizeof(Frame*)));
return nil;
}
for(int32 i = 0; i < this->numFrames; i++){
Frame *f;
stream->read32(&buf, sizeof(buf));
this->frames[i] = f = Frame::create();
if(f == nil){
// TODO: clean up frames?
rwFree(this->frames);
return nil;
}
f->matrix.right = buf.right;
f->matrix.up = buf.up;
f->matrix.at = buf.at;
f->matrix.pos = buf.pos;
f->matrix.optimize();
// RW always removes identity flag
f->matrix.flags &= ~Matrix::IDENTITY;
if(buf.parent >= 0)
this->frames[buf.parent]->addChild(f, rw::streamAppendFrames);
}
for(int32 i = 0; i < this->numFrames; i++)
Frame::s_plglist.streamRead(stream, this->frames[i]);
return this;
}
void
FrameList_::streamWrite(Stream *stream)
{
FrameStreamData buf;
int size = 0, structsize = 0;
structsize = 4 + this->numFrames*sizeof(FrameStreamData);
size += 12 + structsize;
for(int32 i = 0; i < this->numFrames; i++)
size += 12 + Frame::s_plglist.streamGetSize(this->frames[i]);
writeChunkHeader(stream, ID_FRAMELIST, size);
writeChunkHeader(stream, ID_STRUCT, structsize);
stream->writeU32(this->numFrames);
for(int32 i = 0; i < this->numFrames; i++){
Frame *f = this->frames[i];
buf.right = f->matrix.right;
buf.up = f->matrix.up;
buf.at = f->matrix.at;
buf.pos = f->matrix.pos;
buf.parent = findPointer(f->getParent(), (void**)this->frames,
this->numFrames);
buf.matflag = 0; //f->matflag;
stream->write32(&buf, sizeof(buf));
}
for(int32 i = 0; i < this->numFrames; i++)
Frame::s_plglist.streamWrite(stream, this->frames[i]);
}
static Frame*
sizeCB(Frame *f, void *size)
{
*(int32*)size += Frame::s_plglist.streamGetSize(f);
f->forAllChildren(sizeCB, size);
return f;
}
uint32
FrameList_::streamGetSize(Frame *f)
{
int32 numFrames = f->count();
uint32 size = 12 + 12 + 4 + numFrames*(sizeof(FrameStreamData)+12);
sizeCB(f, (void*)&size);
return size;
}
Frame**
makeFrameList(Frame *frame, Frame **flist)
{
*flist++ = frame;
if(frame->next)
flist = makeFrameList(frame->next, flist);
if(frame->child)
flist = makeFrameList(frame->child, flist);
return flist;
}
}
|