Added new shader examples

This commit is contained in:
codeanticode
2012-07-12 03:14:36 +00:00
parent 209de03dbb
commit 03beee8abc
12 changed files with 628 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
// Bad-print shader. Adapted from rom Fluxus Shader Library:
// http://www.pawfal.org/index.php?page=FluxusHardwareShaderLibrary
import controlP5.*;
ControlP5 controlP5;
PShader shader;
PGraphicsOpenGL pg;
boolean enabled = true;
float scaleR = 1.0, scaleG = 1.0, scaleB = 1.0;
float offsetR = 0.2, offsetG = 0.2, offsetB = 0.2;
float registerR = 0.2, registerG = 0.2, registerB = 0.2;
float sizeR = 0.1, sizeG = 0.2, sizeB = 0.1;
public void setup() {
size(800, 800, P3D);
noStroke();
fill(204);
pg = (PGraphicsOpenGL)g;
shader = pg.loadShader("BadPrintVert.glsl", "BadPrintFrag.glsl", PShader.LIT);
pg.setShader(shader, PShader.LIT);
sphereDetail(60);
controlP5 = new ControlP5(this);
controlP5.addToggle("enabled").linebreak();
controlP5.addSlider("scaleR", 0, 1);
controlP5.addSlider("scaleG", 0, 1);
controlP5.addSlider("scaleB", 0, 1).linebreak();
controlP5.addSlider("offsetR", 0, 1);
controlP5.addSlider("offsetG", 0, 1);
controlP5.addSlider("offsetB", 0, 1).linebreak();
controlP5.addSlider("registerR", 0, 1);
controlP5.addSlider("registerG", 0, 1);
controlP5.addSlider("registerB", 0, 1).linebreak();
controlP5.addSlider("sizeR", 0, 1);
controlP5.addSlider("sizeG", 0, 1);
controlP5.addSlider("sizeB", 0, 1).linebreak();
}
public void draw() {
background(0);
if (enabled) {
pg.setShader(shader, PShader.LIT);
shader.setUniform("Scale", scaleR, scaleG, scaleB);
shader.setUniform("Offset", offsetR, offsetG, offsetB);
shader.setUniform("Register", registerR, registerG, registerB);
shader.setUniform("Size", sizeR, sizeG, sizeB);
} else {
pg.defaultShader(PShader.LIT);
}
noStroke();
pointLight(204, 204, 204, 1000, 1000, 1000);
pushMatrix();
translate(width/2, height/2);
rotateX(frameCount * 0.01);
rotateY(frameCount * 0.01);
sphere(200);
popMatrix();
hint(DISABLE_DEPTH_TEST);
noLights();
controlP5.draw();
hint(ENABLE_DEPTH_TEST);
}

View File

@@ -0,0 +1,36 @@
// Copyright (C) 2007 Dave Griffiths
// Licence: GPLv2 (see COPYING)
// Fluxus Shader Library
// ---------------------
// BadPrint NPR Shader
// This shader tries to emulate the effect
// of a bad printing process. Can be controlled
// with different settings for RGB
uniform vec3 Scale;
uniform vec3 Offset;
uniform vec3 Register;
uniform vec3 Size;
varying vec3 N;
varying vec3 P;
varying vec4 S;
varying vec3 L;
void main() {
vec3 l = normalize(L);
vec3 n = normalize(N);
vec2 p = S.xy;
vec2 sr = p * Size.r + Register.r;
vec2 sg = p * Size.g + Register.g;
vec2 sb = p * Size.b + Register.b;
float diffuse = dot(l,n);
float r = (sin(sr.x) + cos(sr.y)) * Scale.r + Offset.r + diffuse;
float g = (sin(sg.x) + cos(sg.y)) * Scale.g + Offset.g + diffuse;
float b = (sin(sb.x) + cos(sb.y)) * Scale.b + Offset.b + diffuse;
gl_FragColor = vec4(step(0.5, r), step(0.5, g), step(0.5, b), 1);
}

View File

@@ -0,0 +1,30 @@
// Copyright (C) 2007 Dave Griffiths
// Licence: GPLv2 (see COPYING)
// Fluxus Shader Library
// ---------------------
// BadPrint NPR Shader
// This shader tries to emulate the effect
// of a bad printing process. Can be controlled
// with different settings for RGB
uniform mat4 projectionMatrix;
uniform mat4 projmodelviewMatrix;
uniform mat3 normalMatrix;
uniform vec4 lightPosition[8];
attribute vec4 inVertex;
attribute vec3 inNormal;
varying vec3 N;
varying vec3 P;
varying vec4 S;
varying vec3 L;
void main() {
N = normalize(normalMatrix * inNormal);
P = inVertex.xyz;
gl_Position = projmodelviewMatrix * inVertex;
L = vec3(lightPosition[0] - gl_Position);
S = projectionMatrix * gl_Position;
}

View File

@@ -0,0 +1,39 @@
// Fish-eye shader, useful for dome projection
PGraphics canvas;
PShader fisheye;
PImage img;
void setup() {
size(400, 400, P3D);
canvas = createGraphics(400, 400, P3D);
PGraphicsOpenGL pg = (PGraphicsOpenGL)g;
fisheye = pg.loadShader("FishEye.glsl", PShader.TEXTURED);
fisheye.setUniform("aperture", 180.0);
pg.setShader(fisheye, PShader.TEXTURED);
}
void draw() {
canvas.beginDraw();
canvas.background(0);
canvas.stroke(255, 0, 0);
for (int i = 0; i < width; i += 10) {
canvas.line(i, 0, i, height);
}
for (int i = 0; i < height; i += 10) {
canvas.line(0, i, width, i);
}
canvas.lights();
canvas.noStroke();
canvas.translate(mouseX, mouseY, 100);
canvas.rotateX(frameCount * 0.01f);
canvas.rotateY(frameCount * 0.01f);
canvas.box(50);
canvas.endDraw();
// The rendering of this image will be done through the fisheye shader, since
// it was set as the PShader.TEXTURED shader of the main surface.
image(canvas, 0, 0, width, height);
}

View File

@@ -0,0 +1,46 @@
// Inspired by the "Angular Fisheye à la Bourke" sketch from
// Jonathan Cremieux, as shown in the OpenProcessing website:
// http://openprocessing.org/visuals/?visualID=12140
// Using the inverse transform of the angular fisheye as
// explained in Paul Bourke's website:
// http://paulbourke.net/miscellaneous/domefisheye/fisheye/
uniform sampler2D textureSampler;
varying vec4 vertColor;
varying vec4 vertTexcoord;
uniform float aperture;
const float PI = 3.1415926535;
void main(void) {
float apertureHalf = 0.5 * aperture * (PI / 180.0);
// This factor ajusts the coordinates in the case that
// the aperture angle is less than 180 degrees, in which
// case the area displayed is not the entire half-sphere.
float maxFactor = sin(apertureHalf);
vec2 pos = 2.0 * vertTexcoord.st - 1.0;
float l = length(pos);
if (l > 1.0) {
gl_FragColor = vec4(0, 0, 0, 1);
} else {
float x = maxFactor * pos.x;
float y = maxFactor * pos.y;
float n = length(vec2(x, y));
float z = sqrt(1.0 - n * n);
float r = atan(n, z) / PI;
float phi = atan(y, x);
float u = r * cos(phi) + 0.5;
float v = r * sin(phi) + 0.5;
gl_FragColor = texture2D(textureSampler, vec2(u, v)) * vertColor;
}
}

View File

@@ -0,0 +1,71 @@
// Fish-eye shader on the main surface, glossy specular reflection shader
// on the offscreen canvas.
PGraphics canvas;
PShader fisheye;
PShader glossy;
PImage img;
PShape ball;
boolean usingFishEye;
void setup() {
size(800, 800, P3D);
canvas = createGraphics(800, 800, P3D);
PGraphicsOpenGL pg = (PGraphicsOpenGL)g;
fisheye = pg.loadShader("FishEye.glsl", PShader.TEXTURED);
fisheye.setUniform("aperture", 180.0);
pg.setShader(fisheye, PShader.TEXTURED);
usingFishEye = true;
glossy = pg.loadShader("GlossyVert.glsl", "GlossyFrag.glsl", PShader.LIT);
glossy.setUniform("AmbientColour", 0, 0, 0);
glossy.setUniform("DiffuseColour", 0.9, 0.2, 0.2);
glossy.setUniform("SpecularColour", 1.0, 1.0, 1.0);
glossy.setUniform("AmbientIntensity", 1.0);
glossy.setUniform("DiffuseIntensity", 1.0);
glossy.setUniform("SpecularIntensity", 0.7);
glossy.setUniform("Roughness", 0.7);
glossy.setUniform("Sharpness", 0.0);
((PGraphicsOpenGL)canvas).setShader(glossy, PShader.LIT);
ball = createShape(SPHERE, 50);
//ball.fill(200, 50, 50);
ball.noStroke();
}
void draw() {
canvas.beginDraw();
canvas.noStroke();
canvas.background(0);
canvas.pushMatrix();
canvas.rotateY(frameCount * 0.01);
canvas.pointLight(204, 204, 204, 1000, 1000, 1000);
canvas.popMatrix();
for (float x = 0; x < canvas.width + 100; x += 100) {
for (float y = 0; y < canvas.height + 100; y += 100) {
for (float z = 0; z < 400; z += 100) {
canvas.pushMatrix();
canvas.translate(x, y, -z);
canvas.shape(ball);
canvas.popMatrix();
}
}
}
canvas.endDraw();
image(canvas, 0, 0, width, height);
println(frameRate);
}
public void mousePressed() {
if (usingFishEye) {
((PGraphicsOpenGL)g).defaultShader(PShader.TEXTURED);
usingFishEye = false;
} else {
((PGraphicsOpenGL)g).setShader(fisheye, PShader.TEXTURED);
usingFishEye = true;
}
}

View File

@@ -0,0 +1,46 @@
// Inspired by the "Angular Fisheye à la Bourke" sketch from
// Jonathan Cremieux, as shown in the OpenProcessing website:
// http://openprocessing.org/visuals/?visualID=12140
// Using the inverse transform of the angular fisheye as
// explained in Paul Bourke's website:
// http://paulbourke.net/miscellaneous/domefisheye/fisheye/
uniform sampler2D textureSampler;
varying vec4 vertColor;
varying vec4 vertTexcoord;
uniform float aperture;
const float PI = 3.1415926535;
void main(void) {
float apertureHalf = 0.5 * aperture * (PI / 180.0);
// This factor ajusts the coordinates in the case that
// the aperture angle is less than 180 degrees, in which
// case the area displayed is not the entire half-sphere.
float maxFactor = sin(apertureHalf);
vec2 pos = 2.0 * vertTexcoord.st - 1.0;
float l = length(pos);
if (l > 1.0) {
gl_FragColor = vec4(0, 0, 0, 1);
} else {
float x = maxFactor * pos.x;
float y = maxFactor * pos.y;
float n = length(vec2(x, y));
float z = sqrt(1.0 - n * n);
float r = atan(n, z) / PI;
float phi = atan(y, x);
float u = r * cos(phi) + 0.5;
float v = r * sin(phi) + 0.5;
gl_FragColor = texture2D(textureSampler, vec2(u, v)) * vertColor;
}
}

View File

@@ -0,0 +1,40 @@
// Copyright (C) 2007 Dave Griffiths
// Copyright (C) 2007 Dave Griffiths
// Licence: GPLv2 (see COPYING)
// Fluxus Shader Library
// ---------------------
// Glossy Specular Reflection Shader
// A more controllable version of blinn shading,
// Useful for ceramic or fluids - from Advanced
// Renderman, thanks to Larry Gritz
uniform vec3 AmbientColour;
uniform vec3 DiffuseColour;
uniform vec3 SpecularColour;
uniform float AmbientIntensity;
uniform float DiffuseIntensity;
uniform float SpecularIntensity;
uniform float Roughness;
uniform float Sharpness;
varying vec3 N;
varying vec3 P;
varying vec3 V;
varying vec3 L;
void main()
{
float w = 0.18*(1.0-Sharpness);
vec3 l = normalize(L);
vec3 n = normalize(N);
vec3 v = normalize(V);
vec3 h = normalize(l+v);
float diffuse = dot(l,n);
float specular = smoothstep(0.72-w,0.72+w,pow(max(0.0,dot(n,h)),1.0/Roughness));
gl_FragColor = vec4(AmbientColour*AmbientIntensity +
DiffuseColour*diffuse*DiffuseIntensity +
SpecularColour*specular*SpecularIntensity,1);
}

View File

@@ -0,0 +1,31 @@
// Copyright (C) 2007 Dave Griffiths
// Licence: GPLv2 (see COPYING)
// Fluxus Shader Library
// ---------------------
// Glossy Specular Reflection Shader
// A more controllable version of blinn shading,
// Useful for ceramic or fluids - from Advanced
// Renderman, thanks to Larry Gritz
uniform mat4 modelviewMatrix;
uniform mat4 projmodelviewMatrix;
uniform mat3 normalMatrix;
uniform vec4 lightPosition[8];
attribute vec4 inVertex;
attribute vec3 inNormal;
varying vec3 N;
varying vec3 P;
varying vec3 V;
varying vec3 L;
void main() {
N = normalize(normalMatrix * inNormal);
P = inVertex.xyz;
V = -vec3(modelviewMatrix * inVertex);
L = vec3(modelviewMatrix * (lightPosition[0] - inVertex));
gl_Position = projmodelviewMatrix * inVertex;
}

View File

@@ -0,0 +1,101 @@
// Draws a triangle using low-level OpenGL calls.
import java.nio.FloatBuffer;
PGraphicsOpenGL pg;
PGL pgl;
PShader shader;
int vertLoc;
int colorLoc;
float[] vertices;
float[] colors;
FloatBuffer vertData;
FloatBuffer colorData;
void setup() {
size(400, 400, P3D);
pg = (PGraphicsOpenGL)g;
// Get the default shader that Processing uses to
// render flat geometry (w/out textures and lights).
shader = pg.getShader(PShader.FLAT);
vertices = new float[12];
vertData = PGL.allocateDirectFloatBuffer(12);
colors = new float[12];
colorData = PGL.allocateDirectFloatBuffer(12);
}
void draw() {
background(0);
// The geometric transformations will be automatically passed
// to the shader.
rotate(frameCount * 0.01f, width, height, 0);
updateGeometry();
pgl = pg.beginPGL();
shader.bind();
vertLoc = pgl.glGetAttribLocation(shader.glProgramObjectID, "inVertex");
colorLoc = pgl.glGetAttribLocation(shader.glProgramObjectID, "inColor");
pgl.glEnableVertexAttribArray(vertLoc);
pgl.glEnableVertexAttribArray(colorLoc);
pgl.glVertexAttribPointer(vertLoc, 4, PGL.GL_FLOAT, false, 0, vertData);
pgl.glVertexAttribPointer(colorLoc, 4, PGL.GL_FLOAT, false, 0, colorData);
pgl.glDrawArrays(PGL.GL_TRIANGLES, 0, 3);
pgl.glDisableVertexAttribArray(vertLoc);
pgl.glDisableVertexAttribArray(colorLoc);
shader.unbind();
pg.endPGL();
}
void updateGeometry() {
// Vertex 1
vertices[0] = 0;
vertices[1] = 0;
vertices[2] = 0;
vertices[3] = 1;
colors[0] = 1;
colors[1] = 0;
colors[2] = 0;
colors[3] = 1;
// Corner 2
vertices[4] = width/2;
vertices[5] = height;
vertices[6] = 0;
vertices[7] = 1;
colors[4] = 0;
colors[5] = 1;
colors[6] = 0;
colors[7] = 1;
// Corner 3
vertices[8] = width;
vertices[9] = 0;
vertices[10] = 0;
vertices[11] = 1;
colors[8] = 0;
colors[9] = 0;
colors[10] = 1;
colors[11] = 1;
vertData.rewind();
vertData.put(vertices);
vertData.position(0);
colorData.rewind();
colorData.put(colors);
colorData.position(0);
}

View File

@@ -0,0 +1,62 @@
// Separable-blur shader (works by applying two succesive passes
// in each direction of the image)
PShader shader;
PGraphicsOpenGL src;
PGraphicsOpenGL pass1, pass2;
void setup() {
size(200, 200, P2D);
shader = ((PGraphicsOpenGL)g).loadShader("blur.glsl", PShader.TEXTURED);
shader.setUniform("blurSize", 9);
shader.setUniform("sigma", 5.0f);
src = (PGraphicsOpenGL)createGraphics(width, height, P2D);
pass1 = (PGraphicsOpenGL)createGraphics(width, height, P2D);
pass1.noSmooth();
pass1.setShader(shader, PShader.TEXTURED);
pass2 = (PGraphicsOpenGL)createGraphics(width, height, P2D);
pass2.noSmooth();
pass2.setShader(shader, PShader.TEXTURED);
}
void draw() {
src.beginDraw();
src.background(0);
src.fill(255);
src.ellipse(width/2, height/2, 100, 100);
src.endDraw();
// Applying the blur shader along the vertical direction
shader.setUniform("horizontalPass", 0);
pass1.beginDraw();
pass1.image(src, 0, 0);
pass1.endDraw();
// Applying the blur shader along the horizontal direction
shader.setUniform("horizontalPass", 1);
pass2.beginDraw();
pass2.image(pass1, 0, 0);
pass2.endDraw();
image(pass2, 0, 0);
}
void keyPressed() {
if (key == '9') {
shader.setUniform("blurSize", 9);
shader.setUniform("sigma", 5.0);
} else if (key == '7') {
shader.setUniform("blurSize", 7);
shader.setUniform("sigma", 3.0);
} else if (key == '5') {
shader.setUniform("blurSize", 5);
shader.setUniform("sigma", 2.0);
} else if (key == '3') {
shader.setUniform("blurSize", 5);
shader.setUniform("sigma", 1.0);
}
}

View File

@@ -0,0 +1,52 @@
// Adapted from:
// http://callumhay.blogspot.com/2010/09/gaussian-blur-shader-glsl.html
uniform sampler2D textureSampler;
// The inverse of the texture dimensions along X and Y
uniform vec2 texcoordOffset;
varying vec4 vertColor;
varying vec4 vertTexcoord;
uniform int blurSize;
uniform int horizontalPass; // 0 or 1 to indicate vertical or horizontal pass
uniform float sigma; // The sigma value for the gaussian function: higher value means more blur
// A good value for 9x9 is around 3 to 5
// A good value for 7x7 is around 2.5 to 4
// A good value for 5x5 is around 2 to 3.5
// ... play around with this based on what you need :)
const float pi = 3.14159265;
void main() {
float numBlurPixelsPerSide = float(blurSize / 2);
vec2 blurMultiplyVec = 0 < horizontalPass ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
// Incremental Gaussian Coefficent Calculation (See GPU Gems 3 pp. 877 - 889)
vec3 incrementalGaussian;
incrementalGaussian.x = 1.0 / (sqrt(2.0 * pi) * sigma);
incrementalGaussian.y = exp(-0.5 / (sigma * sigma));
incrementalGaussian.z = incrementalGaussian.y * incrementalGaussian.y;
vec4 avgValue = vec4(0.0, 0.0, 0.0, 0.0);
float coefficientSum = 0.0;
// Take the central sample first...
avgValue += texture2D(textureSampler, vertTexcoord.st) * incrementalGaussian.x;
coefficientSum += incrementalGaussian.x;
incrementalGaussian.xy *= incrementalGaussian.yz;
// Go through the remaining 8 vertical samples (4 on each side of the center)
for (float i = 1.0; i <= numBlurPixelsPerSide; i++) {
avgValue += texture2D(textureSampler, vertTexcoord.st - i * texcoordOffset *
blurMultiplyVec) * incrementalGaussian.x;
avgValue += texture2D(textureSampler, vertTexcoord.st + i * texcoordOffset *
blurMultiplyVec) * incrementalGaussian.x;
coefficientSum += 2.0 * incrementalGaussian.x;
incrementalGaussian.xy *= incrementalGaussian.yz;
}
gl_FragColor = avgValue / coefficientSum;
}