GraphicalLandscapeMap.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 <landscape/GraphicalLandscapeMap.h>
00022 #include <graph/OptionsDisplay.h>
00023 #include <GLEXT/GLStateExtension.h>
00024 #include <GLEXT/GLVertexBufferObject.h>
00025 
00026 GraphicalLandscapeMap::GraphicalLandscapeMap() : 
00027         heightData_(0), bufferObject_(0), 
00028         width_(0), height_(0), bufferSizeBytes_(0)
00029 {
00030 }
00031 
00032 GraphicalLandscapeMap::~GraphicalLandscapeMap()
00033 {
00034         delete [] heightData_;
00035 }
00036 
00037 void GraphicalLandscapeMap::create(const int width, const int height)
00038 {
00039         width_ = width; 
00040         height_ = height;
00041 
00042         delete [] heightData_;
00043         heightData_ = new HeightData[(width_ + 1) * (height_ + 1)];
00044         int patchVolume = (width_ + 1) * (height_ + 1);
00045         bufferSizeBytes_ = patchVolume * sizeof(GraphicalLandscapeMap::HeightData);
00046 
00047         if (GLStateExtension::hasVBO())
00048         {
00049                 if (!bufferObject_ || bufferObject_->get_map_size() != bufferSizeBytes_) 
00050                 {
00051                         delete bufferObject_;
00052                         bufferObject_ = new GLVertexBufferObject();
00053                         bufferObject_->init_data(bufferSizeBytes_, heightData_, GL_STATIC_DRAW);
00054                 }
00055         }
00056 
00057         reset();
00058 }
00059 
00060 void GraphicalLandscapeMap::reset()
00061 {
00062         HeightData *current = heightData_;
00063 
00064         int texTileX = width_ / 16;
00065         int texTileY = height_ / 16;
00066 
00067         for (int y=0; y<=height_; y++)
00068         {
00069                 for (int x=0; x<=width_; x++)
00070                 {
00071                         current->floatPosition[0] = float(x);
00072                         current->floatPosition[1] = float(y);
00073                         current->floatPosition[2] = 0.0f;
00074 
00075                         current->floatNormal[0] = 0.0f;
00076                         current->floatNormal[1] = 0.0f;
00077                         current->floatNormal[2] = 1.0f;
00078 
00079                         current->texCoord1x = float(x) / float(width_);
00080                         current->texCoord1y = float(y) / float(height_);
00081 
00082                         current->texCoord2x = float(x) / float(width_) * float(texTileX);
00083                         current->texCoord2y = float(y) / float(height_) * float(texTileY);
00084 
00085                         current++;
00086                 }
00087         }
00088 }
00089 
00090 void GraphicalLandscapeMap::setHeight(int w, int h, float height)
00091 {
00092         DIALOG_ASSERT(w >= 0 && h >= 0 && w<=width_ && h<=height_);
00093 
00094         HeightData &data = heightData_[(width_+1) * h + w];
00095         data.floatPosition[2] = height;
00096 }
00097 
00098 void GraphicalLandscapeMap::setNormal(int w, int h, Vector &normal)
00099 {
00100         DIALOG_ASSERT(w >= 0 && h >= 0 && w<=width_ && h<=height_);
00101 
00102         HeightData &data = heightData_[(width_+1) * h + w];
00103         data.floatNormal = normal;
00104 }
00105 
00106 void GraphicalLandscapeMap::updateWholeBuffer()
00107 {
00108         // Generate the VBO if any
00109         if (!GLStateExtension::hasVBO()) return;
00110 
00111         bufferObject_->init_data(bufferSizeBytes_, heightData_, GL_STATIC_DRAW);
00112 }

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