| 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 | #include <osgDB/ReaderWriter> |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | namespace obj |
|---|
| 32 | { |
|---|
| 33 | |
|---|
| 34 | class Material |
|---|
| 35 | { |
|---|
| 36 | public: |
|---|
| 37 | Material(): |
|---|
| 38 | ambient(0.2f,0.2f,0.2f,1.0f), |
|---|
| 39 | diffuse(0.8f,0.8f,0.8f,1.0f), |
|---|
| 40 | specular(0.0f,0.0f,0.0f,1.0f), |
|---|
| 41 | emissive(0.0f,0.0f,0.0f,1.0f), |
|---|
| 42 | sharpness(0.0f), |
|---|
| 43 | illum(2), |
|---|
| 44 | Tf(0.0f,0.0f,0.0f,1.0f), |
|---|
| 45 | Ni(0), |
|---|
| 46 | Ns(0), |
|---|
| 47 | |
|---|
| 48 | alpha(1.0f) {} |
|---|
| 49 | |
|---|
| 50 | std::string name; |
|---|
| 51 | |
|---|
| 52 | osg::Vec4 ambient; |
|---|
| 53 | osg::Vec4 diffuse; |
|---|
| 54 | osg::Vec4 specular; |
|---|
| 55 | osg::Vec4 emissive; |
|---|
| 56 | float sharpness; |
|---|
| 57 | int illum; |
|---|
| 58 | |
|---|
| 59 | osg::Vec4 Tf; |
|---|
| 60 | int Ni; |
|---|
| 61 | int Ns; |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | float alpha; |
|---|
| 65 | |
|---|
| 66 | class Map |
|---|
| 67 | { |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | public: |
|---|
| 74 | enum TextureMapType { |
|---|
| 75 | DIFFUSE=0, |
|---|
| 76 | OPACITY, |
|---|
| 77 | AMBIENT, |
|---|
| 78 | SPECULAR, |
|---|
| 79 | SPECULAR_EXPONENT, |
|---|
| 80 | BUMP, |
|---|
| 81 | DISPLACEMENT, |
|---|
| 82 | REFLECTION, |
|---|
| 83 | UNKNOWN |
|---|
| 84 | }; |
|---|
| 85 | Map(): |
|---|
| 86 | type(UNKNOWN), |
|---|
| 87 | name(""), |
|---|
| 88 | uScale(1.0f), |
|---|
| 89 | vScale(1.0f), |
|---|
| 90 | uOffset(0.0f), |
|---|
| 91 | vOffset(0.0f), |
|---|
| 92 | clamp(false) {} |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | TextureMapType type; |
|---|
| 96 | std::string name; |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | float uScale; |
|---|
| 102 | float vScale; |
|---|
| 103 | float uOffset; |
|---|
| 104 | float vOffset; |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | bool clamp; |
|---|
| 109 | }; |
|---|
| 110 | |
|---|
| 111 | std::vector<Map> maps; |
|---|
| 112 | |
|---|
| 113 | protected: |
|---|
| 114 | }; |
|---|
| 115 | |
|---|
| 116 | class Element : public osg::Referenced |
|---|
| 117 | { |
|---|
| 118 | public: |
|---|
| 119 | |
|---|
| 120 | typedef std::vector<int> IndexList; |
|---|
| 121 | |
|---|
| 122 | enum DataType |
|---|
| 123 | { |
|---|
| 124 | POINTS, |
|---|
| 125 | POLYLINE, |
|---|
| 126 | POLYGON |
|---|
| 127 | }; |
|---|
| 128 | |
|---|
| 129 | Element(DataType type): |
|---|
| 130 | dataType(type) {} |
|---|
| 131 | |
|---|
| 132 | enum CoordinateCombination |
|---|
| 133 | { |
|---|
| 134 | VERTICES, |
|---|
| 135 | VERTICES_NORMALS, |
|---|
| 136 | VERTICES_TEXCOORDS, |
|---|
| 137 | VERTICES_NORMALS_TEXCOORDS |
|---|
| 138 | }; |
|---|
| 139 | |
|---|
| 140 | CoordinateCombination getCoordinateCombination() const |
|---|
| 141 | { |
|---|
| 142 | if (vertexIndices.size()==normalIndices.size()) |
|---|
| 143 | return (vertexIndices.size()==texCoordIndices.size()) ? VERTICES_NORMALS_TEXCOORDS : VERTICES_NORMALS; |
|---|
| 144 | else |
|---|
| 145 | return (vertexIndices.size()==texCoordIndices.size()) ? VERTICES_TEXCOORDS : VERTICES; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | DataType dataType; |
|---|
| 149 | IndexList vertexIndices; |
|---|
| 150 | IndexList normalIndices; |
|---|
| 151 | IndexList texCoordIndices; |
|---|
| 152 | }; |
|---|
| 153 | |
|---|
| 154 | class ElementState |
|---|
| 155 | { |
|---|
| 156 | public: |
|---|
| 157 | |
|---|
| 158 | ElementState(): |
|---|
| 159 | coordinateCombination(Element::VERTICES), |
|---|
| 160 | smoothingGroup(0) {} |
|---|
| 161 | |
|---|
| 162 | bool operator < (const ElementState& rhs) const |
|---|
| 163 | { |
|---|
| 164 | if (materialName<rhs.materialName) return true; |
|---|
| 165 | else if (rhs.materialName<materialName) return false; |
|---|
| 166 | |
|---|
| 167 | if (objectName<rhs.objectName) return true; |
|---|
| 168 | else if (rhs.objectName<objectName) return false; |
|---|
| 169 | |
|---|
| 170 | if (groupName<rhs.groupName) return true; |
|---|
| 171 | else if (rhs.groupName<groupName) return false; |
|---|
| 172 | |
|---|
| 173 | if (coordinateCombination<rhs.coordinateCombination) return true; |
|---|
| 174 | else if (rhs.coordinateCombination<coordinateCombination) return false; |
|---|
| 175 | |
|---|
| 176 | return (smoothingGroup<rhs.smoothingGroup); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | std::string objectName; |
|---|
| 181 | std::string groupName; |
|---|
| 182 | std::string materialName; |
|---|
| 183 | Element::CoordinateCombination coordinateCombination; |
|---|
| 184 | int smoothingGroup; |
|---|
| 185 | }; |
|---|
| 186 | |
|---|
| 187 | class Model |
|---|
| 188 | { |
|---|
| 189 | public: |
|---|
| 190 | Model(): |
|---|
| 191 | currentElementList(0) {} |
|---|
| 192 | |
|---|
| 193 | void setDatabasePath(const std::string& path) { databasePath = path; } |
|---|
| 194 | const std::string& getDatabasePath() const { return databasePath; } |
|---|
| 195 | |
|---|
| 196 | std::string lastComponent(const char* linep); |
|---|
| 197 | bool readMTL(std::istream& fin); |
|---|
| 198 | bool readOBJ(std::istream& fin, const osgDB::ReaderWriter::Options* options); |
|---|
| 199 | |
|---|
| 200 | bool readline(std::istream& fin, char* line, const int LINE_SIZE); |
|---|
| 201 | void addElement(Element* element); |
|---|
| 202 | |
|---|
| 203 | osg::Vec3 averageNormal(const Element& element) const; |
|---|
| 204 | osg::Vec3 computeNormal(const Element& element) const; |
|---|
| 205 | bool needReverse(const Element& element) const; |
|---|
| 206 | |
|---|
| 207 | int remapVertexIndex(int vi) { return (vi<0) ? vertices.size()+vi : vi-1; } |
|---|
| 208 | int remapNormalIndex(int vi) { return (vi<0) ? normals.size()+vi : vi-1; } |
|---|
| 209 | int remapTexCoordIndex(int vi) { return (vi<0) ? texcoords.size()+vi : vi-1; } |
|---|
| 210 | |
|---|
| 211 | typedef std::map<std::string,Material> MaterialMap; |
|---|
| 212 | typedef std::vector< osg::Vec2 > Vec2Array; |
|---|
| 213 | typedef std::vector< osg::Vec3 > Vec3Array; |
|---|
| 214 | typedef std::vector< osg::ref_ptr<Element> > ElementList; |
|---|
| 215 | typedef std::map< ElementState,ElementList > ElementStateMap; |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | std::string databasePath; |
|---|
| 219 | MaterialMap materialMap; |
|---|
| 220 | |
|---|
| 221 | Vec3Array vertices; |
|---|
| 222 | Vec3Array normals; |
|---|
| 223 | Vec2Array texcoords; |
|---|
| 224 | |
|---|
| 225 | ElementState currentElementState; |
|---|
| 226 | |
|---|
| 227 | ElementStateMap elementStateMap; |
|---|
| 228 | ElementList* currentElementList; |
|---|
| 229 | |
|---|
| 230 | }; |
|---|
| 231 | |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | #endif |
|---|