From 2307724a5e6440c212f19c07bf97e102f3246b89 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Mon, 13 Feb 2012 06:09:00 +0000 Subject: [PATCH] Added shader examples --- .../Shaders/EdgeDetect/EdgeDetect.pde | 34 ++++++++++++++++ .../Shaders/EdgeDetect/data/edges.glsl | 38 ++++++++++++++++++ .../Shaders/ToonShading/ToonShading.pde | 39 +++++++++++++++++++ .../Shaders/ToonShading/data/ToonFrag.glsl | 20 ++++++++++ .../Shaders/ToonShading/data/ToonVert.glsl | 29 ++++++++++++++ .../opengl/examples/Transform/Bird/Bird.pde | 2 +- 6 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 java/libraries/opengl/examples/Shaders/EdgeDetect/EdgeDetect.pde create mode 100644 java/libraries/opengl/examples/Shaders/EdgeDetect/data/edges.glsl create mode 100644 java/libraries/opengl/examples/Shaders/ToonShading/ToonShading.pde create mode 100644 java/libraries/opengl/examples/Shaders/ToonShading/data/ToonFrag.glsl create mode 100644 java/libraries/opengl/examples/Shaders/ToonShading/data/ToonVert.glsl diff --git a/java/libraries/opengl/examples/Shaders/EdgeDetect/EdgeDetect.pde b/java/libraries/opengl/examples/Shaders/EdgeDetect/EdgeDetect.pde new file mode 100644 index 000000000..c78bce226 --- /dev/null +++ b/java/libraries/opengl/examples/Shaders/EdgeDetect/EdgeDetect.pde @@ -0,0 +1,34 @@ +// This example shows how to change the default fragment shader used +// in P3D to render textures, by a custom one that applies a simple +// edge detection filter. +// +// Press any key to switch between the custom and the default shader. + +PImage img; +PShader shader; +PGraphicsOpenGL pg; +boolean usingShader; + +void setup() { + size(400, 400, P3D); + img = loadImage("berlin-1.jpg"); + + pg = (PGraphicsOpenGL)g; + shader = pg.loadShader("edges.glsl", FILL_SHADER_TEX); + pg.setShader(shader, FILL_SHADER_TEX); + usingShader = true; +} + +public void draw() { + image(img, 0, 0, width, height); +} + +public void keyPressed() { + if (usingShader) { + pg.resetShader(FILL_SHADER_TEX); + usingShader = false; + } else { + pg.setShader(shader, FILL_SHADER_TEX); + usingShader = true; + } +} diff --git a/java/libraries/opengl/examples/Shaders/EdgeDetect/data/edges.glsl b/java/libraries/opengl/examples/Shaders/EdgeDetect/data/edges.glsl new file mode 100644 index 000000000..8aaf0864a --- /dev/null +++ b/java/libraries/opengl/examples/Shaders/EdgeDetect/data/edges.glsl @@ -0,0 +1,38 @@ +// Edge detection shader + +uniform sampler2D textureSampler; + +// The inverse of the texture dimensions along X and Y +uniform vec2 texcoordOffset; + +varying vec4 vertColor; +varying vec4 vertTexcoord; + +void main() { + vec4 sum = vec4(0); + + float kernel[9]; + kernel[0] = -1.0; kernel[1] = -1.0; kernel[2] = -1.0; + kernel[3] = -1.0; kernel[4] = +8.0; kernel[5] = -1.0; + kernel[6] = -1.0; kernel[7] = -1.0; kernel[8] = -1.0; + + vec2 offset[9]; + offset[0] = vec2(-texcoordOffset.s, -texcoordOffset.t); + offset[1] = vec2( 0.0, -texcoordOffset.t); + offset[2] = vec2(+texcoordOffset.s, -texcoordOffset.t); + + offset[3] = vec2(-texcoordOffset.s, 0.0); + offset[4] = vec2( 0.0, 0.0); + offset[5] = vec2(+texcoordOffset.s, 0.0); + + offset[6] = vec2(-texcoordOffset.s, +texcoordOffset.t); + offset[7] = vec2( 0.0, +texcoordOffset.t); + offset[8] = vec2(+texcoordOffset.s, +texcoordOffset.t); + + for (int i = 0; i < 9; i++) { + vec4 tmp = texture2D(textureSampler, vertTexcoord.st + offset[i]); + sum += tmp * kernel[i]; + } + + gl_FragColor = vec4(sum.rgb, 1.0); +} \ No newline at end of file diff --git a/java/libraries/opengl/examples/Shaders/ToonShading/ToonShading.pde b/java/libraries/opengl/examples/Shaders/ToonShading/ToonShading.pde new file mode 100644 index 000000000..275675153 --- /dev/null +++ b/java/libraries/opengl/examples/Shaders/ToonShading/ToonShading.pde @@ -0,0 +1,39 @@ +// Example showing the use of a custom lighting shader in order +// to apply a toon effect on the scene. + +PShader shader; +PGraphicsOpenGL pg; +boolean usingShader; + +public void setup() { + size(400, 400, P3D); + noStroke(); + fill(204); + + pg = (PGraphicsOpenGL)g; + shader = pg.loadShader("ToonVert.glsl", "ToonFrag.glsl", FILL_SHADER_LIT); + pg.setShader(shader, FILL_SHADER_LIT); + usingShader = true; +} + +public void draw() { + noStroke(); + background(0); + float dirY = (mouseY / float(height) - 0.5) * 2; + float dirX = (mouseX / float(width) - 0.5) * 2; + directionalLight(204, 204, 204, -dirX, -dirY, -1); + translate(width/2, height/2); + sphere(80); +} + +public void keyPressed() { + if (usingShader) { + pg.resetShader(FILL_SHADER_LIT); + usingShader = false; + } + else { + pg.setShader(shader, FILL_SHADER_LIT); + usingShader = true; + } +} + diff --git a/java/libraries/opengl/examples/Shaders/ToonShading/data/ToonFrag.glsl b/java/libraries/opengl/examples/Shaders/ToonShading/data/ToonFrag.glsl new file mode 100644 index 000000000..bb1c72971 --- /dev/null +++ b/java/libraries/opengl/examples/Shaders/ToonShading/data/ToonFrag.glsl @@ -0,0 +1,20 @@ +varying vec3 vertNormal; +varying vec3 vertLightDir; + +void main() { + float intensity; + vec4 color; + intensity = max(0.0, dot(vertLightDir, vertNormal)); + + if (intensity > 0.95) { + color = vec4(1.0, 0.5, 0.5, 1.0); + } else if (intensity > 0.5) { + color = vec4(0.6, 0.3, 0.3, 1.0); + } else if (intensity > 0.25) { + color = vec4(0.4, 0.2, 0.2, 1.0); + } else { + color = vec4(0.2, 0.1, 0.1, 1.0); + } + + gl_FragColor = color; +} \ No newline at end of file diff --git a/java/libraries/opengl/examples/Shaders/ToonShading/data/ToonVert.glsl b/java/libraries/opengl/examples/Shaders/ToonShading/data/ToonVert.glsl new file mode 100644 index 000000000..f314485aa --- /dev/null +++ b/java/libraries/opengl/examples/Shaders/ToonShading/data/ToonVert.glsl @@ -0,0 +1,29 @@ +// Toon shader using per-pixel lighting. Based on the glsl +// tutorial from lighthouse 3D: +// http://www.lighthouse3d.com/tutorials/glsl-tutorial/toon-shader-version-ii/ + +uniform mat4 modelviewMatrix; +uniform mat4 projmodelviewMatrix; +uniform mat3 normalMatrix; + +uniform vec3 lightNormal[8]; + +attribute vec4 inVertex; +attribute vec3 inNormal; + +varying vec3 vertNormal; +varying vec3 vertLightDir; + +void main() { + // Vertex in clip coordinates + gl_Position = projmodelviewMatrix * inVertex; + + // Normal vector in eye coordinates is passed + // to the fragment shader + vertNormal = normalize(normalMatrix * inNormal); + + // Assuming that there is only one directional light. + // Its normal vector is passed to the fragment shader + // in order to perform per-pixel lighting calculation. + vertLightDir = -lightNormal[0]; +} \ No newline at end of file diff --git a/java/libraries/opengl/examples/Transform/Bird/Bird.pde b/java/libraries/opengl/examples/Transform/Bird/Bird.pde index dacd405d1..125d510e3 100755 --- a/java/libraries/opengl/examples/Transform/Bird/Bird.pde +++ b/java/libraries/opengl/examples/Transform/Bird/Bird.pde @@ -11,7 +11,7 @@ float px = 0, py = 0, pz = 0; float flapSpeed = 0.2; void setup(){ - size(640, 360, P3D); + size(640, 360, P3D); noStroke(); }