Strange Errors with Code, Possibly has to do with loading Image?

edited October 2013 in Questions about Code

I am making a program that takes a heightmap and renders it as a 3D mesh. When I run my code it creates the window, then crashes and returns some crazy long java error. I found a random heightmap online and saved it to the programs folder in my sketchbook. Is that the wrong place or somthing? Help would be much appreciated.

PImage heightmap;

PVector v1, v2, v3, v4;

int w;
int h;
int x;
int y;
int z;

void setup() {
  size(480,480,P3D);
  noStroke();
  noLoop();
  heightmap = loadImage("heightmap.jpg");
  heightmap.loadPixels();
  w = heightmap.width;
  h = heightmap.height;
}

void draw() {
  background(195,214,245);
  for (int b = 0; b < h-1; b++) {
    for (int a = 0; a < w-1; a++) {
      x = a * 16;
      y = b * 16;

      v1 = new PVector(x,y,int(pixels[b*w+a])*8);
      v2 = new PVector(x+16,y,int(pixels[b*w+a+1])*8);
      v3 = new PVector(x,y+16,int(pixels[b+1*w+a])*8);
      v4 = new PVector(x+16,y+16,int(pixels[b+1*w+a+1])*8);

      fill(random(150,200));
      beginShape(TRIANGLES);
      vertex(v1.x,v1.y,v1.z);
      vertex(v2.x,v2.y,v2.z);
      vertex(v3.x,v3.y,v3.z);

      vertex(v2.x,v2.y,v2.z);
      vertex(v3.x,v3.y,v3.z);
      vertex(v4.x,v4.y,v4.z);
      endShape();
    }
  }
}

Answers

  • "saved it to the programs folder in my sketchbook"
    Not sure of what it means, but loaded resources (images among other things) should be in a folder named data in the sketch folder (the one where the .pde file is saved).

  • That is where the image file is located. I now think the issue is with converting the color values from the pixel array to an int. Anyone know how to turn color values to an int? The color will always be greyscale BTW.

  • Data-type color is the same as int in Processing Java: @-)
    http://processing.org/reference/color_datatype.html

  • "returns some crazy long java error"
    Seeing the error (at least the first few lines) could help in diagnosing the problem...

  • So I did some testing, and just trying pixels[0]; returns the error, so it seems there is some issue with loading the data into the pixels array. The reference says: "Before accessing this array, the data must loaded with the loadPixels() function. After the array data has been modified, the updatePixels() function must be run to update the changes. Without loadPixels(), running the code may (or will in future releases) result in a NullPointerException."

    It returns NullPointerException, I do not have access to the code right now, i'll post the error later, you can always just run it for yourself and see what happens. Just find a random heightmap image online.

    I am just thinking I might resort to using 2 nested for loops to just loop through the image and get() each pixel from the image and store it to an array.

    Any reflections?

  • "Any reflections?"
    We are not mirrors, but I suggest to stop rejecting answers that aren't even answers, but remarks / suggestions trying to help you...

  • Sorry should have said "Any input?" Referring to the new info I gave. You don't have to be so rude about it...

  • b+1*w+a

    operator precedence errors?

    this is probably doing b + (1 * w ) + a

  • It is rejecting answers which is rude...

    Well spotted, koogs, but I guess the intention was rather (b+1)*w+a instead, since b stops one before h.

    Is it a NullPointerException or an ArrayIndexOutOfBoundsException?

  • Ok, I just figured the issue out... it was right in front of me the whole time facepalm . Apparently I needed heightmap.pixels[] not just pixels[], I solved the issue. One issue is it is really laggy, obviously, so, anyone know how to save a bunch of shapes drawn in 3D for quick redraw?

    The error was a NullPointerException, btw.

  • OK, indeed obvious... once spotted!

    The remarks on the parentheses are still valid, I think.

    A quick speed gain is to do the loop once in setup(), and save the resulting PVectors in arrays. This way, you avoid creating a bunch of objects on each frame, which is costly.

    I am not a specialist on PShape, but perhaps you can use createShape() to retain the created shapes.

  • the use of PVector, specifically the new PVector(...) is a bit wasteful - you're putting them into PVectors and taking them straight back out again. and the constant new is allocating memory left right and centre = more garbage collection.

    i'd start by using v1.set(...); rather than v1 = new PVector(...); (you'll need to initialise them in setup()) but i'd think about removing them altogether, just using floats.

    is this Processing 2? if not, try OPENGL instead of P3D.

    is GlGraphics still a thing? you used to be able to write code that would upload your geometry to the video card and then drawing it would just be a case of one line, no data transfer, no recalculations, nothing. ran fantastically fast. i have the feeling that this has been incorporated into Processing2 in some way.

  • I have already thought of putting the PVectors in arrays, and I guess I can initialize the PVectors in setup... I am pretty sure you can still use OpenGL in processing 2, but I have never used it before so not sure how challenging it is. Isn't there way to export 3D files or models from processing? or at least with libraries? Then I can just redraw the 3D model.

  • Here, we are kind of changing subject, so I suggest (for once!) to open a new topic (in the How To category, I think) asking the above question (make a 3D model for fast redrawing) with a link to this topic. Having a good subject is crucial to attract knowledgeable people... :-)

  • when you say it's really laggy, what do you mean? it's not moving, there's no interactive element other than the changing colours, how can it be laggy?

    how big is the image you are using?

  • When I say laggy, I mean when I add a camera(mouseX,mouseY,0,0,0,0,0,1,0); it lags, because it has to run 2 nested for loops in draw before the camera can move. I tried putting it in setup then placing camera in draw and it didnt do anything at all, obviously. With a 256x256 image it takes about 5 seconds per frame.

Sign In or Register to comment.