Skip to content

Instantly share code, notes, and snippets.

@wh0am1-dev
Created November 8, 2014 19:50
Show Gist options
  • Save wh0am1-dev/c46799fa6d67a929490a to your computer and use it in GitHub Desktop.
Save wh0am1-dev/c46799fa6d67a929490a to your computer and use it in GitHub Desktop.
Webcam Mirror written in Processing
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif
#define PROCESSING_TEXTURE_SHADER
varying vec4 vertTexCoord;
uniform sampler2D texture;
uniform float brightness;
uniform float contrast;
uniform float saturation;
void main() {
vec3 texColor = texture2D(texture, vertTexCoord.st).rgb;
const vec3 LumCoeff = vec3(0.2125, 0.7154, 0.0721);
vec3 AvgLumin = vec3(0.5, 0.5, 0.5);
vec3 intensity = vec3(dot(texColor, LumCoeff));
vec3 satColor = mix(intensity, texColor, saturation);
vec3 conColor = mix(AvgLumin, satColor, contrast);
gl_FragColor = vec4(brightness * conColor, 1.0);
}
import processing.video.*;
int ancho = 640, alto = 480;
Capture webcam;
PShader shade;
PGraphics pg;
color col;
int tam = 20, step = 10;
boolean rawWebCam = false, elipse = true;
void setup() {
size(ancho, alto, P2D);
if (frame != null) {
frame.setResizable(true);
}
noStroke();
setupShader();
setupCam();
pg = createGraphics(ancho, alto);
pg.noStroke();
}
void draw() {
if(webcam.available()) {
webcam.read();
}
setShaderParams();
shader(shade);
if (rawWebCam) {
scale(-1, 1);
image(webcam, 0, 0, -frame.getWidth(), frame.getHeight());
} else {
pg.beginDraw();
for (int a = 0; a < ancho; a += step) {
for (int b = 0; b < alto; b += step) {
col = webcam.get(a, b);
pg.fill(col);
if (elipse) {
pg.ellipse(ancho - a, b, tam, tam);
} else {
pg.rect(ancho - a, b, tam, tam);
}
}
}
for (int b = 0; b < alto; b += step) {
col = webcam.get(ancho - 1, b);
pg.fill(col);
if (elipse) {
pg.ellipse(-2, b, tam, tam);
} else {
pg.rect(-2, b, tam, tam);
}
}
pg.endDraw();
image(pg, 0, 0, ancho, alto);
}
resetShader();
}
void setupShader() {
shade = loadShader("brcosa.glsl");
}
void setupCam() {
webcam = new Capture(this, Capture.list()[0]);
webcam.start();
}
void setShaderParams() {
shade.set("brightness", 1.0);
shade.set("contrast", map(mouseX, 0, width, -5, 5));
shade.set("saturation", map(mouseY, 0, height, -5, 5));
}
void mouseReleased() {
if (mouseButton == LEFT) {
rawWebCam = !rawWebCam;
} else if (mouseButton == RIGHT) {
elipse = !elipse;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment