git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / common / landscapemap / HeightMapLoader.cpp
blobb93779a97be79a3ea41167b14205571ce75c332b
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
3 //
4 // This file is part of Scorched3D.
5 //
6 // Scorched3D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // Scorched3D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with Scorched3D; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ////////////////////////////////////////////////////////////////////////////////
21 #include <landscapemap/HeightMapLoader.h>
22 #include <image/ImageFactory.h>
23 #include <image/ImageBitmap.h>
24 #include <common/RandomGenerator.h>
25 #include <common/Defines.h>
26 #include <common/Logger.h>
27 #include <lang/LangResource.h>
29 void HeightMapLoader::loadTerrain(HeightMap &hmap,
30 Image &bitmap,
31 bool levelSurround,
32 ProgressCounter *counter)
34 if (counter) counter->setNewOp(LANG_RESOURCE("LOADING_LANDSCAPE", "Loading Landscape"));
35 hmap.reset();
37 fixed dhx = fixed(bitmap.getWidth()) / fixed(hmap.getMapWidth()+1);
38 fixed dhy = fixed(bitmap.getHeight()) / fixed(hmap.getMapHeight()+1);
40 int bh = bitmap.getHeight();
41 int bw = bitmap.getWidth();
43 fixed scale(true, 25000);
45 fixed hy = fixed(0);
46 for (int by=0; by<=hmap.getMapHeight(); by++, hy+=dhy)
48 if (counter) counter->setNewPercentage((100.0f * float(by)) / float(hmap.getMapHeight()));
50 int ihy = hy.floor().asInt();
51 int ihy2 = ihy + 1; if (ihy2 >= bh) --ihy2;
53 int offsety = ihy * bw * 3;
54 int offsety2 = ihy2 * bw * 3;
55 unsigned char *posYA = (unsigned char*) (bitmap.getBits() + offsety);
56 unsigned char *posYB = (unsigned char*) (bitmap.getBits() + offsety2);
58 fixed hx = fixed(0);
59 for (int bx=0; bx<=hmap.getMapWidth(); bx++, hx+=dhx)
61 int ihx = hx.floor().asInt();
62 int ihx2 = ihx + 1; if (ihx2 >= bw) --ihx2;
64 unsigned char *posXA1 = posYA + ihx * 3;
65 unsigned char *posXA2 = posYA + ihx2 * 3;
66 unsigned char *posXB1 = posYB + ihx * 3;
67 unsigned char *posXB2 = posYB + ihx2 * 3;
69 fixed heightXA1 = fixed(posXA1[0]);
70 fixed heightXA2 = fixed(posXA2[0]);
71 fixed heightXB1 = fixed(posXB1[0]);
72 fixed heightXB2 = fixed(posXB2[0]);
74 fixed XA = ((heightXA2 - heightXA1) * (hx - fixed(ihx))) + heightXA1;
75 fixed XB = ((heightXB2 - heightXB1) * (hx - fixed(ihx))) + heightXB1;
77 fixed h = ((XB - XA) * (hy - fixed(ihy))) + XA;
79 hmap.setHeight(bx, by, h / scale);
83 if (levelSurround) HeightMapModifier::levelSurround(hmap);
86 bool HeightMapLoader::generateTerrain(
87 unsigned int seed,
88 LandscapeDefnType *defn,
89 HeightMap &hmap,
90 bool &levelSurround,
91 ProgressCounter *counter)
93 // Do we generate or load the landscape
94 if (defn->getType() == LandscapeDefnType::eHeightMapFile)
96 LandscapeDefnHeightMapFile *file =
97 (LandscapeDefnHeightMapFile *) defn;
99 // Load the landscape
100 levelSurround = file->levelsurround;
102 std::string fileName = S3D::getDataFile(file->file.c_str());
103 ImageHandle image = ImageFactory::loadImageHandle(fileName);
104 if (!image.getBits())
106 S3D::dialogMessage("HeightMapLoader", S3D::formatStringBuffer(
107 "Error: Unable to find landscape map \"%s\"",
108 fileName.c_str()));
109 return false;
111 else
113 HeightMapLoader::loadTerrain(
114 hmap,
115 image,
116 file->levelsurround,
117 counter);
120 else if (defn->getType() == LandscapeDefnType::eHeightMapGenerate)
122 LandscapeDefnHeightMapGenerate *generate =
123 (LandscapeDefnHeightMapGenerate *) defn;
125 // Seed the generator and generate the landscape
126 levelSurround = generate->levelsurround;
127 RandomGenerator generator;
128 RandomGenerator offsetGenerator;
129 generator.seed(seed);
130 offsetGenerator.seed(seed);
132 HeightMapModifier::generateTerrain(
133 hmap,
134 *generate,
135 generator,
136 offsetGenerator,
137 counter);
139 else
141 S3D::dialogMessage("HeightMapLoader", S3D::formatStringBuffer(
142 "Error: Unkown generate type %i",
143 defn->getType()));
144 return false;
147 // Make sure normals are correct for drawing
148 if (counter) counter->setNewOp(LANG_RESOURCE("SETTINGS_NORMALS", "Setting Normals"));
149 for (int y=0; y<hmap.getMapHeight(); y++)
151 if (counter) counter->setNewPercentage((100.0f * float(y)) / float(hmap.getMapHeight()));
153 for (int x=0; x<hmap.getMapWidth(); x++)
155 hmap.getNormal(x, y);
159 return true;