- java.lang.Object
-
- processing.opengl.PShader
-
- All Implemented Interfaces:
PConstants
public class PShader extends Object implements PConstants
This class encapsulates a GLSL shader program, including a vertex and a fragment shader. Based on the GLSLShader class from GLGraphics, which in turn was originally based in the code by JohnG: http://processing.org/discourse/beta/num_1159494801.html
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classPShader.UniformValue
-
Field Summary
Fields Modifier and Type Field Description protected intambientLocprotected booleanboundprotected static intCOLORprotected intcolorLocprotected static StringcolorShaderDefRegexpprotected intcontextprotected PGraphicsOpenGLcurrentPGprotected intdirectionLocprotected intemissiveLocprotected FloatBufferfloatBufferprotected StringfragmentFilenameprotected String[]fragmentShaderSourceprotected URLfragmentURLintglFragmentintglProgramintglVertexprotected IntBufferintBufferprotected static intLIGHTprotected intlightAmbientLocprotected intlightCountLocprotected intlightDiffuseLocprotected intlightFalloffLocprotected intlightNormalLocprotected intlightPositionLocprotected static StringlightShaderDefRegexpprotected intlightSpecularLocprotected intlightSpotLocprotected static intLINEprotected static StringlineShaderAttrRegexpprotected static StringlineShaderDefRegexpprotected static StringlineShaderInRegexpprotected booleanloadedAttributesprotected booleanloadedUniformsprotected intmodelviewMatLocprotected intnormalLocprotected intnormalMatLocprotected intoffsetLocprotected PAppletparentprotected intperspectiveLocprotected PGLpglprotected static intPOINTprotected static StringpointShaderAttrRegexpprotected static StringpointShaderDefRegexpprotected static StringpointShaderInRegexpprotected static intPOLYprotected static StringpolyShaderDefRegexpprotected intppixelsLocprotected intppixelsUnitprotected PGraphicsOpenGLprimaryPGprotected intprojectionMatLocprotected static StringquadShaderAttrRegexpprotected intresolutionLocprotected intscaleLocprotected intshininessLocprotected intspecularLocprotected float[]tcmatprotected inttexCoordLocprotected static intTEXLIGHTprotected static StringtexlightShaderDefRegexpprotected inttexMatrixLocprotected inttexOffsetLocprotected static StringtexShaderDefRegexpprotected Texturetextureprotected static intTEXTUREprotected inttextureLocprotected HashMap<Integer,Texture>texturesprotected inttexUnitprotected HashMap<Integer,Integer>texUnitsprotected inttransformMatLocprotected static StringtriShaderAttrRegexpprotected inttypeprotected HashMap<String,PShader.UniformValue>uniformValuesprotected StringvertexFilenameprotected intvertexLocprotected String[]vertexShaderSourceprotected URLvertexURLprotected intviewportLoc-
Fields inherited from interface processing.core.PConstants
ADD, ALPHA, ALT, AMBIENT, ARC, ARGB, ARROW, BACKSPACE, BASELINE, BEVEL, BEZIER_VERTEX, BLEND, BLUR, BOTTOM, BOX, BREAK, BURN, CENTER, CHATTER, CHORD, CLAMP, CLOSE, CODED, COMPLAINT, CONTROL, CORNER, CORNERS, CROSS, CURVE_VERTEX, CUSTOM, DARKEST, DEG_TO_RAD, DELETE, DIAMETER, DIFFERENCE, DILATE, DIRECTIONAL, DISABLE_ASYNC_SAVEFRAME, DISABLE_BUFFER_READING, DISABLE_DEPTH_MASK, DISABLE_DEPTH_SORT, DISABLE_DEPTH_TEST, DISABLE_KEY_REPEAT, DISABLE_NATIVE_FONTS, DISABLE_OPENGL_ERRORS, DISABLE_OPTIMIZED_STROKE, DISABLE_STROKE_PERSPECTIVE, DISABLE_STROKE_PURE, DISABLE_TEXTURE_MIPMAPS, DODGE, DOWN, DXF, ELLIPSE, ENABLE_ASYNC_SAVEFRAME, ENABLE_BUFFER_READING, ENABLE_DEPTH_MASK, ENABLE_DEPTH_SORT, ENABLE_DEPTH_TEST, ENABLE_KEY_REPEAT, ENABLE_NATIVE_FONTS, ENABLE_OPENGL_ERRORS, ENABLE_OPTIMIZED_STROKE, ENABLE_STROKE_PERSPECTIVE, ENABLE_STROKE_PURE, ENABLE_TEXTURE_MIPMAPS, ENTER, EPSILON, ERODE, ESC, EXCLUSION, FX2D, GIF, GRAY, GROUP, HALF_PI, HAND, HARD_LIGHT, HINT_COUNT, HSB, IMAGE, INVERT, JAVA2D, JPEG, LANDSCAPE, LEFT, LIGHTEST, LINE_LOOP, LINE_STRIP, LINES, LINUX, MACOSX, MAX_FLOAT, MAX_INT, MIN_FLOAT, MIN_INT, MITER, MODEL, MODELVIEW, MOVE, MULTIPLY, NORMAL, OPAQUE, OPEN, OPENGL, ORTHOGRAPHIC, OTHER, OVERLAY, P2D, P3D, PATH, PDF, PERSPECTIVE, PI, PIE, platformNames, POINTS, POLYGON, PORTRAIT, POSTERIZE, PROBLEM, PROJECT, PROJECTION, QUAD, QUAD_BEZIER_VERTEX, QUAD_STRIP, QUADRATIC_VERTEX, QUADS, QUARTER_PI, RAD_TO_DEG, RADIUS, RECT, REPEAT, REPLACE, RETURN, RGB, RIGHT, ROUND, SCREEN, SHAPE, SHIFT, SOFT_LIGHT, SPAN, SPHERE, SPOT, SQUARE, SUBTRACT, SVG, TAB, TARGA, TAU, TEXT, THIRD_PI, THRESHOLD, TIFF, TOP, TRIANGLE, TRIANGLE_FAN, TRIANGLE_STRIP, TRIANGLES, TWO_PI, UP, VERTEX, WAIT, WHITESPACE, WINDOWS, X, Y, Z
-
-
Constructor Summary
Constructors Constructor Description PShader()PShader(PApplet parent)PShader(PApplet parent, String[] vertSource, String[] fragSource)PShader(PApplet parent, String vertFilename, String fragFilename)Creates a shader program using the specified vertex and fragment shaders.PShader(PApplet parent, URL vertURL, URL fragURL)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected booleanaccessLightAttribs()protected booleanaccessNormals()protected booleanaccessTexCoords()voidbind()Initializes (if needed) and binds the shader program.protected voidbindTextures()protected voidbindTyped()booleanbound()Returns true if the shader is bound, false otherwise.protected booleancheckPolyType(int type)protected booleancompile()protected booleancompileFragmentShader()protected booleancompileVertexShader()protected voidconsumeUniforms()protected booleancontextIsOutdated()protected voidcreate()protected voiddispose()protected voiddraw(int idxId, int count, int offset)protected intgetAttributeLoc(String name)Returns the ID location of the attribute parameter given its name.protected intgetLastTexUnit()protected static intgetShaderType(String[] source, int defaultType)protected intgetType()protected intgetUniformLoc(String name)Returns the ID location of the uniform parameter given its name.protected booleanhasFragmentShader()protected booleanhasType()protected booleanhasVertexShader()voidinit()protected booleanisLineShader()protected booleanisPointShader()protected booleanisPolyShader()protected voidloadAttributes()protected voidloadUniforms()voidset(String name, boolean x)voidset(String name, boolean[] vec)voidset(String name, boolean[] boolvec, int ncoords)voidset(String name, boolean x, boolean y)voidset(String name, boolean x, boolean y, boolean z)voidset(String name, boolean x, boolean y, boolean z, boolean w)voidset(String name, float x)voidset(String name, float[] vec)voidset(String name, float[] vec, int ncoords)voidset(String name, float x, float y)voidset(String name, float x, float y, float z)voidset(String name, float x, float y, float z, float w)voidset(String name, int x)voidset(String name, int[] vec)voidset(String name, int[] vec, int ncoords)voidset(String name, int x, int y)voidset(String name, int x, int y, int z)voidset(String name, int x, int y, int z, int w)voidset(String name, PImage tex)voidset(String name, PMatrix2D mat)voidset(String name, PMatrix3D mat)voidset(String name, PMatrix3D mat, boolean use3x3)voidset(String name, PVector vec)protected voidsetAmbientAttribute(int vboId, int size, int type, int stride, int offset)protected voidsetAttributeVBO(int loc, int vboId, int size, int type, boolean normalized, int stride, int offset)protected voidsetColorAttribute(int vboId, int size, int type, int stride, int offset)protected voidsetCommonUniforms()protected voidsetEmissiveAttribute(int vboId, int size, int type, int stride, int offset)voidsetFragmentShader(String fragFilename)voidsetFragmentShader(String[] fragSource)voidsetFragmentShader(URL fragURL)protected voidsetLineAttribute(int vboId, int size, int type, int stride, int offset)protected voidsetNormalAttribute(int vboId, int size, int type, int stride, int offset)protected voidsetPointAttribute(int vboId, int size, int type, int stride, int offset)protected voidsetRenderer(PGraphicsOpenGL pg)protected voidsetShininessAttribute(int vboId, int size, int type, int stride, int offset)protected voidsetSpecularAttribute(int vboId, int size, int type, int stride, int offset)protected voidsetTexcoordAttribute(int vboId, int size, int type, int stride, int offset)protected voidsetTexture(Texture tex)protected voidsetType(int type)protected voidsetUniformImpl(String name, int type, Object value)protected voidsetUniformMatrix(int loc, float[] mat)protected voidsetUniformTex(int loc, Texture tex)protected voidsetUniformValue(int loc, float x)protected voidsetUniformValue(int loc, float x, float y)protected voidsetUniformValue(int loc, float x, float y, float z)protected voidsetUniformValue(int loc, float x, float y, float z, float w)protected voidsetUniformValue(int loc, int x)protected voidsetUniformValue(int loc, int x, int y)protected voidsetUniformValue(int loc, int x, int y, int z)protected voidsetUniformValue(int loc, int x, int y, int z, int w)protected voidsetUniformVector(int loc, float[] vec, int ncoords, int length)protected voidsetUniformVector(int loc, int[] vec, int ncoords, int length)protected voidsetup()Extra initialization method that can be used by subclasses, called after compiling and attaching the vertex and fragment shaders, and before linking the shader program.protected voidsetVertexAttribute(int vboId, int size, int type, int stride, int offset)voidsetVertexShader(String vertFilename)voidsetVertexShader(String[] vertSource)voidsetVertexShader(URL vertURL)protected booleansupportLighting()protected booleansupportsTexturing()voidunbind()Unbinds the shader program.protected voidunbindTextures()protected voidunbindTyped()protected voidupdateFloatBuffer(float[] vec)protected voidupdateIntBuffer(int[] vec)protected voidvalidate()
-
-
-
Field Detail
-
POINT
protected static final int POINT
- See Also:
- Constant Field Values
-
LINE
protected static final int LINE
- See Also:
- Constant Field Values
-
POLY
protected static final int POLY
- See Also:
- Constant Field Values
-
COLOR
protected static final int COLOR
- See Also:
- Constant Field Values
-
LIGHT
protected static final int LIGHT
- See Also:
- Constant Field Values
-
TEXTURE
protected static final int TEXTURE
- See Also:
- Constant Field Values
-
TEXLIGHT
protected static final int TEXLIGHT
- See Also:
- Constant Field Values
-
pointShaderAttrRegexp
protected static String pointShaderAttrRegexp
-
pointShaderInRegexp
protected static String pointShaderInRegexp
-
lineShaderAttrRegexp
protected static String lineShaderAttrRegexp
-
lineShaderInRegexp
protected static String lineShaderInRegexp
-
pointShaderDefRegexp
protected static String pointShaderDefRegexp
-
lineShaderDefRegexp
protected static String lineShaderDefRegexp
-
colorShaderDefRegexp
protected static String colorShaderDefRegexp
-
lightShaderDefRegexp
protected static String lightShaderDefRegexp
-
texShaderDefRegexp
protected static String texShaderDefRegexp
-
texlightShaderDefRegexp
protected static String texlightShaderDefRegexp
-
polyShaderDefRegexp
protected static String polyShaderDefRegexp
-
triShaderAttrRegexp
protected static String triShaderAttrRegexp
-
quadShaderAttrRegexp
protected static String quadShaderAttrRegexp
-
parent
protected PApplet parent
-
primaryPG
protected PGraphicsOpenGL primaryPG
-
currentPG
protected PGraphicsOpenGL currentPG
-
pgl
protected PGL pgl
-
context
protected int context
-
type
protected int type
-
glProgram
public int glProgram
-
glVertex
public int glVertex
-
glFragment
public int glFragment
-
vertexURL
protected URL vertexURL
-
fragmentURL
protected URL fragmentURL
-
vertexFilename
protected String vertexFilename
-
fragmentFilename
protected String fragmentFilename
-
vertexShaderSource
protected String[] vertexShaderSource
-
fragmentShaderSource
protected String[] fragmentShaderSource
-
bound
protected boolean bound
-
uniformValues
protected HashMap<String,PShader.UniformValue> uniformValues
-
intBuffer
protected IntBuffer intBuffer
-
floatBuffer
protected FloatBuffer floatBuffer
-
loadedAttributes
protected boolean loadedAttributes
-
loadedUniforms
protected boolean loadedUniforms
-
transformMatLoc
protected int transformMatLoc
-
modelviewMatLoc
protected int modelviewMatLoc
-
projectionMatLoc
protected int projectionMatLoc
-
ppixelsLoc
protected int ppixelsLoc
-
ppixelsUnit
protected int ppixelsUnit
-
viewportLoc
protected int viewportLoc
-
resolutionLoc
protected int resolutionLoc
-
perspectiveLoc
protected int perspectiveLoc
-
scaleLoc
protected int scaleLoc
-
lightCountLoc
protected int lightCountLoc
-
lightPositionLoc
protected int lightPositionLoc
-
lightNormalLoc
protected int lightNormalLoc
-
lightAmbientLoc
protected int lightAmbientLoc
-
lightDiffuseLoc
protected int lightDiffuseLoc
-
lightSpecularLoc
protected int lightSpecularLoc
-
lightFalloffLoc
protected int lightFalloffLoc
-
lightSpotLoc
protected int lightSpotLoc
-
texture
protected Texture texture
-
texUnit
protected int texUnit
-
textureLoc
protected int textureLoc
-
texMatrixLoc
protected int texMatrixLoc
-
texOffsetLoc
protected int texOffsetLoc
-
tcmat
protected float[] tcmat
-
vertexLoc
protected int vertexLoc
-
colorLoc
protected int colorLoc
-
normalLoc
protected int normalLoc
-
texCoordLoc
protected int texCoordLoc
-
normalMatLoc
protected int normalMatLoc
-
directionLoc
protected int directionLoc
-
offsetLoc
protected int offsetLoc
-
ambientLoc
protected int ambientLoc
-
specularLoc
protected int specularLoc
-
emissiveLoc
protected int emissiveLoc
-
shininessLoc
protected int shininessLoc
-
-
Constructor Detail
-
PShader
public PShader()
-
PShader
public PShader(PApplet parent)
-
PShader
public PShader(PApplet parent, String vertFilename, String fragFilename)
Creates a shader program using the specified vertex and fragment shaders.- Parameters:
parent- the parent programvertFilename- name of the vertex shaderfragFilename- name of the fragment shader
-
PShader
public PShader(PApplet parent, URL vertURL, URL fragURL)
- Parameters:
vertURL- network location of the vertex shaderfragURL- network location of the fragment shader
-
-
Method Detail
-
setVertexShader
public void setVertexShader(String vertFilename)
-
setVertexShader
public void setVertexShader(URL vertURL)
-
setVertexShader
public void setVertexShader(String[] vertSource)
-
setFragmentShader
public void setFragmentShader(String fragFilename)
-
setFragmentShader
public void setFragmentShader(URL fragURL)
-
setFragmentShader
public void setFragmentShader(String[] fragSource)
-
bind
public void bind()
Initializes (if needed) and binds the shader program.
-
unbind
public void unbind()
Unbinds the shader program.
-
bound
public boolean bound()
Returns true if the shader is bound, false otherwise.
-
set
public void set(String name, int x)
- Parameters:
name- the name of the uniform variable to modifyx- first component of the variable to modify
-
set
public void set(String name, int x, int y)
- Parameters:
y- second component of the variable to modify. The variable has to be declared with an array/vector type in the shader (i.e.: int[2], vec2)
-
set
public void set(String name, int x, int y, int z)
- Parameters:
z- third component of the variable to modify. The variable has to be declared with an array/vector type in the shader (i.e.: int[3], vec3)
-
set
public void set(String name, int x, int y, int z, int w)
- Parameters:
w- fourth component of the variable to modify. The variable has to be declared with an array/vector type in the shader (i.e.: int[4], vec4)
-
set
public void set(String name, float x)
-
set
public void set(String name, float x, float y)
-
set
public void set(String name, float x, float y, float z)
-
set
public void set(String name, float x, float y, float z, float w)
-
set
public void set(String name, PVector vec)
- Parameters:
vec- modifies all the components of an array/vector uniform variable. PVector can only be used if the type of the variable is vec3.
-
set
public void set(String name, boolean x)
-
set
public void set(String name, boolean x, boolean y)
-
set
public void set(String name, boolean x, boolean y, boolean z)
-
set
public void set(String name, boolean x, boolean y, boolean z, boolean w)
-
set
public void set(String name, int[] vec)
-
set
public void set(String name, int[] vec, int ncoords)
- Parameters:
ncoords- number of coordinates per element, max 4
-
set
public void set(String name, float[] vec)
-
set
public void set(String name, float[] vec, int ncoords)
-
set
public void set(String name, boolean[] vec)
-
set
public void set(String name, boolean[] boolvec, int ncoords)
-
set
public void set(String name, PMatrix3D mat, boolean use3x3)
- Parameters:
use3x3- enforces the matrix is 3 x 3
-
set
public void set(String name, PImage tex)
- Parameters:
tex- sets the sampler uniform variable to read from this image texture
-
setup
protected void setup()
Extra initialization method that can be used by subclasses, called after compiling and attaching the vertex and fragment shaders, and before linking the shader program.
-
draw
protected void draw(int idxId, int count, int offset)
-
getAttributeLoc
protected int getAttributeLoc(String name)
Returns the ID location of the attribute parameter given its name.- Parameters:
name- String- Returns:
- int
-
getUniformLoc
protected int getUniformLoc(String name)
Returns the ID location of the uniform parameter given its name.- Parameters:
name- String- Returns:
- int
-
setAttributeVBO
protected void setAttributeVBO(int loc, int vboId, int size, int type, boolean normalized, int stride, int offset)
-
setUniformValue
protected void setUniformValue(int loc, int x)
-
setUniformValue
protected void setUniformValue(int loc, int x, int y)
-
setUniformValue
protected void setUniformValue(int loc, int x, int y, int z)
-
setUniformValue
protected void setUniformValue(int loc, int x, int y, int z, int w)
-
setUniformValue
protected void setUniformValue(int loc, float x)
-
setUniformValue
protected void setUniformValue(int loc, float x, float y)
-
setUniformValue
protected void setUniformValue(int loc, float x, float y, float z)
-
setUniformValue
protected void setUniformValue(int loc, float x, float y, float z, float w)
-
setUniformVector
protected void setUniformVector(int loc, int[] vec, int ncoords, int length)
-
setUniformVector
protected void setUniformVector(int loc, float[] vec, int ncoords, int length)
-
setUniformMatrix
protected void setUniformMatrix(int loc, float[] mat)
-
setUniformTex
protected void setUniformTex(int loc, Texture tex)
-
consumeUniforms
protected void consumeUniforms()
-
updateIntBuffer
protected void updateIntBuffer(int[] vec)
-
updateFloatBuffer
protected void updateFloatBuffer(float[] vec)
-
bindTextures
protected void bindTextures()
-
unbindTextures
protected void unbindTextures()
-
init
public void init()
-
create
protected void create()
-
compile
protected boolean compile()
-
validate
protected void validate()
-
contextIsOutdated
protected boolean contextIsOutdated()
-
hasVertexShader
protected boolean hasVertexShader()
-
hasFragmentShader
protected boolean hasFragmentShader()
-
compileVertexShader
protected boolean compileVertexShader()
- Parameters:
shaderSource- a string containing the shader's code
-
compileFragmentShader
protected boolean compileFragmentShader()
- Parameters:
shaderSource- a string containing the shader's code
-
dispose
protected void dispose()
-
getShaderType
protected static int getShaderType(String[] source, int defaultType)
-
getType
protected int getType()
-
setType
protected void setType(int type)
-
hasType
protected boolean hasType()
-
isPointShader
protected boolean isPointShader()
-
isLineShader
protected boolean isLineShader()
-
isPolyShader
protected boolean isPolyShader()
-
checkPolyType
protected boolean checkPolyType(int type)
-
getLastTexUnit
protected int getLastTexUnit()
-
setRenderer
protected void setRenderer(PGraphicsOpenGL pg)
-
loadAttributes
protected void loadAttributes()
-
loadUniforms
protected void loadUniforms()
-
setCommonUniforms
protected void setCommonUniforms()
-
bindTyped
protected void bindTyped()
-
unbindTyped
protected void unbindTyped()
-
setTexture
protected void setTexture(Texture tex)
-
supportsTexturing
protected boolean supportsTexturing()
-
supportLighting
protected boolean supportLighting()
-
accessTexCoords
protected boolean accessTexCoords()
-
accessNormals
protected boolean accessNormals()
-
accessLightAttribs
protected boolean accessLightAttribs()
-
setVertexAttribute
protected void setVertexAttribute(int vboId, int size, int type, int stride, int offset)
-
setColorAttribute
protected void setColorAttribute(int vboId, int size, int type, int stride, int offset)
-
setNormalAttribute
protected void setNormalAttribute(int vboId, int size, int type, int stride, int offset)
-
setTexcoordAttribute
protected void setTexcoordAttribute(int vboId, int size, int type, int stride, int offset)
-
setAmbientAttribute
protected void setAmbientAttribute(int vboId, int size, int type, int stride, int offset)
-
setSpecularAttribute
protected void setSpecularAttribute(int vboId, int size, int type, int stride, int offset)
-
setEmissiveAttribute
protected void setEmissiveAttribute(int vboId, int size, int type, int stride, int offset)
-
setShininessAttribute
protected void setShininessAttribute(int vboId, int size, int type, int stride, int offset)
-
setLineAttribute
protected void setLineAttribute(int vboId, int size, int type, int stride, int offset)
-
setPointAttribute
protected void setPointAttribute(int vboId, int size, int type, int stride, int offset)
-
-