2014-12-18 17:26:57 +01:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
#include "rwbase.h"
|
2016-06-17 00:06:37 +02:00
|
|
|
#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;
|
|
|
|
|
2016-06-17 00:06:37 +02:00
|
|
|
#define PLUGIN_ID 2
|
2016-02-13 00:52:32 +01:00
|
|
|
|
2016-06-17 00:06:37 +02:00
|
|
|
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
|
|
|
|
2016-01-11 18:22:59 +01:00
|
|
|
Clump*
|
|
|
|
Clump::create(void)
|
2014-12-18 17:26:57 +01:00
|
|
|
{
|
2016-01-11 18:22:59 +01:00
|
|
|
Clump *clump = (Clump*)malloc(PluginBase::s_size);
|
2016-06-17 00:06:37 +02:00
|
|
|
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);
|
2016-01-11 18:22:59 +01:00
|
|
|
clump->atomics.init();
|
|
|
|
clump->lights.init();
|
2016-01-13 08:58:15 +01:00
|
|
|
clump->cameras.init();
|
2016-01-11 18:22:59 +01:00
|
|
|
clump->constructPlugins();
|
|
|
|
return clump;
|
2014-12-18 17:26:57 +01:00
|
|
|
}
|
|
|
|
|
2016-01-11 18:22:59 +01:00
|
|
|
Clump*
|
|
|
|
Clump::clone(void)
|
2014-12-18 17:26:57 +01:00
|
|
|
{
|
2016-01-11 18:22:59 +01:00
|
|
|
Clump *clump = Clump::create();
|
2016-01-12 00:38:36 +01:00
|
|
|
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();
|
2016-01-11 18:22:59 +01:00
|
|
|
clump->copyPlugins(this);
|
|
|
|
return clump;
|
2014-12-18 17:26:57 +01:00
|
|
|
}
|
|
|
|
|
2016-01-11 18:22:59 +01:00
|
|
|
void
|
|
|
|
Clump::destroy(void)
|
2014-12-18 17:26:57 +01:00
|
|
|
{
|
2016-01-12 00:38:36 +01:00
|
|
|
Frame *f;
|
2014-12-25 11:14:36 +01:00
|
|
|
this->destructPlugins();
|
2016-01-12 00:38:36 +01:00
|
|
|
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();
|
2016-01-12 00:38:36 +01:00
|
|
|
if(f = this->getFrame())
|
|
|
|
f->destroyHierarchy();
|
2016-01-11 18:22:59 +01:00
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
2014-12-18 17:26:57 +01:00
|
|
|
Clump*
|
2014-12-27 23:18:10 +01:00
|
|
|
Clump::streamRead(Stream *stream)
|
2014-12-18 17:26:57 +01:00
|
|
|
{
|
|
|
|
uint32 length, version;
|
|
|
|
int32 buf[3];
|
|
|
|
Clump *clump;
|
2016-06-17 00:06:37 +02:00
|
|
|
|
|
|
|
if(!findChunk(stream, ID_STRUCT, &length, &version)){
|
|
|
|
RWERROR((ERR_CHUNK, "STRUCT"));
|
|
|
|
return nil;
|
|
|
|
}
|
2016-01-11 18:22:59 +01:00
|
|
|
clump = Clump::create();
|
2016-06-17 00:06:37 +02:00
|
|
|
if(clump == nil)
|
|
|
|
return nil;
|
2014-12-27 23:18:10 +01:00
|
|
|
stream->read(buf, length);
|
2016-01-11 18:22:59 +01:00
|
|
|
int32 numAtomics = buf[0];
|
|
|
|
int32 numLights = 0;
|
2016-01-13 08:58:15 +01:00
|
|
|
int32 numCameras = 0;
|
|
|
|
if(version > 0x33000){
|
2016-01-11 18:22:59 +01:00
|
|
|
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
|
|
|
|
2014-12-25 11:14:36 +01:00
|
|
|
Geometry **geometryList = 0;
|
2015-08-16 23:23:41 +02:00
|
|
|
if(version >= 0x30400){
|
|
|
|
// Geometry list
|
|
|
|
int32 numGeometries = 0;
|
2016-06-17 00:06:37 +02:00
|
|
|
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++){
|
2016-06-17 00:06:37 +02:00
|
|
|
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);
|
2016-06-17 00:06:37 +02:00
|
|
|
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
|
2016-06-17 00:06:37 +02:00
|
|
|
Atomic *a;
|
2016-01-11 18:22:59 +01:00
|
|
|
for(int32 i = 0; i < numAtomics; i++){
|
2016-06-17 00:06:37 +02:00
|
|
|
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;
|
|
|
|
}
|
2016-01-11 18:22:59 +01:00
|
|
|
clump->addAtomic(a);
|
2014-12-18 17:26:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lights
|
2016-01-11 18:22:59 +01:00
|
|
|
for(int32 i = 0; i < numLights; i++){
|
2014-12-18 17:26:57 +01:00
|
|
|
int32 frm;
|
2016-06-17 00:06:37 +02:00
|
|
|
if(!findChunk(stream, ID_STRUCT, nil, nil)){
|
|
|
|
RWERROR((ERR_CHUNK, "STRUCT"));
|
|
|
|
// TODO: free
|
|
|
|
return nil;
|
|
|
|
}
|
2014-12-27 23:18:10 +01:00
|
|
|
frm = stream->readI32();
|
2016-06-17 00:06:37 +02:00
|
|
|
if(!findChunk(stream, ID_LIGHT, nil, nil)){
|
|
|
|
RWERROR((ERR_CHUNK, "LIGHT"));
|
|
|
|
// TODO: free
|
|
|
|
return nil;
|
|
|
|
}
|
2016-01-11 18:22:59 +01:00
|
|
|
Light *l = Light::streamRead(stream);
|
2016-06-17 00:06:37 +02:00
|
|
|
if(l == nil){
|
|
|
|
// TODO: free
|
|
|
|
return nil;
|
|
|
|
}
|
2016-01-11 18:22:59 +01:00
|
|
|
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;
|
2016-06-17 00:06:37 +02:00
|
|
|
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();
|
2016-06-17 00:06:37 +02:00
|
|
|
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);
|
2016-06-17 00:06:37 +02:00
|
|
|
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
|
2014-12-27 23:18:10 +01:00
|
|
|
Clump::streamWrite(Stream *stream)
|
2014-12-18 17:26:57 +01:00
|
|
|
{
|
|
|
|
int size = this->streamGetSize();
|
2015-01-25 22:27:03 +01:00
|
|
|
writeChunkHeader(stream, ID_CLUMP, size);
|
2016-01-11 18:22:59 +01:00
|
|
|
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 };
|
2015-08-15 23:22:51 +02:00
|
|
|
size = version > 0x33000 ? 12 : 4;
|
2015-01-25 22:27:03 +01:00
|
|
|
writeChunkHeader(stream, ID_STRUCT, size);
|
2014-12-27 23:18:10 +01:00
|
|
|
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;
|
2016-01-11 18:22:59 +01:00
|
|
|
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);
|
2016-01-11 18:22:59 +01:00
|
|
|
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
|
|
|
|
2016-01-11 18:22:59 +01:00
|
|
|
FORLIST(lnk, this->atomics)
|
|
|
|
Atomic::fromClump(lnk)->streamWriteClump(stream, flist, numFrames);
|
2014-12-18 17:26:57 +01:00
|
|
|
|
2016-01-11 18:22:59 +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;
|
2015-01-25 22:27:03 +01:00
|
|
|
writeChunkHeader(stream, ID_STRUCT, 4);
|
2014-12-27 23:18:10 +01:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2016-06-17 00:06:37 +02:00
|
|
|
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
|
2015-08-15 23:22:51 +02:00
|
|
|
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;
|
2016-01-11 18:22:59 +01:00
|
|
|
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
|
2016-01-11 18:22:59 +01:00
|
|
|
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
|
2016-01-11 18:22:59 +01:00
|
|
|
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
|
|
|
|
2016-06-17 00:06:37 +02:00
|
|
|
bool
|
2014-12-27 23:18:10 +01:00
|
|
|
Clump::frameListStreamRead(Stream *stream, Frame ***flp, int32 *nf)
|
2014-12-18 17:26:57 +01:00
|
|
|
{
|
|
|
|
FrameStreamData buf;
|
|
|
|
int32 numFrames = 0;
|
2016-06-17 00:06:37 +02:00
|
|
|
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;
|
|
|
|
}
|
2014-12-27 23:18:10 +01:00
|
|
|
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;
|
2016-01-11 18:22:59 +01:00
|
|
|
frameList[i] = f = Frame::create();
|
2014-12-27 23:18:10 +01:00
|
|
|
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;
|
2016-02-13 00:52:32 +01:00
|
|
|
//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;
|
2016-06-17 00:06:37 +02:00
|
|
|
return 1;
|
2014-12-18 17:26:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-27 23:18:10 +01:00
|
|
|
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();
|
|
|
|
|
2015-01-25 22:27:03 +01:00
|
|
|
writeChunkHeader(stream, ID_FRAMELIST, size);
|
|
|
|
writeChunkHeader(stream, ID_STRUCT, structsize);
|
2014-12-27 23:18:10 +01:00
|
|
|
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);
|
2016-02-13 00:52:32 +01:00
|
|
|
buf.matflag = 0; //f->matflag;
|
2014-12-27 23:18:10 +01:00
|
|
|
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
|
|
|
|
2016-01-11 18:22:59 +01:00
|
|
|
Atomic*
|
|
|
|
Atomic::create(void)
|
2014-12-18 17:26:57 +01:00
|
|
|
{
|
2016-01-11 18:22:59 +01:00
|
|
|
Atomic *atomic = (Atomic*)malloc(PluginBase::s_size);
|
2016-06-17 00:06:37 +02:00
|
|
|
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);
|
2016-06-17 00:06:37 +02:00
|
|
|
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;
|
2016-06-17 00:06:37 +02:00
|
|
|
atomic->setFrame(nil);
|
|
|
|
atomic->clump = nil;
|
|
|
|
atomic->pipeline = nil;
|
2016-02-13 00:52:32 +01:00
|
|
|
atomic->renderCB = Atomic::defaultRenderCB;
|
2016-06-16 14:08:09 +02:00
|
|
|
atomic->object.object.flags = Atomic::COLLISIONTEST | Atomic::RENDER;
|
2016-01-11 18:22:59 +01:00
|
|
|
atomic->constructPlugins();
|
|
|
|
return atomic;
|
2016-01-13 08:58:15 +01:00
|
|
|
}
|
2014-12-18 17:26:57 +01:00
|
|
|
|
2016-01-11 18:22:59 +01:00
|
|
|
Atomic*
|
|
|
|
Atomic::clone()
|
2014-12-18 17:26:57 +01:00
|
|
|
{
|
2016-01-11 18:22:59 +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;
|
2016-01-12 00:38:36 +01:00
|
|
|
if(this->geometry){
|
|
|
|
atomic->geometry = this->geometry;
|
|
|
|
atomic->geometry->refCount++;
|
|
|
|
}
|
|
|
|
atomic->pipeline = this->pipeline;
|
2016-01-11 18:22:59 +01:00
|
|
|
atomic->copyPlugins(this);
|
|
|
|
return atomic;
|
2014-12-18 17:26:57 +01:00
|
|
|
}
|
|
|
|
|
2016-01-11 18:22:59 +01:00
|
|
|
void
|
|
|
|
Atomic::destroy(void)
|
2014-12-18 17:26:57 +01:00
|
|
|
{
|
2016-01-11 18:22:59 +01:00
|
|
|
this->destructPlugins();
|
2016-01-12 00:38:36 +01:00
|
|
|
if(this->geometry)
|
|
|
|
this->geometry->destroy();
|
2016-02-17 23:59:05 +01:00
|
|
|
if(this->clump)
|
|
|
|
this->inClump.remove();
|
2016-06-17 00:06:37 +02:00
|
|
|
this->setFrame(nil);
|
2016-01-11 18:22:59 +01:00
|
|
|
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();
|
2016-06-17 00:06:37 +02:00
|
|
|
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*
|
2014-12-27 23:18:10 +01:00
|
|
|
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;
|
2016-06-17 00:06:37 +02:00
|
|
|
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);
|
2016-01-11 18:22:59 +01:00
|
|
|
Atomic *atomic = Atomic::create();
|
2016-06-17 00:06:37 +02:00
|
|
|
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){
|
2016-06-17 00:06:37 +02:00
|
|
|
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);
|
2016-06-17 00:06:37 +02:00
|
|
|
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
|
2014-12-27 23:18:10 +01:00
|
|
|
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;
|
2016-06-17 00:06:37 +02:00
|
|
|
if(c == nil)
|
2014-12-18 17:26:57 +01:00
|
|
|
return false;
|
2015-01-25 22:27:03 +01:00
|
|
|
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{
|
2016-01-11 18:22:59 +01:00
|
|
|
buf[1] = 0;
|
|
|
|
FORLIST(lnk, c->atomics){
|
|
|
|
if(Atomic::fromClump(lnk)->geometry == this->geometry)
|
2015-08-16 23:23:41 +02:00
|
|
|
goto foundgeo;
|
2016-01-11 18:22:59 +01:00
|
|
|
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
|
2016-02-13 00:52:32 +01:00
|
|
|
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;
|
2016-06-17 00:06:37 +02:00
|
|
|
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
|
2015-01-25 22:27:03 +01:00
|
|
|
registerAtomicRightsPlugin(void)
|
2015-01-09 20:17:32 +01:00
|
|
|
{
|
2016-06-17 00:06:37 +02: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
|
|
|
}
|