Some refactoring of the code.

Change-Id: I7b2aa13621c9a2e7a952efafbb1aaf4d86461fed
diff --git a/tools/a3dconvert/ColladaGeometry.h b/tools/a3dconvert/ColladaGeometry.h
index 5774997..a89fa69 100644
--- a/tools/a3dconvert/ColladaGeometry.h
+++ b/tools/a3dconvert/ColladaGeometry.h
@@ -27,8 +27,8 @@
     ColladaGeometry();
     bool init(domGeometryRef geometry);
 
-    Mesh *getMesh(Context *rsc) {
-        return mConvertedMesh.getMesh(rsc);
+    SimpleMesh *getMesh() {
+        return &mConvertedMesh;
     }
 
 private:
diff --git a/tools/a3dconvert/ColladaLoader.cpp b/tools/a3dconvert/ColladaLoader.cpp
index 8a74748..32d6250 100644
--- a/tools/a3dconvert/ColladaLoader.cpp
+++ b/tools/a3dconvert/ColladaLoader.cpp
@@ -17,8 +17,6 @@
 #include "ColladaLoader.h"
 #include "ColladaConditioner.h"
 #include "ColladaGeometry.h"
-#include "rsContext.h"
-#include "rsFileA3D.h"
 
 #include <dae.h>
 #include <dom/domCOLLADA.h>
@@ -64,22 +62,8 @@
     return convertSuceeded;
 }
 
-bool ColladaLoader::convertToA3D(const char *a3dFile) {
-    if (mGeometries.size() == 0) {
-        return false;
-    }
-    // Now write all this stuff out
-    Context rsc;
-    FileA3D file(&rsc);
-
-    for (uint32_t i = 0; i < mGeometries.size(); i++) {
-        Mesh *exportedMesh = mGeometries[i]->getMesh(&rsc);
-        file.appendToFile(exportedMesh);
-        delete exportedMesh;
-    }
-
-    file.writeFile(a3dFile);
-    return true;
+SimpleMesh *ColladaLoader::getMesh(uint32_t meshIndex) {
+    return mGeometries[meshIndex]->getMesh();
 }
 
 bool ColladaLoader::convertAllGeometry(domLibrary_geometries *allGeometry) {
diff --git a/tools/a3dconvert/ColladaLoader.h b/tools/a3dconvert/ColladaLoader.h
index aa66e7d..3d7bb7d 100644
--- a/tools/a3dconvert/ColladaLoader.h
+++ b/tools/a3dconvert/ColladaLoader.h
@@ -22,14 +22,20 @@
 class domLibrary_geometries;
 class domGeometry;
 class ColladaGeometry;
+class SimpleMesh;
 
-class ColladaLoader {
+#include "GeometryLoader.h"
+
+class ColladaLoader : public GeometryLoader {
 public:
     ColladaLoader();
-    ~ColladaLoader();
+    virtual ~ColladaLoader();
 
-    bool init(const char *colladaFile);
-    bool convertToA3D(const char *a3dFile);
+    virtual bool init(const char *colladaFile);
+    virtual SimpleMesh *getMesh(uint32_t meshIndex);
+    virtual uint32_t getNumMeshes() const {
+        return mGeometries.size();
+    }
 
 private:
     void clearGeometry();
diff --git a/tools/a3dconvert/GeometryLoader.h b/tools/a3dconvert/GeometryLoader.h
new file mode 100644
index 0000000..e0aca7a
--- /dev/null
+++ b/tools/a3dconvert/GeometryLoader.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _GEOMETRY_LOADER_H_
+#define _GEOMETRY_LOADER_H_
+
+#include "SimpleMesh.h"
+
+class GeometryLoader {
+public:
+    virtual ~GeometryLoader() {
+    }
+    virtual bool init(const char *file) = 0;
+    virtual uint32_t getNumMeshes() const = 0;
+    virtual SimpleMesh *getMesh(uint32_t meshIndex) = 0;
+};
+
+#endif _GEOMETRY_LOADER_H_
diff --git a/tools/a3dconvert/ObjLoader.cpp b/tools/a3dconvert/ObjLoader.cpp
index 4465f4f..19b6e0b 100644
--- a/tools/a3dconvert/ObjLoader.cpp
+++ b/tools/a3dconvert/ObjLoader.cpp
@@ -228,24 +228,6 @@
     return true;
 }
 
-bool ObjLoader::convertToA3D(const char *a3dFile) {
-    if (!getNumMeshes()) {
-        return false;
-    }
-    // Now write all this stuff out
-    Context rsc;
-    FileA3D file(&rsc);
-
-    for (uint32_t i = 0; i < getNumMeshes(); i ++) {
-        Mesh *exportedMesh = getMesh(&rsc, i);
-        file.appendToFile(exportedMesh);
-        delete exportedMesh;
-    }
-
-    file.writeFile(a3dFile);
-    return true;
-}
-
 void ObjLoader::reIndexGeometry() {
     // We want to know where each vertex lands
     mVertexRemap.resize(mObjPositions.size() / mPositionsStride);
diff --git a/tools/a3dconvert/ObjLoader.h b/tools/a3dconvert/ObjLoader.h
index 210b35f..a3eee04 100644
--- a/tools/a3dconvert/ObjLoader.h
+++ b/tools/a3dconvert/ObjLoader.h
@@ -22,28 +22,28 @@
 #include <iostream>
 #include <fstream>
 
-#include "SimpleMesh.h"
-#include <rsContext.h>
+#include "GeometryLoader.h"
 
 using namespace android;
 using namespace android::renderscript;
 
 #define MAX_INDEX 0xffffffff
 
-class ObjLoader {
+class ObjLoader : public GeometryLoader {
 public:
     ObjLoader();
-    bool init(const char *objFile);
-    bool convertToA3D(const char *a3dFile);
-private:
-
-    Mesh *getMesh(Context *rsc, uint32_t meshIndex) {
-        return mMeshes[meshIndex].getMesh(rsc);
+    virtual ~ObjLoader() {
     }
-    uint32_t getNumMeshes() const {
+    virtual bool init(const char *objFile);
+
+    virtual SimpleMesh *getMesh(uint32_t meshIndex) {
+        return &mMeshes[meshIndex];
+    }
+    virtual uint32_t getNumMeshes() const {
         return mMeshes.size();
     }
 
+private:
     // .obj has a global list of vertex data
     std::vector<float> mObjPositions;
     std::vector<float> mObjNormals;
diff --git a/tools/a3dconvert/SimpleMesh.h b/tools/a3dconvert/SimpleMesh.h
index 57e1a7c..4efd838 100644
--- a/tools/a3dconvert/SimpleMesh.h
+++ b/tools/a3dconvert/SimpleMesh.h
@@ -19,6 +19,7 @@
 
 #include <rsContext.h>
 #include <rsMesh.h>
+#include <string>
 using namespace android;
 using namespace android::renderscript;
 
@@ -68,7 +69,7 @@
     }
 
     // Generates a renderscript mesh that could be used for a3d serialization
-    Mesh *getMesh(Context *rsc) {
+    Mesh *getRsMesh(Context *rsc) {
         if (mChannels.size() == 0) {
             return NULL;
         }
@@ -113,19 +114,12 @@
         // Now lets write index data
         const Element *indexElem = Element::create(rsc, RS_TYPE_UNSIGNED_16, RS_KIND_USER, false, 1);
 
-        Mesh *mesh = new Mesh(rsc);
+        Mesh *mesh = new Mesh(rsc, 1, mTriangleLists.size());
         mesh->setName(mName.c_str());
-        mesh->mVertexBufferCount = 1;
-        mesh->mVertexBuffers = new ObjectBaseRef<Allocation>[1];
-        mesh->mVertexBuffers[0].set(vertexAlloc);
-
-        mesh->mPrimitivesCount = mTriangleLists.size();
-        mesh->mPrimitives = new Mesh::Primitive_t *[mesh->mPrimitivesCount];
+        mesh->setVertexBuffer(vertexAlloc, 0);
 
         // load all primitives
-        for (uint32_t pCount = 0; pCount < mesh->mPrimitivesCount; pCount ++) {
-            Mesh::Primitive_t *prim = new Mesh::Primitive_t;
-            mesh->mPrimitives[pCount] = prim;
+        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 );
@@ -143,8 +137,7 @@
                 indexPtr[i * 3 + 2] = (uint16_t)indexList[i * 3 + 2];
             }
             indexAlloc->setName(mTriangleListNames[pCount].c_str());
-            prim->mIndexBuffer.set(indexAlloc);
-            prim->mPrimitive = RS_PRIMITIVE_TRIANGLE;
+            mesh->setPrimitive(indexAlloc, RS_PRIMITIVE_TRIANGLE, pCount);
         }
 
         return mesh;
diff --git a/tools/a3dconvert/a3dconvert.cpp b/tools/a3dconvert/a3dconvert.cpp
index 33f5733..893df10 100644
--- a/tools/a3dconvert/a3dconvert.cpp
+++ b/tools/a3dconvert/a3dconvert.cpp
@@ -19,6 +19,26 @@
 
 #include "ColladaLoader.h"
 #include "ObjLoader.h"
+#include "rsContext.h"
+#include "rsFileA3D.h"
+
+bool convertToA3D(GeometryLoader *loader, const char *a3dFile) {
+    if (!loader->getNumMeshes()) {
+        return false;
+    }
+    // Now write all this stuff out
+    Context rsc;
+    FileA3D file(&rsc);
+
+    for (uint32_t i = 0; i < loader->getNumMeshes(); i ++) {
+        Mesh *exportedMesh = loader->getMesh(i)->getRsMesh(&rsc);
+        file.appendToFile(exportedMesh);
+        delete exportedMesh;
+    }
+
+    file.writeFile(a3dFile);
+    return true;
+}
 
 int main (int argc, char * const argv[]) {
     const char *objExt = ".obj";
@@ -42,24 +62,24 @@
         return 1;
     }
 
+    GeometryLoader *loader = NULL;
     std::string ext = filename.substr(dotPos);
     if (ext == daeExt) {
-        ColladaLoader converter;
-        isSuccessful = converter.init(argv[1]);
-        if (isSuccessful) {
-            isSuccessful = converter.convertToA3D(argv[2]);
-        }
+        loader = new ColladaLoader();
     } else if (ext == objExt) {
-        ObjLoader objConv;
-        isSuccessful = objConv.init(argv[1]);
-        if (isSuccessful) {
-            isSuccessful = objConv.convertToA3D(argv[2]);
-        }
+        loader = new ObjLoader();
     } else {
         printf("Invalid input. Currently .obj and .dae (collada) input files are accepted\n");
         return 1;
     }
 
+    isSuccessful = loader->init(argv[1]);
+    if (isSuccessful) {
+        isSuccessful = convertToA3D(loader, argv[2]);
+    }
+
+    delete loader;
+
     if(isSuccessful) {
         printf("---All done---\n");
     } else {