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/>.
19 #include "nel/3d/point_light_named_array.h"
20 #include "nel/3d/scene.h"
24 using namespace NLMISC
;
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
;
49 bool operator<(const CPointLightNamedSort
&b
) const
51 if (PointLight
->AnimatedLight
< b
.PointLight
->AnimatedLight
)
53 if (PointLight
->AnimatedLight
> b
.PointLight
->AnimatedLight
)
55 return (PointLight
->LightGroup
< b
.PointLight
->LightGroup
);
60 // ***************************************************************************
61 void CPointLightNamedArray::clear()
64 _PointLightGroupMap
.clear();
68 // ***************************************************************************
69 void CPointLightNamedArray::build(const std::vector
<CPointLightNamed
> &pointLights
, std::vector
<uint
> &indexRemap
)
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
;
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
;
98 _PointLightGroupMap
.clear();
99 if (!_PointLights
.empty())
104 // for all sorted pointLights
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
) )
116 _PointLightGroupMap
.back ().EndId
= i
;
119 _PointLightGroupMap
.push_back (CPointLightGroup ());
120 _PointLightGroupMap
.back ().StartId
= i
;
121 _PointLightGroupMap
.back ().AnimationLight
= curName
;
122 _PointLightGroupMap
.back ().LightGroup
= curGroup
;
124 precGroup
= curGroup
;
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 ();
138 for (i
=0; i
<count
; i
++)
141 CPointLightGroup
&lightGroup
= _PointLightGroupMap
[i
];
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
);
175 std::map
<string
, CPointLightGroupV0
> oldMap
;
176 f
.serialCont(oldMap
);
180 f
.serialCont(_PointLightGroupMap
);
185 // ***************************************************************************
186 void CPointLightNamedArray::initAnimatedLightIndex (const CScene
&scene
)
188 // Search in the map.
189 const uint count
= (uint
)_PointLightGroupMap
.size ();
191 for (i
=0; i
<count
; i
++)
194 CPointLightGroup
&lightGroup
= _PointLightGroupMap
[i
];
195 lightGroup
.AnimationLightIndex
= scene
.getAnimatedLightNameToIndex (lightGroup
.AnimationLight
);