root/OpenSceneGraph/trunk/src/osgPlugins/obj/obj.h @ 13041

Revision 13041, 6.5 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2004 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
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
31namespace obj
32{
33
34class Material
35{
36public:
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        // textureReflection(false),
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;            // read but not implemented (would need specific shaders or state manipulation)
58
59    osg::Vec4   Tf;
60    int         Ni;
61    int         Ns; // shininess 0..1000
62
63    // bool        textureReflection;
64    float       alpha;
65
66    class Map
67    {
68        // -o and -s (offset and scale) options supported for the maps
69        // -clamp <on|off> is supported
70        // -blendu, -blendv, -imfchan, not supported
71        // -mm <base> <gain> is parsed but not actually used
72        // -bm <bump_multiplier> is parsed but not used
73    public:
74        enum TextureMapType {
75            DIFFUSE=0,
76            OPACITY,
77            AMBIENT,
78            SPECULAR,
79            SPECULAR_EXPONENT,
80            BUMP,
81            DISPLACEMENT,
82            REFLECTION,        // read of a reflection map will also apply spherical texgen coordinates
83            UNKNOWN            // UNKNOWN has to be the last
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        // Texture scale and offset, used for creating the texture matrix.
99        // Reader only picks u and v from -s u v w, although all u v and w all need to be specified!
100        // e.g. "map_Kd -s u v w <name>" is OK but "map_Kd -s u v <name>" is not, even though tex is only 2D
101        float       uScale;
102        float       vScale;
103        float       uOffset;
104        float       vOffset;
105
106        // According to the spec, if clamping is off (default), the effect is a texture repeat
107        // if clamping is on, then the effect is a decal texture; i.e. the border is transparent
108        bool        clamp;
109    };
110
111    std::vector<Map> maps;
112
113protected:
114};
115
116class Element : public osg::Referenced
117{
118public:
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
154class ElementState
155{
156public:
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
187class Model
188{
189public:
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
Note: See TracBrowser for help on using the browser.