00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
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
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 , 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();
00170
00171 return cubeMap.release();
00172 }
00173
00174
00175
00176
00177
00178
00179
00180 }
00181
00182