WaterWaveDistance.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 <water/WaterWaveDistance.h>
00022 #include <client/ScorchedClient.h>
00023 #include <landscapemap/LandscapeMaps.h>
00024 #include <lang/LangResource.h>
00025 #include <common/ProgressCounter.h>
00026 
00027 WaterWaveDistance::WaterWaveDistance() : waveDistance_(0)
00028 {
00029 }
00030 
00031 WaterWaveDistance::~WaterWaveDistance()
00032 {
00033 }
00034 
00035 void WaterWaveDistance::generate(
00036         int mapWidth, int mapHeight, float waterHeight,
00037         ProgressCounter *counter)
00038 {
00039         if (counter) counter->setNewOp(LANG_RESOURCE("WAVES_DISTANCE", "Waves Distance"));
00040 
00041         // Wave distance
00042         distanceWidthMult_ = 4;
00043         distanceHeightMult_ = 4;
00044         distanceWidth_ = mapWidth / distanceWidthMult_;
00045         distanceHeight_ = mapHeight / distanceHeightMult_;
00046 
00047         delete [] waveDistance_;
00048         waveDistance_ = new float[distanceWidth_ * distanceHeight_];
00049         for (int a=0; a<distanceWidth_; a++)
00050         {
00051                 for (int b=0; b<distanceHeight_; b++)
00052                 {
00053                         waveDistance_[a + b * distanceWidth_] = 255.0f;
00054                 }
00055         }
00056 
00057         const int skip = distanceWidthMult_;
00058         for (int y=0; y<mapHeight; y+=skip)
00059         {
00060                 if (counter) counter->setNewPercentage(float(y)/
00061                         float(mapHeight)*100.0f);
00062 
00063                 for (int x=0; x<mapWidth; x+=skip)
00064                 {
00065                         float height =
00066                                 ScorchedClient::instance()->getLandscapeMaps().
00067                                         getGroundMaps().getHeight(x, y).asFloat();
00068                         if (height > waterHeight - 4.0f && height < waterHeight)
00069                         {
00070                                 float height2=
00071                                         ScorchedClient::instance()->getLandscapeMaps().
00072                                         getGroundMaps().getHeight(x+skip, y).asFloat();
00073                                 float height3=
00074                                         ScorchedClient::instance()->getLandscapeMaps().
00075                                         getGroundMaps().getHeight(x-skip, y).asFloat();
00076                                 float height4=
00077                                         ScorchedClient::instance()->getLandscapeMaps().
00078                                         getGroundMaps().getHeight(x, y+skip).asFloat();
00079                                 float height5=
00080                                         ScorchedClient::instance()->getLandscapeMaps().
00081                                         getGroundMaps().getHeight(x, y-skip).asFloat();
00082 
00083                                 if (height2 > waterHeight || height3 > waterHeight ||
00084                                         height4 > waterHeight || height5 > waterHeight)
00085                                 {
00086                                         Vector posA((float) x, (float) y, 0.0f);
00087                                         for (int b=0; b<distanceHeight_; b++)
00088                                         {
00089                                                 for (int a=0; a<distanceWidth_; a++)
00090                                                 {
00091                                                         Vector posB(
00092                                                                 float(float(a) / float(distanceWidth_) * float(mapWidth)), 
00093                                                                 float(float(b) / float(distanceHeight_) * float(mapHeight)), 0.0f);
00094                                                         float distance = (posB - posA).Magnitude();
00095                                                         waveDistance_[a + b * distanceWidth_] = MIN(waveDistance_[a + b * distanceWidth_], distance);
00096                                                 }
00097                                         }
00098                                 }
00099 
00100                         }
00101                 }
00102         }
00103 }
00104 
00105 float WaterWaveDistance::getWaveDistance(int posx, int posy)
00106 {
00107         int a = int(posx) / distanceWidthMult_;
00108         int b = int(posy) / distanceHeightMult_;
00109         int x = MAX(0, MIN(a, (distanceWidth_ - 1)));
00110         int y = MAX(0, MIN(b, (distanceHeight_ - 1)));
00111 
00112         float distance = waveDistance_[x + y * distanceWidth_];
00113         distance += fabsf(float(a - x)) + fabsf(float(b - y));
00114         return distance;
00115 }

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