blob: 3535b17cc59ab17bbf6344d918181ec064da31c9 [file] [log] [blame]
Alex Sakhartchouk17bd28b2011-02-11 17:51:44 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <iostream>
18#include <vector>
19
20#include "ColladaLoader.h"
21#include "ObjLoader.h"
Alex Sakhartchoukeb6146e2011-06-02 12:23:52 -070022#include <rsContext.h>
23#include <rsFileA3D.h>
24
25bool rsdAllocationInit(const Context *rsc, Allocation *alloc, bool forceZero) {
26 void * ptr = malloc(alloc->mHal.state.type->getSizeBytes());
27 if (!ptr) {
28 return false;
29 }
30
31 alloc->mHal.drvState.mallocPtr = ptr;
32 if (forceZero) {
33 memset(ptr, 0, alloc->mHal.state.type->getSizeBytes());
34 }
35 return true;
36}
37
38void rsdAllocationDestroy(const Context *rsc, Allocation *alloc) {
39 if (alloc->mHal.drvState.mallocPtr) {
40 free(alloc->mHal.drvState.mallocPtr);
41 alloc->mHal.drvState.mallocPtr = NULL;
42 }
43}
44
45// We only care to implement allocation memory initialization and destruction
46// because we need no other renderscript hal features for serialization
47static RsdHalFunctions FunctionTable = {
48 NULL, NULL, NULL, NULL, NULL, NULL, NULL,
49 { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,NULL },
50 {
51 rsdAllocationInit,
52 rsdAllocationDestroy,
Alex Sakhartchouk2dfe0aa2011-07-14 17:05:56 -070053 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
Alex Sakhartchoukeb6146e2011-06-02 12:23:52 -070054 },
55 { NULL, NULL, NULL }, { NULL, NULL, NULL }, { NULL, NULL, NULL },
56 { NULL, NULL, NULL }, { NULL, NULL, NULL }, { NULL, NULL },
57 { NULL, NULL, NULL},
58};
59
60// No-op initizlizer for rs context hal since we only
61bool rsdHalInit(Context *rsc, uint32_t version_major, uint32_t version_minor) {
62 rsc->mHal.funcs = FunctionTable;
63 return true;
64}
Alex Sakhartchouk13ec73c2011-05-04 18:40:09 -070065
66bool convertToA3D(GeometryLoader *loader, const char *a3dFile) {
67 if (!loader->getNumMeshes()) {
68 return false;
69 }
70 // Now write all this stuff out
Alex Sakhartchoukeb6146e2011-06-02 12:23:52 -070071 Context *rsc = Context::createContextLite();
72 rsdHalInit(rsc, 0, 0);
73 FileA3D file(rsc);
Alex Sakhartchouk13ec73c2011-05-04 18:40:09 -070074
75 for (uint32_t i = 0; i < loader->getNumMeshes(); i ++) {
Alex Sakhartchoukeb6146e2011-06-02 12:23:52 -070076 Mesh *exportedMesh = loader->getMesh(i)->getRsMesh(rsc);
Alex Sakhartchouk13ec73c2011-05-04 18:40:09 -070077 file.appendToFile(exportedMesh);
78 delete exportedMesh;
79 }
80
81 file.writeFile(a3dFile);
Alex Sakhartchoukeb6146e2011-06-02 12:23:52 -070082 delete rsc;
Alex Sakhartchouk13ec73c2011-05-04 18:40:09 -070083 return true;
84}
Alex Sakhartchouk17bd28b2011-02-11 17:51:44 -080085
86int main (int argc, char * const argv[]) {
87 const char *objExt = ".obj";
88 const char *daeExt = ".dae";
89
Alex Sakhartchouk8f6a3312011-08-17 16:43:47 -070090 if(argc != 3 && argc != 4) {
Alex Sakhartchouk17bd28b2011-02-11 17:51:44 -080091 printf("-----------------------------------------------------------------\n");
92 printf("Usage:\n");
93 printf("a3dconvert input_file a3d_output_file\n");
94 printf("Currently .obj and .dae (collada) input files are accepted\n");
95 printf("-----------------------------------------------------------------\n");
96 return 1;
97 }
98
99 bool isSuccessful = false;
100
101 std::string filename = argv[1];
102 size_t dotPos = filename.find_last_of('.');
103 if (dotPos == std::string::npos) {
104 printf("Invalid input. Currently .obj and .dae (collada) input files are accepted\n");
105 return 1;
106 }
107
Alex Sakhartchouk8f6a3312011-08-17 16:43:47 -0700108 bool stripColladaGeo = false;
Alex Sakhartchouk13ec73c2011-05-04 18:40:09 -0700109 GeometryLoader *loader = NULL;
Alex Sakhartchouk17bd28b2011-02-11 17:51:44 -0800110 std::string ext = filename.substr(dotPos);
111 if (ext == daeExt) {
Alex Sakhartchouk13ec73c2011-05-04 18:40:09 -0700112 loader = new ColladaLoader();
Alex Sakhartchouk8f6a3312011-08-17 16:43:47 -0700113 if (argc == 4) {
114 std::string option = argv[3];
115 if (option == "-d") {
116 stripColladaGeo = true;
117 }
118 }
Alex Sakhartchouk17bd28b2011-02-11 17:51:44 -0800119 } else if (ext == objExt) {
Alex Sakhartchouk13ec73c2011-05-04 18:40:09 -0700120 loader = new ObjLoader();
Alex Sakhartchouk17bd28b2011-02-11 17:51:44 -0800121 } else {
122 printf("Invalid input. Currently .obj and .dae (collada) input files are accepted\n");
123 return 1;
124 }
125
Alex Sakhartchouk13ec73c2011-05-04 18:40:09 -0700126 isSuccessful = loader->init(argv[1]);
127 if (isSuccessful) {
128 isSuccessful = convertToA3D(loader, argv[2]);
129 }
130
Alex Sakhartchouk8f6a3312011-08-17 16:43:47 -0700131 if (isSuccessful && stripColladaGeo) {
132 ColladaLoader *colladaLoader = (ColladaLoader*)loader;
133 colladaLoader->stripGeometryAndSave();
134 }
135
Alex Sakhartchouk13ec73c2011-05-04 18:40:09 -0700136 delete loader;
137
Alex Sakhartchouk17bd28b2011-02-11 17:51:44 -0800138 if(isSuccessful) {
139 printf("---All done---\n");
140 } else {
141 printf("---Encountered errors, conversion failed---\n");
142 }
143
144 return isSuccessful ? 0 : 1;
145}