Merge branch 'main/rendor-staging' into fixes
[ryzomcore.git] / nel / src / 3d / cube_map_builder.cpp
blob60aec6cdd52dee612e4bdae3eb6fa289bea717c4
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "std3d.h"
21 #include "nel/3d/cube_map_builder.h"
22 #include "nel/3d/texture_cube.h"
23 #include "nel/3d/texture_mem.h"
24 #include "nel/misc/vector.h"
25 #include "nel/misc/rgba.h"
29 #ifdef DEBUG_NEW
30 #define new DEBUG_NEW
31 #endif
33 namespace NL3D
37 // utility function : build a side of a cube map
38 static uint8 *BuildCubeMapTex(const NLMISC::CVector &start,
39 const NLMISC::CVector &uDir,
40 const NLMISC::CVector &vDir,
41 uint size,
42 ICubeMapFunctor &f
45 NLMISC::CRGBA *map = new NLMISC::CRGBA[size * size];
46 NLMISC::CRGBA *destTexel = map;
47 NLMISC::CVector currN = start;
48 NLMISC::CVector uStep = (2.f / size) * uDir;
49 NLMISC::CVector vStep = (2.f / size) * vDir;
51 for (uint y = 0; y < size; ++y)
53 NLMISC::CVector hCurrN = currN;
54 for (uint x = 0; x < size; ++x)
56 destTexel[x + y *size] = f(hCurrN.normed());
57 hCurrN += uStep;
59 currN += vStep;
61 return (uint8 *) map;
64 // utility function : build a side of a cube map, with luminance only
65 static uint8 *BuildCubeMapTexLuminance(const NLMISC::CVector &start,
66 const NLMISC::CVector &uDir,
67 const NLMISC::CVector &vDir,
68 uint size,
69 ICubeMapFunctor &f
72 uint8 *map = new uint8[size * size];
73 uint8 *destTexel = map;
74 NLMISC::CVector currN = start;
75 NLMISC::CVector uStep = (2.f / size) * uDir;
76 NLMISC::CVector vStep = (2.f / size) * vDir;
78 for (uint y = 0; y < size; ++y)
80 NLMISC::CVector hCurrN = currN;
81 for (uint x = 0; x < size; ++x)
83 destTexel[x + y *size] = f(hCurrN.normed()).A;
84 hCurrN += uStep;
86 currN += vStep;
88 return map;
93 CTextureCube *BuildCubeMap(sint mapSize, ICubeMapFunctor &f, bool luminanceOnly /* = false*/, const std::string &shareName /* = "" */)
95 CUniquePtr<CTextureCube> cubeMap(new CTextureCube);
96 CUniquePtr<CTextureMem> faces[6];
98 /// this gives the start (unormalized normal for each face for u,v = 0, 0)
99 static const NLMISC::CVector start[] =
101 NLMISC::CVector(1, 1, 1), /// positive_x
102 NLMISC::CVector(-1, 1, -1), /// negative_x
103 NLMISC::CVector(-1, 1, -1), /// positive_y
104 NLMISC::CVector(-1, -1, 1), /// negative_y
105 NLMISC::CVector(-1, 1, 1), /// positive_z
106 NLMISC::CVector(1, 1, -1) /// negative_z
110 static const NLMISC::CVector uDir[] =
112 NLMISC::CVector::K, /// positive_x
113 - NLMISC::CVector::K, /// negative_x
114 - NLMISC::CVector::I, /// positive_y
115 - NLMISC::CVector::I, /// negative_y
116 NLMISC::CVector::I, /// positive_z
117 -NLMISC::CVector::I, /// negative_z
120 static const NLMISC::CVector vDir[] =
122 - NLMISC::CVector::J, /// positive_x
123 - NLMISC::CVector::J, /// negative_x
124 NLMISC::CVector::K, /// positive_y
125 - NLMISC::CVector::K, /// negative_y
126 NLMISC::CVector::J, /// positive_z
127 NLMISC::CVector::J, /// negative_z
131 uint k;
133 /// build all faces
134 for (k = 0; k < 6; ++k)
136 faces[k].reset(new CTextureMem);
137 uint8 *map = luminanceOnly ? BuildCubeMapTexLuminance(start[k], uDir[k], vDir[k], mapSize, f)
138 : BuildCubeMapTex(start[k], uDir[k], vDir[k], mapSize, f);
139 faces[k]->setPointer(map,
140 mapSize * mapSize * sizeof(uint8) * (luminanceOnly ? 1 : 4),
141 true,
142 false,
143 mapSize,
144 mapSize,
145 luminanceOnly ? CBitmap::Luminance : CBitmap::RGBA
147 if (!shareName.empty())
149 faces[k]->setShareName(shareName + (char) ('0' + k));
153 static const CTextureCube::TFace toTC[] = { CTextureCube::positive_x, CTextureCube::negative_x,
154 CTextureCube::positive_z, CTextureCube::negative_z,
155 CTextureCube::negative_y, CTextureCube::positive_y };
156 /// assign faces
157 for (k = 0; k < 6; ++k)
159 cubeMap->setTexture(toTC[k], faces[k].release());
162 return cubeMap.release();
165 } // NL3D