creating an image with pixels[]

edited December 2014 in JavaScript Mode

So I am trying to draw something in the setup() and then record the pixels that I want and store them in an image that I plan on using as a texture later on.

I am very new to texture() and pixels[] both, so I am looking for some help as to what I am doing wrong here.

I am using Processing.js.

Here is the code from the global declarations and the setup() : The most relevant lines are: 22 and 39-50

int w=1000;
int h=800;
color background_color = color(51,161,201);

boolean grid_drawn=false;
float[] grid_params={0,0,0,0,200,0};
float square_size=25;


int box_depth=50;
float pop_incr=0;
int rot_incr=0;
int box_size=(int) ((w/4)/ (int) square_size);
float real_box_size=(box_size*square_size);
float box_offset=(((w/4)/square_size)*square_size)*1.5;
color box_color=color(0);//black box
int boxes=0;

float[] cam={w,(h/12), (h/2.0) / tan(PI*30.0 / 180.0), w/2, h/2, 0.0, 0.0, 1.0, 0.0};
float[] cur_cam={w/2.0, h/2.0, (h/2.0) / tan(PI*30.0 / 180.0), w/2.0, h/2.0, 0, 0, 1, 0};

PImage img=createImage((int)real_box_size,(int)real_box_size,RGB);

void setup()
{
  size(1000,800,P3D);
  background(background_color);
  fill(40,40,40);
  stroke(60);
  rect(0,0,real_box_size,real_box_size);
  for(int x=square_size; x<real_box_size; x+=square_size)
  {
    for(int y=square_size; y<real_box_size; y+=square_size)
    {
      line(x,0,x,real_box_size);
      line(0,y,real_box_size,y);
    }
  }
  loadPixels();
  img.loadPixels();
  for(y=0; y<real_box_size;y++)
  {
    for(x=0; x<real_box_size;x++)
    {
      img.pixels[x+(real_box_size*y)]=pixels[x+(y*real_box_size)];
    }
  }
  img.updatePixels();
  updatePixels();
  stroke(255);
  smooth();
  background(background_color);
}

Here is a copy of the screen from when i draw the image, i am only interested in recording the rectangle: image alt textsnip1

When I run it, it tells me there is an undefined function somewhere and I have traced it to the loadPixels() call above img.loadPixels(), but this is definitely not an undefined function.

Answers

  • I had to make some changes to run it in Java (loose typing...).

    I am not sure of the point of the last line (background() call) which erase all you have drawn so far. Nor why you use P3D for an apparently 2D sketch.

    I changed these two points, and saw what you show in your screenshot.

    Now, I am not too sure of what you ask for. Perhaps the get() or the copy() methods are what you look for.

  • The sketch will eventually be 3D.

    I want to erase it because I don't want the user to see it, but I want to record this image (the rectangle part) as a texture that I will use on a box later on.

    What ere the changes you made? perhaps that is why I'm getting the error.

  • when you just painting img, the screen isn't changed - no need for background()

  • Answer ✓

    "I want to erase it because I don't want the user to see it"
    Then draw on a PGraphics, it is like drawing on the sketch, but off-screen (until you choose to display the result as an image).

Sign In or Register to comment.