00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <geomipmap/MipMapPatchIndexs.h>
00022 #include <GLEXT/GLStateExtension.h>
00023 #include <GLEXT/GLVertexBufferObject.h>
00024 #include <graph/OptionsDisplay.h>
00025 #include <common/Logger.h>
00026
00027 MipMapPatchIndexs::IndexLevel::IndexLevel()
00028 {
00029 }
00030
00031 MipMapPatchIndexs::IndexLevel::~IndexLevel()
00032 {
00033 while (!borderIndexs_.empty())
00034 {
00035 MipMapPatchIndex *index = borderIndexs_.back();
00036 borderIndexs_.pop_back();
00037 delete index;
00038 }
00039 }
00040
00041 MipMapPatchIndexs::MipMapPatchIndexs() :
00042 bufferObject_(0)
00043 {
00044 }
00045
00046 MipMapPatchIndexs::~MipMapPatchIndexs()
00047 {
00048 }
00049
00050 void MipMapPatchIndexs::generate(int size, int totalsize, unsigned int totallods)
00051 {
00052 while (!levels_.empty())
00053 {
00054 IndexLevel *level = levels_.back();
00055 levels_.pop_back();
00056 delete level;
00057 }
00058
00059 unsigned int totalVerts = 0;
00060 for (int lod=1; lod<=size; lod*=2)
00061 {
00062 IndexLevel *level = new IndexLevel();
00063 levels_.push_back(level);
00064
00065
00066 for (unsigned int borders=0; borders<=4095; borders++)
00067 {
00068 MipMapPatchIndex *index = new MipMapPatchIndex();
00069 index->generate(size, totalsize, lod, borders, totallods);
00070 level->borderIndexs_.push_back(index);
00071 if (index->getIndices())
00072 {
00073 totalVerts += index->getSize();
00074 }
00075 }
00076 }
00077
00078 int totalBufferSizeBytes = totalVerts * sizeof(unsigned short);
00079 Logger::log(S3D::formatStringBuffer(
00080 "Index Memory Size : %u bytes", totalBufferSizeBytes));
00081
00082
00083 if (GLStateExtension::hasVBO())
00084 {
00085 if (!bufferObject_ || bufferObject_->get_map_size() != totalBufferSizeBytes)
00086 {
00087 delete bufferObject_;
00088 bufferObject_ = new GLVertexBufferObject(true);
00089 bufferObject_->init_data(totalBufferSizeBytes, 0, GL_STATIC_DRAW);
00090 }
00091
00092 unsigned int offsetBytes = 0;
00093 int i = 0;
00094 for (int lod=1; lod<=size; lod*=2, i++)
00095 {
00096 IndexLevel *level = levels_[i];
00097 for (unsigned int borders=0; borders<=4095; borders++)
00098 {
00099 MipMapPatchIndex *index = level->borderIndexs_[borders];
00100 if (index->getIndices())
00101 {
00102 unsigned int bufferSizeBytes = index->getSize() * sizeof(unsigned short);
00103 bufferObject_->init_sub_data(offsetBytes, bufferSizeBytes, index->getIndices());
00104 index->setBufferOffSet(offsetBytes);
00105 offsetBytes += bufferSizeBytes;
00106 }
00107 }
00108 }
00109 }
00110 }