Some refactoring of the code.

Change-Id: I7b2aa13621c9a2e7a952efafbb1aaf4d86461fed
This commit is contained in:
Alex Sakhartchouk
2011-05-04 18:40:09 -07:00
parent d78fc7acbc
commit 13ec73c22c
8 changed files with 91 additions and 75 deletions

View File

@@ -27,8 +27,8 @@ public:
ColladaGeometry();
bool init(domGeometryRef geometry);
Mesh *getMesh(Context *rsc) {
return mConvertedMesh.getMesh(rsc);
SimpleMesh *getMesh() {
return &mConvertedMesh;
}
private:

View File

@@ -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 @@ bool ColladaLoader::init(const char *colladaFile) {
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) {

View File

@@ -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();

View File

@@ -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_

View File

@@ -228,24 +228,6 @@ bool ObjLoader::init(const char *fileName) {
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);

View File

@@ -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;

View File

@@ -19,6 +19,7 @@
#include <rsContext.h>
#include <rsMesh.h>
#include <string>
using namespace android;
using namespace android::renderscript;
@@ -68,7 +69,7 @@ public:
}
// 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 @@ public:
// 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 @@ public:
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;

View File

@@ -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 @@ int main (int argc, char * const argv[]) {
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 {