1 //
2 //   Copyright 2013 Pixar
3 //
4 //   Licensed under the Apache License, Version 2.0 (the "Apache License")
5 //   with the following modification; you may not use this file except in
6 //   compliance with the Apache License and the following modification to it:
7 //   Section 6. Trademarks. is deleted and replaced with:
8 //
9 //   6. Trademarks. This License does not grant permission to use the trade
10 //      names, trademarks, service marks, or product names of the Licensor
11 //      and its affiliates, except as required to comply with Section 4(c) of
12 //      the License and to reproduce the content of the NOTICE file.
13 //
14 //   You may obtain a copy of the Apache License at
15 //
16 //       http://www.apache.org/licenses/LICENSE-2.0
17 //
18 //   Unless required by applicable law or agreed to in writing, software
19 //   distributed under the Apache License with the above modification is
20 //   distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 //   KIND, either express or implied. See the Apache License for the specific
22 //   language governing permissions and limitations under the Apache License.
23 //
24 
25 #ifndef SHAPE_UTILS_H
26 #define SHAPE_UTILS_H
27 
28 #include <string>
29 #include <vector>
30 #include <map>
31 
32 //------------------------------------------------------------------------------
33 
34 enum Scheme {
35   kBilinear=0,
36   kCatmark,
37   kLoop
38 };
39 
40 struct ShapeDesc
41 {
42     ShapeDesc(char const * iname, std::string const & idata, Scheme ischeme,
43         bool iIsLeftHanded = false) :
nameShapeDesc44         name(iname), data(idata), scheme(ischeme), isLeftHanded(iIsLeftHanded)
45 	{ }
46 
47     std::string name;
48     std::string data;
49     Scheme      scheme;
50     bool        isLeftHanded;
51 };
52 
53 //------------------------------------------------------------------------------
54 
55 struct Shape {
56     // full(er) spec here: http://paulbourke.net/dataformats/mtl/
57     struct material {
58 
59         material();
60 
61         std::string name;
62 
63         float ka[3],         // ambient
64               kd[3],         // diffuse
65               ks[3],         // specular
66               ns,            // specular exponent
67               ni,            // optical density (1.0=no refraction, glass=1.5)
68               sharpness,     // reflection sharpness
69               tf[3],         // transmission filter
70               d;             // dissolve factor (1.0 = opaque)
71 
72         int illum;
73     };
74 
75     struct tag {
76 
77         static tag * parseTag(char const * stream);
78 
79         std::string genTag() const;
80 
81         std::string              name;
82         std::vector<int>         intargs;
83         std::vector<float>       floatargs;
84         std::vector<std::string> stringargs;
85     };
86 
87     static Shape * parseObj(ShapeDesc const & shapeDesc, bool parsemtl=false);
88     static Shape * parseObj(char const * shapeString, Scheme shapeScheme,
89                             bool isLeftHanded=false, bool parsemtl=false);
90 
91     void parseMtllib(char const * stream);
92 
93     std::string genShape(char const * name) const;
94 
95     std::string genObj() const;
96 
97     std::string genRIB() const;
98 
ShapeShape99     Shape() : scheme(kCatmark), isLeftHanded(false) { }
100 
101     ~Shape();
102 
GetNumVerticesShape103     int GetNumVertices() const { return (int)verts.size()/3; }
104 
GetNumFacesShape105     int GetNumFaces() const { return (int)nvertsPerFace.size(); }
106 
HasUVShape107     bool HasUV() const { return ! (uvs.empty() || faceuvs.empty()); }
108 
GetFVarWidthShape109     int GetFVarWidth() const { return HasUV() ? 2 : 0; }
110 
111     std::vector<float>      verts;
112     std::vector<float>      uvs;
113     std::vector<float>      normals;
114     std::vector<int>        nvertsPerFace;
115     std::vector<int>        faceverts;
116     std::vector<int>        faceuvs;
117     std::vector<int>        facenormals;
118     std::vector<tag *>      tags;
119     Scheme                  scheme;
120     bool                    isLeftHanded;
121 
FindMaterialShape122     char FindMaterial(char const * name) {
123         for (int i=0; i<(int)mtls.size(); ++i) {
124             if (mtls[i]->name==name) {
125                 return i;
126             }
127         }
128         return -1;
129     }
130 
131     std::string                 mtllib;
132     std::vector<unsigned short> mtlbind;
133     std::vector<material *>     mtls;
134 };
135 
136 //------------------------------------------------------------------------------
137 
138 #endif /* SHAPE_UTILS_H */
139