1 /* 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 #ifndef _OBJ_LOADER_H_ 18 #define _OBJ_LOADER_H_ 19 20 #include <vector> 21 #include <string> 22 #include <iostream> 23 #include <fstream> 24 25 #include "GeometryLoader.h" 26 27 using namespace android; 28 using namespace android::renderscript; 29 30 #define MAX_INDEX 0xffffffff 31 32 class ObjLoader : public GeometryLoader { 33 public: 34 ObjLoader(); 35 virtual ~ObjLoader() { 36 } 37 virtual bool init(const char *objFile); 38 39 virtual SimpleMesh *getMesh(uint32_t meshIndex) { 40 return &mMeshes[meshIndex]; 41 } 42 virtual uint32_t getNumMeshes() const { 43 return mMeshes.size(); 44 } 45 46 private: 47 // .obj has a global list of vertex data 48 std::vector<float> mObjPositions; 49 std::vector<float> mObjNormals; 50 std::vector<float> mObjTextureCoords; 51 52 struct PrimitiveVtx { 53 uint32_t vertIdx; 54 uint32_t normIdx; 55 uint32_t texIdx; 56 57 PrimitiveVtx() : vertIdx(MAX_INDEX), 58 normIdx(MAX_INDEX), 59 texIdx(MAX_INDEX){ 60 } 61 }; 62 63 // Scratch buffer for faces 64 std::vector<std::string> mRawFaces; 65 std::vector<PrimitiveVtx> mParsedFaces; 66 std::string mLastMtl; 67 68 // Groups are used to separate multiple meshes within the same .obj file 69 class ObjMesh : public SimpleMesh { 70 public: 71 72 std::vector<std::vector<PrimitiveVtx> > mUnfilteredFaces; 73 74 void appendUnfilteredFaces(std::string name) { 75 appendFaceList(name); 76 mUnfilteredFaces.push_back(std::vector<PrimitiveVtx>()); 77 // Reserve some space for index data 78 static const uint32_t numReserveIndecies = 128; 79 mUnfilteredFaces.back().reserve(numReserveIndecies); 80 } 81 82 ObjMesh() { 83 appendChannel("position", 3); 84 appendChannel("normal", 3); 85 appendChannel("texture0", 2); 86 } 87 }; 88 89 std::vector<ObjMesh> mMeshes; 90 void checkNewMeshCreation(std::string &newGroup); 91 92 void parseRawFaces(); 93 void handleObjLine(char *line); 94 95 void reIndexGeometry(); 96 uint32_t reIndexGeometryPrim(ObjMesh &mesh, PrimitiveVtx &prim); 97 98 unsigned int mPositionsStride; 99 unsigned int mNormalsStride; 100 unsigned int mTextureCoordsStride; 101 102 // This vector is used to remap a position index into a list 103 // of all divergent vertices 104 std::vector<std::vector<unsigned int> > mVertexRemap; 105 }; 106 107 #endif //_OBJ_LOADER_H_ 108