From 6cf649a0ce78fbd6e511ee6e8fd390543e869cd5 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Wed, 4 Sep 2013 19:04:43 -0400 Subject: [PATCH] cleanup inner shader classes, and minor tweaks --- .../processing/opengl/PGraphicsOpenGL.java | 194 +++++------------- core/src/processing/opengl/PShapeOpenGL.java | 6 +- 2 files changed, 57 insertions(+), 143 deletions(-) diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index 9d5a2635a..dc63067fc 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -80,8 +80,8 @@ public class PGraphicsOpenGL extends PGraphics { "Unsupported shape format"; static final String INVALID_FILTER_SHADER_ERROR = "Your shader needs to be of TEXTURE type to be used as a filter"; - static final String INVALID_PROCESSING_SHADER_ERROR = - "The GLSL code doesn't seem to contain a valid shader to use in Processing"; + static final String INCONSISTENT_SHADER_TYPES = + "The vertex and fragment shaders have different types"; static final String WRONG_SHADER_TYPE_ERROR = "shader() called with a wrong shader"; static final String UNKNOWN_SHADER_KIND_ERROR = @@ -2400,16 +2400,15 @@ public class PGraphicsOpenGL extends PGraphics { 4 * voffset * PGL.SIZEOF_BYTE); shader.setShininessAttribute(glPolyShininess, 1, PGL.FLOAT, 0, voffset * PGL.SIZEOF_FLOAT); - } - - if (tex != null) { + } else { shader.setNormalAttribute(glPolyNormal, 3, PGL.FLOAT, 0, 3 * voffset * PGL.SIZEOF_FLOAT); shader.setTexcoordAttribute(glPolyTexcoord, 2, PGL.FLOAT, 0, 2 * voffset * PGL.SIZEOF_FLOAT); - shader.setTexture(tex); + if (tex != null) shader.setTexture(tex); } + pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, glPolyIndex); pgl.drawElements(PGL.TRIANGLES, icount, PGL.INDEX_TYPE, ioffset * PGL.SIZEOF_INDEX); @@ -6255,10 +6254,7 @@ public class PGraphicsOpenGL extends PGraphics { @Override public PShader loadShader(String fragFilename) { int shaderType = getShaderType(fragFilename); - if (shaderType == -1) { - PGraphics.showWarning(INVALID_PROCESSING_SHADER_ERROR); - return null; - } + if (shaderType == -1) shaderType = PShader.COLOR; PShader shader = null; if (shaderType == PShader.POINT) { shader = new PointShader(parent); @@ -6286,12 +6282,20 @@ public class PGraphicsOpenGL extends PGraphics { @Override public PShader loadShader(String fragFilename, String vertFilename) { - int shaderType = getShaderType(vertFilename); - if (shaderType == -1) { - shaderType = getShaderType(fragFilename); - } - if (shaderType == -1) { - PGraphics.showWarning(INVALID_PROCESSING_SHADER_ERROR); + int vertType = getShaderType(vertFilename); + int fragType = getShaderType(fragFilename); + + int shaderType = -1; + if (vertType == -1 && fragType == -1) { + shaderType = PShader.COLOR; + } else if (vertType == -1) { + shaderType = fragType; + } else if (fragType == -1) { + shaderType = vertType; + } else if (fragType == vertType) { + shaderType = vertType; + } else { + PGraphics.showWarning(INCONSISTENT_SHADER_TYPES); return null; } @@ -6349,14 +6353,14 @@ public class PGraphicsOpenGL extends PGraphics { flush(); // Flushing geometry drawn with a different shader. if (kind == TRIANGLES || kind == QUADS || kind == POLYGON) { - if (shader instanceof TextureShader) { - textureShader = (TextureShader) shader; - } else if (shader instanceof ColorShader) { - colorShader = (ColorShader) shader; - } else if (shader instanceof TexlightShader) { + if (shader instanceof TexlightShader) { texlightShader = (TexlightShader) shader; + } else if (shader instanceof TextureShader) { + textureShader = (TextureShader) shader; } else if (shader instanceof LightShader) { lightShader = (LightShader) shader; + } else if (shader instanceof ColorShader) { + colorShader = (ColorShader) shader; } else { PGraphics.showWarning(WRONG_SHADER_TYPE_ERROR); } @@ -6684,6 +6688,9 @@ public class PGraphicsOpenGL extends PGraphics { protected class ColorShader extends BaseShader { protected int vertexLoc; protected int colorLoc; + protected int normalLoc; + protected int texCoordLoc; + protected int normalMatrixLoc; public ColorShader(PApplet parent) { super(parent); @@ -6702,11 +6709,15 @@ public class PGraphicsOpenGL extends PGraphics { public void loadAttributes() { vertexLoc = getAttributeLoc("vertex"); colorLoc = getAttributeLoc("color"); + texCoordLoc = getAttributeLoc("texCoord"); + normalLoc = getAttributeLoc("normal"); } @Override public void loadUniforms() { super.loadUniforms(); + + normalMatrixLoc = getUniformLoc("normalMatrix"); } @Override @@ -6721,6 +6732,18 @@ public class PGraphicsOpenGL extends PGraphics { setAttributeVBO(colorLoc, vboId, size, type, true, stride, offset); } + @Override + public void setNormalAttribute(int vboId, int size, int type, + int stride, int offset) { + setAttributeVBO(normalLoc, vboId, size, type, false, stride, offset); + } + + @Override + public void setTexcoordAttribute(int vboId, int size, int type, + int stride, int offset) { + setAttributeVBO(texCoordLoc, vboId, size, type, false, stride, offset); + } + @Override public void bind() { super.bind(); @@ -6729,26 +6752,32 @@ public class PGraphicsOpenGL extends PGraphics { loadAttributes(); loadUniforms(); } + setCommonUniforms(); if (-1 < vertexLoc) pgl.enableVertexAttribArray(vertexLoc); if (-1 < colorLoc) pgl.enableVertexAttribArray(colorLoc); + if (-1 < texCoordLoc) pgl.enableVertexAttribArray(texCoordLoc); + if (-1 < normalLoc) pgl.enableVertexAttribArray(normalLoc); - setCommonUniforms(); + if (-1 < normalMatrixLoc) { + pgCurrent.updateGLNormal(); + setUniformMatrix(normalMatrixLoc, pgCurrent.glNormal); + } } @Override public void unbind() { if (-1 < vertexLoc) pgl.disableVertexAttribArray(vertexLoc); if (-1 < colorLoc) pgl.disableVertexAttribArray(colorLoc); + if (-1 < texCoordLoc) pgl.disableVertexAttribArray(texCoordLoc); + if (-1 < normalLoc) pgl.disableVertexAttribArray(normalLoc); super.unbind(); } } - protected class LightShader extends BaseShader { - protected int normalMatrixLoc; - + protected class LightShader extends ColorShader { protected int lightCountLoc; protected int lightPositionLoc; protected int lightNormalLoc; @@ -6758,10 +6787,6 @@ public class PGraphicsOpenGL extends PGraphics { protected int lightFalloffLoc; protected int lightSpotLoc; - protected int vertexLoc; - protected int colorLoc; - protected int normalLoc; - protected int ambientLoc; protected int specularLoc; protected int emissiveLoc; @@ -6782,10 +6807,7 @@ public class PGraphicsOpenGL extends PGraphics { @Override public void loadAttributes() { - vertexLoc = getAttributeLoc("vertex"); - colorLoc = getAttributeLoc("color"); - normalLoc = getAttributeLoc("normal"); - + super.loadAttributes(); ambientLoc = getAttributeLoc("ambient"); specularLoc = getAttributeLoc("specular"); emissiveLoc = getAttributeLoc("emissive"); @@ -6796,8 +6818,6 @@ public class PGraphicsOpenGL extends PGraphics { public void loadUniforms() { super.loadUniforms(); - normalMatrixLoc = getUniformLoc("normalMatrix"); - lightCountLoc = getUniformLoc("lightCount"); lightPositionLoc = getUniformLoc("lightPosition"); lightNormalLoc = getUniformLoc("lightNormal"); @@ -6808,24 +6828,6 @@ public class PGraphicsOpenGL extends PGraphics { lightSpotLoc = getUniformLoc("lightSpot"); } - @Override - public void setVertexAttribute(int vboId, int size, int type, - int stride, int offset) { - setAttributeVBO(vertexLoc, vboId, size, type, false, stride, offset); - } - - @Override - public void setColorAttribute(int vboId, int size, int type, - int stride, int offset) { - setAttributeVBO(colorLoc, vboId, size, type, true, stride, offset); - } - - @Override - public void setNormalAttribute(int vboId, int size, int type, - int stride, int offset) { - setAttributeVBO(normalLoc, vboId, size, type, false, stride, offset); - } - @Override public void setAmbientAttribute(int vboId, int size, int type, int stride, int offset) { @@ -6853,26 +6855,12 @@ public class PGraphicsOpenGL extends PGraphics { @Override public void bind() { super.bind(); - if (pgCurrent == null) { - setRenderer(PGraphicsOpenGL.pgCurrent); - loadAttributes(); - loadUniforms(); - } - - if (-1 < vertexLoc) pgl.enableVertexAttribArray(vertexLoc); - if (-1 < colorLoc) pgl.enableVertexAttribArray(colorLoc); - if (-1 < normalLoc) pgl.enableVertexAttribArray(normalLoc); if (-1 < ambientLoc) pgl.enableVertexAttribArray(ambientLoc); if (-1 < specularLoc) pgl.enableVertexAttribArray(specularLoc); if (-1 < emissiveLoc) pgl.enableVertexAttribArray(emissiveLoc); if (-1 < shininessLoc) pgl.enableVertexAttribArray(shininessLoc); - if (-1 < normalMatrixLoc) { - pgCurrent.updateGLNormal(); - setUniformMatrix(normalMatrixLoc, pgCurrent.glNormal); - } - int count = pgCurrent.lightCount; setUniformValue(lightCountLoc, count); setUniformVector(lightPositionLoc, pgCurrent.lightPosition, 4, count); @@ -6883,16 +6871,10 @@ public class PGraphicsOpenGL extends PGraphics { setUniformVector(lightFalloffLoc, pgCurrent.lightFalloffCoefficients, 3, count); setUniformVector(lightSpotLoc, pgCurrent.lightSpotParameters, 2, count); - - setCommonUniforms(); } @Override public void unbind() { - if (-1 < vertexLoc) pgl.disableVertexAttribArray(vertexLoc); - if (-1 < colorLoc) pgl.disableVertexAttribArray(colorLoc); - if (-1 < normalLoc) pgl.disableVertexAttribArray(normalLoc); - if (-1 < ambientLoc) pgl.disableVertexAttribArray(ambientLoc); if (-1 < specularLoc) pgl.disableVertexAttribArray(specularLoc); if (-1 < emissiveLoc) pgl.disableVertexAttribArray(emissiveLoc); @@ -6906,15 +6888,11 @@ public class PGraphicsOpenGL extends PGraphics { protected class TextureShader extends ColorShader { protected Texture texture; protected int texUnit; - protected int texCoordLoc; protected int textureLoc; protected int texMatrixLoc; protected int texOffsetLoc; - protected int normalMatrixLoc; - protected int normalLoc; - protected float[] tcmat; public TextureShader(PApplet parent) { @@ -6937,29 +6915,6 @@ public class PGraphicsOpenGL extends PGraphics { textureLoc = getUniformLoc("texture"); texMatrixLoc = getUniformLoc("texMatrix"); texOffsetLoc = getUniformLoc("texOffset"); - - normalMatrixLoc = getUniformLoc("normalMatrix"); - } - - @Override - public void loadAttributes() { - super.loadAttributes(); - - texCoordLoc = getAttributeLoc("texCoord"); - - normalLoc = getAttributeLoc("normal"); - } - - @Override - public void setNormalAttribute(int vboId, int size, int type, - int stride, int offset) { - setAttributeVBO(normalLoc, vboId, size, type, false, stride, offset); - } - - @Override - public void setTexcoordAttribute(int vboId, int size, int type, - int stride, int offset) { - setAttributeVBO(texCoordLoc, vboId, size, type, false, stride, offset); } @Override @@ -7011,24 +6966,8 @@ public class PGraphicsOpenGL extends PGraphics { } } - @Override - public void bind() { - super.bind(); - - if (-1 < texCoordLoc) pgl.enableVertexAttribArray(texCoordLoc); - - if (-1 < normalLoc) pgl.enableVertexAttribArray(normalLoc); - if (-1 < normalMatrixLoc) { - pgCurrent.updateGLNormal(); - setUniformMatrix(normalMatrixLoc, pgCurrent.glNormal); - } - } - @Override public void unbind() { - if (-1 < texCoordLoc) pgl.disableVertexAttribArray(texCoordLoc); - if (-1 < normalLoc) pgl.disableVertexAttribArray(normalLoc); - if (-1 < textureLoc && texture != null) { pgl.activeTexture(PGL.TEXTURE0 + texUnit); texture.unbind(); @@ -7044,7 +6983,6 @@ public class PGraphicsOpenGL extends PGraphics { protected class TexlightShader extends LightShader { protected Texture texture; protected int texUnit; - protected int texCoordLoc; protected int textureLoc; protected int texMatrixLoc; @@ -7074,19 +7012,6 @@ public class PGraphicsOpenGL extends PGraphics { texOffsetLoc = getUniformLoc("texOffset"); } - @Override - public void loadAttributes() { - super.loadAttributes(); - - texCoordLoc = getAttributeLoc("texCoord"); - } - - @Override - public void setTexcoordAttribute(int vboId, int size, int type, - int stride, int offset) { - setAttributeVBO(texCoordLoc, vboId, size, type, false, stride, offset); - } - @Override public int getLastTexUnit() { return -1 < bufferUnit ? bufferUnit : super.getLastTexUnit(); @@ -7136,17 +7061,8 @@ public class PGraphicsOpenGL extends PGraphics { } } - @Override - public void bind() { - super.bind(); - - if (-1 < texCoordLoc) pgl.enableVertexAttribArray(texCoordLoc); - } - @Override public void unbind() { - if (-1 < texCoordLoc) pgl.disableVertexAttribArray(texCoordLoc); - if (-1 < textureLoc && texture != null) { pgl.activeTexture(PGL.TEXTURE0 + texUnit); texture.unbind(); diff --git a/core/src/processing/opengl/PShapeOpenGL.java b/core/src/processing/opengl/PShapeOpenGL.java index 43e172384..9dcfd90cf 100644 --- a/core/src/processing/opengl/PShapeOpenGL.java +++ b/core/src/processing/opengl/PShapeOpenGL.java @@ -4475,14 +4475,12 @@ public class PShapeOpenGL extends PShape { 0, 4 * voffset * PGL.SIZEOF_BYTE); shader.setShininessAttribute(root.glPolyShininess, 1, PGL.FLOAT, 0, voffset * PGL.SIZEOF_FLOAT); - } - - if (tex != null) { + } else { shader.setNormalAttribute(root.glPolyNormal, 3, PGL.FLOAT, 0, 3 * voffset * PGL.SIZEOF_FLOAT); shader.setTexcoordAttribute(root.glPolyTexcoord, 2, PGL.FLOAT, 0, 2 * voffset * PGL.SIZEOF_FLOAT); - shader.setTexture(tex); + if (tex != null) shader.setTexture(tex); } pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, root.glPolyIndex);