blob: a3eee04e46345d6148771358bba45e13fcde9770 [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#ifndef _OBJ_LOADER_H_
18#define _OBJ_LOADER_H_
19
20#include <vector>
21#include <string>
22#include <iostream>
23#include <fstream>
24
Alex Sakhartchouk13ec73c2011-05-04 18:40:09 -070025#include "GeometryLoader.h"
Alex Sakhartchouk17bd28b2011-02-11 17:51:44 -080026
27using namespace android;
28using namespace android::renderscript;
29
30#define MAX_INDEX 0xffffffff
31
Alex Sakhartchouk13ec73c2011-05-04 18:40:09 -070032class ObjLoader : public GeometryLoader {
Alex Sakhartchouk17bd28b2011-02-11 17:51:44 -080033public:
34 ObjLoader();
Alex Sakhartchouk13ec73c2011-05-04 18:40:09 -070035 virtual ~ObjLoader() {
Alex Sakhartchouk17bd28b2011-02-11 17:51:44 -080036 }
Alex Sakhartchouk13ec73c2011-05-04 18:40:09 -070037 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 {
Alex Sakhartchouk17bd28b2011-02-11 17:51:44 -080043 return mMeshes.size();
44 }
45
Alex Sakhartchouk13ec73c2011-05-04 18:40:09 -070046private:
Alex Sakhartchouk17bd28b2011-02-11 17:51:44 -080047 // .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_