Merge branch 'main/rendor-staging' into fixes
[ryzomcore.git] / nel / src / 3d / ig_surface_light.cpp
blobef4a59a46704a85beb5104f0ac251b7d90cf1637
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"
19 #include "nel/3d/ig_surface_light.h"
21 #ifdef DEBUG_NEW
22 #define new DEBUG_NEW
23 #endif
25 namespace NL3D
28 // ***************************************************************************
29 CIGSurfaceLight::CIGSurfaceLight()
31 /* ***********************************************
32 * WARNING: This Class/Method must be thread-safe (ctor/dtor/serial): no static access for instance
33 * It can be loaded/called through CAsyncFileManager for instance
34 * ***********************************************/
36 _Owner= NULL;
37 _CellSize= 1;
38 _OOCellSize= 1;
41 // ***************************************************************************
42 void CIGSurfaceLight::setOwner(CInstanceGroup *owner)
44 nlassert(owner!=NULL);
45 _Owner= owner;
48 // ***************************************************************************
49 void CIGSurfaceLight::build(const TRetrieverGridMap &retrieverGridMap, float cellSize,
50 const std::vector<uint> &plRemap)
52 _RetrieverGridMap= retrieverGridMap;
53 nlassert(cellSize>0);
54 _CellSize= cellSize;
55 _OOCellSize= 1.f/_CellSize;
57 // remap indices.
58 ItRetrieverGridMap it;
59 for(it= _RetrieverGridMap.begin(); it!=_RetrieverGridMap.end(); it++)
61 // For all grids of this retriever
62 for(uint iGrid= 0; iGrid<it->second.Grids.size(); iGrid++)
64 CSurfaceLightGrid &grid= it->second.Grids[iGrid];
66 // For all cells of this grid.
67 for(uint iCell= 0; iCell<grid.Cells.size(); iCell++)
69 CSurfaceLightGrid::CCellCorner &cell= grid.Cells[iCell];
71 // For all point light id.
72 for(uint lid= 0; lid<CSurfaceLightGrid::NumLightPerCorner; lid++)
74 if(cell.Light[lid] == 0xFF)
75 break;
76 else
77 cell.Light[lid]= plRemap[cell.Light[lid]];
80 // remap ambient light
81 if(cell.LocalAmbientId!=0xFF)
82 cell.LocalAmbientId= plRemap[cell.LocalAmbientId];
88 // ***************************************************************************
89 void CIGSurfaceLight::clear()
91 _RetrieverGridMap.clear();
92 _CellSize= 1;
93 _OOCellSize= 1;
96 // ***************************************************************************
97 void CIGSurfaceLight::serial(NLMISC::IStream &f)
99 /* ***********************************************
100 * WARNING: This Class/Method must be thread-safe (ctor/dtor/serial): no static access for instance
101 * It can be loaded/called through CAsyncFileManager for instance
102 * ***********************************************/
105 Version 1:
106 - The retriever grid map is now a map<uint,CRetrieverLightGrid>. Discard compatibility but.
108 sint ver= f.serialVersion(1);
110 f.serial(_CellSize);
111 f.serial(_OOCellSize);
112 if(ver<1)
114 std::map<std::string, CRetrieverLightGrid> oldFormatRetrieverGridMap;
115 f.serialCont(oldFormatRetrieverGridMap);
116 _RetrieverGridMap.clear();
118 else
120 f.serialCont(_RetrieverGridMap);
124 // ***************************************************************************
125 bool CIGSurfaceLight::getStaticLightSetup(NLMISC::CRGBA sunAmbient, uint retrieverIdentifier, sint surfaceId, const CVector &localPos,
126 std::vector<CPointLightInfluence> &pointLightList, uint8 &sunContribution, NLMISC::CRGBA &localAmbient)
128 nlassert(_Owner);
130 // default
131 sunContribution= 255;
132 localAmbient= sunAmbient;
134 ItRetrieverGridMap it;
135 it= _RetrieverGridMap.find(retrieverIdentifier);
136 // If bad ident
137 if(it==_RetrieverGridMap.end())
138 return false;
139 CRetrieverLightGrid &rlg= it->second;
140 // if bad surfaceId
141 if(surfaceId<0 || surfaceId>= (sint)rlg.Grids.size())
142 return false;
144 // Else, ok, get it.
145 rlg.Grids[surfaceId].getStaticLightSetup(sunAmbient, localPos, pointLightList, sunContribution, *this, localAmbient);
146 return true;
152 } // NL3D