00001 00005 /* Copyright, 2001 Nevrax Ltd. 00006 * 00007 * This file is part of NEVRAX NEL. 00008 * NEVRAX NEL is free software; you can redistribute it and/or modify 00009 * it under the terms of the GNU General Public License as published by 00010 * the Free Software Foundation; either version 2, or (at your option) 00011 * any later version. 00012 00013 * NEVRAX NEL is distributed in the hope that it will be useful, but 00014 * WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * General Public License for more details. 00017 00018 * You should have received a copy of the GNU General Public License 00019 * along with NEVRAX NEL; see the file COPYING. If not, write to the 00020 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 00021 * MA 02111-1307, USA. 00022 */ 00023 00024 #include "std3d.h" 00025 00026 #include "nel/3d/landscape_face_vector_manager.h" 00027 #include "nel/misc/debug.h" 00028 00029 00030 using namespace std; 00031 using namespace NLMISC; 00032 00033 00034 namespace NL3D 00035 { 00036 00037 00038 // *************************************************************************** 00039 #define NL3D_FACE_VECTOR_NUMBLOCK 33 00040 00041 // *************************************************************************** 00042 CLandscapeFaceVectorManager::CLandscapeFaceVectorManager() 00043 { 00044 // Allow 2^32 triangles at max. each list has at max 2^i triangles. 00045 _Blocks.resize(NL3D_FACE_VECTOR_NUMBLOCK, NULL); 00046 } 00047 00048 // *************************************************************************** 00049 CLandscapeFaceVectorManager::~CLandscapeFaceVectorManager() 00050 { 00051 purge(); 00052 } 00053 00054 // *************************************************************************** 00055 void CLandscapeFaceVectorManager::purge() 00056 { 00057 for(uint i=0; i<NL3D_FACE_VECTOR_NUMBLOCK; i++) 00058 { 00059 TLandscapeIndexType *ptr= _Blocks[i]; 00060 // For each node in list, delete. 00061 while(ptr) 00062 { 00063 // Get the ptr on next free list. 00064 TLandscapeIndexType *next= *(TLandscapeIndexType**)ptr; 00065 delete [] ptr; 00066 ptr= next; 00067 } 00068 // list is empty. 00069 _Blocks[i]= NULL; 00070 } 00071 } 00072 00073 // *************************************************************************** 00074 uint CLandscapeFaceVectorManager::getBlockIdFromNumTri(uint numTris) 00075 { 00076 return getPowerOf2(numTris); 00077 } 00078 00079 // *************************************************************************** 00080 TLandscapeIndexType *CLandscapeFaceVectorManager::createFaceVector(uint numTri) 00081 { 00082 // get the BlockId from the number of tri in this fv 00083 uint blockId= getBlockIdFromNumTri(numTri); 00084 00085 // If no more free FaceVector, allocate. 00086 if(_Blocks[blockId]==NULL) 00087 { 00088 // Allocate a block of max tris. +1 is for the NumTris entry at index 0. 00089 uint numTriMax= 1<<blockId; 00090 // allocate max of (sizeof(uint32*), (numTriMax*3+1)*sizeof(uint32)); 00091 uint sizeInByteToAllocate= max(sizeof(TLandscapeIndexType*), (numTriMax*3 + 1)*sizeof(TLandscapeIndexType)); 00092 _Blocks[blockId]= new TLandscapeIndexType[(sizeInByteToAllocate + (sizeof(TLandscapeIndexType) - 1)) /sizeof(TLandscapeIndexType)]; 00093 // Init it as a free faceVector, with no Next. 00094 *(TLandscapeIndexType**)_Blocks[blockId]= NULL; 00095 } 00096 00097 // Pop a FaceVector from the free list. 00098 TLandscapeIndexType *ret= _Blocks[blockId]; 00099 // Make the head list point to next 00100 _Blocks[blockId]= *(TLandscapeIndexType**)ret; 00101 00102 // There is numTri triangles. 00103 *ret= numTri; 00104 00105 return ret; 00106 } 00107 00108 // *************************************************************************** 00109 void CLandscapeFaceVectorManager::deleteFaceVector(TLandscapeIndexType *fv) 00110 { 00111 // get the BlockId from the number of tri in this fv (ie *fv) 00112 uint blockId= getBlockIdFromNumTri(*fv); 00113 00114 // Append this block to the free list. Write the ptr directly on fv. 00115 *(TLandscapeIndexType**)fv= _Blocks[blockId]; 00116 _Blocks[blockId]= fv; 00117 } 00118 00119 00120 } // NL3D
1.6.1