Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ObjLoader.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Barbarian Group
3  All rights reserved.
4 
5  Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
8  the following conditions are met:
9 
10  * Redistributions of source code must retain the above copyright notice, this list of conditions and
11  the following disclaimer.
12  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
13  the following disclaimer in the documentation and/or other materials provided with the distribution.
14 
15  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
16  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
18  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
19  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22  POSSIBILITY OF SUCH DAMAGE.
23 */
24 
25 #pragma once
26 
27 #include "cinder/TriMesh.h"
28 #include "cinder/Stream.h"
29 
30 #include <boost/logic/tribool.hpp>
31 #include <boost/tuple/tuple_comparison.hpp>
32 #include <map>
33 
34 namespace cinder {
35 
47 class ObjLoader {
48  public:
52  ObjLoader( std::shared_ptr<IStreamCinder> aStream, bool includeUVs = true );
56  ObjLoader( DataSourceRef dataSource, bool includeUVs = true );
60  ObjLoader( DataSourceRef dataSource, DataSourceRef materialSource, bool includeUVs = true );
61  ~ObjLoader();
62 
68  void load( TriMesh *destTriMesh, boost::tribool loadNormals = boost::logic::indeterminate, boost::tribool loadTexCoords = boost::logic::indeterminate, bool optimizeVertices = true );
73  void load( size_t groupIndex, TriMesh *destTriMesh, boost::tribool loadNormals = boost::logic::indeterminate, boost::tribool loadTexCoords = boost::logic::indeterminate, bool optimizeVertices = true );
74 
75  struct Material {
77  Ka[0] = Ka[1] = Ka[2] = 0;
78  Kd[0] = Kd[1] = Kd[2] = 1;
79  }
80 
81  Material( const Material& rhs ) {
82  mName = rhs.mName;
83  Ka[0] = rhs.Ka[0];
84  Ka[1] = rhs.Ka[1];
85  Ka[2] = rhs.Ka[2];
86  Kd[0] = rhs.Kd[0];
87  Kd[1] = rhs.Kd[1];
88  Kd[2] = rhs.Kd[2];
89  }
90 
92  float Ka[3];
93  float Kd[3];
94  };
95 
96  struct Face {
98  std::vector<int> mVertexIndices;
99  std::vector<int> mTexCoordIndices;
100  std::vector<int> mNormalIndices;
102  };
103 
104  struct Group {
107  std::vector<Face> mFaces;
110  };
111 
113  static void write( DataTargetRef dataTarget, const TriMesh &mesh, bool writeNormals = true, bool writeUVs = true );
114 
116  size_t getNumGroups() const { return mGroups.size(); }
117 
119  const std::vector<Group>& getGroups() const { return mGroups; }
120 
121  private:
122  typedef boost::tuple<int,int> VertexPair;
123  typedef boost::tuple<int,int,int> VertexTriple;
124 
125  void parse( bool includeUVs );
126 
127  void parseFace( Group *group, const Material *material, const std::string &s, bool includeUVs );
128  void parseMaterial( std::shared_ptr<IStreamCinder> material );
129  void loadInternalNoOptimize( const Group &group, TriMesh *destTriMesh, bool texCoords, bool normals );
130  void loadInternalNormalsTextures( const Group &group, std::map<boost::tuple<int,int,int>,int> &uniqueVerts, TriMesh *destTriMesh );
131  void loadInternalNormals( const Group &group, std::map<boost::tuple<int,int>,int> &uniqueVerts, TriMesh *destTriMesh );
132  void loadInternalTextures( const Group &group, std::map<boost::tuple<int,int>,int> &uniqueVerts, TriMesh *destTriMesh );
133  void loadInternal( const Group &group, std::map<int,int> &uniqueVerts, TriMesh *destTriMesh );
134 
135  std::shared_ptr<IStreamCinder> mStream;
136  std::vector<Vec3f> mVertices, mNormals;
137  std::vector<Vec2f> mTexCoords;
138  std::vector<Group> mGroups;
139  std::map<std::string, Material> mMaterials;
140 };
141 
142 } // namespace cinder
std::vector< Face > mFaces
Definition: ObjLoader.h:107
int mBaseNormalOffset
Definition: ObjLoader.h:106
GLsizei const GLchar ** string
Definition: GLee.h:2427
Definition: ObjLoader.h:104
const std::vector< Group > & getGroups() const
Returns a vector<> of the Groups in the OBJ.
Definition: ObjLoader.h:119
std::string mName
Definition: ObjLoader.h:105
ObjLoader(std::shared_ptr< IStreamCinder > aStream, bool includeUVs=true)
Definition: ObjLoader.cpp:38
Definition: ObjLoader.h:96
float Kd[3]
Definition: ObjLoader.h:93
static void write(DataTargetRef dataTarget, const TriMesh &mesh, bool writeNormals=true, bool writeUVs=true)
Writes a new OBJ file to dataTarget.
Definition: ObjLoader.cpp:589
std::vector< int > mTexCoordIndices
Definition: ObjLoader.h:99
std::string mName
Definition: ObjLoader.h:91
Loads Alias|Wavefront .OBJ file format.
Definition: ObjLoader.h:47
bool mHasNormals
Definition: ObjLoader.h:109
Material()
Definition: ObjLoader.h:76
float Ka[3]
Definition: ObjLoader.h:92
std::vector< int > mVertexIndices
Definition: ObjLoader.h:98
~ObjLoader()
Definition: ObjLoader.cpp:57
Material(const Material &rhs)
Definition: ObjLoader.h:81
Definition: ObjLoader.h:75
void load(TriMesh *destTriMesh, boost::tribool loadNormals=boost::logic::indeterminate, boost::tribool loadTexCoords=boost::logic::indeterminate, bool optimizeVertices=true)
Definition: ObjLoader.cpp:260
const Material * mMaterial
Definition: ObjLoader.h:101
int mBaseTexCoordOffset
Definition: ObjLoader.h:106
int mBaseVertexOffset
Definition: ObjLoader.h:106
bool mHasTexCoords
Definition: ObjLoader.h:108
std::shared_ptr< class DataTarget > DataTargetRef
Definition: DataTarget.h:33
std::vector< int > mNormalIndices
Definition: ObjLoader.h:100
size_t getNumGroups() const
Returns the total number of groups.
Definition: ObjLoader.h:116
std::shared_ptr< class DataSource > DataSourceRef
Definition: DataSource.h:35
GLdouble s
Definition: GLee.h:1378
int mNumVertices
Definition: ObjLoader.h:97
Definition: TriMesh.h:66