MipMapPatchIndexs.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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                 // borders
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         // Store this array in a vertex buffer (if available)
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 }

Generated on Mon Feb 16 15:14:38 2009 for Scorched3D by  doxygen 1.5.3