Hello folks!
Some simple and fun code exploring PGraphics.
// Author: GLV
// Date: 2026-03-01
// Description:
// Some fun with PGraphics
// Watch while listening to:
// https://open.spotify.com/track/1i2HCDqbRb6onboeRJmPhQ?si=1f77bee27f554d1f
float yaw, pitch, roll;
int counter;
color col;
PGraphics pg3d, pglis;
PImage lisBackground;
PImage lis(int numStars)
{
pglis.beginDraw();
pglis.background(0); // black
pglis.stroke(255);
for(int i=0; i<numStars; i++)
{
pglis.strokeWeight(random(1, 4));
pglis.stroke(random(128, 255));
pglis.point(random(pglis.width), random(pglis.height));
}
pglis.endDraw();
return pglis.get();
}
void pg3dUpdate(float yaw, float pitch, float roll, color col, float s)
{
//pg3d.smooth(4); // smooth() can only be used before beginDraw()
pg3d.beginDraw();
//pg3d.background(0, 20); // A bit of transparencey for testing
//pg3d.background(0); // Black
pg3d.clear();
pg3d.push();
pg3d.translate(pg3d.width/2, pg3d.height/2); // origin at center of PGraphic pg3d
pg3d.lights();
pg3d.fill(col);
pg3d.noStroke();
pg3d.rotateY(-HALF_PI); // For model alignment.
pg3d.rotateY(radians(yaw)); // yaw
pg3d.rotateZ(radians(pitch)); // pitch
pg3d.rotateX(radians(roll)); // roll
pg3d.box(s*90, s*10, s*40); // Long along x-axis; later rotated to point "forward"
pg3d.pop();
pg3d.endDraw();
}
void setup()
{
size(800, 800, P3D);
surface.setLocation(10, 10); // Fixes occasional issue of not displaying!
surface.setTitle("Lost in Space");
pg3d = createGraphics(300, 300, P3D); // 3D box
pglis = createGraphics(width, height, P2D); // stars
lisBackground = lis(500); // Make background of stars
// Debug
println("pg3d width: ", pg3d.width);
println("pg3d height:", pg3d.height);
println("lis width: ", pglis.width);
println("lis height:", pglis.height);
println("width: ", width);
println("height:", height);
}
float a, d;
void draw()
{
background(lisBackground);
//image(lisBackground, 0, 0);
if(frameCount%10 == 0)
{
a = random(120, 255);
d = random(2, 4);
}
pushStyle();
noStroke();
fill(255, 255, 255, a);
ellipse(width/2, height/2, d, d);
popStyle();
if(frameCount < 3*60)
{
textSize(96);
textAlign(CENTER, CENTER);
text("Lost in Space", width/2, height/2);
// Fixed:
col = color(255, 255, 0);
pg3dUpdate(90, 30, 30, col, 2); // Updates pg3D
imageMode(CENTER);
image(pg3d, width/2, 200); // Centers it middle top
return; // Start after 3 seconds
}
ypr(); // yaw, pitch roll controls
counter++;
//// Fixed:
// col = color(255, 255, 0);
// pg3dUpdate(yaw, pitch, roll, col, 2); // Updates pg3D
// imageMode(CORNER);
// image(pg3d, 10, 10); // Centers it upper left
// Mouse moves:
col = color(0, 255, 0);
pg3dUpdate(yaw, pitch, roll, col, 2); // Updates pg3D
imageMode(CENTER);
image(pg3d, mouseX, mouseY); // Move with mouse
push();
translate(width/2, height/2);
float x = 250*cos(counter*(TAU/(4*360)));
float y = 250*sin(counter*(TAU/(4*360)));
col = color(255, 0, 0);
pg3dUpdate(yaw, pitch, roll, col, 1); // Updates pg3D
imageMode(CENTER);
image(pg3d, x, y);
col = color(0, 255, 255);
pg3dUpdate(-yaw, pitch, roll, col, 1); // Updates pg3D
imageMode(CENTER);
image(pg3d, -x, -y);
pop();
}
// // yaw, pitch roll controls
void ypr()
{
yaw = counter%(4*360); //Max is 4*360 4 revolutions
pitch = 30*sin(counter*(TAU/720));
roll = 30*sin(counter*(TAU/120));
}
:)
