Added shader examples to OpenGL Android

This commit is contained in:
codeanticode
2012-02-13 06:48:31 +00:00
parent e5e6dcac22
commit b8e9aa7ccf
8 changed files with 199 additions and 0 deletions
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package=""
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:label=""
android:icon="@drawable/icon"
android:debuggable="true">
<activity android:name="">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
@@ -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;
PGraphicsAndroid3D pg;
boolean usingShader;
void setup() {
size(400, 400, P3D);
img = loadImage("berlin-1.jpg");
pg = (PGraphicsAndroid3D)g;
shader = pg.loadShader("edges.glsl", FILL_SHADER_TEX);
pg.setShader(shader, FILL_SHADER_TEX);
usingShader = true;
}
void draw() {
image(img, 0, 0, width, height);
}
void mousePressed() {
if (usingShader) {
pg.resetShader(FILL_SHADER_TEX);
usingShader = false;
} else {
pg.setShader(shader, FILL_SHADER_TEX);
usingShader = true;
}
}
@@ -0,0 +1,40 @@
// Edge detection shader
precision mediump float;
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);
}
@@ -0,0 +1 @@
mode=Android
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package=""
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:label=""
android:icon="@drawable/icon"
android:debuggable="true">
<activity android:name="">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
@@ -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;
PGraphicsAndroid3D pg;
boolean usingShader;
public void setup() {
size(400, 400, P3D);
noStroke();
fill(204);
pg = (PGraphicsAndroid3D)g;
shader = pg.loadShader("ToonVert.glsl", "ToonFrag.glsl", FILL_SHADER_LIT);
pg.setShader(shader, FILL_SHADER_LIT);
usingShader = true;
}
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);
}
void keyPressed() {
if (usingShader) {
pg.resetShader(FILL_SHADER_LIT);
usingShader = false;
}
else {
pg.setShader(shader, FILL_SHADER_LIT);
usingShader = true;
}
}
@@ -0,0 +1,22 @@
precision mediump float;
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;
}
@@ -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];
}