We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › heightmap generation with pixels[]
Page Index Toggle Pages: 1
heightmap generation with pixels[] (Read 433 times)
heightmap generation with pixels[]
Jun 27th, 2008, 11:46pm
 
I'm trying to generate an image that will ultimately be a heightmap, but I've run into a problem with modifying the pixels array. It seems like the color is looping back around to 0 from 255, for something of a solarized effect that I don't want. I've tried using constrain(foo,0,255) in a few spots, but I either get solid white circles or an all black screen.

Code:
void setup() {
 size(200,200);
 background(0);
 noLoop();
}

void draw() {
 loadPixels();
 
 // 20 hills
 for (int i = 0; i < 20; i++) {
   // hill radius
   int hillr = int(random(0,40));
   // hill location
   int hillx = int(random(hillr*2, width-hillr*2));
   int hilly = int(random(hillr*2, height-hillr*2));
   
   for (int x = 0; x < width; x++) {
     for (int y = 0; y < height; y++) {
       // hill height
       int hillh = hillr*hillr - (x - hillx)*(x - hillx) - (y - hilly)*(y - hilly);
       if (hillh > 0) {
         // brightness by height
         float bright = float(hillh) / 1600 * 255;
         pixels[x+y*width] += color(bright);
       }
     }
   }
 }
 updatePixels();
}
Re: heightmap generation with pixels[]
Reply #1 - Jun 28th, 2008, 12:09am
 
such operations with colors won't give the expected result. you should better store the height into another array, then "paste" the constrained values to the pixels array.

Quote:
int[] h;

void setup() {
size(200,200);
background(0);
noLoop();
h = new int[width*height];
}

void draw() {  
// 20 hills
for (int i = 0; i < 20; i++) {
 // hill radius
 int hillr = int(random(0,40));
 // hill location
 int hillx = int(random(hillr*2, width-hillr*2));
 int hilly = int(random(hillr*2, height-hillr*2));
 
 for (int x = 0; x < width; x++) {
  for (int y = 0; y < height; y++) {
   // hill height
   int hillh = hillr*hillr - (x - hillx)*(x - hillx) - (y - hilly)*(y - hilly);
   if (hillh > 0) {
    // brightness by height
    float bright = float(hillh) / 1600 * 255;
    h[x+y*width] += bright;
   }
  }
 }
}
loadPixels();
for (int i = 0; i < h.length; i++) pixels[i] = color(constrain(h[i], 0, 255));
updatePixels();
}
Page Index Toggle Pages: 1