From 8f6a33136eb116434d5074dfb77f5e9f0f934f4d Mon Sep 17 00:00:00 2001 From: Alex Sakhartchouk Date: Wed, 17 Aug 2011 16:43:47 -0700 Subject: [PATCH] Adding ability to strip geometry from collada files. Fixing element/type creation. Build test. Change-Id: I9f7378db3ce2a3ad102cde4de672648fbfcb6754 --- tools/a3dconvert/ColladaConditioner.cpp | 38 ++++++++++++++++++++++++- tools/a3dconvert/ColladaConditioner.h | 2 ++ tools/a3dconvert/ColladaLoader.cpp | 29 ++++++++++++++++--- tools/a3dconvert/ColladaLoader.h | 3 ++ tools/a3dconvert/SimpleMesh.h | 27 ++++++++++++------ tools/a3dconvert/a3dconvert.cpp | 14 ++++++++- 6 files changed, 98 insertions(+), 15 deletions(-) diff --git a/tools/a3dconvert/ColladaConditioner.cpp b/tools/a3dconvert/ColladaConditioner.cpp index 0b0f694f0..0a99e0ecc 100644 --- a/tools/a3dconvert/ColladaConditioner.cpp +++ b/tools/a3dconvert/ColladaConditioner.cpp @@ -200,4 +200,40 @@ bool ColladaConditioner::triangulate(const char *inputFile) { } return convertSuceeded; -} \ No newline at end of file +} + +bool ColladaConditioner::stripGeometry(DAE *dae) { + bool convertSuceeded = true; + int geometryElementCount = (int)(dae->getDatabase()->getElementCount(NULL, + "library_geometries" )); + + for(int currentGeometry = 0; currentGeometry < geometryElementCount; currentGeometry++) { + + daeElement * element = 0; + int error = dae->getDatabase()->getElement(&element, currentGeometry, + NULL, "library_geometries"); + daeBool removed = daeElement::removeFromParent(element); + convertSuceeded = convertSuceeded && removed; + } + return convertSuceeded; +} + +bool ColladaConditioner::stripGeometry(const char *inputFile) { + DAE dae; + bool convertSuceeded = true; + domCOLLADA* root = dae.open(inputFile); + + if (!root) { + printf("Failed to read file %s.\n", inputFile); + return false; + } + + stripGeometry(&dae); + + dae.writeAll(); + if(!convertSuceeded) { + printf("Encountered errors\n"); + } + + return convertSuceeded; +} diff --git a/tools/a3dconvert/ColladaConditioner.h b/tools/a3dconvert/ColladaConditioner.h index 470b7b6c7..bd5e5c94b 100644 --- a/tools/a3dconvert/ColladaConditioner.h +++ b/tools/a3dconvert/ColladaConditioner.h @@ -23,6 +23,8 @@ private: public: bool triangulate(DAE *dae); bool triangulate(const char *inputFile); + bool stripGeometry(DAE *dae); + bool stripGeometry(const char *inputFile); }; #endif //COLLADA_CONDITIONER diff --git a/tools/a3dconvert/ColladaLoader.cpp b/tools/a3dconvert/ColladaLoader.cpp index 32d625051..3fc954da5 100644 --- a/tools/a3dconvert/ColladaLoader.cpp +++ b/tools/a3dconvert/ColladaLoader.cpp @@ -26,6 +26,9 @@ ColladaLoader::ColladaLoader() { } ColladaLoader::~ColladaLoader() { + if (mDae) { + delete mDae; + } clearGeometry(); } @@ -37,13 +40,16 @@ void ColladaLoader::clearGeometry() { } bool ColladaLoader::init(const char *colladaFile) { - DAE dae; - + if (mDae) { + delete mDae; + } clearGeometry(); + mDae = new DAE(); + bool convertSuceeded = true; - domCOLLADA* root = dae.open(colladaFile); + domCOLLADA* root = mDae->open(colladaFile); if (!root) { fprintf(stderr, "Failed to read file %s.\n", colladaFile); return false; @@ -51,7 +57,7 @@ bool ColladaLoader::init(const char *colladaFile) { // We only want to deal with triangulated meshes since rendering complex polygons is not feasible ColladaConditioner conditioner; - conditioner.triangulate(&dae); + conditioner.triangulate(mDae); domLibrary_geometries *allGeometry = daeSafeCast(root->getDescendant("library_geometries")); @@ -102,3 +108,18 @@ bool ColladaLoader::convertGeometry(domGeometry *geometry) { return convertSuceeded; } + +bool ColladaLoader::stripGeometryAndSave() { + + ColladaConditioner conditioner; + bool convertSuceeded = conditioner.stripGeometry(mDae); + + mDae->writeAll(); + if(!convertSuceeded) { + printf("Encountered errors\n"); + } else { + printf("Stripped geometry data from collada file\n"); + } + + return convertSuceeded; +} diff --git a/tools/a3dconvert/ColladaLoader.h b/tools/a3dconvert/ColladaLoader.h index 3d7bb7d0a..b1a6e3b88 100644 --- a/tools/a3dconvert/ColladaLoader.h +++ b/tools/a3dconvert/ColladaLoader.h @@ -19,6 +19,7 @@ #include +class DAE; class domLibrary_geometries; class domGeometry; class ColladaGeometry; @@ -36,8 +37,10 @@ public: virtual uint32_t getNumMeshes() const { return mGeometries.size(); } + bool stripGeometryAndSave(); private: + DAE *mDae; void clearGeometry(); std::vector mGeometries; diff --git a/tools/a3dconvert/SimpleMesh.h b/tools/a3dconvert/SimpleMesh.h index 15205cb31..c87bb7d60 100644 --- a/tools/a3dconvert/SimpleMesh.h +++ b/tools/a3dconvert/SimpleMesh.h @@ -75,22 +75,28 @@ public: } // Generate the element that describes our channel layout - rsc->mStateElement.elementBuilderBegin(); + Element::Builder vtxBuilder; for (uint32_t c = 0; c < mChannels.size(); c ++) { // Skip empty channels if (mChannels[c].mData.size() == 0) { continue; } - const Element *subElem = Element::create(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, mChannels[c].mStride); - rsc->mStateElement.elementBuilderAdd(subElem, mChannels[c].mName.c_str(), 1); + ObjectBaseRef subElem = Element::createRef(rsc, + RS_TYPE_FLOAT_32, + RS_KIND_USER, + false, + mChannels[c].mStride); + vtxBuilder.add(subElem.get(), mChannels[c].mName.c_str(), 1); } - const Element *vertexDataElem = rsc->mStateElement.elementBuilderCreate(rsc); + ObjectBaseRef vertexDataElem = vtxBuilder.create(rsc); uint32_t numVerts = mChannels[0].mData.size()/mChannels[0].mStride; - Type *vertexDataType = Type::getType(rsc, vertexDataElem, numVerts, 0, 0, false, false); + ObjectBaseRef vertexDataType = Type::getTypeRef(rsc, vertexDataElem.get(), + numVerts, 0, 0, false, false); vertexDataType->compute(); - Allocation *vertexAlloc = Allocation::createAllocation(rsc, vertexDataType, RS_ALLOCATION_USAGE_SCRIPT); + Allocation *vertexAlloc = Allocation::createAllocation(rsc, vertexDataType.get(), + RS_ALLOCATION_USAGE_SCRIPT); uint32_t vertexSize = vertexDataElem->getSizeBytes()/sizeof(float); // Fill this allocation with some data @@ -112,7 +118,8 @@ public: } // Now lets write index data - const Element *indexElem = Element::create(rsc, RS_TYPE_UNSIGNED_16, RS_KIND_USER, false, 1); + ObjectBaseRef indexElem = Element::createRef(rsc, RS_TYPE_UNSIGNED_16, + RS_KIND_USER, false, 1); Mesh *mesh = new Mesh(rsc, 1, mTriangleLists.size()); mesh->setName(mName.c_str()); @@ -122,11 +129,13 @@ public: for (uint32_t pCount = 0; pCount < mTriangleLists.size(); pCount ++) { uint32_t numIndicies = mTriangleLists[pCount].size(); - Type *indexType = Type::getType(rsc, indexElem, numIndicies, 0, 0, false, false ); + ObjectBaseRef indexType = Type::getTypeRef(rsc, indexElem.get(), + numIndicies, 0, 0, false, false ); indexType->compute(); - Allocation *indexAlloc = Allocation::createAllocation(rsc, indexType, RS_ALLOCATION_USAGE_SCRIPT); + Allocation *indexAlloc = Allocation::createAllocation(rsc, indexType.get(), + RS_ALLOCATION_USAGE_SCRIPT); uint16_t *indexPtr = (uint16_t*)indexAlloc->getPtr(); const std::vector &indexList = mTriangleLists[pCount]; uint32_t numTries = numIndicies / 3; diff --git a/tools/a3dconvert/a3dconvert.cpp b/tools/a3dconvert/a3dconvert.cpp index 562af7f5c..3535b17cc 100644 --- a/tools/a3dconvert/a3dconvert.cpp +++ b/tools/a3dconvert/a3dconvert.cpp @@ -87,7 +87,7 @@ int main (int argc, char * const argv[]) { const char *objExt = ".obj"; const char *daeExt = ".dae"; - if(argc != 3) { + if(argc != 3 && argc != 4) { printf("-----------------------------------------------------------------\n"); printf("Usage:\n"); printf("a3dconvert input_file a3d_output_file\n"); @@ -105,10 +105,17 @@ int main (int argc, char * const argv[]) { return 1; } + bool stripColladaGeo = false; GeometryLoader *loader = NULL; std::string ext = filename.substr(dotPos); if (ext == daeExt) { loader = new ColladaLoader(); + if (argc == 4) { + std::string option = argv[3]; + if (option == "-d") { + stripColladaGeo = true; + } + } } else if (ext == objExt) { loader = new ObjLoader(); } else { @@ -121,6 +128,11 @@ int main (int argc, char * const argv[]) { isSuccessful = convertToA3D(loader, argv[2]); } + if (isSuccessful && stripColladaGeo) { + ColladaLoader *colladaLoader = (ColladaLoader*)loader; + colladaLoader->stripGeometryAndSave(); + } + delete loader; if(isSuccessful) {