1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
///Headers
#include <vector>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <png++/png.hpp>
#include <png++/image.hpp>
#include <png++/rgb_pixel.hpp>
using namespace std;
/// Terraub Functions
double * getFieldSquareTerrain(int x0,int y0, int x1, int y1, unsigned int iterations);
double displace(unsigned int iterations,int x, int y);
double getMaxDeviation(unsigned int iterations);
double PRH(unsigned iterations, int x, int y);
double * getFieldDiamondSquaredMap(int x, int y, int width, int height, unsigned int iterations);
//Size of the grid to generate
//This must be 2^n+1 (e.g. 257)
const int DATA_SIZE = 1024;
void SaveTerrFile(const double * image);
/// Main function
int main()
{
/// Define Settings
int width=1024;
int height=1024;
int iterations=6;
int x=0;
int y=0;
// allocate memory
double * map=new double[width*height];
map=getFieldDiamondSquaredMap(x,y,width,height,iterations);
SaveTerrFile(map);
return 1;
}
double * getFieldDiamondSquaredMap(int x, int y, int width, int height, unsigned int iterations)
{
/// Allocate memory for final mapdouble
double * map = new double[width*height];
map = getFieldSquareTerrain(x, y, x+width, y+height, iterations);
double maxdeviation = getMaxDeviation(iterations);
for (unsigned int x = 0; x < width; x++)
{
for (unsigned int y = 0; y < height; y++)
{
map[x*y] = map[x*y] / maxdeviation;
}
}
return map;
}
double * getFieldSquareTerrain(int x0, int y0, int x1, int y1, unsigned int iterations)
{
/// Define final height and width
int finalwidth = x1 - x0;
int finalheight = y1 - y0;
/// Allocate memory for final map
double * finalmap = new double[finalwidth*finalheight];
/// Test iteration
if (iterations == 0)
{
for (unsigned int x = 0; x < finalwidth; x++)
{
for (unsigned int y = 0; y < finalheight; y++)
{
finalmap[x*y] = displace(iterations,x0+x,y0+y) ;
}
return finalmap;
}
}
/// Define ceil and floor
int upper_x0=floor(x0/2) - 1;
int upper_y0=floor(y0/2) - 1;
int upper_x1=ceil(x1/2)+1;
int upper_y1=ceil(y1/2)+1;
/// Define upper height and width for upper maps
int upper_xwidth= upper_x1-upper_x0;
int upper_yheight= upper_y1-upper_y0;
/// Allocate memory for upper map width and height
double * uppermap = new double[upper_xwidth*upper_yheight];
/// Pass another iteration
uppermap = getFieldSquareTerrain(upper_x0, upper_y0, upper_x1, upper_y1, iterations-1);
/// Define counter height and width
int counter_x0= upper_x0 * 2;
int counter_y0= upper_y0 * 2;
int counter_width = upper_xwidth*2-1;
int counter_height = upper_yheight*2-1;
/// Allocate memory for currentmap using counter height and width
double * currentmap = new double[counter_width*counter_height];
/// Copy information to double map
for (unsigned int x = 0; x < upper_xwidth; x++)
{
for (unsigned int y = 0; y< upper_yheight; y++)
{
currentmap[(x*2)*(y*2)] = uppermap[x*y];
}
}
/// Define offset
int xoff = x0 - counter_x0;
int yoff = y0 - counter_y0;
/// Use a diamond mehod algorithm
for (unsigned int x = 1; x < counter_width-1; x += 2)
{
for (unsigned int y = 1; y < counter_height-1; y += 2)
{
currentmap[x*y] = ((currentmap[(x - 1)*(y - 1)] + currentmap[(x - 1)*(y + 1)] + currentmap[(x + 1)*(y - 1)] + currentmap[(x + 1)*(y + 1)]) / 4) + displace(iterations,counter_x0+x,counter_y0+y);
}
}
for (unsigned int x = 1; x < counter_width-1; x += 2)
{
for (unsigned int y = 2; y < counter_height-1; y += 2)
{
currentmap[x*y] = ((currentmap[(x - 1)*y] + currentmap[(x + 1)*y] + currentmap[x*(y - 1)] + currentmap[x*(y + 1)]) / 4) + displace(iterations,counter_x0+x,counter_y0+y);
}
}
for (unsigned int x = 2; x < counter_width-1; x += 2)
{
for (unsigned int y = 1; y < counter_height-1; y += 2)
{
currentmap[x*y] = ((currentmap[(x - 1)*y] + currentmap[(x + 1)*y] + currentmap[x*(y - 1)] + currentmap[x*(y + 1)]) / 4) + displace(iterations,counter_x0+x,counter_y0+y);
}
}
/// Copy actual information to returned map
for (unsigned int x = 0; x < finalwidth; x++)
{
for (unsigned int y = 0; y < finalheight; y++)
{
finalmap[x*y] = currentmap[(x+xoff)*(y+yoff)];
}
}
return finalmap;
}
/// Random function to offset
double displace(unsigned int iterations, int x, int y)
{
return (((PRH(iterations,x,y) - 0.5)*2)) / (iterations+1);
}
/// Get maximum deviations
double getMaxDeviation(unsigned int iterations)
{
double dev = 0.5 / (iterations+1);
if (iterations <= 0) return dev;
return getMaxDeviation(iterations-1) + dev;
}
///This function returns the same result for given values but should be somewhat random.
double PRH(unsigned iterations, int x, int y)
{
unsigned long long hash;
x &= 0xFFF;
y &= 0xFFF;
iterations &= 0xFF;
hash = (iterations << 24);
hash |= (y << 12);
hash |= x;
unsigned long long rem = hash & 3;
unsigned long long h = hash;
switch (rem)
{
case 3:
hash += h;
hash ^= hash << 32;
hash ^= h << 36;
hash += hash >> 22;
break;
case 2:
hash += h;
hash ^= hash << 22;
hash += hash >> 34;
break;
case 1:
hash += h;
hash ^= hash << 20;
hash += hash >> 2;
}
hash ^= hash << 6;
hash += hash >> 10;
hash ^= hash << 8;
hash += hash >> 34;
hash ^= hash << 50;
hash += hash >> 12;
return (hash & 0xFFFF) / static_cast<double>(0xFFFF);
}
void SaveTerrFile(const double * image)
{
png::image< png::rgb_pixel > newimage(1024, 1024);
for (unsigned int y = 0; y < newimage.get_width(); ++y)
{
for (unsigned int x = 0; x < newimage.get_height(); ++x)
{
int col=(int)(image[x*y]+1)*255;
newimage[y][x] = png::rgb_pixel(col,col,col);
// non-checking equivalent of image.set_pixel(x, y, ...);
}
}
newimage.write("rgb.png");
}
|