Merge branch 'main/rendor-staging' into fixes
[ryzomcore.git] / nel / src / 3d / landscape_face_vector_manager.cpp
blob0ba4b07cf960b03e72709b63aab58cca06235a17
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/landscape_face_vector_manager.h"
20 #include "nel/misc/debug.h"
23 using namespace std;
24 using namespace NLMISC;
26 #ifdef DEBUG_NEW
27 #define new DEBUG_NEW
28 #endif
30 namespace NL3D
34 // ***************************************************************************
35 #define NL3D_FACE_VECTOR_NUMBLOCK 33
37 // ***************************************************************************
38 CLandscapeFaceVectorManager::CLandscapeFaceVectorManager()
40 // Allow 2^32 triangles at max. each list has at max 2^i triangles.
41 _Blocks.resize(NL3D_FACE_VECTOR_NUMBLOCK, NULL);
44 // ***************************************************************************
45 CLandscapeFaceVectorManager::~CLandscapeFaceVectorManager()
47 purge();
50 // ***************************************************************************
51 void CLandscapeFaceVectorManager::purge()
53 for(uint i=0; i<NL3D_FACE_VECTOR_NUMBLOCK; i++)
55 TLandscapeIndexType *ptr= _Blocks[i];
56 // For each node in list, delete.
57 while(ptr)
59 // Get the ptr on next free list.
60 TLandscapeIndexType *next= *(TLandscapeIndexType**)ptr;
61 delete [] ptr;
62 ptr= next;
64 // list is empty.
65 _Blocks[i]= NULL;
69 // ***************************************************************************
70 uint CLandscapeFaceVectorManager::getBlockIdFromNumTri(uint numTris)
72 return getPowerOf2(numTris);
75 // ***************************************************************************
76 TLandscapeIndexType *CLandscapeFaceVectorManager::createFaceVector(uint numTri)
78 // get the BlockId from the number of tri in this fv
79 uint blockId= getBlockIdFromNumTri(numTri);
81 // If no more free FaceVector, allocate.
82 if(_Blocks[blockId]==NULL)
84 // Allocate a block of max tris. +1 is for the NumTris entry at index 0.
85 uint numTriMax= 1<<blockId;
86 // allocate max of (sizeof(uint32*), (numTriMax*3+1)*sizeof(uint32));
87 uint sizeInByteToAllocate= (uint)max(sizeof(TLandscapeIndexType*), (numTriMax*3 + 1)*sizeof(TLandscapeIndexType));
88 _Blocks[blockId]= new TLandscapeIndexType[(sizeInByteToAllocate + (sizeof(TLandscapeIndexType) - 1)) /sizeof(TLandscapeIndexType)];
89 // Init it as a free faceVector, with no Next.
90 *(TLandscapeIndexType**)_Blocks[blockId]= NULL;
93 // Pop a FaceVector from the free list.
94 TLandscapeIndexType *ret= _Blocks[blockId];
95 // Make the head list point to next
96 _Blocks[blockId]= *(TLandscapeIndexType**)ret;
98 // There is numTri triangles.
99 *ret= numTri;
101 return ret;
104 // ***************************************************************************
105 void CLandscapeFaceVectorManager::deleteFaceVector(TLandscapeIndexType *fv)
107 // get the BlockId from the number of tri in this fv (ie *fv)
108 uint blockId= getBlockIdFromNumTri(*fv);
110 // Append this block to the free list. Write the ptr directly on fv.
111 *(TLandscapeIndexType**)fv= _Blocks[blockId];
112 _Blocks[blockId]= fv;
116 } // NL3D