Visualization Library v1.0.3A lightweight C++ OpenGL middleware for 2D/3D graphics |
[Download] [Tutorials] [All Classes] [Grouped Classes] |
00001 /**************************************************************************************/ 00002 /* */ 00003 /* Visualization Library */ 00004 /* http://visualizationlibrary.org */ 00005 /* */ 00006 /* Copyright (c) 2005-2010, Michele Bosi */ 00007 /* All rights reserved. */ 00008 /* */ 00009 /* Redistribution and use in source and binary forms, with or without modification, */ 00010 /* are permitted provided that the following conditions are met: */ 00011 /* */ 00012 /* - Redistributions of source code must retain the above copyright notice, this */ 00013 /* list of conditions and the following disclaimer. */ 00014 /* */ 00015 /* - Redistributions in binary form must reproduce the above copyright notice, this */ 00016 /* list of conditions and the following disclaimer in the documentation and/or */ 00017 /* other materials provided with the distribution. */ 00018 /* */ 00019 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */ 00020 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */ 00021 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */ 00022 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */ 00023 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */ 00024 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */ 00025 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */ 00026 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */ 00027 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */ 00028 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 00029 /* */ 00030 /**************************************************************************************/ 00031 00032 #if !defined(LoadOBJ_INCLUDE_ONCE) 00033 #define LoadOBJ_INCLUDE_ONCE 00034 00035 #include <vlCore/String.hpp> 00036 #include <vlGraphics/Geometry.hpp> 00037 #include <vlCore/ResourceLoadWriter.hpp> 00038 #include <vlCore/ResourceDatabase.hpp> 00039 #include <map> 00040 00041 namespace vl 00042 { 00043 class VirtualFile; 00044 00045 //----------------------------------------------------------------------------- 00047 VLGRAPHICS_EXPORT ref<ResourceDatabase> loadOBJ( const String& path ); 00048 //----------------------------------------------------------------------------- 00050 VLGRAPHICS_EXPORT ref<ResourceDatabase> loadOBJ( VirtualFile* file ); 00051 //--------------------------------------------------------------------------- 00052 // LoadWriterOBJ 00053 //--------------------------------------------------------------------------- 00057 class LoadWriterOBJ: public ResourceLoadWriter 00058 { 00059 VL_INSTRUMENT_CLASS(vl::LoadWriterOBJ, ResourceLoadWriter) 00060 00061 public: 00062 LoadWriterOBJ(): ResourceLoadWriter("|obj|", "|obj|") {} 00063 00064 void registerLoadWriter(); 00065 00066 ref<ResourceDatabase> loadResource(const String& path) const 00067 { 00068 return loadOBJ(path); 00069 } 00070 00071 ref<ResourceDatabase> loadResource(VirtualFile* file) const 00072 { 00073 return loadOBJ(file); 00074 } 00075 00077 bool writeResource(const String& /*path*/, ResourceDatabase* /*resource*/) const 00078 { 00079 return false; 00080 } 00081 00083 bool writeResource(VirtualFile* /*file*/, ResourceDatabase* /*resource*/) const 00084 { 00085 return false; 00086 } 00087 }; 00088 //----------------------------------------------------------------------------- 00089 // ObjTexture 00090 //----------------------------------------------------------------------------- 00092 class VLGRAPHICS_EXPORT ObjTexture 00093 { 00094 public: 00095 ObjTexture(); 00096 const ObjTexture& parseLine(const String& line, const String& file); 00097 bool valid() const { return !mFileName.empty(); } 00099 void print(); 00100 00102 const String& path() const { return mFileName; } 00104 bool blendU() const { return mBlendU; } 00106 bool blendV() const { return mBlendV; } 00108 bool cc() const { return mCC; } 00110 bool clamp() const { return mClamp; } 00112 float mm_Base() const { return mMM_Base; } 00114 float mm_Gain() const { return mMM_Gain; } 00116 const fvec3& o_UVW() const { return mO_UVW; } 00118 const fvec3& s_UVW() const { return mS_UVW; } 00120 const fvec3& t_UVW() const { return mT_UVW; } 00122 float texres_Value() const { return mTexres_Value; } 00124 char imfchan() const { return mImfchan; } 00126 float bm() const { return mBM; } 00127 00129 void setPath(const String& filename) { mFileName = filename; } 00131 void setBlendU(bool on) { mBlendU = on; } 00133 void setBlendV(bool on) { mBlendV = on; } 00135 void setCC(bool on) { mCC = on; } 00137 void setClamp(bool on) { mClamp = on; } 00139 void setMM_Base(float base) { mMM_Base = base; } 00141 void setMM_Gain(float gain) { mMM_Gain = gain; } 00143 void setO_UVW(const fvec3& o_uvw) { mO_UVW = o_uvw; } 00145 void setS_UVW(const fvec3& s_uvw) { mS_UVW = s_uvw; } 00147 void setT_UVW(const fvec3& t_uvw) { mT_UVW = t_uvw; } 00149 void setTexres_Value(float value) { mTexres_Value = value; } 00151 void setImfchan(char val) { mImfchan = val; } 00153 void setBM(float bm) { mBM = bm; } 00154 00155 protected: 00157 String mFileName; 00159 bool mBlendU; 00161 bool mBlendV; 00163 bool mCC; 00165 bool mClamp; 00167 float mMM_Base; 00169 float mMM_Gain; 00171 fvec3 mO_UVW; 00173 fvec3 mS_UVW; 00175 fvec3 mT_UVW; 00177 float mTexres_Value; 00179 char mImfchan; 00181 float mBM; 00182 }; 00183 //----------------------------------------------------------------------------- 00184 // ObjMaterial 00185 //----------------------------------------------------------------------------- 00187 class ObjMaterial: public Object 00188 { 00189 VL_INSTRUMENT_CLASS(vl::ObjMaterial, Object) 00190 00191 public: 00192 ObjMaterial(): mTr(1.0f), mNs(0.0f), mIllum(0), mNi(1.0f) {} 00193 00195 const fvec3& ka() const { return mKa; } 00197 const fvec3& kd() const { return mKd; } 00199 const fvec3& ks() const { return mKs; } 00201 const fvec3& ke() const { return mKe; } 00203 float tr() const { return mTr; } 00205 float ns() const { return mNs; } 00207 int illum() const { return mIllum; } 00209 float ni() const { return mNi; } 00211 const ObjTexture& map_Kd() const { return mMap_Kd; } 00213 const ObjTexture& map_Ka() const { return mMap_Ka; } 00215 const ObjTexture& map_Ks() const { return mMap_Ks; } 00217 const ObjTexture& map_Ns() const { return mMap_Ns; } 00219 const ObjTexture& map_d() const { return mMap_d; } 00221 const ObjTexture& map_Decal() const { return mMap_Decal; } 00223 const ObjTexture& map_Disp() const { return mMap_Disp; } 00225 const ObjTexture& map_Bump() const { return mMap_Bump; } 00226 00228 void setKa(const fvec3& ka) { mKa = ka; } 00230 void setKd(const fvec3& kd) { mKd = kd; } 00232 void setKs(const fvec3& ks) { mKs = ks; } 00234 void setKe(const fvec3& ke) { mKe = ke; } 00236 void setTr(float tr) { mTr = tr; } 00238 void setNs(float ns) { mNs = ns; } 00240 void setIllum(int illum) { mIllum = illum; } 00242 void setNi(float ni) { mNi = ni; } 00244 void setMap_Kd(const ObjTexture& map_kd) { mMap_Kd = map_kd; } 00246 void setMap_Ka(const ObjTexture& map_ka) { mMap_Ka = map_ka; } 00248 void setMap_Ks(const ObjTexture& map_ks) { mMap_Ks = map_ks; } 00250 void setMap_Ns(const ObjTexture& map_ns) { mMap_Ns = map_ns; } 00252 void setMap_d(const ObjTexture& map_d) { mMap_d = map_d; } 00254 void setMap_Decal(const ObjTexture& map_decal) { mMap_Decal = map_decal; } 00256 void setMap_Disp(const ObjTexture& map_disp) { mMap_Disp = map_disp; } 00258 void setMap_Bump(const ObjTexture& map_bump) { mMap_Bump = map_bump; } 00259 00260 protected: 00262 fvec3 mKa; 00264 fvec3 mKd; 00266 fvec3 mKs; 00268 fvec3 mKe; 00270 float mTr; 00272 float mNs; 00274 int mIllum; 00276 float mNi; 00278 ObjTexture mMap_Kd; 00280 ObjTexture mMap_Ka; 00282 ObjTexture mMap_Ks; 00284 ObjTexture mMap_Ns; 00286 ObjTexture mMap_d; 00288 ObjTexture mMap_Decal; 00290 ObjTexture mMap_Disp; 00292 ObjTexture mMap_Bump; 00293 }; 00294 //----------------------------------------------------------------------------- 00295 // ObjMesh 00296 //----------------------------------------------------------------------------- 00298 class ObjMesh: public Object 00299 { 00300 VL_INSTRUMENT_CLASS(vl::ObjMesh, Object) 00301 00302 public: 00304 void setMaterial(ObjMaterial* mat) { mMaterial = mat; } 00305 00307 const ObjMaterial* material() const { return mMaterial.get(); } 00309 const std::vector<int>& facePositionIndex() const { return mFace_icoords; } 00311 const std::vector<int>& faceNormalIndex() const { return mFace_inormals; } 00313 const std::vector<int>& faceTexCoordIndex() const { return mFace_itexcoords; } 00316 const std::vector<int>& face_type() const { return mFace_type; } 00317 00319 ObjMaterial* material() { return mMaterial.get(); } 00321 std::vector<int>& facePositionIndex() { return mFace_icoords; } 00323 std::vector<int>& faceNormalIndex() { return mFace_inormals; } 00325 std::vector<int>& faceTexCoordIndex() { return mFace_itexcoords; } 00328 std::vector<int>& face_type() { return mFace_type; } 00329 00330 protected: 00331 ref<ObjMaterial> mMaterial; 00332 std::vector<int> mFace_icoords; 00333 std::vector<int> mFace_inormals; 00334 std::vector<int> mFace_itexcoords; 00335 std::vector<int> mFace_type; 00336 }; 00337 //----------------------------------------------------------------------------- 00338 // ObjLoader 00339 //----------------------------------------------------------------------------- 00341 class ObjLoader 00342 { 00343 public: 00344 const std::vector<fvec4>& vertexArray() const { return mCoords; } 00345 const std::vector<fvec3>& normalArray() const { return mNormals; } 00346 const std::vector<fvec3>& texCoordsArray() const { return mTexCoords; } 00347 const std::map< std::string, ref<ObjMaterial> >& materials() const { return mMaterials; } 00348 const std::vector< ref<ObjMesh> >& meshes() const { return mMeshes; } 00349 00350 std::vector<fvec4>& vertexArray() { return mCoords; } 00351 std::vector<fvec3>& normalArray() { return mNormals; } 00352 std::vector<fvec3>& texCoordsArray() { return mTexCoords; } 00353 std::map< std::string, ref<ObjMaterial> >& materials() { return mMaterials; } 00354 std::vector< ref<ObjMesh> >& meshes() { return mMeshes; } 00355 00358 ref<ResourceDatabase> loadOBJ( VirtualFile* file ); 00359 00363 void loadObjMaterials(VirtualFile* file, std::vector<ObjMaterial>& materials ); 00364 00365 protected: 00366 std::vector<fvec4> mCoords; 00367 std::vector<fvec3> mNormals; 00368 std::vector<fvec3> mTexCoords; 00369 std::map< std::string, ref<ObjMaterial> > mMaterials; 00370 std::vector< ref<ObjMesh> > mMeshes; 00371 }; 00372 //----------------------------------------------------------------------------- 00373 } 00374 00375 #endif