0

该代码应该淡化窗口的图像并将其复制到缓冲区 f,然后将 f 绘制回窗口,但要进行平移、旋转和缩放。当您将插入电视的相机指向电视时,我正在尝试创建类似于反馈循环的效果。

我已经尝试了我能想到的一切,记录了我能想到的每个变量,但似乎 image(f,0,0) 做错了什么或出乎意料。

我错过了什么?

关于x轴的双像镜图片:

应该只是一串正方形

PGraphics f;
int rect_size;
int midX;
int midY;

 void setup(){
  size(1000, 1000, P2D);
  f = createGraphics(width, height, P2D);
  midX = width/2;
  midY = height/2;
  rect_size = 300;
  imageMode(CENTER);
  rectMode(CENTER);
  smooth();
  background(0,0,0);
  fill(0,0);
  stroke(255,255);
}

void draw(){
  fade_and_copy_pixels(f); //fades window pixels and then copies pixels to f
  background(0,0,0);//without this the corners dont get repainted.
  //transform display window (instead of f)
  pushMatrix();
  float scaling = 0.90; // x>1 makes image bigger
  float rot = 5; //angle in degrees
  translate(midX,midY); //makes it so rotations are always around the center
  rotate(radians(rot));
  scale(scaling);
  imageMode(CENTER);
  image(f,0,0); //weird double image must have something not working around here
  popMatrix();//returns window matrix to normal
  int x = mouseX;
  int y = mouseY;
  rectMode(CENTER);
  rect(x,y,rect_size,rect_size);
}

//fades window pixels and then copies pixels to f
void fade_and_copy_pixels(PGraphics f){
  loadPixels();  //load windows pixels. dont need because I am only reading pixels?
  f.loadPixels(); //loads feedback loops pixels
  // Loop through every pixel in window
  //it is faster to grab data from pixels[] array, so dont use get and set, use this

  for (int i = 0; i < pixels.length; i++) {
      //////////////FADE PIXELS in window and COPY to f:///////////////
      color p = pixels[i];

      //get color values, mask then shift
      int r = (p & 0x00FF0000) >> 16;
      int g = (p & 0x0000FF00) >> 8;
      int b =  p & 0x000000FF; //no need for shifting

      // reduce value for each color proportional 
      // between fade_amount between 0-1 for 0 being totallty transparent, and 1 totally none
      // min is 0.0039 (when using floor function and 255 as molorModes for colors)
      float fade_percent= 0.005; //0.05 = 5%

      int r_new = floor(float(r) - (float(r) * fade_percent));
      int g_new = floor(float(g) - (float(g) * fade_percent));
      int b_new = floor(float(b) - (float(b) * fade_percent));
      //maybe later rewrite in a way to save what the difference is and round it differently, like maybe faster at first and slow later, 
       //round doesn't work because it never first subtracts one to get the ball rolling
      //floor has a minimum of always subtracting 1 from each value each time. cant just subtract 1 ever n loops
      //keep a list of all the pixel as floats? too much memory?
      //ill stick with floor for now
      // the lowest percent that will make a difference with floor is 0.0039?... because thats slightly more than 1/255

      //shift back and or together
      p = 0xFF000000 | (r_new << 16) | (g_new << 8) | b_new; // or-ing all the new hex together back into AARRGGBB

      f.pixels[i] = p;
      ////////pixels now copied
  }
  f.updatePixels(); 

}
4

2 回答 2

0

一个更简单的,就像一个魅力:

添加 f.beginDraw(); 之前和 f.endDraw(); 使用 f 后:

loadPixels();  //load windows pixels. dont need because I am only reading pixels?
  f.loadPixels(); //loads feedback loops pixels
  // Loop through every pixel in window
  //it is faster to grab data from pixels[] array, so dont use get and set, use this

  f.beginDraw();

f.updatePixels(); 
  f.endDraw();

处理必须知道它何时在缓冲区中绘制,何时不在。

在这张图片中,您可以看到有效

于 2017-04-28T18:49:24.410 回答
0

这是一个奇怪的问题。但是,让我们从一个隔离问题的更简单的 MCVE 开始:

PGraphics f;
void setup() {
  size(500, 500, P2D);
  f = createGraphics(width, height, P2D);
}

void draw() {
  background(0);
  rect(mouseX, mouseY, 100, 100);
  copyPixels(f); 
  image(f, 0, 0);
}

void copyPixels(PGraphics f) {
  loadPixels();
  f.loadPixels(); 

  for (int i = 0; i < pixels.length; i++) {
    color p = pixels[i];
    f.pixels[i] = p;
  }
  f.updatePixels();
}

此代码显示与您的代码相同的问题,没有任何额外的逻辑。我希望这段代码在鼠标所在的任何地方都显示一个矩形,而是在 X 轴上反射的位置显示一个矩形。如果鼠标在窗口顶部,则矩形在窗口底部,反之亦然。

认为这是由于P2D渲染器是 OpenGL 造成的,它有一个倒置的 Y 轴(0 位于底部而不是顶部)。所以看起来当你复制像素时,它会从屏幕空间转移到 OpenGL 空间......或其他东西。不过,这肯定看起来有问题。

目前,有两件事似乎可以解决这个问题。首先,您可以只使用默认渲染器而不是P2D. 这似乎可以解决问题。

或者你可以摆脱函数for内部的循环,现在就做。这似乎也解决了问题,但又感觉很糟糕。copyPixels()f.pixels = pixels;

如果其他人(寻呼乔治)明天没有提供更好的解释,我会在Processing 的 GitHub 上提交一个错误。(如果你愿意,我可以为你做。)

编辑:我在这里提交了一个问题,所以希望我们能在接下来的几天内收到开发人员的回复。

编辑二:看起来已经实施了修复,并且应该在下一版本的处理中可用。如果您现在需要它,您可以随时从源代码构建处理。

于 2017-04-18T03:24:03.970 回答