Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreMaterialManager.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright © 2000-2002 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #include "OgreStableHeaders.h"
00026 #include "OgreMaterialManager.h"
00027 
00028 #include "OgreMaterial.h"
00029 #include "OgreStringVector.h"
00030 #include "OgreLogManager.h"
00031 #include "OgreSDDataChunk.h"
00032 #include "OgreArchiveEx.h"
00033 #include "OgreStringConverter.h"
00034 #include "OgreBlendMode.h"
00035 #include "OgreTechnique.h"
00036 #include "OgrePass.h"
00037 #include "OgreTextureUnitState.h"
00038 #include "OgreException.h"
00039 
00040 namespace Ogre {
00041 
00042     //-----------------------------------------------------------------------
00043     template<> MaterialManager* Singleton<MaterialManager>::ms_Singleton = 0;
00044     MaterialManager* MaterialManager::getSingletonPtr(void)
00045     {
00046         return ms_Singleton;
00047     }
00048     MaterialManager& MaterialManager::getSingleton(void)
00049     {  
00050         assert( ms_Singleton );  return ( *ms_Singleton );  
00051     }
00052     //-----------------------------------------------------------------------
00053     MaterialManager::MaterialManager()
00054     {
00055         mDefaultMinFilter = FO_LINEAR;
00056         mDefaultMagFilter = FO_LINEAR;
00057         mDefaultMipFilter = FO_POINT;
00058         mDefaultMaxAniso = 1;
00059 
00060 
00061     }
00062     //-----------------------------------------------------------------------
00063     MaterialManager::~MaterialManager()
00064     {
00065         delete Material::mDefaultSettings;
00066         // Resources cleared by superclass
00067     }
00068     //-----------------------------------------------------------------------
00069     void MaterialManager::initialise(void)
00070     {
00071         // Set up default material - don't use name contructor as we want to avoid applying defaults
00072         Material::mDefaultSettings = new Material();
00073         Material::mDefaultSettings->mName = "DefaultSettings";
00074         // Add a single technique and pass, non-programmable
00075         Material::mDefaultSettings->createTechnique()->createPass();
00076 
00077         // Set up a lit base white material
00078         Material* baseWhite = (Material*)this->create("BaseWhite");
00079         // Set up an unlit base white material
00080         Material* baseWhiteNoLighting = (Material*)this->create("BaseWhiteNoLighting");
00081         baseWhiteNoLighting->setLightingEnabled(false);
00082 
00083         // Parse all .program scripts first
00084         parseAllSources(".program");
00085         // Parse all .material scripts
00086         parseAllSources(".material");
00087 
00088     }
00089     //-----------------------------------------------------------------------
00090     void MaterialManager::parseScript(DataChunk& chunk)
00091     {
00092         // Delegate to serializer
00093         mSerializer.parseScript(chunk);
00094     }
00095     //-----------------------------------------------------------------------
00096     void MaterialManager::parseAllSources(const String& extension)
00097     {
00098         StringVector materialFiles;
00099         DataChunk* pChunk;
00100 
00101         std::vector<ArchiveEx*>::iterator i = mVFS.begin();
00102 
00103         // Specific archives
00104         for (; i != mVFS.end(); ++i)
00105         {
00106             materialFiles = (*i)->getAllNamesLike( "./", extension);
00107             for (StringVector::iterator si = materialFiles.begin(); si != materialFiles.end(); ++si)
00108             {
00109                 SDDataChunk dat; pChunk = &dat;
00110                 (*i)->fileRead(si[0], &pChunk );
00111                 LogManager::getSingleton().logMessage("Parsing material script: " + si[0]);
00112                 mSerializer.parseScript(dat, si[0]);
00113             }
00114 
00115         }
00116         // search common archives
00117         for (i = mCommonVFS.begin(); i != mCommonVFS.end(); ++i)
00118         {
00119             materialFiles = (*i)->getAllNamesLike( "./", extension);
00120             for (StringVector::iterator si = materialFiles.begin(); si != materialFiles.end(); ++si)
00121             {
00122                 SDDataChunk dat; pChunk = &dat;
00123                 (*i)->fileRead(si[0], &pChunk );
00124                 LogManager::getSingleton().logMessage("Parsing material script: " + si[0]);
00125                 mSerializer.parseScript(dat, si[0]);
00126             }
00127         }
00128 
00129 
00130     }
00131     //-----------------------------------------------------------------------
00132     Resource* MaterialManager::create( const String& name)
00133     {
00134         // Check name not already used
00135         if (getByName(name) != 0)
00136             Except(Exception::ERR_DUPLICATE_ITEM, "Material " + name + " already exists.",
00137                 "MaterialManager::create");
00138 
00139         Material* m = new Material(name);
00140         this->add(m);
00141 
00142         return m;
00143     }
00144     //-----------------------------------------------------------------------
00145     void MaterialManager::setDefaultTextureFiltering(TextureFilterOptions fo)
00146     {
00147         switch (fo)
00148         {
00149         case TFO_NONE:
00150             setDefaultTextureFiltering(FO_POINT, FO_POINT, FO_NONE);
00151             break;
00152         case TFO_BILINEAR:
00153             setDefaultTextureFiltering(FO_LINEAR, FO_LINEAR, FO_POINT);
00154             break;
00155         case TFO_TRILINEAR:
00156             setDefaultTextureFiltering(FO_LINEAR, FO_LINEAR, FO_LINEAR);
00157             break;
00158         case TFO_ANISOTROPIC:
00159             setDefaultTextureFiltering(FO_ANISOTROPIC, FO_ANISOTROPIC, FO_LINEAR);
00160             break;
00161         }
00162     }
00163     //-----------------------------------------------------------------------
00164     void MaterialManager::setDefaultAnisotropy(unsigned int maxAniso)
00165     {
00166         mDefaultMaxAniso = maxAniso;
00167     }
00168     //-----------------------------------------------------------------------
00169     unsigned int MaterialManager::getDefaultAnisotropy() const
00170     {
00171         return mDefaultMaxAniso;
00172     }
00173     //-----------------------------------------------------------------------
00174     void MaterialManager::setDefaultTextureFiltering(FilterType ftype, FilterOptions opts)
00175     {
00176         switch (ftype)
00177         {
00178         case FT_MIN:
00179             mDefaultMinFilter = opts;
00180             break;
00181         case FT_MAG:
00182             mDefaultMagFilter = opts;
00183             break;
00184         case FT_MIP:
00185             mDefaultMipFilter = opts;
00186             break;
00187         }
00188     }
00189     //-----------------------------------------------------------------------
00190     void MaterialManager::setDefaultTextureFiltering(FilterOptions minFilter, 
00191         FilterOptions magFilter, FilterOptions mipFilter)
00192     {
00193         mDefaultMinFilter = minFilter;
00194         mDefaultMagFilter = magFilter;
00195         mDefaultMipFilter = mipFilter;
00196     }
00197     //-----------------------------------------------------------------------
00198     FilterOptions MaterialManager::getDefaultTextureFiltering(FilterType ftype) const
00199     {
00200         switch (ftype)
00201         {
00202         case FT_MIN:
00203             return mDefaultMinFilter;
00204         case FT_MAG:
00205             return mDefaultMagFilter;
00206         case FT_MIP:
00207             return mDefaultMipFilter;
00208         }
00209         // to keep compiler happy
00210         return mDefaultMinFilter;
00211     }
00212 }

Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:31 2004