Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
ioOBJ.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #if !defined(LoadOBJ_INCLUDE_ONCE)
33 #define LoadOBJ_INCLUDE_ONCE
34 
35 #include <vlCore/String.hpp>
36 #include <vlGraphics/Geometry.hpp>
39 #include <map>
40 
41 namespace vl
42 {
43  class VirtualFile;
44 
45 //-----------------------------------------------------------------------------
47  VLGRAPHICS_EXPORT ref<ResourceDatabase> loadOBJ( const String& path );
48 //-----------------------------------------------------------------------------
50  VLGRAPHICS_EXPORT ref<ResourceDatabase> loadOBJ( VirtualFile* file );
51 //---------------------------------------------------------------------------
52 // LoadWriterOBJ
53 //---------------------------------------------------------------------------
58  {
60 
61  public:
62  LoadWriterOBJ(): ResourceLoadWriter("|obj|", "|obj|") {}
63 
64  void registerLoadWriter();
65 
67  {
68  return loadOBJ(path);
69  }
70 
72  {
73  return loadOBJ(file);
74  }
75 
77  bool writeResource(const String& /*path*/, ResourceDatabase* /*resource*/) const
78  {
79  return false;
80  }
81 
83  bool writeResource(VirtualFile* /*file*/, ResourceDatabase* /*resource*/) const
84  {
85  return false;
86  }
87  };
88 //-----------------------------------------------------------------------------
89 // ObjTexture
90 //-----------------------------------------------------------------------------
93  {
94  public:
95  ObjTexture();
96  const ObjTexture& parseLine(const String& line, const String& file);
97  bool valid() const { return !mFileName.empty(); }
99  void print();
100 
102  const String& path() const { return mFileName; }
104  bool blendU() const { return mBlendU; }
106  bool blendV() const { return mBlendV; }
108  bool cc() const { return mCC; }
110  bool clamp() const { return mClamp; }
112  float mm_Base() const { return mMM_Base; }
114  float mm_Gain() const { return mMM_Gain; }
116  const fvec3& o_UVW() const { return mO_UVW; }
118  const fvec3& s_UVW() const { return mS_UVW; }
120  const fvec3& t_UVW() const { return mT_UVW; }
122  float texres_Value() const { return mTexres_Value; }
124  char imfchan() const { return mImfchan; }
126  float bm() const { return mBM; }
127 
129  void setPath(const String& filename) { mFileName = filename; }
131  void setBlendU(bool on) { mBlendU = on; }
133  void setBlendV(bool on) { mBlendV = on; }
135  void setCC(bool on) { mCC = on; }
137  void setClamp(bool on) { mClamp = on; }
139  void setMM_Base(float base) { mMM_Base = base; }
141  void setMM_Gain(float gain) { mMM_Gain = gain; }
143  void setO_UVW(const fvec3& o_uvw) { mO_UVW = o_uvw; }
145  void setS_UVW(const fvec3& s_uvw) { mS_UVW = s_uvw; }
147  void setT_UVW(const fvec3& t_uvw) { mT_UVW = t_uvw; }
149  void setTexres_Value(float value) { mTexres_Value = value; }
151  void setImfchan(char val) { mImfchan = val; }
153  void setBM(float bm) { mBM = bm; }
154 
155  protected:
159  bool mBlendU;
161  bool mBlendV;
163  bool mCC;
165  bool mClamp;
167  float mMM_Base;
169  float mMM_Gain;
179  char mImfchan;
181  float mBM;
182  };
183 //-----------------------------------------------------------------------------
184 // ObjMaterial
185 //-----------------------------------------------------------------------------
187  class ObjMaterial: public Object
188  {
190 
191  public:
192  ObjMaterial(): mTr(1.0f), mNs(0.0f), mIllum(0), mNi(1.0f) {}
193 
195  const fvec3& ka() const { return mKa; }
197  const fvec3& kd() const { return mKd; }
199  const fvec3& ks() const { return mKs; }
201  const fvec3& ke() const { return mKe; }
203  float tr() const { return mTr; }
205  float ns() const { return mNs; }
207  int illum() const { return mIllum; }
209  float ni() const { return mNi; }
211  const ObjTexture& map_Kd() const { return mMap_Kd; }
213  const ObjTexture& map_Ka() const { return mMap_Ka; }
215  const ObjTexture& map_Ks() const { return mMap_Ks; }
217  const ObjTexture& map_Ns() const { return mMap_Ns; }
219  const ObjTexture& map_d() const { return mMap_d; }
221  const ObjTexture& map_Decal() const { return mMap_Decal; }
223  const ObjTexture& map_Disp() const { return mMap_Disp; }
225  const ObjTexture& map_Bump() const { return mMap_Bump; }
226 
228  void setKa(const fvec3& ka) { mKa = ka; }
230  void setKd(const fvec3& kd) { mKd = kd; }
232  void setKs(const fvec3& ks) { mKs = ks; }
234  void setKe(const fvec3& ke) { mKe = ke; }
236  void setTr(float tr) { mTr = tr; }
238  void setNs(float ns) { mNs = ns; }
240  void setIllum(int illum) { mIllum = illum; }
242  void setNi(float ni) { mNi = ni; }
244  void setMap_Kd(const ObjTexture& map_kd) { mMap_Kd = map_kd; }
246  void setMap_Ka(const ObjTexture& map_ka) { mMap_Ka = map_ka; }
248  void setMap_Ks(const ObjTexture& map_ks) { mMap_Ks = map_ks; }
250  void setMap_Ns(const ObjTexture& map_ns) { mMap_Ns = map_ns; }
252  void setMap_d(const ObjTexture& map_d) { mMap_d = map_d; }
254  void setMap_Decal(const ObjTexture& map_decal) { mMap_Decal = map_decal; }
256  void setMap_Disp(const ObjTexture& map_disp) { mMap_Disp = map_disp; }
258  void setMap_Bump(const ObjTexture& map_bump) { mMap_Bump = map_bump; }
259 
260  protected:
270  float mTr;
272  float mNs;
274  int mIllum;
276  float mNi;
293  };
294 //-----------------------------------------------------------------------------
295 // ObjMesh
296 //-----------------------------------------------------------------------------
298  class ObjMesh: public Object
299  {
301 
302  public:
304  void setMaterial(ObjMaterial* mat) { mMaterial = mat; }
305 
307  const ObjMaterial* material() const { return mMaterial.get(); }
309  const std::vector<int>& facePositionIndex() const { return mFace_icoords; }
311  const std::vector<int>& faceNormalIndex() const { return mFace_inormals; }
313  const std::vector<int>& faceTexCoordIndex() const { return mFace_itexcoords; }
316  const std::vector<int>& face_type() const { return mFace_type; }
317 
319  ObjMaterial* material() { return mMaterial.get(); }
321  std::vector<int>& facePositionIndex() { return mFace_icoords; }
323  std::vector<int>& faceNormalIndex() { return mFace_inormals; }
325  std::vector<int>& faceTexCoordIndex() { return mFace_itexcoords; }
328  std::vector<int>& face_type() { return mFace_type; }
329 
330  protected:
332  std::vector<int> mFace_icoords;
333  std::vector<int> mFace_inormals;
334  std::vector<int> mFace_itexcoords;
335  std::vector<int> mFace_type;
336  };
337 //-----------------------------------------------------------------------------
338 // ObjLoader
339 //-----------------------------------------------------------------------------
341  class ObjLoader
342  {
343  public:
344  const std::vector<fvec4>& vertexArray() const { return mCoords; }
345  const std::vector<fvec3>& normalArray() const { return mNormals; }
346  const std::vector<fvec3>& texCoordsArray() const { return mTexCoords; }
347  const std::map< std::string, ref<ObjMaterial> >& materials() const { return mMaterials; }
348  const std::vector< ref<ObjMesh> >& meshes() const { return mMeshes; }
349 
350  std::vector<fvec4>& vertexArray() { return mCoords; }
351  std::vector<fvec3>& normalArray() { return mNormals; }
352  std::vector<fvec3>& texCoordsArray() { return mTexCoords; }
353  std::map< std::string, ref<ObjMaterial> >& materials() { return mMaterials; }
354  std::vector< ref<ObjMesh> >& meshes() { return mMeshes; }
355 
359 
363  void loadObjMaterials(VirtualFile* file, std::vector<ObjMaterial>& materials );
364 
365  protected:
366  std::vector<fvec4> mCoords;
367  std::vector<fvec3> mNormals;
368  std::vector<fvec3> mTexCoords;
369  std::map< std::string, ref<ObjMaterial> > mMaterials;
370  std::vector< ref<ObjMesh> > mMeshes;
371  };
372 //-----------------------------------------------------------------------------
373 }
374 
375 #endif
float ns() const
Ns - specular exponent.
Definition: ioOBJ.hpp:205
VLGRAPHICS_EXPORT ref< ResourceDatabase > loadOBJ(const String &path)
Loads a Wavefront OBJ file. See also ObjLoader.
Definition: ioOBJ.cpp:926
void setMM_Base(float base)
-mm base gain
Definition: ioOBJ.hpp:139
std::vector< fvec4 > & vertexArray()
Definition: ioOBJ.hpp:350
ref< ObjMaterial > mMaterial
Definition: ioOBJ.hpp:331
int illum() const
illum - illumination model
Definition: ioOBJ.hpp:207
const std::vector< int > & faceTexCoordIndex() const
Index into ObjLoader::texCoordsArray() vector.
Definition: ioOBJ.hpp:313
std::vector< fvec4 > mCoords
Definition: ioOBJ.hpp:366
std::vector< int > & faceNormalIndex()
Index into ObjLoader::normalArray() vector.
Definition: ioOBJ.hpp:323
void setClamp(bool on)
-clamp on | off
Definition: ioOBJ.hpp:137
const std::map< std::string, ref< ObjMaterial > > & materials() const
Definition: ioOBJ.hpp:347
void setO_UVW(const fvec3 &o_uvw)
-o u v w
Definition: ioOBJ.hpp:143
const fvec3 & ka() const
Ka - ambient color.
Definition: ioOBJ.hpp:195
float mm_Base() const
-mm base gain
Definition: ioOBJ.hpp:112
fvec3 mKe
Ke - emissive color.
Definition: ioOBJ.hpp:268
bool clamp() const
-clamp on | off
Definition: ioOBJ.hpp:110
float tr() const
Tr/d - transparency.
Definition: ioOBJ.hpp:203
std::vector< ref< ObjMesh > > & meshes()
Definition: ioOBJ.hpp:354
ObjTexture mMap_Bump
bump - bump map
Definition: ioOBJ.hpp:292
void setNi(float ni)
Ni - optical density / index of refraction.
Definition: ioOBJ.hpp:242
fvec3 mT_UVW
-t u v w
Definition: ioOBJ.hpp:175
An abstract class representing a file.
Definition: VirtualFile.hpp:60
Loads a Wavefront OBJ file.
Definition: ioOBJ.hpp:341
bool mClamp
-clamp on | off
Definition: ioOBJ.hpp:165
ref< ResourceDatabase > loadResource(const String &path) const
Definition: ioOBJ.hpp:66
std::vector< int > & face_type()
Each entry represents a face, the number represents how many vertices the face has.
Definition: ioOBJ.hpp:328
const ObjTexture & map_Disp() const
disp - displace map
Definition: ioOBJ.hpp:223
void setImfchan(char val)
-imfchan r | g | b | m | l | z
Definition: ioOBJ.hpp:151
fvec3 mKa
Ka - ambient color.
Definition: ioOBJ.hpp:262
fvec3 mS_UVW
-s u v w
Definition: ioOBJ.hpp:173
void setMap_d(const ObjTexture &map_d)
map_d - transparency map
Definition: ioOBJ.hpp:252
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
void setMap_Ks(const ObjTexture &map_ks)
map_Ks - specular color map
Definition: ioOBJ.hpp:248
void setMap_Kd(const ObjTexture &map_kd)
map_Kd - ambient diffuse
Definition: ioOBJ.hpp:244
const fvec3 & kd() const
Kd - diffuse color.
Definition: ioOBJ.hpp:197
ref< ResourceDatabase > loadResource(VirtualFile *file) const
Definition: ioOBJ.hpp:71
ObjTexture mMap_Ns
map_Ns - specular exponent map
Definition: ioOBJ.hpp:284
const std::vector< int > & faceNormalIndex() const
Index into ObjLoader::normalArray() vector.
Definition: ioOBJ.hpp:311
std::vector< ref< ObjMesh > > mMeshes
Definition: ioOBJ.hpp:370
fvec3 mKd
Kd - diffuse color.
Definition: ioOBJ.hpp:264
String mFileName
Texture file name.
Definition: ioOBJ.hpp:157
std::vector< fvec3 > & normalArray()
Definition: ioOBJ.hpp:351
void setBlendU(bool on)
-blendu on | off
Definition: ioOBJ.hpp:131
bool mBlendU
-blendu on | off
Definition: ioOBJ.hpp:159
bool mBlendV
-blendv on | off
Definition: ioOBJ.hpp:161
void setMap_Decal(const ObjTexture &map_decal)
decal - decal map
Definition: ioOBJ.hpp:254
const std::vector< fvec3 > & normalArray() const
Definition: ioOBJ.hpp:345
float mNi
Ni - optical density / index of refraction.
Definition: ioOBJ.hpp:276
std::map< std::string, ref< ObjMaterial > > & materials()
Definition: ioOBJ.hpp:353
float texres_Value() const
-texres value
Definition: ioOBJ.hpp:122
bool mCC
-cc on | off
Definition: ioOBJ.hpp:163
const ObjTexture & map_Ks() const
map_Ks - specular color map
Definition: ioOBJ.hpp:215
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
const String & path() const
Texture file name.
Definition: ioOBJ.hpp:102
std::vector< int > mFace_itexcoords
Definition: ioOBJ.hpp:334
void setKe(const fvec3 &ke)
Ke - emissive color.
Definition: ioOBJ.hpp:234
void setBlendV(bool on)
-blendv on | off
Definition: ioOBJ.hpp:133
float mMM_Gain
-mm base gain
Definition: ioOBJ.hpp:169
Visualization Library main namespace.
float mTexres_Value
-texres value
Definition: ioOBJ.hpp:177
Represents a Wavefront OBJ mesh. See also ObjLoader.
Definition: ioOBJ.hpp:298
const ObjTexture & map_Ka() const
map_Ka - ambient color map
Definition: ioOBJ.hpp:213
char imfchan() const
-imfchan r | g | b | m | l | z
Definition: ioOBJ.hpp:124
const std::vector< fvec3 > & texCoordsArray() const
Definition: ioOBJ.hpp:346
void setMap_Bump(const ObjTexture &map_bump)
bump - bump map
Definition: ioOBJ.hpp:258
const fvec3 & ke() const
Ke - emissive color.
Definition: ioOBJ.hpp:201
float mNs
Ns - specular exponent.
Definition: ioOBJ.hpp:272
std::vector< int > & faceTexCoordIndex()
Index into ObjLoader::texCoordsArray() vector.
Definition: ioOBJ.hpp:325
float mBM
-bm mult
Definition: ioOBJ.hpp:181
const ObjTexture & map_Bump() const
bump - bump map
Definition: ioOBJ.hpp:225
The LoadWriterOBJ class is a ResourceLoadWriter capable of reading OBJ files.
Definition: ioOBJ.hpp:57
ObjTexture mMap_Decal
decal - decal map
Definition: ioOBJ.hpp:288
void setKa(const fvec3 &ka)
Ka - ambient color.
Definition: ioOBJ.hpp:228
The base class for all the reference counted objects.
Definition: Object.hpp:158
Represents a Wavefront OBJ texture. See also ObjMaterial and ObjLoader.
Definition: ioOBJ.hpp:92
fvec3 mO_UVW
-o u v w
Definition: ioOBJ.hpp:171
void setKs(const fvec3 &ks)
Ks - specular color.
Definition: ioOBJ.hpp:232
ObjTexture mMap_Kd
map_Kd - ambient diffuse
Definition: ioOBJ.hpp:278
float mm_Gain() const
-mm base gain
Definition: ioOBJ.hpp:114
ObjTexture mMap_Ka
map_Ka - ambient color map
Definition: ioOBJ.hpp:280
void setNs(float ns)
Ns - specular exponent.
Definition: ioOBJ.hpp:238
std::map< std::string, ref< ObjMaterial > > mMaterials
Definition: ioOBJ.hpp:369
void setPath(const String &filename)
Texture file name.
Definition: ioOBJ.hpp:129
std::vector< fvec3 > mNormals
Definition: ioOBJ.hpp:367
ObjMaterial * material()
The material associated to this mesh.
Definition: ioOBJ.hpp:319
std::vector< fvec3 > & texCoordsArray()
Definition: ioOBJ.hpp:352
void setS_UVW(const fvec3 &s_uvw)
-s u v w
Definition: ioOBJ.hpp:145
bool blendU() const
-blendu on | off
Definition: ioOBJ.hpp:104
const std::vector< int > & face_type() const
Each entry represents a face, the number represents how many vertices the face has.
Definition: ioOBJ.hpp:316
const ObjTexture & map_d() const
map_d - transparency map
Definition: ioOBJ.hpp:219
void setTexres_Value(float value)
-texres value
Definition: ioOBJ.hpp:149
bool blendV() const
-blendv on | off
Definition: ioOBJ.hpp:106
const ObjTexture & map_Ns() const
map_Ns - specular exponent map
Definition: ioOBJ.hpp:217
fvec3 mKs
Ks - specular color.
Definition: ioOBJ.hpp:266
float ni() const
Ni - optical density / index of refraction.
Definition: ioOBJ.hpp:209
void setIllum(int illum)
illum - illumination model
Definition: ioOBJ.hpp:240
const std::vector< ref< ObjMesh > > & meshes() const
Definition: ioOBJ.hpp:348
void setT_UVW(const fvec3 &t_uvw)
-t u v w
Definition: ioOBJ.hpp:147
bool valid() const
Definition: ioOBJ.hpp:97
std::vector< int > & facePositionIndex()
Index into ObjLoader::vertexArray() vector.
Definition: ioOBJ.hpp:321
int mIllum
illum - illumination model
Definition: ioOBJ.hpp:274
const ObjMaterial * material() const
The material associated to this mesh.
Definition: ioOBJ.hpp:307
bool cc() const
-cc on | off
Definition: ioOBJ.hpp:108
const std::vector< int > & facePositionIndex() const
Index into ObjLoader::vertexArray() vector.
Definition: ioOBJ.hpp:309
bool writeResource(const String &, ResourceDatabase *) const
Not supported yet.
Definition: ioOBJ.hpp:77
char mImfchan
-imfchan r | g | b | m | l | z
Definition: ioOBJ.hpp:179
float mMM_Base
-mm base gain
Definition: ioOBJ.hpp:167
void setMap_Disp(const ObjTexture &map_disp)
disp - displace map
Definition: ioOBJ.hpp:256
void setKd(const fvec3 &kd)
Kd - diffuse color.
Definition: ioOBJ.hpp:230
float bm() const
-bm mult
Definition: ioOBJ.hpp:126
const fvec3 & t_UVW() const
-t u v w
Definition: ioOBJ.hpp:120
std::vector< int > mFace_icoords
Definition: ioOBJ.hpp:332
void registerLoadWriter()
bool writeResource(VirtualFile *, ResourceDatabase *) const
Not supported yet.
Definition: ioOBJ.hpp:83
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
std::vector< int > mFace_inormals
Definition: ioOBJ.hpp:333
const ObjTexture & map_Kd() const
map_Kd - ambient diffuse
Definition: ioOBJ.hpp:211
const fvec3 & s_UVW() const
-s u v w
Definition: ioOBJ.hpp:118
ObjTexture mMap_d
map_d - transparency map
Definition: ioOBJ.hpp:286
Represents a Wavefront OBJ material as loaded from an MTL file. See also ObjLoader.
Definition: ioOBJ.hpp:187
void setBM(float bm)
-bm mult
Definition: ioOBJ.hpp:153
std::vector< fvec3 > mTexCoords
Definition: ioOBJ.hpp:368
std::vector< int > mFace_type
Definition: ioOBJ.hpp:335
The ResourceLoadWriter class is an abstract class used to implement read/write support for one or mor...
void setMM_Gain(float gain)
-mm base gain
Definition: ioOBJ.hpp:141
void setTr(float tr)
Tr/d - transparency.
Definition: ioOBJ.hpp:236
const ObjTexture & map_Decal() const
decal - decal map
Definition: ioOBJ.hpp:221
const fvec3 & ks() const
Ks - specular color.
Definition: ioOBJ.hpp:199
void setMaterial(ObjMaterial *mat)
The material associated to this mesh.
Definition: ioOBJ.hpp:304
void setMap_Ka(const ObjTexture &map_ka)
map_Ka - ambient color map
Definition: ioOBJ.hpp:246
void setMap_Ns(const ObjTexture &map_ns)
map_Ns - specular exponent map
Definition: ioOBJ.hpp:250
const fvec3 & o_UVW() const
-o u v w
Definition: ioOBJ.hpp:116
ObjTexture mMap_Disp
disp - displace map
Definition: ioOBJ.hpp:290
float mTr
Tr/d - transparency.
Definition: ioOBJ.hpp:270
The ResourceDatabase class contains and manipulates a set of resources.
const std::vector< fvec4 > & vertexArray() const
Definition: ioOBJ.hpp:344
ObjTexture mMap_Ks
map_Ks - specular color map
Definition: ioOBJ.hpp:282
void setCC(bool on)
-cc on | off
Definition: ioOBJ.hpp:135