-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplayFlow.pde
More file actions
78 lines (73 loc) · 2.55 KB
/
DisplayFlow.pde
File metadata and controls
78 lines (73 loc) · 2.55 KB
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
static int CameraUpdateResetValue = -30000;
int lastCameraUpdateTime = CameraUpdateResetValue;
PImage cameraBuffer;
void displayFlow(PImage cameraFrame)
{
if (mode == 0) {
flowmap.filter(BLUR);
distortionMapShader.set("u_flowmap", flowmap);
int time = millis();
println(averageFlow);
if (time - lastCameraUpdateTime >= cameraDelayMs || averageFlow.mag() > 1.5)
{
lastCameraUpdateTime = time;
cameraBuffer = cameraFrame.copy();
distortionMapShader.set("u_camera", cameraBuffer);
}
filter(distortionMapShader);
return;
}
cameraFrame.loadPixels();
flowmap.loadPixels();
loadPixels();
for (int x=0; x<width; x++)
{
for (int y=0; y<height; y++)
{
// Calculate this pixels indices in the varying coordinate frames
PVector windowCoord = new PVector(x,y);
int windowIndex = coordToIndex(windowCoord, windowSize);
if (flipHorizontal) flipHorizontal(windowCoord, windowSize);
int cameraIndex = windowCoordToCameraIndex(windowCoord);
int gridIndex = windowCoordToGridIndex(windowCoord);
// Take samples from the different frames
PVector flow;
if (doGridBilinearInterpolation) {
PVector gridCoord = coordFrameToCoordFrame(windowCoord, windowSize, gridSize, false);
flow = bilinearInterpolation(gridCoord, gridSize, flowmap.pixels);
} else {
flow = colorToVector(flowmap.pixels[gridIndex]);
}
float pct = map(flow.y, 0,255, -1,1);
PVector cameraSample = colorToVector(cameraFrame.pixels[cameraIndex]);
cameraSample = PVector.add(cameraSample, new PVector(127,127,127));
PVector windowSample = colorToVector(pixels[windowIndex]);
windowSample = PVector.add(windowSample, new PVector(127,127,127));
// Display results according to the mixing mode
switch (mode)
{
case 0: // Shader above
break;
case 1: // Black clouds
if (abs(pct) > 0.1) windowSample = PVector.mult(windowSample,pct);
pixels[windowIndex] = vectorToColor(windowSample, 0.2);
break;
case 2:
if (abs(pct) < 0.4) pixels[windowIndex] = int(pixels[windowIndex] * pct);
if (abs(pct) < 0.3)
{
windowSample.y = windowSample.x; windowSample.z = windowSample.x;
}
else multInPlace(windowSample, abs(pct)+1);
subInPlace(windowSample, HalfColor);
pixels[windowIndex] = vectorToColor(windowSample);
break;
case 3: // Simple mix of webcam input with flow direction, based on flow intensity
PVector motion = mix( cameraSample, flow, 0.5 );
pixels[windowIndex] = mixColor( pixels[windowIndex], vectorToColor(motion), pct );
break;
}
}
}
updatePixels();
}