OBJReader.hh 7.13 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
/* ========================================================================= *
Jan Möbius's avatar
Jan Möbius committed
2 3
 *                                                                           *
 *                               OpenMesh                                    *
Jan Möbius's avatar
Jan Möbius committed
4
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
Jan Möbius's avatar
Typo  
Jan Möbius committed
5
 *           Department of Computer Graphics and Multimedia                  *
Jan Möbius's avatar
Jan Möbius committed
6 7
 *                          All rights reserved.                             *
 *                            www.openmesh.org                               *
Jan Möbius's avatar
Jan Möbius committed
8
 *                                                                           *
9
 *---------------------------------------------------------------------------*
Jan Möbius's avatar
Jan Möbius committed
10 11
 * This file is part of OpenMesh.                                            *
 *---------------------------------------------------------------------------*
Jan Möbius's avatar
Jan Möbius committed
12
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
13 14 15
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
Jan Möbius's avatar
Jan Möbius committed
16
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
17 18
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
Jan Möbius's avatar
Jan Möbius committed
19
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
20 21 22
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
Jan Möbius's avatar
Jan Möbius committed
23
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
24 25 26
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
27
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
28 29 30 31 32 33 34 35 36 37 38
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39 40
 *                                                                           *
 * ========================================================================= */
41

42

Jan Möbius's avatar
Jan Möbius committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58


//=============================================================================
//
//  Implements an reader module for OBJ files
//
//=============================================================================


#ifndef __OBJREADER_HH__
#define __OBJREADER_HH__


//=== INCLUDES ================================================================


59
#include <iosfwd>
Jan Möbius's avatar
Jan Möbius committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#include <string>
#include <map>

#include <OpenMesh/Core/System/config.h>
#include <OpenMesh/Core/Utils/SingletonT.hh>
#include <OpenMesh/Core/IO/importer/BaseImporter.hh>
#include <OpenMesh/Core/IO/reader/BaseReader.hh>


//== NAMESPACES ===============================================================


namespace OpenMesh {
namespace IO {


//== IMPLEMENTATION ===========================================================


79
/**
Jan Möbius's avatar
Jan Möbius committed
80 81
    Implementation of the OBJ format reader.
*/
82
class OPENMESHDLLEXPORT _OBJReader_ : public BaseReader
Jan Möbius's avatar
Jan Möbius committed
83 84 85 86 87 88 89
{
public:

  _OBJReader_();

  virtual ~_OBJReader_() { }

Jan Möbius's avatar
Jan Möbius committed
90 91
  std::string get_description() const override { return "Alias/Wavefront"; }
  std::string get_extensions()  const override { return "obj"; }
92 93 94

  bool read(const std::string& _filename,
	    BaseImporter& _bi,
Jan Möbius's avatar
Jan Möbius committed
95
	    Options& _opt) override;
Jan Möbius's avatar
Jan Möbius committed
96

97 98
  bool read(std::istream& _in,
          BaseImporter& _bi,
Jan Möbius's avatar
Jan Möbius committed
99
          Options& _opt) override;
100

Jan Möbius's avatar
Jan Möbius committed
101 102 103 104 105 106 107 108 109 110 111
private:

#ifndef DOXY_IGNORE_THIS
  class Material
  {
  public:

    Material() { cleanup(); }

    void cleanup()
    {
112 113 114 115 116
      Kd_is_set_     = false;
      Ka_is_set_     = false;
      Ks_is_set_     = false;
      Tr_is_set_     = false;
      map_Kd_is_set_ = false;
Jan Möbius's avatar
Jan Möbius committed
117 118
    }

119
    bool is_valid(void) const
120
    { return Kd_is_set_ || Ka_is_set_ || Ks_is_set_ || Tr_is_set_ || map_Kd_is_set_; }
Jan Möbius's avatar
Jan Möbius committed
121

122 123 124 125 126 127 128
    bool has_Kd(void)     { return Kd_is_set_;     }
    bool has_Ka(void)     { return Ka_is_set_;     }
    bool has_Ks(void)     { return Ks_is_set_;     }
    bool has_Tr(void)     { return Tr_is_set_;     }
    bool has_map_Kd(void) { return map_Kd_is_set_; }

    void set_Kd( float r, float g, float b )
Jan Möbius's avatar
Jan Möbius committed
129 130
    { Kd_=Vec3f(r,g,b); Kd_is_set_=true; }

131
    void set_Ka( float r, float g, float b )
Jan Möbius's avatar
Jan Möbius committed
132 133
    { Ka_=Vec3f(r,g,b); Ka_is_set_=true; }

134
    void set_Ks( float r, float g, float b )
Jan Möbius's avatar
Jan Möbius committed
135
    { Ks_=Vec3f(r,g,b); Ks_is_set_=true; }
136

Jan Möbius's avatar
Jan Möbius committed
137 138
    void set_Tr( float t )
    { Tr_=t;            Tr_is_set_=true; }
139 140 141 142

    void set_map_Kd( std::string _name, int _index_Kd )
    { map_Kd_ = _name, index_Kd_ = _index_Kd; map_Kd_is_set_ = true; };

Jan Möbius's avatar
Jan Möbius committed
143 144 145 146
    const Vec3f& Kd( void ) const { return Kd_; }
    const Vec3f& Ka( void ) const { return Ka_; }
    const Vec3f& Ks( void ) const { return Ks_; }
    float  Tr( void ) const { return Tr_; }
147 148 149
    const std::string& map_Kd( void ) { return map_Kd_ ; }
    const int& map_Kd_index( void ) { return index_Kd_ ; }

Jan Möbius's avatar
Jan Möbius committed
150 151
  private:

152 153 154 155 156 157 158
    Vec3f Kd_;                          bool Kd_is_set_; // diffuse
    Vec3f Ka_;                          bool Ka_is_set_; // ambient
    Vec3f Ks_;                          bool Ks_is_set_; // specular
    float Tr_;                          bool Tr_is_set_; // transperency

    std::string map_Kd_; int index_Kd_; bool map_Kd_is_set_; // Texture

Jan Möbius's avatar
Jan Möbius committed
159 160 161 162 163 164 165 166 167
  };
#endif

  typedef std::map<std::string, Material> MaterialList;

  MaterialList materials_;

  bool read_material( std::fstream& _in );

168

Jan Möbius's avatar
Jan Möbius committed
169 170
private:

171 172 173 174 175 176 177 178
  bool read_vertices(std::istream& _in, BaseImporter& _bi, Options& _opt,
                     std::vector<Vec3f> & normals,
                     std::vector<Vec3f> & colors,
                     std::vector<Vec3f> & texcoords3d,
                     std::vector<Vec2f> & texcoords,
                     std::vector<VertexHandle> & vertexHandles,
                     Options & fileOptions);

Jan Möbius's avatar
Jan Möbius committed
179 180 181 182 183 184 185 186 187
  std::string path_;

};


//== TYPE DEFINITION ==========================================================


extern _OBJReader_  __OBJReaderInstance;
Mike Kremer's avatar
Mike Kremer committed
188
OPENMESHDLLEXPORT _OBJReader_& OBJReader();
Jan Möbius's avatar
Jan Möbius committed
189 190 191 192 193 194 195 196


//=============================================================================
} // namespace IO
} // namespace OpenMesh
//=============================================================================
#endif
//=============================================================================