# 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  

cube_map_builder.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 
00028 
00029 
00030 #include "3d/cube_map_builder.h"
00031 #include "3d/texture_cube.h"
00032 #include "3d/texture_mem.h"
00033 #include "nel/misc/vector.h"
00034 #include "nel/misc/rgba.h"
00035 
00036 
00037 
00038 
00039 namespace NL3D
00040 {
00041 
00042 
00043 // utility function : build  a side of a cube map
00044 static uint8 *BuildCubeMapTex(const NLMISC::CVector &start,
00045                                                           const NLMISC::CVector &uDir,
00046                                                           const NLMISC::CVector &vDir,
00047                                                           uint size,
00048                                                           ICubeMapFunctor &f
00049                                                          )
00050 {
00051         NLMISC::CRGBA *map = new NLMISC::CRGBA[size * size];
00052         NLMISC::CRGBA *destTexel = map;
00053         NLMISC::CVector currN = start;
00054         NLMISC::CVector uStep = (2.f / size) * uDir;
00055         NLMISC::CVector vStep = (2.f / size) * vDir;
00056 
00057         for (uint y = 0; y < size; ++y)
00058         {
00059                 NLMISC::CVector hCurrN = currN;
00060                 for (uint x = 0; x < size; ++x)
00061                 {                                               
00062                         destTexel[x + y *size] = f(hCurrN.normed());                    
00063                         hCurrN += uStep;
00064                 }
00065                 currN += vStep;
00066         }
00067         return (uint8 *) map;
00068 }
00069 
00070 // utility function : build  a side of a cube map, with luminance only
00071 static uint8 *BuildCubeMapTexLuminance(const NLMISC::CVector &start,
00072                                                           const NLMISC::CVector &uDir,
00073                                                           const NLMISC::CVector &vDir,
00074                                                           uint size,
00075                                                           ICubeMapFunctor &f
00076                                                          )
00077 {
00078         uint8 *map = new uint8[size * size];
00079         uint8 *destTexel = map;
00080         NLMISC::CVector currN = start;
00081         NLMISC::CVector uStep = (2.f / size) * uDir;
00082         NLMISC::CVector vStep = (2.f / size) * vDir;
00083 
00084         for (uint y = 0; y < size; ++y)
00085         {
00086                 NLMISC::CVector hCurrN = currN;
00087                 for (uint x = 0; x < size; ++x)
00088                 {                                               
00089                         destTexel[x + y *size] = f(hCurrN.normed()).A;          
00090                         hCurrN += uStep;
00091                 }
00092                 currN += vStep;
00093         }
00094         return map;
00095 }
00096 
00097 
00098 
00099 CTextureCube *BuildCubeMap(sint mapSize, ICubeMapFunctor &f, bool luminanceOnly /* = false*/, const std::string &shareName /* = "" */)
00100 {       
00101         std::auto_ptr<CTextureCube> cubeMap(new CTextureCube);
00102         std::auto_ptr<CTextureMem> faces[6];            
00103 
00105         static const NLMISC::CVector start[] = 
00106         { 
00107                 NLMISC::CVector(1, 1, 1),               
00108                 NLMISC::CVector(-1, 1, -1),             
00109                 NLMISC::CVector(-1, 1, -1),             
00110                 NLMISC::CVector(-1, -1, 1),             
00111                 NLMISC::CVector(-1, 1, 1),              
00112                 NLMISC::CVector(1, 1, -1)               
00113         };
00114 
00115 
00116         static const NLMISC::CVector uDir[] = 
00117         { 
00118                 - NLMISC::CVector::K,           
00119                 NLMISC::CVector::K,                     
00120                 NLMISC::CVector::I,                     
00121                 NLMISC::CVector::I,                     
00122                 NLMISC::CVector::I,                     
00123                 -NLMISC::CVector::I,                    
00124         };
00125 
00126         static const NLMISC::CVector vDir[] = 
00127         { 
00128                 - NLMISC::CVector::J,           
00129                 - NLMISC::CVector::J,           
00130                 NLMISC::CVector::K,                     
00131                 - NLMISC::CVector::K,           
00132                 - NLMISC::CVector::J,           
00133                 - NLMISC::CVector::J,           
00134         };
00135         
00136 
00137 
00138         uint k;
00139 
00141         for (k = 0;  k < 6; ++k)
00142         {                               
00143                 faces[k].reset(new CTextureMem);
00144                 uint8 *map = luminanceOnly ?    BuildCubeMapTexLuminance(start[k], uDir[k], vDir[k], mapSize, f)
00145                                                            :    BuildCubeMapTex(start[k], uDir[k], vDir[k], mapSize, f);                                
00146                 faces[k]->setPointer(map,
00147                                                          mapSize * mapSize * sizeof(uint8) * (luminanceOnly ? 1 : 4),
00148                                                          true,
00149                                                          false,
00150                                                          mapSize,
00151                                                          mapSize,
00152                                                          luminanceOnly ? CBitmap::Luminance : CBitmap::RGBA
00153                                                         );
00154                 if (!shareName.empty())
00155                 {
00156                         faces[k]->setShareName(shareName + (char) ('0' + k));
00157                 }
00158         }
00159                 
00160         static const CTextureCube::TFace toTC[] = { CTextureCube::positive_x, CTextureCube::negative_x,
00161                                                                                                 CTextureCube::positive_z, CTextureCube::negative_z,
00162                                                                                                 CTextureCube::negative_y, CTextureCube::positive_y };
00164         for (k = 0;  k < 6; ++k)
00165         {
00166                 cubeMap->setTexture(toTC[k], faces[k].release());       
00167         }
00168 
00169         cubeMap->setNoFlip(); // keep faces texture as it
00170 
00171         return cubeMap.release();
00172 }
00173 
00174 
00175 
00176 
00177 
00178 
00179 
00180 } // NL3D
00181  
00182