Nataly J
Nataly J

Reputation: 45

How do only update pixels once

My pixels are updating every frame causing the effect to be re-applied to the previous frame. How can i make this effect only happen once and without using noLoop(). I just want there to be a large circle around the triangle. Please help. Thanks.

Here is the whole program. I set the frameRate to 1 so you can see the problem easier:

boolean up;
int x =-300;
int y =-300;

void setup()
{
  size(600, 600);
  frameRate(1);
}

void draw()
{
  pushMatrix();
  translate(300, 300);
  float a = atan2(mouseY-300, mouseX-300);
  rotate(a);

  for (int i = x; i < x+width; i+=40)
    for (int j = y; j < y+height; j+=40)
      rect(i, j, 40, 40);

  loadPixels();
  for (int i = 0; i < pixels.length; i++)
  {
    x = i%width;
    y = i/width;
    color c = pixels[x+y*width];
    float d = dist(x, y, width/2, height/2);
    pixels[x+y*width] = color(brightness(c) - d);
  }
  updatePixels();

  popMatrix();

  fill(255, 0, 0);
  triangle(280, 300, 300, 200, 320, 300);

  if (up) 
  {
    x += sin(a)*5; 
    y += cos(a)*5;
  }
}

void keyPressed()
{
  if (key=='w')up=true;
}

void keyReleased()
{
  if (key=='w')up=false;
}

Upvotes: 0

Views: 95

Answers (2)

Majlik
Majlik

Reputation: 1082

If you want just use your filter and move fluently around update your effect like this:

  for (int y = 0; y < height; y++)
  {
    for (int x = 0; x < width; x++)
    {
      color c = pixels[x+y*width];
      float d = dist(x, y, width/2, height/2);
      pixels[x+y*width] = color(brightness(c) - d);
    }
  }

You had unnecessary calculation that consume lot of CPU resources. Redrawing background also helps to make clearer animation.

If you want generate this effect only once and then apply it. PGraphics could achieve something similar.

Upvotes: 1

bvoq
bvoq

Reputation: 319

Re-draw everything in one frame.

Remember before you use your filter, you must undo the filter effects of the last time. The usual ordering in your draw() function goes as follows:

  1. Add a background / clear all the objects you added in the last frame & clearing the filter of your last frame.
  2. Add your objects.
  3. Lay your filter on top.

Try to refrain from doing any graphic related stuff in setup, hence it will be destroyed by this draw() function - paradigma.

This should already suffice as your answer. Quick note: When you work with for e.g. a 3D - Shadow filter, applying the filter can take a very long time. Instead we try to store as many calculations we did on the previous frame, so we don't need to calculate everything over again. The same goes for the objects-layer. You don't want to calculate the shortest-path for a minion every frame, instead you calculate the shortest path once and only recalculate it, when something changes: Position of a box, player position, etc..

Upvotes: 2

Related Questions