Skip to content

Commit

Permalink
Add support to construct a dspans from geospans
Browse files Browse the repository at this point in the history
  • Loading branch information
branan committed Mar 14, 2011
1 parent 81fcc53 commit d4e2120
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 80 deletions.
102 changes: 101 additions & 1 deletion core/PRP/Geometry/plDrawableSpans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void plDrawableSpans::read(hsStream* S, plResManager* mgr) {
for (size_t i=0; i<fSourceSpans.getSize(); i++)
delete fSourceSpans[i];
fSourceSpans.setSizeNull(S->readInt());
if (fSourceSpans.getSize() > 0)
if (fSourceSpans.getSize() > 0 && !S->getVer().isUniversal())
plDebug::Debug("Reading deprecated SourceSpans");
for (size_t i=0; i<fSourceSpans.getSize(); i++) {
fSourceSpans[i] = new plGeometrySpan();
Expand Down Expand Up @@ -723,3 +723,103 @@ void plDrawableSpans::setSpaceTree(plSpaceTree* tree) {
delete fSpaceTree;
fSpaceTree = tree;
}

void plDrawableSpans::composeGeometry(bool clearspans) {
std::map<unsigned int, std::pair<plGBufferGroup*,size_t> > groups;
for (size_t i=0; i<fGroups.getSize(); i++)
delete fGroups[i];
for (size_t i=0; i<fSpans.getSize(); i++)
delete fSpans[i];

for(size_t i=0; i<fSourceSpans.getSize(); i++) {
plGeometrySpan *span = fSourceSpans[i];
unsigned int format = span->getFormat();
plGBufferGroup *group = groups[format].first;
if(!group) {
group = new plGBufferGroup(format);
groups[format] = std::make_pair(group, fGroups.getSize());
fGroups.append(group);
}
size_t buf_idx = group->getNumVertBuffers();
hsTArray<unsigned short> indices = span->getIndices();
hsTArray<plGeometrySpan::TempVertex> verts = span->getVertices();

plGBufferCell cell;
cell.fColorStart = -1;
cell.fVtxStart = 0;
cell.fLength = verts.getSize();
hsTArray<plGBufferCell> cell_tmp;
cell_tmp.append(cell);

hsTArray<plGBufferVertex> new_verts;
for(size_t j=0; j<verts.getSize(); j++) {
plGeometrySpan::TempVertex v1 = verts[j];
plGBufferVertex v2;
v2.fPos = v1.fPosition;
v2.fNormal = v1.fNormal;
for(size_t k=0; k<8; k++) {
v2.fUVWs[k] = v1.fUVs[k];
}
for(size_t k=0; k<3; k++) {
v2.fSkinWeights[k] = v1.fWeights[k];
}
v2.fSkinIdx = v1.fIndices;
v2.fColor = v1.fColor;
new_verts.append(v2);
}
group->addIndices(indices);
group->addVertices(new_verts);
group->addCells(cell_tmp);

size_t material_idx = fMaterials.find(span->getMaterial());
if(material_idx == (size_t)-1) {
material_idx = fMaterials.getSize();
fMaterials.append(span->getMaterial());
}

plIcicle icicle;
icicle.setIBufferIdx(buf_idx);
icicle.setIStartIdx(0);
icicle.setILength(indices.getSize());
icicle.setVBufferIdx(buf_idx);
icicle.setVStartIdx(0);
icicle.setVLength(verts.getSize());
icicle.setCellIdx(buf_idx);
icicle.setCellOffset(0);
icicle.setLocalToWorld(span->getLocalToWorld());
icicle.setWorldToLocal(span->getWorldToLocal());
icicle.setGroupIdx(groups[format].second);
icicle.setProps(plSpan::deswizzleGeoFlags(span->getProps()));
icicle.setBaseMatrix(span->getBaseMatrix());
icicle.setFogEnvironment(span->getFogEnvironment());
icicle.setLocalBounds(span->getLocalBounds());
icicle.setWorldBounds(span->getWorldBounds());
icicle.setLocalUVWChans(span->getLocalUVWChans());
icicle.setMaxBoneIdx(span->getMaxBoneIdx());
icicle.setMinDist(span->getMinDist());
icicle.setMaxDist(span->getMaxDist());
icicle.setWaterHeight(span->getWaterHeight());
icicle.setPenBoneIdx(span->getPenBoneIdx());
icicle.setNumMatrices(span->getNumMatrices());
addIcicle(icicle);
}

// TODO: delete and rebuild space tree
// delete fSpaceTree;

if(clearspans) {
for(size_t i=0; i<fSourceSpans.getSize(); ++i)
delete fSourceSpans[i];
fSourceSpans.clear();
}
}

size_t plDrawableSpans::buildDIIndex(hsTArray< plGeometrySpan*> spans) {
plDISpanIndex di_idx;
for(size_t i=0; i<spans.getSize(); ++i) {
di_idx.fIndices.append(fSourceSpans.find(spans[i]));
}
size_t result = fDIIndices.getSize();
fDIIndices.append(di_idx);
return result;
}
7 changes: 7 additions & 0 deletions core/PRP/Geometry/plDrawableSpans.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "PRP/Misc/plRenderLevel.h"
#include "PRP/Surface/hsGMaterial.h"
#include "plGBufferGroup.h"
#include "plGeometrySpan.h"
#include "plSpaceTree.h"
#include "plIcicle.h"
#include "plGeometrySpan.h"
Expand Down Expand Up @@ -199,6 +200,12 @@ class PLASMA_DLL plDrawableSpans : public virtual plDrawable {
void setCriteria(unsigned int crit) { fCriteria = crit; }
void setRenderLevel(unsigned int level) { fRenderLevel = level; }
void setSceneNode(plKey node) { fSceneNode = node; }

void composeGeometry(bool clearspans=true);
size_t buildDIIndex(hsTArray<plGeometrySpan*>);

const hsTArray<plGeometrySpan*> getSourceSpans() const { return fSourceSpans; }
hsTArray<plGeometrySpan*> getSourceSpans() { return fSourceSpans; }
};

#endif
106 changes: 36 additions & 70 deletions core/PRP/Geometry/plGeometrySpan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@
#include "plGeometrySpan.h"
#include "Debug/plDebug.h"

plGeometrySpan::plGeometrySpan()
: fVertexData(NULL), fIndexData(NULL), fMultColor(NULL),
fAddColor(NULL), fDiffuseRGBA(NULL), fSpecularRGBA(NULL) { }

plGeometrySpan::~plGeometrySpan() {
delete[] fVertexData;
delete[] fMultColor;
delete[] fAddColor;
delete[] fDiffuseRGBA;
delete[] fSpecularRGBA;
delete[] fIndexData;
}

unsigned int plGeometrySpan::CalcVertexSize(unsigned char format) {
unsigned int size = ((format & kUVCountMask) + 2) * 12;
size += ((format & kSkinWeightMask) >> 4) * sizeof(float);
Expand Down Expand Up @@ -73,41 +60,34 @@ void plGeometrySpan::read(hsStream* S) {
if (fProps & kWaterHeight)
fWaterHeight = S->readFloat();

delete[] fVertexData;
delete[] fMultColor;
delete[] fAddColor;
delete[] fDiffuseRGBA;
delete[] fSpecularRGBA;
delete[] fIndexData;

if (fNumVerts > 0) {
unsigned int stride = CalcVertexSize(fFormat);
fVertexData = new unsigned char[fNumVerts * stride];
S->read(fNumVerts * stride, fVertexData);
fVertexData.setSize(fNumVerts * stride);
S->read(fNumVerts * stride, &(fVertexData[0]));

fMultColor = new hsColorRGBA[fNumVerts];
fAddColor = new hsColorRGBA[fNumVerts];
fMultColor.setSize(fNumVerts);
fAddColor.setSize(fNumVerts);
for (unsigned int i=0; i<fNumVerts; i++) {
fMultColor[i].read(S);
fAddColor[i].read(S);
}
fDiffuseRGBA = new unsigned int[fNumVerts];
fSpecularRGBA = new unsigned int[fNumVerts];
S->readInts(fNumVerts, (uint32_t*)fDiffuseRGBA);
S->readInts(fNumVerts, (uint32_t*)fSpecularRGBA);
fDiffuseRGBA.setSize(fNumVerts);
fSpecularRGBA.setSize(fNumVerts);
S->readInts(fNumVerts, (uint32_t*)&(fDiffuseRGBA[0]));
S->readInts(fNumVerts, (uint32_t*)&(fSpecularRGBA[0]));
} else {
fVertexData = NULL;
fMultColor = NULL;
fAddColor = NULL;
fDiffuseRGBA = NULL;
fSpecularRGBA = NULL;
fVertexData.clear();
fMultColor.clear();
fAddColor.clear();
fDiffuseRGBA.clear();
fSpecularRGBA.clear();
}

if (fNumIndices > 0) {
fIndexData = new unsigned short[fNumIndices];
S->readShorts(fNumIndices, (uint16_t*)fIndexData);
fIndexData.setSize(fNumIndices);
S->readShorts(fNumIndices, (uint16_t*)&(fIndexData[0]));
} else {
fIndexData = NULL;
fIndexData.clear();
}

fInstanceGroup = S->readInt();
Expand Down Expand Up @@ -143,17 +123,17 @@ void plGeometrySpan::write(hsStream* S) {
S->writeFloat(fWaterHeight);

if (fNumVerts > 0) {
S->write(fNumVerts * CalcVertexSize(fFormat), fVertexData);
S->write(fNumVerts * CalcVertexSize(fFormat), &(fVertexData[0]));
for (unsigned int i=0; i<fNumVerts; i++) {
fMultColor[i].write(S);
fAddColor[i].write(S);
}
S->writeInts(fNumVerts, (uint32_t*)fDiffuseRGBA);
S->writeInts(fNumVerts, (uint32_t*)fSpecularRGBA);
S->writeInts(fNumVerts, (uint32_t*)&(fDiffuseRGBA[0]));
S->writeInts(fNumVerts, (uint32_t*)&(fSpecularRGBA[0]));

}
if (fNumIndices > 0)
S->writeShorts(fNumIndices, (uint16_t*)fIndexData);
S->writeShorts(fNumIndices, (uint16_t*)&(fIndexData[0]));

S->writeInt(fInstanceGroup);
if (fInstanceGroup != 0) {
Expand Down Expand Up @@ -266,18 +246,12 @@ void plGeometrySpan::prcParse(const pfPrcTag* tag) {
fDecalLevel = tag->getParam("DecalLevel", "0").toUint();
fWaterHeight = tag->getParam("WaterHeight", "0").toFloat();

delete[] fVertexData;
delete[] fMultColor;
delete[] fAddColor;
delete[] fDiffuseRGBA;
delete[] fSpecularRGBA;
delete[] fIndexData;
fVertexData = NULL;
fMultColor = NULL;
fAddColor = NULL;
fDiffuseRGBA = NULL;
fSpecularRGBA = NULL;
fIndexData = NULL;
fVertexData.clear();
fMultColor.clear();
fAddColor.clear();
fDiffuseRGBA.clear();
fSpecularRGBA.clear();
fIndexData.clear();

const pfPrcTag* child = tag->getFirstChild();
while (child != NULL) {
Expand Down Expand Up @@ -323,7 +297,7 @@ void plGeometrySpan::prcParse(const pfPrcTag* tag) {
} else if (child->getName() == "MultColors") {
if (child->countChildren() != fNumVerts)
throw pfPrcParseException(__FILE__, __LINE__, "Incorrect number of colors");
fMultColor = new hsColorRGBA[fNumVerts];
fMultColor.setSize(fNumVerts);
const pfPrcTag* clrChild = child->getFirstChild();
for (size_t i=0; i<fNumVerts; i++) {
fMultColor[i].prcParse(clrChild);
Expand All @@ -332,7 +306,7 @@ void plGeometrySpan::prcParse(const pfPrcTag* tag) {
} else if (child->getName() == "AddColors") {
if (child->countChildren() != fNumVerts)
throw pfPrcParseException(__FILE__, __LINE__, "Incorrect number of colors");
fAddColor = new hsColorRGBA[fNumVerts];
fAddColor.setSize(fNumVerts);
const pfPrcTag* clrChild = child->getFirstChild();
for (size_t i=0; i<fNumVerts; i++) {
fAddColor[i].prcParse(clrChild);
Expand All @@ -341,7 +315,7 @@ void plGeometrySpan::prcParse(const pfPrcTag* tag) {
} else if (child->getName() == "DiffuseColors") {
if (child->countChildren() != fNumVerts)
throw pfPrcParseException(__FILE__, __LINE__, "Incorrect number of colors");
fDiffuseRGBA = new unsigned int[fNumVerts];
fDiffuseRGBA.setSize(fNumVerts);
const pfPrcTag* clrChild = child->getFirstChild();
for (size_t i=0; i<fNumVerts; i++) {
hsColor32 cl;
Expand All @@ -352,7 +326,7 @@ void plGeometrySpan::prcParse(const pfPrcTag* tag) {
} else if (child->getName() == "SpecularColors") {
if (child->countChildren() != fNumVerts)
throw pfPrcParseException(__FILE__, __LINE__, "Incorrect number of colors");
fSpecularRGBA = new unsigned int[fNumVerts];
fSpecularRGBA.setSize(fNumVerts);
const pfPrcTag* clrChild = child->getFirstChild();
for (size_t i=0; i<fNumVerts; i++) {
hsColor32 cl;
Expand All @@ -362,7 +336,7 @@ void plGeometrySpan::prcParse(const pfPrcTag* tag) {
}
} else if (child->getName() == "Triangles") {
fNumIndices = child->countChildren() * 3;
fIndexData = new unsigned short[fNumIndices];
fIndexData.setSize(fNumIndices);
const pfPrcTag* triChild = child->getFirstChild();
for (size_t i=0; i<fNumIndices; i += 3) {
if (triChild->getName() != "Triangle")
Expand All @@ -389,7 +363,7 @@ hsTArray<plGeometrySpan::TempVertex> plGeometrySpan::getVertices() const {
hsTArray<TempVertex> buf;
buf.setSize(fNumVerts);

unsigned char* cp = fVertexData;
unsigned char* cp = const_cast<unsigned char*>(&(fVertexData[0]));
for (size_t i=0; i<fNumVerts; i++) {
buf[i].fPosition.X = *(float*)cp; cp += sizeof(float);
buf[i].fPosition.Y = *(float*)cp; cp += sizeof(float);
Expand Down Expand Up @@ -419,12 +393,12 @@ hsTArray<plGeometrySpan::TempVertex> plGeometrySpan::getVertices() const {
}

void plGeometrySpan::setVertices(const hsTArray<TempVertex>& verts) {
delete[] fVertexData;
fVertexData.clear();;
unsigned int stride = CalcVertexSize(fFormat);
fNumVerts = verts.getSize();
fVertexData = new unsigned char[fNumVerts * stride];
fVertexData.setSize(fNumVerts * stride);

unsigned char* cp = fVertexData;
unsigned char* cp = const_cast<unsigned char*>(&(fVertexData[0]));
for (size_t i=0; i<fNumVerts; i++) {
*(float*)cp = verts[i].fPosition.X; cp += sizeof(float);
*(float*)cp = verts[i].fPosition.Y; cp += sizeof(float);
Expand Down Expand Up @@ -461,13 +435,5 @@ hsTArray<unsigned short> plGeometrySpan::getIndices() const {
}

void plGeometrySpan::setIndices(const hsTArray<unsigned short>& indices) {
delete[] fIndexData;
fNumIndices = indices.getSize();
if (fNumIndices > 0) {
fIndexData = new unsigned short[fNumIndices];
for (size_t i=0; i<fNumIndices; i++)
fIndexData[i] = indices[i];
} else {
fIndexData = NULL;
}
fIndexData = indices;
}
32 changes: 23 additions & 9 deletions core/PRP/Geometry/plGeometrySpan.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,19 @@ class PLASMA_DLL plGeometrySpan {
float fWaterHeight;
unsigned int fProps;
unsigned int fNumVerts, fNumIndices;
unsigned char* fVertexData;
unsigned short* fIndexData;
hsTArray<unsigned char> fVertexData;
hsTArray<unsigned short> fIndexData;
unsigned int fDecalLevel;
hsColorRGBA* fMultColor;
hsColorRGBA* fAddColor;
unsigned int* fDiffuseRGBA;
unsigned int* fSpecularRGBA;
hsTArray<hsColorRGBA> fMultColor;
hsTArray<hsColorRGBA> fAddColor;
hsTArray<unsigned int> fDiffuseRGBA;
hsTArray<unsigned int> fSpecularRGBA;
unsigned int fInstanceGroup;
hsMatrix44 fLocalToOBB, fOBBToLocal;

unsigned int numInstanceRefs;

public:
plGeometrySpan();
~plGeometrySpan();

static unsigned int CalcVertexSize(unsigned char format);

void read(hsStream* S);
Expand All @@ -117,6 +114,23 @@ class PLASMA_DLL plGeometrySpan {
void setVertices(const hsTArray<TempVertex>& verts);
hsTArray<unsigned short> getIndices() const;
void setIndices(const hsTArray<unsigned short>& indices);

hsMatrix44 getLocalToWorld() const { return fLocalToWorld; }
hsMatrix44 getWorldToLocal() const { return fWorldToLocal; }
hsBounds3Ext getLocalBounds() const { return fLocalBounds; }
hsBounds3Ext getWorldBounds() const { return fWorldBounds; }
plKey getMaterial() const { return fMaterial; }
plKey getFogEnvironment() const { return fFogEnviron; }
float getMinDist() const { return fMinDist; }
float getMaxDist() const { return fMaxDist; }
unsigned int getFormat() const { return fFormat; }
unsigned int getNumMatrices() const { return fNumMatrices; }
unsigned int getProps() const { return fProps; }
unsigned int getBaseMatrix() const { return fBaseMatrix; }
unsigned int getLocalUVWChans() const { return fLocalUVWChans; }
unsigned int getMaxBoneIdx() const { return fMaxBoneIdx; }
unsigned int getWaterHeight() const { return fWaterHeight; }
unsigned int getPenBoneIdx() const { return fPenBoneIdx; }
};

#endif

0 comments on commit d4e2120

Please sign in to comment.