Ported shader improvements to Android

This commit is contained in:
codeanticode
2012-02-13 06:47:44 +00:00
parent 2307724a5e
commit e5e6dcac22
5 changed files with 203 additions and 110 deletions
@@ -23,13 +23,11 @@ precision mediump float;
uniform sampler2D textureSampler;
uniform vec2 texcoordScale;
uniform vec2 texcoordOffset;
varying vec4 vertColor;
varying vec2 vertTexcoord;
varying vec4 vertTexcoord;
void main() {
vec2 uv = vertTexcoord * texcoordScale + texcoordOffset;
gl_FragColor = texture2D(textureSampler, uv) * vertColor;
gl_FragColor = texture2D(textureSampler, vertTexcoord.st) * vertColor;
}
@@ -22,6 +22,7 @@
uniform mat4 modelviewMatrix;
uniform mat4 projmodelviewMatrix;
uniform mat3 normalMatrix;
uniform mat4 texcoordMatrix;
uniform int lightCount;
uniform vec4 lightPosition[8];
@@ -43,7 +44,7 @@ attribute vec4 inEmissive;
attribute float inShine;
varying vec4 vertColor;
varying vec2 vertTexcoord;
varying vec4 vertTexcoord;
const float zero_float = 0.0;
const float one_float = 1.0;
@@ -129,6 +130,6 @@ void main() {
vec4(totalSpecular, 1) * inSpecular +
inEmissive;
// Passing texture coordinates to the fragment shader
vertTexcoord = inTexcoord;
// Calculating texture coordinates, with r and q set both to one
vertTexcoord = texcoordMatrix * vec4(inTexcoord, 1.0, 1.0);
}
@@ -20,17 +20,18 @@
*/
uniform mat4 projmodelviewMatrix;
uniform mat4 texcoordMatrix;
attribute vec4 inVertex;
attribute vec4 inColor;
attribute vec2 inTexcoord;
varying vec4 vertColor;
varying vec2 vertTexcoord;
varying vec4 vertTexcoord;
void main() {
gl_Position = projmodelviewMatrix * inVertex;
vertColor = inColor;
vertTexcoord = inTexcoord;
vertTexcoord = texcoordMatrix * vec4(inTexcoord, 1.0, 1.0);
}
@@ -32,7 +32,6 @@ import java.util.Set;
import java.util.HashSet;
import java.util.Stack;
// drawPixels is missing...calls to glDrawPixels are commented out
// setRasterPos() is also commented out
@@ -5564,9 +5563,37 @@ public class PGraphicsAndroid3D extends PGraphics {
return null;
}
}
public PShader loadShader(String fragFilename, int kind) {
PShader shader;
if (kind == FILL_SHADER_SIMPLE) {
shader = new FillShaderSimple(parent);
shader.setVertexShader(defFillShaderVertSimpleURL);
} else if (kind == FILL_SHADER_LIT) {
shader = new FillShaderLit(parent);
shader.setVertexShader(defFillShaderVertLitURL);
} else if (kind == FILL_SHADER_TEX) {
shader = new FillShaderTex(parent);
shader.setVertexShader(defFillShaderVertTexURL);
} else if (kind == FILL_SHADER_FULL) {
shader = new FillShaderFull(parent);
shader.setVertexShader(defFillShaderVertFullURL);
} else if (kind == LINE_SHADER) {
shader = new LineShader(parent);
shader.setVertexShader(defLineShaderVertURL);
} else if (kind == POINT_SHADER) {
shader = new PointShader(parent);
shader.setVertexShader(defPointShaderVertURL);
} else {
PGraphics.showWarning("Wrong shader type");
return null;
}
shader.setFragmentShader(fragFilename);
return shader;
}
public void setShader(PShader shader, int kind) {
if (kind == FILL_SHADER_SIMPLE) {
if (kind == FILL_SHADER_SIMPLE) {
fillShaderSimple = (FillShaderSimple) shader;
} else if (kind == FILL_SHADER_LIT) {
fillShaderLit = (FillShaderLit) shader;
@@ -5619,42 +5646,42 @@ public class PGraphicsAndroid3D extends PGraphics {
}
}
protected FillShader getFillShader(boolean lit, boolean tex) {
protected FillShader getFillShader(boolean lit, boolean tex) {
FillShader shader;
if (lit) {
if (tex) {
if (defFillShaderFull == null) {
defFillShaderFull = new FillShaderFull(parent, defFillShaderVertFullURL, defFillShaderFragTexURL);
}
if (fillShaderFull == null) {
if (defFillShaderFull == null) {
defFillShaderFull = new FillShaderFull(parent, defFillShaderVertFullURL, defFillShaderFragTexURL);
}
fillShaderFull = defFillShaderFull;
}
shader = fillShaderFull;
} else {
if (defFillShaderLit == null) {
if (defFillShaderLit == null) {
defFillShaderLit = new FillShaderLit(parent, defFillShaderVertLitURL, defFillShaderFragNoTexURL);
}
defFillShaderLit = new FillShaderLit(parent, defFillShaderVertLitURL, defFillShaderFragNoTexURL);
}
if (fillShaderLit == null) {
fillShaderLit = defFillShaderLit;
}
shader = fillShaderLit;
}
} else {
if (tex) {
if (defFillShaderTex == null) {
defFillShaderTex = new FillShaderTex(parent, defFillShaderVertTexURL, defFillShaderFragTexURL);
}
if (fillShaderTex == null) {
if (defFillShaderTex == null) {
defFillShaderTex = new FillShaderTex(parent, defFillShaderVertTexURL, defFillShaderFragTexURL);
}
fillShaderTex = defFillShaderTex;
}
shader = fillShaderTex;
} else {
if (defFillShaderSimple == null) {
defFillShaderSimple = new FillShaderSimple(parent, defFillShaderVertSimpleURL, defFillShaderFragNoTexURL);
}
if (fillShaderSimple == null) {
if (defFillShaderSimple == null) {
defFillShaderSimple = new FillShaderSimple(parent, defFillShaderVertSimpleURL, defFillShaderFragNoTexURL);
}
fillShaderSimple = defFillShaderSimple;
}
}
shader = fillShaderSimple;
}
}
@@ -5664,11 +5691,11 @@ public class PGraphicsAndroid3D extends PGraphics {
}
protected LineShader getLineShader() {
if (defLineShader == null) {
defLineShader = new LineShader(parent, defLineShaderVertURL, defLineShaderFragURL);
}
if (lineShader == null) {
if (defLineShader == null) {
defLineShader = new LineShader(parent, defLineShaderVertURL, defLineShaderFragURL);
}
lineShader = defLineShader;
lineShader = defLineShader;
}
lineShader.loadAttributes();
lineShader.loadUniforms();
@@ -5676,18 +5703,22 @@ public class PGraphicsAndroid3D extends PGraphics {
}
protected PointShader getPointShader() {
if (pointShader == null) {
if (defPointShader == null) {
defPointShader = new PointShader(parent, defPointShaderVertURL, defPointShaderFragURL);
}
pointShader = defPointShader;
}
if (defPointShader == null) {
defPointShader = new PointShader(parent, defPointShaderVertURL, defPointShaderFragURL);
}
if (pointShader == null) {
pointShader = defPointShader;
}
pointShader.loadAttributes();
pointShader.loadUniforms();
return pointShader;
}
protected class FillShader extends PShader {
public FillShader(PApplet parent) {
super(parent);
}
public FillShader(PApplet parent, String vertFilename, String fragFilename) {
super(parent, vertFilename, fragFilename);
}
@@ -5699,9 +5730,11 @@ public class PGraphicsAndroid3D extends PGraphics {
public void loadAttributes() { }
public void loadUniforms() { }
public void setAttribute(int loc, int vboId, int size, int type, boolean normalized, int stride, int offset) {
pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, vboId);
pgl.glVertexAttribPointer(loc, size, type, normalized, stride, offset);
public void setAttribute(int loc, int vboId, int size, int type, boolean normalized, int stride, int offset) {
if (-1 < loc) {
pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, vboId);
pgl.glVertexAttribPointer(loc, size, type, normalized, stride, offset);
}
}
public void setVertexAttribute(int vboId, int size, int type, int stride, int offset) { }
@@ -5720,6 +5753,10 @@ public class PGraphicsAndroid3D extends PGraphics {
protected int inVertexLoc;
protected int inColorLoc;
public FillShaderSimple(PApplet parent) {
super(parent);
}
public FillShaderSimple(PApplet parent, String vertFilename, String fragFilename) {
super(parent, vertFilename, fragFilename);
@@ -5749,16 +5786,16 @@ public class PGraphicsAndroid3D extends PGraphics {
public void start() {
super.start();
pgl.glEnableVertexAttribArray(inVertexLoc);
pgl.glEnableVertexAttribArray(inColorLoc);
if (-1 < inVertexLoc) pgl.glEnableVertexAttribArray(inVertexLoc);
if (-1 < inColorLoc) pgl.glEnableVertexAttribArray(inColorLoc);
updateGLProjmodelview();
set4x4MatUniform(projmodelviewMatrixLoc, glProjmodelview);
}
public void stop() {
pgl.glDisableVertexAttribArray(inVertexLoc);
pgl.glDisableVertexAttribArray(inColorLoc);
if (-1 < inVertexLoc) pgl.glDisableVertexAttribArray(inVertexLoc);
if (-1 < inColorLoc) pgl.glDisableVertexAttribArray(inColorLoc);
pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, 0);
@@ -5789,6 +5826,10 @@ public class PGraphicsAndroid3D extends PGraphics {
protected int inEmissiveLoc;
protected int inShineLoc;
public FillShaderLit(PApplet parent) {
super(parent);
}
public FillShaderLit(PApplet parent, String vertFilename, String fragFilename) {
super(parent, vertFilename, fragFilename);
}
@@ -5799,7 +5840,7 @@ public class PGraphicsAndroid3D extends PGraphics {
public void loadAttributes() {
inVertexLoc = getAttribLocation("inVertex");
inColorLoc = getAttribLocation("inColor");
inColorLoc = getAttribLocation("inColor");
inNormalLoc = getAttribLocation("inNormal");
inAmbientLoc = getAttribLocation("inAmbient");
@@ -5854,14 +5895,14 @@ public class PGraphicsAndroid3D extends PGraphics {
public void start() {
super.start();
pgl.glEnableVertexAttribArray(inVertexLoc);
pgl.glEnableVertexAttribArray(inColorLoc);
pgl.glEnableVertexAttribArray(inNormalLoc);
if (-1 < inVertexLoc) pgl.glEnableVertexAttribArray(inVertexLoc);
if (-1 < inColorLoc) pgl.glEnableVertexAttribArray(inColorLoc);
if (-1 < inNormalLoc) pgl.glEnableVertexAttribArray(inNormalLoc);
pgl.glEnableVertexAttribArray(inAmbientLoc);
pgl.glEnableVertexAttribArray(inSpecularLoc);
pgl.glEnableVertexAttribArray(inEmissiveLoc);
pgl.glEnableVertexAttribArray(inShineLoc);
if (-1 < inAmbientLoc) pgl.glEnableVertexAttribArray(inAmbientLoc);
if (-1 < inSpecularLoc) pgl.glEnableVertexAttribArray(inSpecularLoc);
if (-1 < inEmissiveLoc) pgl.glEnableVertexAttribArray(inEmissiveLoc);
if (-1 < inShineLoc) pgl.glEnableVertexAttribArray(inShineLoc);
updateGLProjmodelview();
set4x4MatUniform(projmodelviewMatrixLoc, glProjmodelview);
@@ -5882,15 +5923,15 @@ public class PGraphicsAndroid3D extends PGraphics {
set2FloatVecUniform(lightSpotParametersLoc, lightSpotParameters);
}
public void stop() {
pgl.glDisableVertexAttribArray(inVertexLoc);
pgl.glDisableVertexAttribArray(inColorLoc);
pgl.glDisableVertexAttribArray(inNormalLoc);
public void stop() {
if (-1 < inVertexLoc) pgl.glDisableVertexAttribArray(inVertexLoc);
if (-1 < inColorLoc) pgl.glDisableVertexAttribArray(inColorLoc);
if (-1 < inNormalLoc) pgl.glDisableVertexAttribArray(inNormalLoc);
pgl.glDisableVertexAttribArray(inAmbientLoc);
pgl.glDisableVertexAttribArray(inSpecularLoc);
pgl.glDisableVertexAttribArray(inEmissiveLoc);
pgl.glDisableVertexAttribArray(inShineLoc);
if (-1 < inAmbientLoc) pgl.glDisableVertexAttribArray(inAmbientLoc);
if (-1 < inSpecularLoc) pgl.glDisableVertexAttribArray(inSpecularLoc);
if (-1 < inEmissiveLoc) pgl.glDisableVertexAttribArray(inEmissiveLoc);
if (-1 < inShineLoc) pgl.glDisableVertexAttribArray(inShineLoc);
pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, 0);
@@ -5899,15 +5940,19 @@ public class PGraphicsAndroid3D extends PGraphics {
}
protected class FillShaderTex extends FillShaderSimple {
protected int texcoordScaleLoc;
protected int texcoordOffsetLoc;
protected int inTexcoordLoc;
PTexture texture;
protected int texcoordMatrixLoc;
protected int texcoordOffsetLoc;
protected float[] tcmat;
public FillShaderTex(PApplet parent) {
super(parent);
}
public FillShaderTex(PApplet parent, String vertFilename, String fragFilename) {
super(parent, vertFilename, fragFilename);
super(parent, vertFilename, fragFilename);
}
public FillShaderTex(PApplet parent, URL vertURL, URL fragURL) {
@@ -5917,7 +5962,7 @@ public class PGraphicsAndroid3D extends PGraphics {
public void loadUniforms() {
super.loadUniforms();
texcoordScaleLoc = getUniformLocation("texcoordScale");
texcoordMatrixLoc = getUniformLocation("texcoordMatrix");
texcoordOffsetLoc = getUniformLocation("texcoordOffset");
}
@@ -5932,33 +5977,37 @@ public class PGraphicsAndroid3D extends PGraphics {
}
public void setTexture(PTexture tex) {
texture = tex;
// Calculate scale and offset for texture coordinates
// and passing them to the fragment shader.
float scaleu = tex.maxTexCoordU;
float scalev = tex.maxTexCoordV;
float offsetu = 0;
float offsetv = 0;
float dispu = 0;
float dispv = 0;
if (tex.isFlippedY()) {
scalev *= -1;
offsetv = 1;
dispv = 1;
}
if (tcmat == null) {
tcmat = new float[16];
}
set2FloatUniform(texcoordScaleLoc, scaleu, scalev);
set2FloatUniform(texcoordOffsetLoc, offsetu, offsetv);
tcmat[0] = scaleu; tcmat[4] = 0; tcmat[ 8] = 0; tcmat[12] = dispu;
tcmat[1] = 0; tcmat[5] = scalev; tcmat[ 9] = 0; tcmat[13] = dispv;
tcmat[2] = 0; tcmat[6] = 0; tcmat[10] = 0; tcmat[14] = 0;
tcmat[3] = 0; tcmat[7] = 0; tcmat[11] = 0; tcmat[15] = 0;
set4x4MatUniform(texcoordMatrixLoc, tcmat);
set2FloatUniform(texcoordOffsetLoc, 1.0f / tex.width, 1.0f / tex.height);
}
public void start() {
super.start();
pgl.glEnableVertexAttribArray(inTexcoordLoc);
if (-1 < inTexcoordLoc) pgl.glEnableVertexAttribArray(inTexcoordLoc);
}
public void stop() {
pgl.glDisableVertexAttribArray(inTexcoordLoc);
if (-1 < inTexcoordLoc) pgl.glDisableVertexAttribArray(inTexcoordLoc);
super.stop();
}
@@ -5967,9 +6016,15 @@ public class PGraphicsAndroid3D extends PGraphics {
protected class FillShaderFull extends FillShaderLit {
protected int inTexcoordLoc;
protected int texcoordScaleLoc;
protected int texcoordMatrixLoc;
protected int texcoordOffsetLoc;
protected float[] tcmat;
public FillShaderFull(PApplet parent) {
super(parent);
}
public FillShaderFull(PApplet parent, String vertFilename, String fragFilename) {
super(parent, vertFilename, fragFilename);
}
@@ -5981,7 +6036,7 @@ public class PGraphicsAndroid3D extends PGraphics {
public void loadUniforms() {
super.loadUniforms();
texcoordScaleLoc = getUniformLocation("texcoordScale");
texcoordMatrixLoc = getUniformLocation("texcoordMatrix");
texcoordOffsetLoc = getUniformLocation("texcoordOffset");
}
@@ -5996,33 +6051,37 @@ public class PGraphicsAndroid3D extends PGraphics {
}
public void setTexture(PTexture tex) {
texture = tex;
// Calculate scale and offset for texture coordinates
// and passing them to the fragment shader.
float scaleu = tex.maxTexCoordU;
float scalev = tex.maxTexCoordV;
float offsetu = 0;
float offsetv = 0;
float dispu = 0;
float dispv = 0;
if (tex.isFlippedY()) {
scalev *= -1;
offsetv = 1;
dispv = 1;
}
if (tcmat == null) {
tcmat = new float[16];
}
set2FloatUniform(texcoordScaleLoc, scaleu, scalev);
set2FloatUniform(texcoordOffsetLoc, offsetu, offsetv);
tcmat[0] = scaleu; tcmat[4] = 0; tcmat[ 8] = 0; tcmat[12] = dispu;
tcmat[1] = 0; tcmat[5] = scalev; tcmat[ 9] = 0; tcmat[13] = dispv;
tcmat[2] = 0; tcmat[6] = 0; tcmat[10] = 0; tcmat[14] = 0;
tcmat[3] = 0; tcmat[7] = 0; tcmat[11] = 0; tcmat[15] = 0;
set4x4MatUniform(texcoordMatrixLoc, tcmat);
set2FloatUniform(texcoordOffsetLoc, 1.0f / tex.width, 1.0f / tex.height);
}
public void start() {
super.start();
pgl.glEnableVertexAttribArray(inTexcoordLoc);
if (-1 < inTexcoordLoc) pgl.glEnableVertexAttribArray(inTexcoordLoc);
}
public void stop() {
pgl.glDisableVertexAttribArray(inTexcoordLoc);
if (-1 < inTexcoordLoc) pgl.glDisableVertexAttribArray(inTexcoordLoc);
super.stop();
}
@@ -6039,6 +6098,10 @@ public class PGraphicsAndroid3D extends PGraphics {
protected int inColorLoc;
protected int inDirWidthLoc;
public LineShader(PApplet parent) {
super(parent);
}
public LineShader(PApplet parent, String vertFilename, String fragFilename) {
super(parent, vertFilename, fragFilename);
}
@@ -6081,9 +6144,9 @@ public class PGraphicsAndroid3D extends PGraphics {
public void start() {
super.start();
pgl.glEnableVertexAttribArray(inVertexLoc);
pgl.glEnableVertexAttribArray(inColorLoc);
pgl.glEnableVertexAttribArray(inDirWidthLoc);
if (-1 < inVertexLoc) pgl.glEnableVertexAttribArray(inVertexLoc);
if (-1 < inColorLoc) pgl.glEnableVertexAttribArray(inColorLoc);
if (-1 < inDirWidthLoc) pgl.glEnableVertexAttribArray(inDirWidthLoc);
updateGLProjection();
set4x4MatUniform(projectionMatrixLoc, glProjection);
@@ -6101,9 +6164,9 @@ public class PGraphicsAndroid3D extends PGraphics {
}
public void stop() {
pgl.glDisableVertexAttribArray(inVertexLoc);
pgl.glDisableVertexAttribArray(inColorLoc);
pgl.glDisableVertexAttribArray(inDirWidthLoc);
if (-1 < inVertexLoc) pgl.glDisableVertexAttribArray(inVertexLoc);
if (-1 < inColorLoc) pgl.glDisableVertexAttribArray(inColorLoc);
if (-1 < inDirWidthLoc) pgl.glDisableVertexAttribArray(inDirWidthLoc);
pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, 0);
@@ -6118,6 +6181,10 @@ public class PGraphicsAndroid3D extends PGraphics {
protected int inVertexLoc;
protected int inColorLoc;
protected int inSizeLoc;
public PointShader(PApplet parent) {
super(parent);
}
public PointShader(PApplet parent, String vertFilename, String fragFilename) {
super(parent, vertFilename, fragFilename);
@@ -6158,9 +6225,9 @@ public class PGraphicsAndroid3D extends PGraphics {
public void start() {
super.start();
pgl.glEnableVertexAttribArray(inVertexLoc);
pgl.glEnableVertexAttribArray(inColorLoc);
pgl.glEnableVertexAttribArray(inSizeLoc);
if (-1 < inVertexLoc) pgl.glEnableVertexAttribArray(inVertexLoc);
if (-1 < inColorLoc) pgl.glEnableVertexAttribArray(inColorLoc);
if (-1 < inSizeLoc) pgl.glEnableVertexAttribArray(inSizeLoc);
updateGLProjection();
set4x4MatUniform(projectionMatrixLoc, glProjection);
@@ -6170,16 +6237,16 @@ public class PGraphicsAndroid3D extends PGraphics {
}
public void stop() {
pgl.glDisableVertexAttribArray(inVertexLoc);
pgl.glDisableVertexAttribArray(inColorLoc);
pgl.glDisableVertexAttribArray(inSizeLoc);
if (-1 < inVertexLoc) pgl.glDisableVertexAttribArray(inVertexLoc);
if (-1 < inColorLoc) pgl.glDisableVertexAttribArray(inColorLoc);
if (-1 < inSizeLoc) pgl.glDisableVertexAttribArray(inSizeLoc);
pgl.glBindBuffer(PGL.GL_ARRAY_BUFFER, 0);
super.stop();
}
}
//////////////////////////////////////////////////////////////
// Input (raw) and Tessellated geometry, tessellator.
+33 -7
View File
@@ -61,6 +61,13 @@ public class PShader {
fragmentShader = 0;
}
public PShader(PApplet parent) {
this();
this.parent = parent;
pg = (PGraphicsAndroid3D) parent.g;
pgl = pg.pgl;
}
/**
* Creates a shader program using the specified vertex and fragment
* shaders.
@@ -115,6 +122,21 @@ public class PShader {
}
}
public void setVertexShader(String vertFilename) {
this.vertexFilename = vertFilename;
}
public void setVertexShader(URL vertURL) {
this.vertexURL = vertURL;
}
public void setFragmentShader(String fragFilename) {
this.fragmentFilename = fragFilename;
}
public void setFragmentShader(URL fragURL) {
this.fragmentURL = fragURL;
}
/**
* Starts the execution of the shader program.
@@ -267,20 +289,25 @@ public class PShader {
pgl.glVertexAttrib4f(loc, x, y, z, w);
}
}
protected void init() {
if (programObject == 0) {
programObject = pg.createGLSLProgramObject();
if (vertexFilename != null && fragmentFilename != null) {
if (vertexFilename != null) {
loadVertexShader(vertexFilename);
loadFragmentShader(fragmentFilename);
} else if (vertexURL != null && fragmentURL != null) {
} else if (vertexURL != null) {
loadVertexShader(vertexURL);
loadFragmentShader(fragmentURL);
} else {
PGraphics.showException("Shader filenames and URLs are both null!");
PGraphics.showException("Vertex shader filenames and URLs are both null!");
}
if (fragmentFilename != null) {
loadFragmentShader(fragmentFilename);
} else if (fragmentURL != null) {
loadFragmentShader(fragmentURL);
} else {
PGraphics.showException("Fragment shader filenames and URLs are both null!");
}
checkLogInfo("Vertex shader " + vertexFilename + " compilation: ", vertexShader);
@@ -394,4 +421,3 @@ public class PShader {
}
}
}