| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #ifndef OBJ_H |
|---|
| 15 | #define OBJ_H |
|---|
| 16 | |
|---|
| 17 | #include <string> |
|---|
| 18 | #include <vector> |
|---|
| 19 | #include <map> |
|---|
| 20 | #include <istream> |
|---|
| 21 | |
|---|
| 22 | #include <osg/ref_ptr> |
|---|
| 23 | #include <osg/Referenced> |
|---|
| 24 | #include <osg/Vec2> |
|---|
| 25 | #include <osg/Vec3> |
|---|
| 26 | #include <osg/Vec4> |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | namespace obj |
|---|
| 30 | { |
|---|
| 31 | |
|---|
| 32 | class Material |
|---|
| 33 | { |
|---|
| 34 | public: |
|---|
| 35 | Material(): |
|---|
| 36 | ambient(0.2f,0.2f,0.2f,1.0f), |
|---|
| 37 | diffuse(0.8f,0.8f,0.8f,1.0f), |
|---|
| 38 | specular(0.0f,0.0f,0.0f,1.0f), |
|---|
| 39 | emissive(0.0f,0.0f,0.0f,1.0f), |
|---|
| 40 | shininess(0.0f), |
|---|
| 41 | sharpness(0.0f), |
|---|
| 42 | illum(0), |
|---|
| 43 | Tf(0.0f,0.0f,0.0f,1.0f), |
|---|
| 44 | textureReflection(false), |
|---|
| 45 | alpha(1.0f), |
|---|
| 46 | uScale(1.0f), |
|---|
| 47 | vScale(1.0f), |
|---|
| 48 | uOffset(1.0f), |
|---|
| 49 | vOffset(1.0f) {} |
|---|
| 50 | |
|---|
| 51 | std::string name; |
|---|
| 52 | |
|---|
| 53 | osg::Vec4 ambient; |
|---|
| 54 | osg::Vec4 diffuse; |
|---|
| 55 | osg::Vec4 specular; |
|---|
| 56 | osg::Vec4 emissive; |
|---|
| 57 | float shininess; |
|---|
| 58 | float sharpness; |
|---|
| 59 | int illum; |
|---|
| 60 | |
|---|
| 61 | osg::Vec4 Tf; |
|---|
| 62 | int Ni; |
|---|
| 63 | int Ns; |
|---|
| 64 | |
|---|
| 65 | std::string map_Ka; |
|---|
| 66 | std::string map_Kd; |
|---|
| 67 | std::string map_Ks; |
|---|
| 68 | bool textureReflection; |
|---|
| 69 | float alpha; |
|---|
| 70 | float uScale; |
|---|
| 71 | float vScale; |
|---|
| 72 | float uOffset; |
|---|
| 73 | float vOffset; |
|---|
| 74 | |
|---|
| 75 | protected: |
|---|
| 76 | |
|---|
| 77 | }; |
|---|
| 78 | |
|---|
| 79 | class Element : public osg::Referenced |
|---|
| 80 | { |
|---|
| 81 | public: |
|---|
| 82 | |
|---|
| 83 | typedef std::vector<int> IndexList; |
|---|
| 84 | |
|---|
| 85 | enum DataType |
|---|
| 86 | { |
|---|
| 87 | POINTS, |
|---|
| 88 | POLYLINE, |
|---|
| 89 | POLYGON |
|---|
| 90 | }; |
|---|
| 91 | |
|---|
| 92 | Element(DataType type): |
|---|
| 93 | dataType(type) {} |
|---|
| 94 | |
|---|
| 95 | enum CoordinateCombination |
|---|
| 96 | { |
|---|
| 97 | VERTICES, |
|---|
| 98 | VERTICES_NORMALS, |
|---|
| 99 | VERTICES_TEXCOORDS, |
|---|
| 100 | VERTICES_NORMALS_TEXCOORDS |
|---|
| 101 | }; |
|---|
| 102 | |
|---|
| 103 | CoordinateCombination getCoordinateCombination() const |
|---|
| 104 | { |
|---|
| 105 | if (vertexIndices.size()==normalIndices.size()) |
|---|
| 106 | return (vertexIndices.size()==texCoordIndices.size()) ? VERTICES_NORMALS_TEXCOORDS : VERTICES_NORMALS; |
|---|
| 107 | else |
|---|
| 108 | return (vertexIndices.size()==texCoordIndices.size()) ? VERTICES_TEXCOORDS : VERTICES; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | DataType dataType; |
|---|
| 112 | IndexList vertexIndices; |
|---|
| 113 | IndexList normalIndices; |
|---|
| 114 | IndexList texCoordIndices; |
|---|
| 115 | }; |
|---|
| 116 | |
|---|
| 117 | class ElementState |
|---|
| 118 | { |
|---|
| 119 | public: |
|---|
| 120 | |
|---|
| 121 | ElementState(): |
|---|
| 122 | coordinateCombination(Element::VERTICES), |
|---|
| 123 | smoothingGroup(0) {} |
|---|
| 124 | |
|---|
| 125 | bool operator < (const ElementState& rhs) const |
|---|
| 126 | { |
|---|
| 127 | if (materialName<rhs.materialName) return true; |
|---|
| 128 | else if (rhs.materialName<materialName) return false; |
|---|
| 129 | |
|---|
| 130 | if (objectName<rhs.objectName) return true; |
|---|
| 131 | else if (rhs.objectName<objectName) return false; |
|---|
| 132 | |
|---|
| 133 | if (groupName<rhs.groupName) return true; |
|---|
| 134 | else if (rhs.groupName<groupName) return false; |
|---|
| 135 | |
|---|
| 136 | if (coordinateCombination<rhs.coordinateCombination) return true; |
|---|
| 137 | else if (rhs.coordinateCombination<coordinateCombination) return false; |
|---|
| 138 | |
|---|
| 139 | return (smoothingGroup<rhs.smoothingGroup); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | std::string objectName; |
|---|
| 144 | std::string groupName; |
|---|
| 145 | std::string materialName; |
|---|
| 146 | Element::CoordinateCombination coordinateCombination; |
|---|
| 147 | int smoothingGroup; |
|---|
| 148 | }; |
|---|
| 149 | |
|---|
| 150 | class Model |
|---|
| 151 | { |
|---|
| 152 | public: |
|---|
| 153 | Model(): |
|---|
| 154 | currentElementList(0) {} |
|---|
| 155 | |
|---|
| 156 | bool readMTL(std::istream& fin); |
|---|
| 157 | bool readOBJ(std::istream& fin); |
|---|
| 158 | |
|---|
| 159 | bool readline(std::istream& fin, char* line, const int LINE_SIZE); |
|---|
| 160 | void addElement(Element* element); |
|---|
| 161 | |
|---|
| 162 | int remapVertexIndex(int vi) { return (vi<0) ? vertices.size()+vi : vi-1; } |
|---|
| 163 | int remapNormalIndex(int vi) { return (vi<0) ? normals.size()+vi : vi-1; } |
|---|
| 164 | int remapTexCoordIndex(int vi) { return (vi<0) ? texcoords.size()+vi : vi-1; } |
|---|
| 165 | |
|---|
| 166 | typedef std::map<std::string,Material> MaterialMap; |
|---|
| 167 | typedef std::vector< osg::Vec2 > Vec2Array; |
|---|
| 168 | typedef std::vector< osg::Vec3 > Vec3Array; |
|---|
| 169 | typedef std::vector< osg::ref_ptr<Element> > ElementList; |
|---|
| 170 | typedef std::map< ElementState,ElementList > ElementStateMap; |
|---|
| 171 | |
|---|
| 172 | MaterialMap materialMap; |
|---|
| 173 | |
|---|
| 174 | Vec3Array vertices; |
|---|
| 175 | Vec3Array normals; |
|---|
| 176 | Vec2Array texcoords; |
|---|
| 177 | |
|---|
| 178 | ElementState currentElementState; |
|---|
| 179 | |
|---|
| 180 | ElementStateMap elementStateMap; |
|---|
| 181 | ElementList* currentElementList; |
|---|
| 182 | |
|---|
| 183 | }; |
|---|
| 184 | |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | #endif |
|---|