1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
42 #include "OSGConfig.h"
45 #include "OSGRenderTreeNodePool.h"
46 #include "OSGStateSorter.h"
47 #include "OSGBaseFunctions.h"
48 #include "OSGStateOverride.h"
50 //#define OSG_DUMP_SORTING
54 /*! \class OSG::StateSorter
55 \ingroup GrpSystemRenderingBackend
58 /*-------------------------------------------------------------------------*/
62 StateSorter::StateSorter(void) :
67 _eSortMode (StateKey
),
72 StateSorter::~StateSorter(void)
77 void StateSorter::add(RenderTreeNode
*pNode
,
79 StateOverride
*pStateOverride
,
85 _pNodePool
->create
<RenderTreeNode
>(_uiNodePoolIdx
);
86 RenderTreeNode
*pL1Root
=
87 _pNodePool
->create
<RenderTreeNode
>(_uiNodePoolIdx
);
89 _oSorter
.setupLevel1Root(pL1Root
);
91 _pRoot
->addChild(pL1Root
);
94 if(_eSortMode
== StateKey
)
96 UInt32 uiSortKey
= pState
->getSortKey(uiKeyGen
);
98 if(pStateOverride
!= NULL
)
100 pStateOverride
->updateSortKey(uiSortKey
, uiKeyGen
);
103 // Default Mat Id sorting
104 if(uiSortKey
> State::DefaultKeyMask
)
106 MapSorterIt msIt
= _mFallbackSorter
.lower_bound(uiSortKey
);
108 RenderTreeNode
*pMatElem
= NULL
;
110 if(msIt
== _mFallbackSorter
.end() || msIt
->first
!= uiSortKey
)
112 pMatElem
= _pNodePool
->create
<RenderTreeNode
>(_uiNodePoolIdx
);
114 pMatElem
->setState (pState
);
115 pMatElem
->setStateOverride(pStateOverride
);
117 _mFallbackSorter
.insert(msIt
, std::make_pair(uiSortKey
,
120 _pRoot
->addChild(pMatElem
);
124 pMatElem
= msIt
->second
;
128 pMatElem
->addChild(pNode
);
132 #ifdef OSG_DUMP_SORTING
133 fprintf(stderr
, "Sort by chunk\n");
136 UInt32 uiKey1
= uiSortKey
& State::Key1Mask
;
137 UInt32 uiKey2
= (uiSortKey
& State::Key2Mask
) >> 10;
138 UInt32 uiKey3
= (uiSortKey
& State::Key3Mask
) >> 20;
140 #ifdef OSG_DUMP_SORTING
141 fprintf(stderr
, "Got Keys %d %d %d\n", uiKey1
, uiKey2
, uiKey3
);
144 RenderTreeNode
*pMatElem
= _oSorter
.find(uiKey1
,
148 #ifdef OSG_DUMP_SORTING
149 fprintf(stderr
, "got %p\n", pMatElem
);
154 pMatElem
= _pNodePool
->create
<RenderTreeNode
>(_uiNodePoolIdx
);
156 // pMatElem->setState (pState );
157 // pMatElem->setStateOverride(pStateOverride);
159 _oSorter
.insert( uiKey1
,
165 #ifdef OSG_DUMP_SORTING
166 fprintf(stderr
, "Insert %p\n", pMatElem
);
170 pMatElem
->addChild(pNode
);
172 pNode
->setState (pState
);
173 pNode
->setStateOverride(pStateOverride
);
176 else if(_eSortMode
== ScalarKey
)
178 if(_pRoot
->getFirstChild() == NULL
)
180 _pRoot
->addChild(pNode
);
184 RenderTreeNode
*pCurrent
= _pRoot
->getFirstChild();
186 RenderTreeNode
*pLast
= NULL
;
191 if(pNode
->getScalar() > pCurrent
->getScalar())
194 pCurrent
= pCurrent
->getBrother();
201 } while(bFound
== false &&
208 _pRoot
->insertFirstChild(pNode
);
212 _pRoot
->insertChildAfter(pLast
, pNode
);
217 _pRoot
->addChild(pNode
);
223 FFATAL(("StateSorter::add: unknown sort mode: %d!\n", _eSortMode
));