summaryrefslogtreecommitdiffstats
path: root/src/graphics
diff options
context:
space:
mode:
authorkrzys-h <krzys_h@interia.pl>2014-04-10 21:50:26 +0200
committerkrzys-h <krzys_h@interia.pl>2014-04-10 21:50:26 +0200
commitf71658e38dbcfc5635a6b2a9c6c4168582728bb5 (patch)
tree964d5d80141f1bb3bf22e44e3fa32edfe9689b74 /src/graphics
parentbfa226d64e8bbfb2617974abf19c6fc67a951884 (diff)
downloadcolobot-f71658e38dbcfc5635a6b2a9c6c4168582728bb5.tar.gz
colobot-f71658e38dbcfc5635a6b2a9c6c4168582728bb5.tar.bz2
colobot-f71658e38dbcfc5635a6b2a9c6c4168582728bb5.zip
Random relief generator
Diffstat (limited to 'src/graphics')
-rw-r--r--src/graphics/engine/terrain.cpp60
-rw-r--r--src/graphics/engine/terrain.h4
2 files changed, 63 insertions, 1 deletions
diff --git a/src/graphics/engine/terrain.cpp b/src/graphics/engine/terrain.cpp
index 4b5384e..e6ca824 100644
--- a/src/graphics/engine/terrain.cpp
+++ b/src/graphics/engine/terrain.cpp
@@ -331,6 +331,66 @@ bool CTerrain::LoadRelief(const std::string &fileName, float scaleRelief,
return true;
}
+bool CTerrain::RandomizeRelief()
+{
+ // Perlin noise
+ // Based on Python implementation by Marek Rogalski (mafik)
+ // http://amt2014.pl/archiwum/perlin.py
+
+ int size = (m_mosaicCount*m_brickCount)+1;
+ const int ilosc_oktaw = 6;
+
+ float* oktawy[ilosc_oktaw];
+ for(int i=0; i<ilosc_oktaw; i++)
+ {
+ int pxCount = static_cast<int>(pow(2, (i+1)*2));
+ oktawy[i] = new float[pxCount];
+ for(int j=0; j<pxCount; j++)
+ {
+ oktawy[i][j] = Math::Rand();
+ }
+ }
+
+ for(int y2=0; y2 < size; y2++)
+ {
+ float y = static_cast<float>(y2) / size;
+ for(int x2=0; x2 < size; x2++)
+ {
+ float x = static_cast<float>(x2) / size;
+
+ float wart = 0;
+ for(int i=0; i<ilosc_oktaw; i++)
+ {
+ int rozmiar_oktawy = sqrt(static_cast<int>(pow(2, (i+1)*2)));
+ double xi, yi, a, b;
+ a = modf(x * (rozmiar_oktawy-1), &xi);
+ b = modf(y * (rozmiar_oktawy-1), &yi);
+ /*int xi = floor(x * (rozmiar_oktawy-1));
+ int yi = floor(y * (rozmiar_oktawy-1));
+ float a = (x * (rozmiar_oktawy-1)) - xi;
+ float b = (y * (rozmiar_oktawy-1)) - yi;*/
+ //CLogger::GetInstancePointer()->Error("%f %f %f %f\n", xi, yi, a, b);
+
+ float lg = oktawy[i][static_cast<int>(yi * rozmiar_oktawy + xi)];
+ float pg = oktawy[i][static_cast<int>(yi * rozmiar_oktawy + xi + 1)];
+ float ld = oktawy[i][static_cast<int>((yi+1) * rozmiar_oktawy + xi)];
+ float pd = oktawy[i][static_cast<int>((yi+1) * rozmiar_oktawy + xi + 1)];
+ //CLogger::GetInstancePointer()->Error("%f %f %f %f\n", lg, pg, ld, pd);
+
+ float g = pg * a + lg * (1-a);
+ float d = pd * a + ld * (1-a);
+ float res = d * b + g * (1-b);
+ wart += res;
+ }
+
+ wart /= ilosc_oktaw;
+
+ m_relief[x2+y2*size] = wart * 255.0f;
+ }
+ }
+ return true;
+}
+
bool CTerrain::AddReliefPoint(Math::Vector pos, float scaleRelief)
{
float dim = (m_mosaicCount*m_brickCount*m_brickSize)/2.0f;
diff --git a/src/graphics/engine/terrain.h b/src/graphics/engine/terrain.h
index e618691..75a8975 100644
--- a/src/graphics/engine/terrain.h
+++ b/src/graphics/engine/terrain.h
@@ -212,7 +212,7 @@ struct FlyingLimit
* where relief data is specifically adjusted to level space to allow
* construction of buildings.
*
- * Undergound resources can be supplied by loading them from image like relief data.
+ * Underground resources can be supplied by loading them from image like relief data.
*
* Terrain also specifies flying limits for player: one global level and possible
* additional spherical restrictions.
@@ -243,6 +243,8 @@ public:
void FlushRelief();
//! Load relief from image
bool LoadRelief(const std::string& fileName, float scaleRelief, bool adjustBorder);
+ //! Load ramdomized relief
+ bool RandomizeRelief();
//! Load resources from image
bool LoadResources(const std::string& fileName);