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/>.
18 #include "nel/3d/texture_near.h"
20 using namespace NLMISC
;
30 // ***************************************************************************
31 CTextureNear::CTextureNear(sint size
)
33 nlassert(size
>=NL_TILE_LIGHTMAP_SIZE
);
34 nlassert(NLMISC::isPowerOf2(size
));
36 // The near texture always reside in memory...
37 // NB: this is simplier like that, and this is not a problem, since only 1 or 2 Mo are allocated :o)
40 CBitmap::resize(size
, size
, CBitmap::RGBA
);
41 // Format of texture, 32 bits and no mipmaps.
42 setUploadFormat(ITexture::RGBA8888
);
43 setFilterMode(ITexture::Linear
, ITexture::LinearMipMapOff
);
45 // Fill the array of free tiles.
46 uint nbTilesByLine
= size
/NL_TILE_LIGHTMAP_SIZE
;
47 _FreeTiles
.resize(nbTilesByLine
*nbTilesByLine
);
48 _AvailableTiles
.resize(nbTilesByLine
*nbTilesByLine
);
49 for(sint i
=0;i
<(sint
)_FreeTiles
.size();i
++)
51 // This tile is free!!
53 _AvailableTiles
[i
]= true;
58 // ***************************************************************************
59 sint
CTextureNear::getNbAvailableTiles()
61 return (sint
)_FreeTiles
.size();
63 // ***************************************************************************
64 uint
CTextureNear::getTileAndFillRect(CRGBA map
[NL_TILE_LIGHTMAP_SIZE
*NL_TILE_LIGHTMAP_SIZE
])
66 nlassert(getNbAvailableTiles()>0);
67 uint id
= _FreeTiles
.back();
68 // This tile is now more free.
69 _AvailableTiles
[id
]= false;
70 _FreeTiles
.pop_back();
78 // ***************************************************************************
79 void CTextureNear::refillRect(uint id
, CRGBA map
[NL_TILE_LIGHTMAP_SIZE
*NL_TILE_LIGHTMAP_SIZE
])
81 uint dstWidth
= getWidth();
82 // Copy the map into the texture
83 uint nbTilesByLine
= dstWidth
/NL_TILE_LIGHTMAP_SIZE
;
84 uint s
= id
% nbTilesByLine
;
85 uint t
= id
/ nbTilesByLine
;
86 s
*= NL_TILE_LIGHTMAP_SIZE
;
87 t
*= NL_TILE_LIGHTMAP_SIZE
;
89 CRGBA
*dst
= (CRGBA
*)getPixels().getPtr();
91 for(sint n
= NL_TILE_LIGHTMAP_SIZE
;n
>0;n
--, src
+= NL_TILE_LIGHTMAP_SIZE
, dst
+= dstWidth
)
93 memcpy(dst
, src
, NL_TILE_LIGHTMAP_SIZE
*sizeof(CRGBA
));
96 // Invalidate the rectangle.
97 ITexture::touchRect(CRect(s
, t
, NL_TILE_LIGHTMAP_SIZE
, NL_TILE_LIGHTMAP_SIZE
));
100 // ***************************************************************************
101 void CTextureNear::releaseTile(uint id
)
103 nlassert(!_AvailableTiles
[id
]);
104 // This tile is now free.
105 _AvailableTiles
[id
]= true;
106 // insert this tile at the end of the free list.
107 _FreeTiles
.push_back(id
);