00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 #include "SkyBoxComponent.h"
00019 #include "../../core/GameManager.h"
00020 
00021 
00022 
00023 
00024 SkyBoxComponent::SkyBoxComponent(Entity *parent)
00025 : SceneComponent(parent, true)
00026 {
00027     init();
00028 }
00029 
00030 
00031 SkyBoxComponent::SkyBoxComponent(Entity *parent, ITexture *top, ITexture *bottom, ITexture *left,
00032                                  ITexture *right, ITexture *front, ITexture *back)
00033 : SceneComponent(parent, true)
00034 {
00035     init();
00036     setTextures(top, bottom, left, right, front, back);
00037 }
00038 
00039 
00040 SkyBoxComponent::SkyBoxComponent(Entity *parent, const std::string &top, const std::string &bottom,
00041                                  const std::string &left, const std::string &right,
00042                                  const std::string &front, const std::string &back)
00043 : SceneComponent(parent, true)
00044 {
00045     init();
00046     setTextures(top, bottom, left, right, front, back);
00047 }
00048 
00049 
00050 SkyBoxComponent::~SkyBoxComponent()
00051 {
00052 }
00053 
00054 
00055 void SkyBoxComponent::init()
00056 {
00057     
00058     mSceneNode = GameManager::Instance()->getSceneManager()->addSkyBoxSceneNode(0, 0, 0, 0, 0, 0, 0,
00059                                                                                 pParent->getID());
00060 
00061     
00062     mTriSelector = NULL;
00063     mMetaSelector = NULL;
00064 }
00065 
00066 
00067 void SkyBoxComponent::setTextures(const std::string &top, const std::string &bottom,
00068                                   const std::string &left, const std::string &right,
00069                                   const std::string &front, const std::string &back,
00070                                   bool ignoreNull)
00071 {
00072     if( (front != "") || !ignoreNull)
00073       setTexture(0, front);
00074 
00075     if( (left != "") || !ignoreNull)
00076       setTexture(1, left);
00077 
00078     if( (back != "") || !ignoreNull)
00079       setTexture(2, back);
00080 
00081     if( (right != "") || !ignoreNull)
00082       setTexture(3, right);
00083 
00084     if( (top != "") || !ignoreNull)
00085       setTexture(4, top);
00086 
00087     if( (bottom != "") || !ignoreNull)
00088       setTexture(5, bottom);
00089 }
00090 
00091 
00092 void SkyBoxComponent::setTextures(ITexture *top, ITexture *bottom, ITexture *left, ITexture *right,
00093                                   ITexture *front, ITexture *back, bool ignoreNull)
00094 {
00095     if(front || !ignoreNull)
00096       setTexture(0, front);
00097 
00098     if(left || !ignoreNull)
00099       setTexture(1, left);
00100 
00101     if(back || !ignoreNull)
00102       setTexture(2, back);
00103 
00104     if(right || !ignoreNull)
00105       setTexture(3, right);
00106 
00107     if(top || !ignoreNull)
00108       setTexture(4, top);
00109 
00110     if(bottom || !ignoreNull)
00111       setTexture(5, bottom);
00112 }
00113 
00114 
00115 void SkyBoxComponent::setTexture(u32 side, const std::string &fileName)
00116 {
00117     
00118     if(side >= 6)
00119       return;
00120 
00121     
00122     AssetGroup *assets = pParent->getAssetGroup();
00123     ITexture *texture = 0;
00124 
00125     
00126     if(assets != NULL)
00127     {
00128         
00129         assets->disconnectEventSignal(std::string("textures/") + mTextureFileNames[side], this);
00130 
00131         
00132         TextureProcessor *processor = static_cast<TextureProcessor*>
00133                                       (assets->getAssetProcessor("textures"));
00134         texture = processor->getTexture(fileName);
00135 
00136         if(texture)
00137         {
00138            assets->connectEventSignal(std::string("textures/") + fileName, this,
00139                                             &SkyBoxComponent::onTextureSkyBox);
00140            mTextureFileNames[side] = fileName;
00141         }
00142     }
00143 
00144     else
00145       texture = GameManager::Instance()->getDriver()->getTexture(fileName.c_str());
00146 
00147     
00148     mSceneNode->getMaterial(side).setTexture(0, texture);
00149 }
00150 
00151 
00152 void SkyBoxComponent::setTexture(u32 side, ITexture *texture)
00153 {
00154     
00155     AssetGroup *assets = pParent->getAssetGroup();
00156 
00157     
00158     if(assets != NULL)
00159     {
00160        assets->disconnectEventSignal(std::string("textures/") + mTextureFileNames[side], this);
00161        mTextureFileNames[side] = "";
00162     }
00163 
00164     
00165     mSceneNode->getMaterial(side).setTexture(0, texture);
00166 }
00167 
00168 
00169 void SkyBoxComponent::onTextureSkyBox(void *p)
00170 {
00171     
00172     ITexture *texture = reinterpret_cast<ITexture*>(p);
00173 
00174     
00175     std::string fileName = (GameManager::Instance()->getDevice()->getFileSystem()->
00176                               getFileBasename(texture->getName())).c_str();
00177 
00178     u32 side = 6;
00179 
00180     for(u32 i = 0; i < 6; i++)
00181     {
00182         if(mTextureFileNames[i] == fileName)
00183         {
00184            side = i;
00185            break;
00186         }
00187     }
00188 
00189     if(side == 6)
00190       return;
00191 
00192     
00193     mSceneNode->getMaterial(side).setTexture(0, texture);
00194 }
00195 
00196 
00197 bool SkyBoxComponent::parseXML(IXMLReader *file, Entity *entity)
00198 {
00199     
00200     SkyBoxComponent *component = NULL;
00201 
00202     
00203     if(file->getNodeType() == io::EXN_ELEMENT)
00204     {
00205        if(stringw("SkyBoxComponent") == file->getNodeName())
00206        {
00207           
00208           component = new SkyBoxComponent(entity);
00209        }
00210     }
00211 
00212     if(component == NULL)
00213       return false;
00214 
00215     
00216     while(file->read())
00217     {
00218        switch(file->getNodeType())
00219        {
00220           case io::EXN_ELEMENT:
00221 
00222              
00223              if(stringw("textures") == file->getNodeName())
00224              {
00225                 stringc top = file->getAttributeValue(L"top");
00226                 stringc bottom = file->getAttributeValue(L"bottom");
00227                 stringc left = file->getAttributeValue(L"left");
00228                 stringc right = file->getAttributeValue(L"right");
00229                 stringc front = file->getAttributeValue(L"front");
00230                 stringc back = file->getAttributeValue(L"back");
00231 
00232                 component->setTextures(top.c_str(), bottom.c_str(), left.c_str(), right.c_str(),
00233                                        front.c_str(), back.c_str(), false);
00234              }
00235 
00236              
00237              else SceneComponent::parseBaseXML(file, component);
00238 
00239              break;
00240 
00241           case io::EXN_ELEMENT_END:
00242 
00243              
00244              if(stringw("SkyBoxComponent") == file->getNodeName())
00245                return true;
00246 
00247              break;
00248 
00249           default:
00250 
00251              break;
00252        }
00253     }
00254 
00255     
00256     return false;
00257 }
00258 
00259