JoshuaDavis icon

buildCube as replacement for box()

JoshuaDavis | PRO | 03/09/19 05:25:21 PM UTC | 0 ⭐ | 405 👁️ | Never ⏰ | []
Java |

2.33 KB

|

None

|

0 👍

/

0 👎

/*
 
// find out what cam you have by making this code in a sketch
 
import processing.video.*;
Capture cam;
 
void setup() {
    size(1280,720);
    println( Capture.list() );
}
 
*/
 
import processing.video.*;
Capture cam;
 
int    stageW      = 1280;
int    stageH      = 720;
 
int    camW        = 1280;
int    camH        = 720;
float  cubeSpacing = 10.0;
float  cubeSize    = 9.0;
 
PImage _t;
 
void settings() {
    size(stageW, stageH, P3D);
}
 
void setup() {
    _t = loadImage("imageName.png");
 
    cam = new Capture(this, camW, camH, "HD Pro Webcam C920", 30);
    cam.start();
}
 
void draw() {
    background(0);
 
    translate(stageW/2, stageH/2);
 
    rotateY(frameCount*0.01);
 
    for (int x = 0; x < camW; x += cubeSpacing) {
        for (int y = 0; y < camH; y += cubeSpacing) {
            color pixelColor = cam.get(x, y);
            float b = map(brightness(pixelColor), 0, 255, 0, 15);
            pushMatrix();
                translate(x - camW/2, y - camH/2);
 
                strokeWeight(0);
                noStroke();
                noFill();
 
                tint(pixelColor);
 
                buildCube(cubeSize/2, cubeSize/2, (b*cubeSpacing)/2 );
            popMatrix();
        }
    }
    noTint();
}
 
void buildCube(float _w, float _h, float _d) {
    beginShape(QUADS);
        textureMode(NORMAL);
        texture(_t);
 
        // FRONT
        vertex( -(_w), -(_h),  (_d),   0, 0);
        vertex(  (_w), -(_h),  (_d),   1, 0);
        vertex(  (_w),  (_h),  (_d),   1, 1);
        vertex( -(_w),  (_h),  (_d),   0, 1);
 
        // BACK
        vertex(  (_w), -(_h), -(_d),   0, 0);
        vertex( -(_w), -(_h), -(_d),   1, 0);
        vertex( -(_w),  (_h), -(_d),   1, 1);
        vertex(  (_w),  (_h), -(_d),   0, 1);
 
        // TOP
        vertex( -(_w), -(_h), -(_d),   0, 0);
        vertex(  (_w), -(_h), -(_d),   1, 0);
        vertex(  (_w), -(_h),  (_d),   1, 1);
        vertex( -(_w), -(_h),  (_d),   0, 1);
 
        // BOTTOM
        vertex( -(_w),  (_h),  (_d),   0, 0);
        vertex(  (_w),  (_h),  (_d),   1, 0);
        vertex(  (_w),  (_h), -(_d),   1, 1);
        vertex( -(_w),  (_h), -(_d),   0, 1);
 
        // LEFT
        vertex( -(_w), -(_h), -(_d),   0, 0);
        vertex( -(_w), -(_h),  (_d),   1, 0);
        vertex( -(_w),  (_h),  (_d),   1, 1);
        vertex( -(_w),  (_h), -(_d),   0, 1);
 
        // RIGHT
        vertex(  (_w), -(_h),  (_d),   0, 0);
        vertex(  (_w), -(_h), -(_d),   1, 0);
        vertex(  (_w),  (_h), -(_d),   1, 1);
        vertex(  (_w),  (_h),  (_d),   0, 1);
    endShape(CLOSE);
}
 
void captureEvent(Capture c) {
    c.read();
}

Comments