# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

hls_texture_bank.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000-2002 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #include "std3d.h"
00027 #include "3d/hls_texture_bank.h"
00028 
00029 
00030 using   namespace std;
00031 using   namespace NLMISC;
00032 
00033 namespace NL3D 
00034 {
00035 
00036 
00037 // ***************************************************************************
00038 // ***************************************************************************
00039 // CHLSTextureBank
00040 // ***************************************************************************
00041 // ***************************************************************************
00042 
00043 
00044 // ***************************************************************************
00045 CHLSTextureBank::CHLSTextureBank()
00046 {
00047 }
00048 // ***************************************************************************
00049 void                    CHLSTextureBank::reset()
00050 {
00051         contReset(_ColorTextures);
00052         contReset(_TextureInstanceData);
00053         contReset(_TextureInstances);
00054 }
00055 // ***************************************************************************
00056 uint32                  CHLSTextureBank::addColorTexture(const CHLSColorTexture &tex)
00057 {
00058         _ColorTextures.push_back(tex);
00059         return _ColorTextures.size()-1;
00060 }
00061 // ***************************************************************************
00062 void                    CHLSTextureBank::addTextureInstance(const std::string &name, uint32 colorTextureId, const vector<CHLSColorDelta> &cols)
00063 {
00064         string  nameLwr= strlwr(name);
00065 
00066         // checks
00067         nlassert(colorTextureId<_ColorTextures.size());
00068         CHLSColorTexture        &colText= _ColorTextures[colorTextureId];
00069         nlassert(cols.size()==colText.getNumMasks());
00070 
00071         // new instance
00072         CTextureInstance        textInst;
00073         textInst._ColorTextureId= colorTextureId;
00074         textInst._DataIndex= _TextureInstanceData.size();
00075         // leave ptrs undefined
00076         textInst._DataPtr= NULL;
00077         textInst._ColorTexturePtr= NULL;
00078 
00079         // allocate/fill data
00080         uint32  nameSize= (nameLwr.size()+1);
00081         uint32  colSize= cols.size()*sizeof(CHLSColorDelta);
00082         _TextureInstanceData.resize(_TextureInstanceData.size() + nameSize + colSize);
00083         // copy name
00084         memcpy(&_TextureInstanceData[textInst._DataIndex], nameLwr.c_str(), nameSize);
00085         // copy cols
00086         memcpy(&_TextureInstanceData[textInst._DataIndex+nameSize], &cols[0], colSize);
00087 
00088         // add the instance.
00089         _TextureInstances.push_back(textInst);
00090 }
00091 // ***************************************************************************
00092 void                    CHLSTextureBank::compilePtrs()
00093 {
00094         uint8   *data= &_TextureInstanceData[0];
00095 
00096         // For all texture instances, compute ptr.
00097         for(uint i=0;i<_TextureInstances.size();i++)
00098         {
00099                 CTextureInstance        &text= _TextureInstances[i];
00100                 text._DataPtr= data + text._DataIndex;
00101                 text._ColorTexturePtr= &_ColorTextures[text._ColorTextureId];
00102         }
00103 }
00104 
00105 
00106 // ***************************************************************************
00107 void                    CHLSTextureBank::compile()
00108 {
00109         // compile the ptrs.
00110         compilePtrs();
00111 
00112         // No other ops for now.
00113 }
00114 
00115 
00116 // ***************************************************************************
00117 void                    CHLSTextureBank::serial(NLMISC::IStream &f)
00118 {
00119         sint    ver= f.serialVersion(0);
00120 
00121         f.serialCont(_ColorTextures);
00122         f.serialCont(_TextureInstanceData);
00123         f.serialCont(_TextureInstances);
00124 
00125         // Must compile ptrs.
00126         if(f.isReading())
00127         {
00128                 // compile the ptrs only.
00129                 compilePtrs();
00130         }
00131 }
00132 
00133 
00134 // ***************************************************************************
00135 void                    CHLSTextureBank::fillHandleArray(std::vector<CTextureInstanceHandle> &array)
00136 {
00137         for(uint i=0;i<_TextureInstances.size();i++)
00138         {
00139                 CTextureInstanceHandle  h;
00140                 h.Texture= &_TextureInstances[i];
00141                 array.push_back(h);
00142         }
00143 }
00144 
00145 
00146 // ***************************************************************************
00147 // ***************************************************************************
00148 // CHLSTextureBank::CTextureInstance
00149 // ***************************************************************************
00150 // ***************************************************************************
00151 
00152 
00153 // ***************************************************************************
00154 void                    CHLSTextureBank::CTextureInstance::serial(NLMISC::IStream &f)
00155 {
00156         sint    ver= f.serialVersion(0);
00157 
00158         f.serial(_DataIndex);
00159         f.serial(_ColorTextureId);
00160 }
00161 
00162 
00163 // ***************************************************************************
00164 bool                    CHLSTextureBank::CTextureInstance::operator<(const CTextureInstance &t) const
00165 {
00166         // compare the 2 strings.
00167         return (strcmp((const char*)_DataPtr, (const char*)t._DataPtr)<0);
00168 }
00169 // ***************************************************************************
00170 bool                    CHLSTextureBank::CTextureInstance::operator<=(const CTextureInstance &t) const
00171 {
00172         // compare the 2 strings.
00173         return (strcmp((const char*)_DataPtr, (const char*)t._DataPtr)<=0);
00174 }
00175 
00176 
00177 // ***************************************************************************
00178 bool                    CHLSTextureBank::CTextureInstance::sameName(const char *str)
00179 {
00180         return (strcmp((const char*)_DataPtr, str)==0);
00181 }
00182 
00183 
00184 // ***************************************************************************
00185 void                    CHLSTextureBank::CTextureInstance::buildColorVersion(NLMISC::CBitmap &out)
00186 {
00187         // get ptr to color deltas.
00188         uint    nameSize= strlen((const char*)_DataPtr)+1;
00189         CHLSColorDelta          *colDeltas= (CHLSColorDelta*)(_DataPtr + nameSize);
00190 
00191         // build the texture.
00192         _ColorTexturePtr->buildColorVersion(colDeltas, out);
00193 }
00194 
00195 
00196 } // NL3D