Updated shader examples

This commit is contained in:
codeanticode
2012-08-30 00:00:33 +00:00
parent c3b98cb978
commit ba3cc93584
11 changed files with 120 additions and 80 deletions

View File

@@ -19,8 +19,7 @@ public void setup() {
noStroke();
fill(204);
badPrint = loadShader(PShader.LIT, "BadPrintFrag.glsl", "BadPrintVert.glsl");
shader(badPrint);
badPrint = loadShader("BadPrintFrag.glsl", "BadPrintVert.glsl");
sphereDetail(60);
@@ -42,8 +41,8 @@ public void setup() {
public void draw() {
background(0);
if (enabled) {
if (enabled) {
shader(badPrint);
badPrint.set("Scale", scaleR, scaleG, scaleB);
@@ -51,7 +50,7 @@ public void draw() {
badPrint.set("Register", registerR, registerG, registerB);
badPrint.set("Size", sizeR, sizeG, sizeB);
} else {
resetShader(PShader.LIT);
resetShader();
}
noStroke();
@@ -66,6 +65,7 @@ public void draw() {
hint(DISABLE_DEPTH_TEST);
noLights();
resetShader();
controlP5.draw();
hint(ENABLE_DEPTH_TEST);
}
}

View File

@@ -6,27 +6,20 @@
PImage img;
PShader edges;
boolean customShader;
boolean enabled = true;
void setup() {
size(400, 400, P2D);
img = loadImage("berlin-1.jpg");
edges = loadShader(PShader.TEXTURED, "edges.glsl");
shader(edges);
customShader = true;
img = loadImage("berlin-1.jpg");
edges = loadShader("edges.glsl");
}
public void draw() {
void draw() {
if (enabled) shader(edges);
image(img, 0, 0, width, height);
}
public void mousePressed() {
if (customShader) {
resetShader(PShader.TEXTURED);
customShader = false;
} else {
shader(edges);
customShader = true;
}
}
void mousePressed() {
enabled = !enabled;
if (!enabled) resetShader();
}

View File

@@ -18,8 +18,7 @@ void setup() {
canvas = createGraphics(width, height, P2D);
canvas.noSmooth();
fxaa = loadShader(PShader.TEXTURED, "fxaa.glsl");
shader(fxaa);
fxaa = loadShader("fxaa.glsl");
usingShader = true;
canvas.beginDraw();
@@ -45,6 +44,7 @@ public void draw() {
canvas.endDraw();
}
if (usingShader) shader(fxaa);
image(canvas, 0, 0);
drawMessage();
@@ -52,11 +52,10 @@ public void draw() {
public void mousePressed() {
if (!drawing && width - msgLen < mouseX && height - 23 < mouseY) {
if (usingShader) {
resetShader(PShader.TEXTURED);
if (usingShader) {
usingShader = false;
resetShader();
} else {
shader(fxaa);
usingShader = true;
}
updateMessage();
@@ -79,14 +78,7 @@ void updateMessage() {
}
void drawMessage() {
if (usingShader) {
// We need the default texture shader to
// render text.
resetShader(PShader.TEXTURED);
}
resetShader();
fill(0);
text(message, width - msgLen, height - 5);
if (usingShader) {
shader(fxaa);
}
}
}

View File

@@ -8,9 +8,8 @@ void setup() {
size(400, 400, P3D);
canvas = createGraphics(400, 400, P3D);
fisheye = loadShader(PShader.TEXTURED, "FishEye.glsl");
fisheye.set("aperture", 180.0);
shader(fisheye);
fisheye = loadShader("FishEye.glsl");
fisheye.set("aperture", 180.0);
}
void draw() {
@@ -31,7 +30,6 @@ void draw() {
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.
shader(fisheye);
image(canvas, 0, 0, width, height);
}
}

View File

@@ -13,12 +13,12 @@ void setup() {
size(800, 800, P3D);
canvas = createGraphics(800, 800, P3D);
fisheye = loadShader(PShader.TEXTURED, "FishEye.glsl");
fisheye = loadShader("FishEye.glsl");
fisheye.set("aperture", 180.0);
shader(fisheye);
usingFishEye = true;
glossy = loadShader(PShader.LIT, "GlossyFrag.glsl", "GlossyVert.glsl");
glossy = loadShader("GlossyFrag.glsl", "GlossyVert.glsl");
glossy.set("AmbientColour", 0, 0, 0);
glossy.set("DiffuseColour", 0.9, 0.2, 0.2);
glossy.set("SpecularColour", 1.0, 1.0, 1.0);
@@ -27,15 +27,14 @@ void setup() {
glossy.set("SpecularIntensity", 0.7);
glossy.set("Roughness", 0.7);
glossy.set("Sharpness", 0.0);
canvas.shader(glossy);
ball = createShape(SPHERE, 50);
//ball.fill(200, 50, 50);
ball.noStroke();
}
void draw() {
canvas.beginDraw();
canvas.shader(glossy);
canvas.noStroke();
canvas.background(0);
canvas.pushMatrix();
@@ -53,7 +52,8 @@ void draw() {
}
}
canvas.endDraw();
if (usingFishEye) shader(fisheye);
image(canvas, 0, 0, width, height);
println(frameRate);
@@ -61,10 +61,9 @@ void draw() {
public void mousePressed() {
if (usingFishEye) {
resetShader(PShader.TEXTURED);
usingFishEye = false;
resetShader();
} else {
shader(fisheye);
usingFishEye = true;
}
}
}

View File

@@ -21,5 +21,4 @@ void draw() {
shader(maskShader);
image(srcImage, 0, 0, width, height);
resetShader();
}

View File

@@ -20,9 +20,9 @@ void setup() {
pg = (PGraphicsOpenGL)g;
// Get the default shader that Processing uses to
// render flat geometry (w/out textures and lights).
flatShader = getShader(PShader.FLAT);
// Loads a shader to render geometry w/out
// textures and lights.
flatShader = loadShader("frag.glsl", "vert.glsl");
vertices = new float[12];
vertData = allocateDirectFloatBuffer(12);
@@ -103,4 +103,4 @@ void updateGeometry() {
FloatBuffer allocateDirectFloatBuffer(int n) {
return ByteBuffer.allocateDirect(n * Float.SIZE/8).order(ByteOrder.nativeOrder()).asFloatBuffer();
}
}

View File

@@ -0,0 +1,30 @@
/*
Part of the Processing project - http://processing.org
Copyright (c) 2011-12 Ben Fry and Casey Reas
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif
varying vec4 vertColor;
void main() {
gl_FragColor = vertColor;
}

View File

@@ -0,0 +1,32 @@
/*
Part of the Processing project - http://processing.org
Copyright (c) 2011-12 Ben Fry and Casey Reas
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
uniform mat4 projmodelviewMatrix;
attribute vec4 inVertex;
attribute vec4 inColor;
varying vec4 vertColor;
void main() {
gl_Position = projmodelviewMatrix * inVertex;
vertColor = inColor;
}

View File

@@ -8,19 +8,17 @@ PGraphics pass1, pass2;
void setup() {
size(200, 200, P2D);
blur = loadShader(PShader.TEXTURED, "blur.glsl");
blur = loadShader("blur.glsl");
blur.set("blurSize", 9);
blur.set("sigma", 5.0f);
src = createGraphics(width, height, P2D);
pass1 = createGraphics(width, height, P2D);
pass1.noSmooth();
pass1.shader(blur);
pass1.noSmooth();
pass2 = createGraphics(width, height, P2D);
pass2.noSmooth();
pass2.shader(blur);
}
void draw() {
@@ -32,13 +30,15 @@ void draw() {
// Applying the blur shader along the vertical direction
blur.set("horizontalPass", 0);
pass1.beginDraw();
pass1.beginDraw();
pass1.shader(blur);
pass1.image(src, 0, 0);
pass1.endDraw();
// Applying the blur shader along the horizontal direction
blur.set("horizontalPass", 1);
pass2.beginDraw();
pass2.beginDraw();
pass2.shader(blur);
pass2.image(pass1, 0, 0);
pass2.endDraw();
@@ -59,4 +59,4 @@ void keyPressed() {
blur.set("blurSize", 5);
blur.set("sigma", 1.0);
}
}
}

View File

@@ -4,19 +4,19 @@
// http://www.lighthouse3d.com/tutorials/glsl-tutorial/toon-shader-version-ii/
PShader toon;
boolean customShader;
boolean enabled = true;
public void setup() {
void setup() {
size(400, 400, P3D);
noStroke();
fill(204);
toon = loadShader(PShader.LIT, "ToonFrag.glsl", "ToonVert.glsl");
shader(toon);
customShader = true;
toon = loadShader("ToonFrag.glsl", "ToonVert.glsl");
}
public void draw() {
void draw() {
if (enabled) shader(toon);
noStroke();
background(0);
float dirY = (mouseY / float(height) - 0.5) * 2;
@@ -26,16 +26,13 @@ public void draw() {
sphere(80);
}
public void mousePressed() {
void mousePressed() {
if (dist(mouseX, mouseY, width/2, height/2) < 80) {
if (customShader) {
resetShader(PShader.LIT);
customShader = false;
}
else {
shader(toon);
customShader = true;
if (enabled) {
enabled = false;
resetShader();
} else {
enabled = true;
}
}
}