-
Notifications
You must be signed in to change notification settings - Fork 0
/
SceneRenderer.pde
177 lines (153 loc) · 3.74 KB
/
SceneRenderer.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import processing.video.*;
import controlP5.*;
ControlP5 cp5;
Capture video;
PImage img;
Movie m;
int points = 100;
float x;
float y;
color c = 0;
int size = (int)random(10, 30);
boolean useCam = false;
boolean useMovie = true;
boolean useImage = false;
boolean useSlider = false;
boolean useCircles = false;
boolean useRings = false;
boolean useTriangles = true;
boolean saveImage = false;
boolean drawScene = true;
void setup() {
size(640, 480);
background(0);
setupSlider();
setupCam();
setupImage();
setupMovie();
}
void draw() {
if (drawScene) {
drawScene();
} else {
checkVideoResolution();
}
}
void setupSlider() {
if (useSlider) {
cp5 = new ControlP5(this);
cp5.addSlider("points").setRange(50, 500)
.setSliderMode(Slider.FLEXIBLE)
.setColorActive(255)
.setColorBackground(0)
.setCaptionLabel("");
}
}
void setupCam() {
if (useCam) {
video = new Capture(this, 640, 480, 30);
video.start();
}
}
void setupImage() {
if (useImage) {
img = loadImage("imageToDraw.jpg");
}
}
void setupMovie() {
if (useMovie) {
m = new Movie(this, "movieToDraw.mp4");
m.loop();
}
}
void drawScene() {
for (int i = 0; i < points; i++) {
x = random(width);
y = random(height);
if (useCam) {
video.read();
c = video.get(int(x), int(y));
}
if (useImage) {
c = img.get(int(x), int(y));
}
if (useMovie) {
m.read();
c=m.get(int(x), int(y));
}
useCircles();
useRings();
useTriangles();
saveImage();
}
}
void useCircles() {
if (useCircles) {
fill(c, (int)random(50, 80));
noStroke();
ellipse(x, y, size, size);
}
}
void useRings() {
if (useRings) {
noFill();
stroke(c, (int)random(50, 80));
strokeWeight((int)random(1, 3));
ellipse(x, y, size, size);
}
}
void useTriangles() {
if (useTriangles) {
noFill();
stroke(c, (int)random(50, 80));
strokeWeight((int)random(1, 3));
float direction = random(0, 1);
if (direction > 0.5) {
triangle(x-size/2, y-size/2, x+size/2, y-size/2, x, y+size/2);
} else {
triangle(x+size/2, y+size/2, x-size/2, y+size/2, x, y-size/2);
}
}
}
void saveImage() {
if (saveImage) {
if (millis() % 1000 == 0) {
save("scene_"+millis()/1000+".jpg");
}
}
}
void checkVideoResolution() {
Capture cam;
int camwidth =0;
int camheight =0;
String[] cameras = Capture.list();
if (cameras.length == 0) {
println("There are no cameras available for capture.");
exit();
} else {
println("Available cameras:");
for (int i = 0; i < cameras.length; i++) {
// Search where the fps number is
int p = cameras[i].indexOf("fps=");
// Search where the width begins
int q = cameras[i].indexOf("size=");
// Search where the width ends and height begins
int r = cameras[i].indexOf("x");
// Search where the height ends
int s = cameras[i].indexOf(",fps");
println("fps= " + cameras[i].substring(p+4));
// transforms the numeral string into an int
int fps = Integer.parseInt(cameras[i].substring(p+4));
println("width= " + cameras[i].substring(q+5, r));
println("height= " + cameras[i].substring(r+1, s));
// test the fps... I'm not overly picky.
if ( fps > 20) {
// if the fps is faster than 20, select it as camera height&width!
camwidth = Integer.parseInt(cameras[i].substring(q+5, r));
camheight = Integer.parseInt(cameras[i].substring(r+1, s));
}
println(cameras[i]);
}
}
cam = new Capture(this, camwidth, camheight);
}