forked from pent0/ka3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLightSorter.cpp
74 lines (59 loc) · 1.54 KB
/
LightSorter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <hgr/LightSorter.h>
#include <lang/algorithm/sort.h>
#include <config.h>
USING_NAMESPACE(gr)
USING_NAMESPACE(lang)
USING_NAMESPACE(math)
BEGIN_NAMESPACE(hgr)
LightSorter::LightSorter()
{
}
Array<Light*>& LightSorter::getLightsByDistance( const float3& worldpos, int maxlights )
{
// compute light distance (squared) to object
int lights = m_lightData.size();
m_lightSorter.resize( lights );
for ( int i = 0 ; i < lights ; ++i )
{
m_lightData[i].v = (m_lightData[i].wpos - worldpos).lengthSquared();
m_lightSorter[i].data = &m_lightData[i];
}
LANG_SORT( m_lightSorter.begin(), m_lightSorter.end() );
// return lights
if ( lights > maxlights )
lights = maxlights;
m_lights.resize( lights );
for ( int i = 0 ; i < lights ; ++i )
m_lights[i] = m_lightSorter[i].data->obj;
return m_lights;
}
void LightSorter::addLight( Light* obj )
{
LightData objdata;
objdata.v = 0.f;
objdata.obj = obj;
objdata.wpos = obj->worldTransform().translation();
m_lightData.add( objdata );
}
void LightSorter::removeLights()
{
m_lightData.clear();
m_lightSorter.clear();
m_lights.clear();
}
void LightSorter::collectLights( Node* root )
{
assert( !root->parent() );
m_lightData.clear();
for ( Node* node = root ; node != 0 ; node = node->next(root) )
{
if ( Node::NODE_LIGHT == node->classId() )
{
Light* obj = static_cast<Light*>( node );
if ( obj->enabled() )
addLight( obj );
}
}
}
END_NAMESPACE() // hgr
// Copyright (C) 2004-2006 Pixelgene Ltd. All rights reserved. Consult your license regarding permissions and restrictions.