Skip to content

Commit 96f5c6e

Browse files
authored
Merge pull request #7 from codeanticode/new-gl-demos
New gl demos
2 parents e29994d + 66aaa12 commit 96f5c6e

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// GetTessGroups, by Andres Colubri
2+
// The getTessellation() function in Processing 4 returns a group shape
3+
// where the first shape contains only the fill geometry, the second,
4+
// the stroke lines, and third, only the points (if any).
5+
6+
PShape box;
7+
PShape tess;
8+
9+
boolean onlyStrokeLines = false;
10+
11+
void setup() {
12+
size(600, 600, P3D);
13+
14+
strokeWeight(5);
15+
box = createShape(BOX, 200);
16+
17+
tess = box.getTessellation();
18+
}
19+
20+
void draw() {
21+
background(180);
22+
lights();
23+
24+
translate(width/2, height/2);
25+
rotateX(frameCount * 0.01);
26+
rotateY(frameCount * 0.01);
27+
for (int i = 0; i < tess.getChildCount(); i++) {
28+
if (!onlyStrokeLines || i == 1) {
29+
shape(tess.getChild(i));
30+
}
31+
}
32+
}
33+
34+
void keyPressed() {
35+
onlyStrokeLines = !onlyStrokeLines;
36+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// MultipleWindows, by Andres Colubri
2+
// Adapted from a PixelFlow example by Thomas Diewald
3+
// Demonstration of a multiple window sketch with OpenGL
4+
// (P2D or P3D) renderers, including resource sharing
5+
// across windows.
6+
7+
ChildApplet childA;
8+
ChildApplet childB;
9+
ChildApplet childC;
10+
11+
PShape pointer;
12+
13+
void setup() {
14+
size(400, 300, P2D);
15+
println("Creating window 1");
16+
17+
// This PShape can be shared across all windows
18+
pointer = createShape(ELLIPSE, 0, 0, 20, 20);
19+
20+
childA = new ChildApplet(2, 500, 0, 400, 300);
21+
childA.bckColor = color(227, 173, 37);
22+
23+
// Change location of parent window after creating child window.
24+
surface.setLocation(100, 0);
25+
}
26+
27+
void draw() {
28+
background(32);
29+
30+
fill(160);
31+
textAlign(CENTER, CENTER);
32+
text("MAIN window", width/2, height/2);
33+
34+
translate(mouseX, mouseY);
35+
shape(pointer);
36+
37+
String txt = String.format("Window 1 %6.2fps", frameRate);
38+
surface.setTitle(txt);
39+
}
40+
41+
public void keyPressed() {
42+
if (childB == null) {
43+
childB = new ChildApplet(3, 500, 353, 400, 300);
44+
childB.bckColor = color(137, 227, 37);
45+
} else if (childC == null) {
46+
childC = new ChildApplet(4, 100, 353, 400, 300);
47+
childC.bckColor = color(51, 157, 209);
48+
}
49+
}
50+
51+
class ChildApplet extends PApplet {
52+
int id, vx, vy, vw, vh;
53+
int bckColor;
54+
55+
ChildApplet(int id, int vx, int vy, int vw, int vh) {
56+
super();
57+
this.id = id;
58+
this.vx = vx;
59+
this.vy = vy;
60+
this.vw = vw;
61+
this.vh = vh;
62+
63+
PApplet.runSketch(new String[] { this.getClass().getName() }, this);
64+
}
65+
66+
void settings() {
67+
size(vw, vh, P2D);
68+
smooth(0);
69+
println("Creating window "+ id);
70+
}
71+
72+
void setup() {
73+
surface.setLocation(vx, vy);
74+
surface.setResizable(true);
75+
}
76+
77+
void draw() {
78+
background(bckColor);
79+
textAlign(CENTER, CENTER);
80+
text("CHILD window "+ id, width/2, height/2);
81+
82+
translate(mouseX, mouseY);
83+
shape(pointer);
84+
85+
String txt = String.format("Window %d %6.2fps", id, frameRate);
86+
surface.setTitle(txt);
87+
}
88+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// TessUpdate, by Andres Colubri
2+
// The begin/endTessallation API in Processing 4
3+
// enables dynamic modification of PShape objects.
4+
5+
PShape group;
6+
7+
int numBoxes = 100000;
8+
float boxSize = 2;
9+
10+
PMatrix3D mat = new PMatrix3D();
11+
12+
int fcount, lastm;
13+
float frate;
14+
int fint = 3;
15+
16+
void setup() {
17+
size(600, 600, P3D);
18+
noSmooth();
19+
20+
noStroke();
21+
group = createShape(GROUP);
22+
23+
for (int i = 0; i < numBoxes; i++) {
24+
PShape s = createShape(BOX, boxSize, boxSize, boxSize);
25+
s.setFill(#F5CB40);
26+
s.translate(random(-width/2, width/2), random(-height/2, height/2), random(-1000, 1000));
27+
group.addChild(s);
28+
}
29+
}
30+
31+
void draw() {
32+
background(0);
33+
lights();
34+
PVector v = new PVector();
35+
36+
// PShape.set*() calls between beginTessellation() and endTessellation
37+
// will modify the tessellated geometry directly, so this should result
38+
// better performance for shapes that change dynamically during drawing
39+
group.beginTessellation(TRIANGLES);
40+
// getChildCount() returns number of vertices in the tessellated geometry,
41+
// which could be different from the original number of vertices in the shape.
42+
for (int ci = 0; ci < group.getChildCount(); ci++) {
43+
PShape child = group.getChild(ci);
44+
float rx = random(-1, 1);
45+
float ry = random(-1, 1);
46+
float rz = random(-1, 1);
47+
for (int vi = 0; vi < child.getVertexCount(); vi++) {
48+
child.getVertex(vi, v);
49+
v.x += rx;
50+
v.y += ry;
51+
v.z += rz;
52+
child.setVertex(vi, v.x, v.y, v.z);
53+
}
54+
}
55+
group.endTessellation();
56+
57+
translate(width/2, height/2, 0);
58+
rotateY(frameCount * 0.001);
59+
rotateX(frameCount * 0.001);
60+
shape(group);
61+
62+
String txt = String.format("FPS: %6.2fps", frameRate);
63+
surface.setTitle(txt);
64+
}

0 commit comments

Comments
 (0)