Merge branch 'main/rendor-staging' into fixes
[ryzomcore.git] / nel / src / 3d / point_light_named_array.cpp
blob0c6c34cc9b321a583be57f4a3ae28392af11427a
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/point_light_named_array.h"
20 #include "nel/3d/scene.h"
23 using namespace std;
24 using namespace NLMISC;
26 #ifdef DEBUG_NEW
27 #define new DEBUG_NEW
28 #endif
30 namespace NL3D {
33 // ***************************************************************************
34 CPointLightNamedArray::CPointLightNamedArray()
36 /* ***********************************************
37 * WARNING: This Class/Method must be thread-safe (ctor/dtor/serial): no static access for instance
38 * It can be loaded/called through CAsyncFileManager for instance
39 * ***********************************************/
43 // ***************************************************************************
44 struct CPointLightNamedSort
46 const CPointLightNamed *PointLight;
47 uint SrcId;
49 bool operator<(const CPointLightNamedSort &b) const
51 if (PointLight->AnimatedLight < b.PointLight->AnimatedLight)
52 return true;
53 if (PointLight->AnimatedLight > b.PointLight->AnimatedLight)
54 return false;
55 return (PointLight->LightGroup < b.PointLight->LightGroup);
60 // ***************************************************************************
61 void CPointLightNamedArray::clear()
63 _PointLights.clear();
64 _PointLightGroupMap.clear();
68 // ***************************************************************************
69 void CPointLightNamedArray::build(const std::vector<CPointLightNamed> &pointLights, std::vector<uint> &indexRemap)
71 uint i;
73 // sort by name.
74 //----------
75 // Fill Sort array
76 vector<CPointLightNamedSort> pointLightSorts;
77 pointLightSorts.resize(pointLights.size());
78 for(i=0; i<pointLightSorts.size(); i++)
80 pointLightSorts[i].PointLight= &pointLights[i];
81 pointLightSorts[i].SrcId= i;
83 // sort
84 sort(pointLightSorts.begin(), pointLightSorts.end());
85 // Copy data, and Fill indexRemap array
86 _PointLights.resize(pointLights.size());
87 indexRemap.resize(pointLights.size());
88 for(i=0; i<pointLightSorts.size(); i++)
90 // Copy yhe PointLight to its new destination
91 _PointLights[i]= *pointLightSorts[i].PointLight;
92 // set the new index at the old position.
93 indexRemap[pointLightSorts[i].SrcId]= i;
96 // Regroup.
97 // ---------
98 _PointLightGroupMap.clear();
99 if (!_PointLights.empty())
101 bool first= true;
102 string precName;
103 uint precGroup= 0;
104 // for all sorted pointLights
105 uint i;
106 for(i=0;i<_PointLights.size();i++)
108 const std::string &curName = _PointLights[i].AnimatedLight;
109 const uint curGroup = _PointLights[i].LightGroup;
110 if ( first || (precName!=curName) || (precGroup != curGroup) )
112 // End last group
113 if(first)
114 first= false;
115 else
116 _PointLightGroupMap.back ().EndId= i;
118 // Start new group
119 _PointLightGroupMap.push_back (CPointLightGroup ());
120 _PointLightGroupMap.back ().StartId= i;
121 _PointLightGroupMap.back ().AnimationLight = curName;
122 _PointLightGroupMap.back ().LightGroup = curGroup;
123 precName = curName;
124 precGroup = curGroup;
127 // End last group.
128 _PointLightGroupMap.back ().EndId= i;
132 // ***************************************************************************
133 void CPointLightNamedArray::setPointLightFactor(const CScene &scene)
135 // Search in the map.
136 const uint count = (uint)_PointLightGroupMap.size ();
137 uint i;
138 for (i=0; i<count; i++)
140 // Ref
141 CPointLightGroup &lightGroup = _PointLightGroupMap[i];
143 // Get the factor
144 CRGBA factorAnimated= scene.getAnimatedLightFactor (lightGroup.AnimationLightIndex, lightGroup.LightGroup);
145 CRGBA factorNotAnimated= scene.getLightmapGroupColor (lightGroup.LightGroup);;
147 // Found the group. what entries in the array?
148 uint startId= lightGroup.StartId;
149 const uint endId= lightGroup.EndId;
150 nlassert(endId<=_PointLights.size());
152 // for all entries, setLightFactor
153 for(uint i=startId;i<endId;i++)
155 _PointLights[i].setLightFactor (factorAnimated, factorNotAnimated);
161 // ***************************************************************************
162 void CPointLightNamedArray::serial(NLMISC::IStream &f)
164 /* ***********************************************
165 * WARNING: This Class/Method must be thread-safe (ctor/dtor/serial): no static access for instance
166 * It can be loaded/called through CAsyncFileManager for instance
167 * ***********************************************/
169 sint ver = f.serialVersion(1);
171 f.serialCont(_PointLights);
173 if (ver == 0)
175 std::map<string, CPointLightGroupV0> oldMap;
176 f.serialCont(oldMap);
178 else
180 f.serialCont(_PointLightGroupMap);
185 // ***************************************************************************
186 void CPointLightNamedArray::initAnimatedLightIndex (const CScene &scene)
188 // Search in the map.
189 const uint count = (uint)_PointLightGroupMap.size ();
190 uint i;
191 for (i=0; i<count; i++)
193 // Ref
194 CPointLightGroup &lightGroup = _PointLightGroupMap[i];
195 lightGroup.AnimationLightIndex = scene.getAnimatedLightNameToIndex (lightGroup.AnimationLight);
199 } // NL3D