forked from mhenstell/acw
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Fire.pde
101 lines (82 loc) · 2.76 KB
/
Fire.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
class Fire extends Routine {
// This will contain the pixels used to calculate the fire effect
int[][] fire;
// Flame colors
color[] palette;
float angle;
int[] calc1, calc2, calc3, calc4, calc5;
PGraphics pg;
void setup(PApplet parent) {
super.setup(parent);
// Create buffered image for 3d cube
pg = createGraphics(Config.WIDTH, Config.HEIGHT, P3D);
calc1 = new int[Config.WIDTH];
calc3 = new int[Config.WIDTH];
calc4 = new int[Config.WIDTH];
calc2 = new int[Config.HEIGHT];
calc5 = new int[Config.HEIGHT];
draw.colorMode(HSB);
fire = new int[Config.WIDTH][Config.HEIGHT];
palette = new color[255];
// Generate the palette
for (int x = 0; x < palette.length; x++) {
//Hue goes from 0 to 85: red to yellow
//Saturation is always the maximum: 255
//Lightness is 0..255 for x=0..128, and 255 for x=128..255
palette[x] = color(x/3, 255, constrain(x*3, 0, 255));
}
// Precalculate which pixel values to add during animation loop
// this speeds up the effect by 10fps
for (int x = 0; x < Config.WIDTH; x++) {
calc1[x] = x % Config.WIDTH;
calc3[x] = (x - 1 + Config.WIDTH) % Config.WIDTH;
calc4[x] = (x + 1) % Config.WIDTH;
}
for (int y = 0; y < Config.HEIGHT; y++) {
calc2[y] = (y + 1) % Config.HEIGHT;
calc5[y] = (y + 2) % Config.HEIGHT;
}
draw.colorMode(RGB);
}
void draw() {
angle = angle + 0.05;
// // Rotating wireframe cube
// pg.beginDraw();
// pg.translate(Config.WIDTH >> 1, Config.HEIGHT >> 1);
// pg.rotateX(sin(angle/2));
// pg.rotateY(cos(angle/2));
// pg.background(0);
// pg.stroke(128);
// pg.scale(25);
// pg.noFill();
// pg.box(4);
// pg.endDraw();
// Randomize the bottom row of the fire buffer
for (int x = 0; x < Config.WIDTH; x++)
{
fire[x][Config.HEIGHT-1] = int(random(0, 190)) ;
}
draw.loadPixels();
int counter = 0;
// Do the fire calculations for every pixel, from top to bottom
for (int y = 0; y < Config.HEIGHT; y++) {
for (int x = 0; x < Config.WIDTH; x++) {
// Add pixel values around current pixel
fire[x][y] =
((fire[calc3[x]][calc2[y]]
+ fire[calc1[x]][calc2[y]]
+ fire[calc4[x]][calc2[y]]
+ fire[calc1[x]][calc5[y]]) << 5) / 129;
// Output everything to screen using our palette colors
draw.pixels[counter] = palette[fire[x][y]];
// Extract the red value using right shift and bit mask
// equivalent of red(pg.pixels[x+y*w])
if ((pg.pixels[counter++] >> 16 & 0xFF) == 128) {
// Only map 3D cube 'lit' pixels onto fire array needed for next frame
fire[x][y] = 128;
}
}
}
draw.updatePixels();
}
}