1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
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.
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/>.
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"
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
,
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());
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
,
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
;
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
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),
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
};
157 for (k
= 0; k
< 6; ++k
)
159 cubeMap
->setTexture(toTC
[k
], faces
[k
].release());
162 return cubeMap
.release();