|
| 1 | +package webgpu; |
| 2 | + |
| 3 | +import processing.core.PApplet; |
| 4 | + |
| 5 | +public class UpdatePixels extends PApplet { |
| 6 | + |
| 7 | + static final int RECT_W = 10; |
| 8 | + static final int RECT_H = 10; |
| 9 | + |
| 10 | + boolean firstFrame = true; |
| 11 | + |
| 12 | + public void settings() { |
| 13 | + size(100, 100, WEBGPU); |
| 14 | + } |
| 15 | + |
| 16 | + public void draw() { |
| 17 | + background(0); // Black background |
| 18 | + |
| 19 | + loadPixels(); |
| 20 | + |
| 21 | + for (int y = 20; y < 20 + RECT_H; y++) { |
| 22 | + for (int x = 20; x < 20 + RECT_W; x++) { |
| 23 | + pixels[y * width + x] = color(255, 0, 0); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + for (int y = 60; y < 60 + RECT_H; y++) { |
| 28 | + for (int x = 60; x < 60 + RECT_W; x++) { |
| 29 | + pixels[y * width + x] = color(0, 0, 255); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + updatePixels(); |
| 34 | + |
| 35 | + if (firstFrame) { |
| 36 | + firstFrame = false; |
| 37 | + |
| 38 | + println("Total pixels: " + pixels.length); |
| 39 | + |
| 40 | + for (int y = 0; y < height; y++) { |
| 41 | + StringBuilder row = new StringBuilder(); |
| 42 | + for (int x = 0; x < width; x++) { |
| 43 | + int idx = y * width + x; |
| 44 | + int pixel = pixels[idx]; |
| 45 | + float r = red(pixel); |
| 46 | + float b = blue(pixel); |
| 47 | + float a = alpha(pixel); |
| 48 | + |
| 49 | + if (r > 127) { |
| 50 | + row.append("R"); |
| 51 | + } else if (b > 127) { |
| 52 | + row.append("B"); |
| 53 | + } else if (a > 127) { |
| 54 | + row.append("."); |
| 55 | + } else { |
| 56 | + row.append(" "); |
| 57 | + } |
| 58 | + } |
| 59 | + println(row.toString()); |
| 60 | + } |
| 61 | + |
| 62 | + println("\nSample pixels:"); |
| 63 | + println("(25, 25): " + hex(pixels[25 * width + 25])); |
| 64 | + println("(65, 65): " + hex(pixels[65 * width + 65])); |
| 65 | + println("(0, 0): " + hex(pixels[0])); |
| 66 | + println("(50, 50): " + hex(pixels[50 * width + 50])); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public static void main(String[] args) { |
| 71 | + PApplet.disableAWT = true; |
| 72 | + PApplet.main(UpdatePixels.class.getName()); |
| 73 | + } |
| 74 | +} |
0 commit comments