librw/src/clump.cpp

597 lines
13 KiB
C++
Raw Normal View History

2014-12-18 17:26:57 +01:00
#include <cstdio>
#include <cstdlib>
#include "rwbase.h"
#include "rwerror.h"
2016-06-16 14:08:09 +02:00
#include "rwplg.h"
2015-07-11 23:48:11 +02:00
#include "rwpipeline.h"
2014-12-23 15:59:14 +01:00
#include "rwobjects.h"
2016-06-16 14:08:09 +02:00
#include "rwengine.h"
2014-12-18 17:26:57 +01:00
using namespace std;
#define PLUGIN_ID 2
namespace rw {
2014-12-18 17:26:57 +01:00
2015-01-09 20:17:32 +01:00
//
// Clump
//
2014-12-18 17:26:57 +01:00
Clump*
Clump::create(void)
2014-12-18 17:26:57 +01:00
{
Clump *clump = (Clump*)malloc(PluginBase::s_size);
if(clump == nil){
RWERROR((ERR_ALLOC, PluginBase::s_size));
return nil;
}
2016-01-13 08:58:15 +01:00
clump->object.init(Clump::ID, 0);
clump->atomics.init();
clump->lights.init();
2016-01-13 08:58:15 +01:00
clump->cameras.init();
clump->constructPlugins();
return clump;
2014-12-18 17:26:57 +01:00
}
Clump*
Clump::clone(void)
2014-12-18 17:26:57 +01:00
{
Clump *clump = Clump::create();
Frame *root = this->getFrame()->cloneHierarchy();
clump->setFrame(root);
FORLIST(lnk, this->atomics){
Atomic *a = Atomic::fromClump(lnk);
Atomic *atomic = a->clone();
atomic->setFrame(a->getFrame()->root);
clump->addAtomic(atomic);
}
root->purgeClone();
clump->copyPlugins(this);
return clump;
2014-12-18 17:26:57 +01:00
}
void
Clump::destroy(void)
2014-12-18 17:26:57 +01:00
{
Frame *f;
this->destructPlugins();
FORLIST(lnk, this->atomics)
Atomic::fromClump(lnk)->destroy();
FORLIST(lnk, this->lights)
Light::fromClump(lnk)->destroy();
2016-01-13 08:58:15 +01:00
FORLIST(lnk, this->cameras)
Camera::fromClump(lnk)->destroy();
if(f = this->getFrame())
f->destroyHierarchy();
free(this);
}
2014-12-18 17:26:57 +01:00
Clump*
Clump::streamRead(Stream *stream)
2014-12-18 17:26:57 +01:00
{
uint32 length, version;
int32 buf[3];
Clump *clump;
if(!findChunk(stream, ID_STRUCT, &length, &version)){
RWERROR((ERR_CHUNK, "STRUCT"));
return nil;
}
clump = Clump::create();
if(clump == nil)
return nil;
stream->read(buf, length);
int32 numAtomics = buf[0];
int32 numLights = 0;
2016-01-13 08:58:15 +01:00
int32 numCameras = 0;
if(version > 0x33000){
numLights = buf[1];
2016-01-13 08:58:15 +01:00
numCameras = buf[2];
}
2014-12-18 17:26:57 +01:00
// Frame list
Frame **frameList;
int32 numFrames;
clump->frameListStreamRead(stream, &frameList, &numFrames);
2016-01-13 08:58:15 +01:00
clump->setFrame(frameList[0]);
2014-12-18 17:26:57 +01:00
Geometry **geometryList = 0;
2015-08-16 23:23:41 +02:00
if(version >= 0x30400){
// Geometry list
int32 numGeometries = 0;
if(!findChunk(stream, ID_GEOMETRYLIST, nil, nil)){
RWERROR((ERR_CHUNK, "GEOMETRYLIST"));
// TODO: free
return nil;
}
if(!findChunk(stream, ID_STRUCT, nil, nil)){
RWERROR((ERR_CHUNK, "STRUCT"));
// TODO: free
return nil;
}
2015-08-16 23:23:41 +02:00
numGeometries = stream->readI32();
if(numGeometries)
geometryList = new Geometry*[numGeometries];
for(int32 i = 0; i < numGeometries; i++){
if(!findChunk(stream, ID_GEOMETRY, nil, nil)){
RWERROR((ERR_CHUNK, "GEOMETRY"));
// TODO: free
return nil;
}
2015-08-16 23:23:41 +02:00
geometryList[i] = Geometry::streamRead(stream);
if(geometryList[i] == nil){
// TODO: free
return nil;
}
2015-08-16 23:23:41 +02:00
}
2014-12-18 17:26:57 +01:00
}
// Atomics
Atomic *a;
for(int32 i = 0; i < numAtomics; i++){
if(!findChunk(stream, ID_ATOMIC, nil, nil)){
RWERROR((ERR_CHUNK, "ATOMIC"));
// TODO: free
return nil;
}
a = Atomic::streamReadClump(stream, frameList, geometryList);
if(a == nil){
// TODO: free
return nil;
}
clump->addAtomic(a);
2014-12-18 17:26:57 +01:00
}
// Lights
for(int32 i = 0; i < numLights; i++){
2014-12-18 17:26:57 +01:00
int32 frm;
if(!findChunk(stream, ID_STRUCT, nil, nil)){
RWERROR((ERR_CHUNK, "STRUCT"));
// TODO: free
return nil;
}
frm = stream->readI32();
if(!findChunk(stream, ID_LIGHT, nil, nil)){
RWERROR((ERR_CHUNK, "LIGHT"));
// TODO: free
return nil;
}
Light *l = Light::streamRead(stream);
if(l == nil){
// TODO: free
return nil;
}
l->setFrame(frameList[frm]);
clump->addLight(l);
2014-12-18 17:26:57 +01:00
}
2016-01-13 08:58:15 +01:00
// Cameras
for(int32 i = 0; i < numCameras; i++){
int32 frm;
if(!findChunk(stream, ID_STRUCT, nil, nil)){
RWERROR((ERR_CHUNK, "STRUCT"));
// TODO: free
return nil;
}
2016-01-13 08:58:15 +01:00
frm = stream->readI32();
if(!findChunk(stream, ID_CAMERA, nil, nil)){
RWERROR((ERR_CHUNK, "CAMERA"));
// TODO: free
return nil;
}
2016-01-13 08:58:15 +01:00
Camera *cam = Camera::streamRead(stream);
if(cam == nil){
// TODO: free
return nil;
}
2016-01-13 08:58:15 +01:00
cam->setFrame(frameList[frm]);
clump->addCamera(cam);
}
2014-12-18 17:26:57 +01:00
delete[] frameList;
clump->streamReadPlugins(stream);
return clump;
}
bool
Clump::streamWrite(Stream *stream)
2014-12-18 17:26:57 +01:00
{
int size = this->streamGetSize();
writeChunkHeader(stream, ID_CLUMP, size);
int32 numAtomics = this->countAtomics();
int32 numLights = this->countLights();
2016-01-24 01:42:51 +01:00
int32 numCameras = this->countCameras();
int buf[3] = { numAtomics, numLights, numCameras };
size = version > 0x33000 ? 12 : 4;
writeChunkHeader(stream, ID_STRUCT, size);
stream->write(buf, size);
2014-12-18 17:26:57 +01:00
2016-01-13 08:58:15 +01:00
int32 numFrames = this->getFrame()->count();
2014-12-18 17:26:57 +01:00
Frame **flist = new Frame*[numFrames];
2016-01-13 08:58:15 +01:00
makeFrameList(this->getFrame(), flist);
2014-12-18 17:26:57 +01:00
this->frameListStreamWrite(stream, flist, numFrames);
2015-08-16 23:23:41 +02:00
if(rw::version >= 0x30400){
size = 12+4;
FORLIST(lnk, this->atomics)
size += 12 + Atomic::fromClump(lnk)->geometry->streamGetSize();
2015-08-16 23:23:41 +02:00
writeChunkHeader(stream, ID_GEOMETRYLIST, size);
writeChunkHeader(stream, ID_STRUCT, 4);
stream->writeI32(numAtomics); // same as numGeometries
FORLIST(lnk, this->atomics)
Atomic::fromClump(lnk)->geometry->streamWrite(stream);
2015-08-16 23:23:41 +02:00
}
2014-12-18 17:26:57 +01:00
FORLIST(lnk, this->atomics)
Atomic::fromClump(lnk)->streamWriteClump(stream, flist, numFrames);
2014-12-18 17:26:57 +01:00
FORLIST(lnk, this->lights){
Light *l = Light::fromClump(lnk);
2016-01-13 08:58:15 +01:00
int frm = findPointer(l->getFrame(), (void**)flist, numFrames);
2014-12-18 17:26:57 +01:00
if(frm < 0)
return false;
writeChunkHeader(stream, ID_STRUCT, 4);
stream->writeI32(frm);
2014-12-18 17:26:57 +01:00
l->streamWrite(stream);
}
2016-01-13 08:58:15 +01:00
FORLIST(lnk, this->cameras){
Camera *c = Camera::fromClump(lnk);
int frm = findPointer(c->getFrame(), (void**)flist, numFrames);
if(frm < 0)
return false;
writeChunkHeader(stream, ID_STRUCT, 4);
stream->writeI32(frm);
c->streamWrite(stream);
}
2014-12-18 17:26:57 +01:00
delete[] flist;
this->streamWritePlugins(stream);
return true;
}
struct FrameStreamData
{
2016-02-14 20:56:05 +01:00
V3d right, up, at, pos;
2014-12-18 17:26:57 +01:00
int32 parent;
int32 matflag;
};
static Frame*
sizeCB(Frame *f, void *size)
{
*(int32*)size += f->streamGetPluginSize();
f->forAllChildren(sizeCB, size);
return f;
}
2014-12-18 17:26:57 +01:00
uint32
Clump::streamGetSize(void)
{
uint32 size = 0;
size += 12; // Struct
size += 4; // numAtomics
if(version > 0x33000)
2014-12-18 17:26:57 +01:00
size += 8; // numLights, numCameras
2016-01-13 08:58:15 +01:00
// Frame list
int32 numFrames = this->getFrame()->count();
2014-12-18 17:26:57 +01:00
size += 12 + 12 + 4 + numFrames*(sizeof(FrameStreamData)+12);
2016-01-13 08:58:15 +01:00
sizeCB(this->getFrame(), (void*)&size);
2014-12-18 17:26:57 +01:00
2015-08-16 23:23:41 +02:00
if(rw::version >= 0x30400){
2016-01-13 08:58:15 +01:00
// Geometry list
2015-08-16 23:23:41 +02:00
size += 12 + 12 + 4;
FORLIST(lnk, this->atomics)
size += 12 + Atomic::fromClump(lnk)->geometry->streamGetSize();
2015-08-16 23:23:41 +02:00
}
2014-12-18 17:26:57 +01:00
2016-01-13 08:58:15 +01:00
// Atomics
FORLIST(lnk, this->atomics)
size += 12 + Atomic::fromClump(lnk)->streamGetSize();
2014-12-18 17:26:57 +01:00
2016-01-13 08:58:15 +01:00
// Lights
FORLIST(lnk, this->lights)
size += 16 + 12 + Light::fromClump(lnk)->streamGetSize();
2014-12-18 17:26:57 +01:00
2016-01-13 08:58:15 +01:00
// Cameras
FORLIST(lnk, this->cameras)
size += 16 + 12 + Camera::fromClump(lnk)->streamGetSize();
2014-12-18 17:26:57 +01:00
size += 12 + this->streamGetPluginSize();
return size;
}
2016-02-14 20:56:05 +01:00
void
Clump::render(void)
{
Atomic *a;
FORLIST(lnk, this->atomics){
a = Atomic::fromClump(lnk);
2016-06-16 14:08:09 +02:00
if(a->object.object.flags & Atomic::RENDER)
2016-02-14 20:56:05 +01:00
a->render();
}
}
2014-12-18 17:26:57 +01:00
bool
Clump::frameListStreamRead(Stream *stream, Frame ***flp, int32 *nf)
2014-12-18 17:26:57 +01:00
{
FrameStreamData buf;
int32 numFrames = 0;
if(!findChunk(stream, ID_FRAMELIST, nil, nil)){
RWERROR((ERR_CHUNK, "FRAMELIST"));
return 0;
}
if(!findChunk(stream, ID_STRUCT, nil, nil)){
RWERROR((ERR_CHUNK, "STRUCT"));
return 0;
}
numFrames = stream->readI32();
2014-12-18 17:26:57 +01:00
Frame **frameList = new Frame*[numFrames];
for(int32 i = 0; i < numFrames; i++){
Frame *f;
frameList[i] = f = Frame::create();
stream->read(&buf, sizeof(buf));
2016-02-14 20:56:05 +01:00
f->matrix.right = buf.right;
f->matrix.rightw = 0.0f;
f->matrix.up = buf.up;
f->matrix.upw = 0.0f;
f->matrix.at = buf.at;
f->matrix.atw = 0.0f;
f->matrix.pos = buf.pos;
f->matrix.posw = 1.0f;
//f->matflag = buf.matflag;
2014-12-18 17:26:57 +01:00
if(buf.parent >= 0)
frameList[buf.parent]->addChild(f);
}
for(int32 i = 0; i < numFrames; i++)
frameList[i]->streamReadPlugins(stream);
*nf = numFrames;
*flp = frameList;
return 1;
2014-12-18 17:26:57 +01:00
}
void
Clump::frameListStreamWrite(Stream *stream, Frame **frameList, int32 numFrames)
2014-12-18 17:26:57 +01:00
{
FrameStreamData buf;
int size = 0, structsize = 0;
structsize = 4 + numFrames*sizeof(FrameStreamData);
size += 12 + structsize;
for(int32 i = 0; i < numFrames; i++)
size += 12 + frameList[i]->streamGetPluginSize();
writeChunkHeader(stream, ID_FRAMELIST, size);
writeChunkHeader(stream, ID_STRUCT, structsize);
stream->writeU32(numFrames);
2014-12-18 17:26:57 +01:00
for(int32 i = 0; i < numFrames; i++){
Frame *f = frameList[i];
2016-02-14 20:56:05 +01:00
buf.right = f->matrix.right;
buf.up = f->matrix.up;
buf.at = f->matrix.at;
buf.pos = f->matrix.pos;
2016-01-13 08:58:15 +01:00
buf.parent = findPointer(f->getParent(), (void**)frameList,
2015-08-11 21:48:23 +02:00
numFrames);
buf.matflag = 0; //f->matflag;
stream->write(&buf, sizeof(buf));
2014-12-18 17:26:57 +01:00
}
for(int32 i = 0; i < numFrames; i++)
frameList[i]->streamWritePlugins(stream);
}
2015-01-09 20:17:32 +01:00
//
// Atomic
//
2014-12-18 17:26:57 +01:00
Atomic*
Atomic::create(void)
2014-12-18 17:26:57 +01:00
{
Atomic *atomic = (Atomic*)malloc(PluginBase::s_size);
if(atomic == nil){
RWERROR((ERR_ALLOC, PluginBase::s_size));
return nil;
}
2016-06-16 14:08:09 +02:00
atomic->object.object.init(Atomic::ID, 0);
atomic->geometry = nil;
2016-02-18 14:56:10 +01:00
atomic->worldBoundingSphere.center.set(0.0f, 0.0f, 0.0f);
atomic->worldBoundingSphere.radius = 0.0f;
atomic->setFrame(nil);
atomic->clump = nil;
atomic->pipeline = nil;
atomic->renderCB = Atomic::defaultRenderCB;
2016-06-16 14:08:09 +02:00
atomic->object.object.flags = Atomic::COLLISIONTEST | Atomic::RENDER;
atomic->constructPlugins();
return atomic;
2016-01-13 08:58:15 +01:00
}
2014-12-18 17:26:57 +01:00
Atomic*
Atomic::clone()
2014-12-18 17:26:57 +01:00
{
Atomic *atomic = Atomic::create();
2016-06-16 14:08:09 +02:00
atomic->object.object.copy(&this->object.object);
atomic->object.object.privateFlags |= 1;
if(this->geometry){
atomic->geometry = this->geometry;
atomic->geometry->refCount++;
}
atomic->pipeline = this->pipeline;
atomic->copyPlugins(this);
return atomic;
2014-12-18 17:26:57 +01:00
}
void
Atomic::destroy(void)
2014-12-18 17:26:57 +01:00
{
this->destructPlugins();
if(this->geometry)
this->geometry->destroy();
2016-02-17 23:59:05 +01:00
if(this->clump)
this->inClump.remove();
this->setFrame(nil);
free(this);
2014-12-18 17:26:57 +01:00
}
2016-06-14 23:07:16 +02:00
void
Atomic::removeFromClump(void)
{
if(this->clump){
this->inClump.remove();
this->clump = nil;
2016-06-14 23:07:16 +02:00
}
}
2016-02-18 14:56:10 +01:00
Sphere*
Atomic::getWorldBoundingSphere(void)
{
Sphere *s = &this->worldBoundingSphere;
if(!this->getFrame()->dirty() &&
2016-06-16 14:08:09 +02:00
(this->object.object.privateFlags & WORLDBOUNDDIRTY) == 0)
2016-02-18 14:56:10 +01:00
return s;
Matrix *ltm = this->getFrame()->getLTM();
// TODO: support scaling
// TODO: if we ever support morphing, fix this:
2016-02-24 07:41:55 +01:00
s->center = ltm->transPoint(this->geometry->morphTargets[0].boundingSphere.center);
2016-02-18 14:56:10 +01:00
s->radius = this->geometry->morphTargets[0].boundingSphere.radius;
2016-06-16 14:08:09 +02:00
this->object.object.privateFlags &= ~WORLDBOUNDDIRTY;
2016-02-18 14:56:10 +01:00
return s;
}
2015-01-10 22:13:27 +01:00
static uint32 atomicRights[2];
2014-12-18 17:26:57 +01:00
Atomic*
Atomic::streamReadClump(Stream *stream,
2014-12-18 17:26:57 +01:00
Frame **frameList, Geometry **geometryList)
{
int32 buf[4];
2015-08-16 23:23:41 +02:00
uint32 version;
if(!findChunk(stream, ID_STRUCT, nil, &version)){
RWERROR((ERR_CHUNK, "STRUCT"));
return nil;
}
2015-08-16 23:23:41 +02:00
stream->read(buf, version < 0x30400 ? 12 : 16);
Atomic *atomic = Atomic::create();
if(atomic == nil)
return nil;
2016-01-11 11:23:26 +01:00
atomic->setFrame(frameList[buf[0]]);
2015-08-16 23:23:41 +02:00
if(version < 0x30400){
if(!findChunk(stream, ID_GEOMETRY, nil, nil)){
RWERROR((ERR_CHUNK, "STRUCT"));
// TODO: free
return nil;
}
2015-08-16 23:23:41 +02:00
atomic->geometry = Geometry::streamRead(stream);
if(atomic->geometry == nil){
// TODO: free
return nil;
}
2015-08-16 23:23:41 +02:00
}else
atomic->geometry = geometryList[buf[1]];
2016-06-16 14:08:09 +02:00
atomic->object.object.flags = buf[2];
2015-01-10 22:13:27 +01:00
atomicRights[0] = 0;
2014-12-18 17:26:57 +01:00
atomic->streamReadPlugins(stream);
2015-01-10 22:13:27 +01:00
if(atomicRights[0])
atomic->assertRights(atomicRights[0], atomicRights[1]);
2014-12-18 17:26:57 +01:00
return atomic;
}
bool
Atomic::streamWriteClump(Stream *stream, Frame **frameList, int32 numFrames)
2014-12-18 17:26:57 +01:00
{
2016-02-14 20:56:05 +01:00
int32 buf[4] = { 0, 0, 0, 0 };
2014-12-18 17:26:57 +01:00
Clump *c = this->clump;
if(c == nil)
2014-12-18 17:26:57 +01:00
return false;
writeChunkHeader(stream, ID_ATOMIC, this->streamGetSize());
2015-08-16 23:23:41 +02:00
writeChunkHeader(stream, ID_STRUCT, rw::version < 0x30400 ? 12 : 16);
2016-01-13 08:58:15 +01:00
buf[0] = findPointer(this->getFrame(), (void**)frameList, numFrames);
2014-12-18 17:26:57 +01:00
2015-08-16 23:23:41 +02:00
if(version < 0x30400){
2016-06-16 14:08:09 +02:00
buf[1] = this->object.object.flags;
2015-08-16 23:23:41 +02:00
stream->write(buf, sizeof(int[3]));
this->geometry->streamWrite(stream);
}else{
buf[1] = 0;
FORLIST(lnk, c->atomics){
if(Atomic::fromClump(lnk)->geometry == this->geometry)
2015-08-16 23:23:41 +02:00
goto foundgeo;
buf[1]++;
}
2015-08-16 23:23:41 +02:00
return false;
foundgeo:
2016-06-16 14:08:09 +02:00
buf[2] = this->object.object.flags;
2015-08-16 23:23:41 +02:00
stream->write(buf, sizeof(buf));
}
2014-12-18 17:26:57 +01:00
this->streamWritePlugins(stream);
return true;
}
uint32
Atomic::streamGetSize(void)
{
2015-08-16 23:23:41 +02:00
uint32 size = 12 + 12 + 12 + this->streamGetPluginSize();
if(rw::version < 0x30400)
size += 12 + this->geometry->streamGetSize();
else
size += 4;
return size;
2014-12-18 17:26:57 +01:00
}
2015-08-03 18:30:10 +02:00
ObjPipeline*
Atomic::getPipeline(void)
{
return this->pipeline ?
this->pipeline :
2016-06-16 14:08:09 +02:00
engine[platform].defaultPipeline;
2015-08-03 18:30:10 +02:00
}
void
Atomic::defaultRenderCB(Atomic *atomic)
{
atomic->getPipeline()->render(atomic);
2015-08-03 18:30:10 +02:00
}
2015-01-09 20:17:32 +01:00
// Atomic Rights plugin
static void
readAtomicRights(Stream *stream, int32, void *, int32, int32)
{
2015-01-10 22:13:27 +01:00
stream->read(atomicRights, 8);
}
static void
writeAtomicRights(Stream *stream, int32, void *object, int32, int32)
{
Atomic *atomic = (Atomic*)object;
uint32 buffer[2];
buffer[0] = atomic->pipeline->pluginID;
buffer[1] = atomic->pipeline->pluginData;
stream->write(buffer, 8);
}
static int32
getSizeAtomicRights(void *object, int32, int32)
{
Atomic *atomic = (Atomic*)object;
if(atomic->pipeline == nil || atomic->pipeline->pluginID == 0)
2015-01-10 22:13:27 +01:00
return -1;
return 8;
2015-01-09 20:17:32 +01:00
}
void
registerAtomicRightsPlugin(void)
2015-01-09 20:17:32 +01:00
{
Atomic::registerPlugin(0, ID_RIGHTTORENDER, nil, nil, nil);
2015-01-09 20:17:32 +01:00
Atomic::registerPluginStream(ID_RIGHTTORENDER,
2015-01-10 22:13:27 +01:00
readAtomicRights,
writeAtomicRights,
getSizeAtomicRights);
2015-01-09 20:17:32 +01:00
}
2014-12-18 17:26:57 +01:00
}