Merge branch 'main/rendor-staging' into fixes
[ryzomcore.git] / nel / src / 3d / vegetable_blend_layer_model.cpp
blob9c3b9dd54173ea6c10ed88b0398fc62560740900
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/vegetable_blend_layer_model.h"
20 #include "nel/3d/vegetable_manager.h"
21 #include "nel/3d/vegetable_sort_block.h"
22 #include "nel/3d/render_trav.h"
23 #include "nel/3d/clip_trav.h"
24 #include "nel/misc/debug.h"
25 #include "nel/misc/hierarchical_timer.h"
26 #include "nel/3d/scene.h"
29 using namespace NLMISC;
31 #ifdef DEBUG_NEW
32 #define new DEBUG_NEW
33 #endif
35 namespace NL3D {
38 // ***************************************************************************
39 void CVegetableBlendLayerModel::registerBasic()
41 CScene::registerModel(VegetableBlendLayerModelId, TransformId, CVegetableBlendLayerModel::creator);
45 // ***************************************************************************
46 CVegetableBlendLayerModel::CVegetableBlendLayerModel()
48 VegetableManager= NULL;
50 // The model must always be renderer in transparency pass only.
51 setTransparency(true);
52 setOpacity(false);
54 // The model is of course renderable
55 CTransform::setIsRenderable(true);
59 // ***************************************************************************
60 void CVegetableBlendLayerModel::setWorldPos(const CVector &pos)
62 // setup directly the local matrix.
63 _LocalMatrix.setPos(pos);
65 // setup directly the world matrix.
66 _WorldMatrix.setPos(pos);
71 // ***************************************************************************
72 void CVegetableBlendLayerModel::render(IDriver *driver)
74 H_AUTO( NL3D_Vegetable_Render );
76 nlassert(VegetableManager);
78 if(SortBlocks.empty())
79 return;
81 // Setup VegetableManager renderState (like pre-setuped material)
82 //==================
83 VegetableManager->setupRenderStateForBlendLayerModel(driver);
86 // Render SortBlocks of this layer
87 //==================
88 uint rdrPass= NL3D_VEGETABLE_RDRPASS_UNLIT_2SIDED_ZSORT;
90 // first time, activate the hard VB.
91 bool precVBHardMode= true;
92 CVegetableVBAllocator *vbAllocator= &VegetableManager->getVBAllocatorForRdrPassAndVBHardMode(rdrPass, 1);
93 vbAllocator->activate();
95 // profile
96 CPrimitiveProfile ppIn, ppOut;
97 driver->profileRenderedPrimitives(ppIn, ppOut);
98 uint precNTriRdr= ppOut.NTriangles;
100 // render from back to front the list setuped in CVegetableManager::render()
101 for(uint i=0; i<SortBlocks.size();i++)
103 CVegetableSortBlock *ptrSortBlock= SortBlocks[i];
105 // change of VertexBuffer (soft / hard) if needed.
106 if(ptrSortBlock->ZSortHardMode != precVBHardMode)
108 // setup new VB for hardMode.
109 CVegetableVBAllocator *vbAllocator= &VegetableManager->getVBAllocatorForRdrPassAndVBHardMode(rdrPass, ptrSortBlock->ZSortHardMode);
110 vbAllocator->activate();
111 // prec.
112 precVBHardMode= ptrSortBlock->ZSortHardMode;
115 // render him. we are sure that size > 0, because tested before.
116 driver->activeIndexBuffer(ptrSortBlock->_SortedTriangleArray);
117 #ifdef NL_DEBUG
118 if (ptrSortBlock->ZSortHardMode)
120 nlassert(ptrSortBlock->_SortedTriangleArray.getFormat() == CIndexBuffer::Indices16);
122 else
124 nlassert(ptrSortBlock->_SortedTriangleArray.getFormat() == CIndexBuffer::Indices32);
126 #endif
127 driver->renderSimpleTriangles(
128 ptrSortBlock->_SortedTriangleIndices[ptrSortBlock->_QuadrantId],
129 ptrSortBlock->_NTriangles);
132 // add number of triangles rendered with vegetable manager.
133 driver->profileRenderedPrimitives(ppIn, ppOut);
134 VegetableManager->_NumVegetableFaceRendered+= ppOut.NTriangles-precNTriRdr;
137 // refresh list now!
138 // We must do it here, because if CVegetableManager::render() is no more called (eg: disabled),
139 // then the blend layers models must do nothing.
140 SortBlocks.clear();
143 // Reset RenderState.
144 //==================
145 VegetableManager->exitRenderStateForBlendLayerModel(driver);
150 // ***************************************************************************
151 void CVegetableBlendLayerModel::traverseRender()
153 CRenderTrav &rTrav= getOwnerScene()->getRenderTrav();
154 render(rTrav.getDriver());
158 } // NL3D