Package processing.opengl
Class PShader
java.lang.Object
processing.opengl.PShader
- All Implemented Interfaces:
PConstants
This class encapsulates a GLSL shader program, including a vertex
and a fragment shader. It is compatible with P2D and P3D, but not
with the default renderer.
Use the loadShader() function to load your shader code.
Note: It's strongly encouraged to use loadShader() to create
a PShader object, rather than calling the PShader
constructor manually.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intprotected booleanprotected static final intprotected intprotected static Stringprotected intprotected PGraphicsOpenGLprotected intprotected intprotected FloatBufferprotected Stringprotected String[]protected URLintintintprotected IntBufferprotected static final intprotected intprotected intprotected intprotected intprotected intprotected intprotected static Stringprotected intprotected intprotected static final intprotected static Stringprotected static Stringprotected static Stringprotected booleanprotected booleanprotected intprotected intprotected intprotected intprotected PAppletprotected intprotected PGLprotected static final intprotected static Stringprotected static Stringprotected static Stringprotected static final intprotected static Stringprotected intprotected intprotected PGraphicsOpenGLprotected intprotected static Stringprotected intprotected intprotected intprotected intprotected float[]protected intprotected static final intprotected static Stringprotected intprotected intprotected static Stringprotected Textureprotected static final intprotected intprotected intprotected intprotected static Stringprotected intprotected HashMap<String,PShader.UniformValue> protected Stringprotected intprotected String[]protected URLprotected intFields inherited from interface processing.core.PConstants
ADD, ALPHA, ALT, AMBIENT, ARC, ARGB, ARROW, BACKSPACE, BASELINE, BEVEL, BEZIER_VERTEX, BICUBIC, BILINEAR, 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, MACOS, MACOSX, MAX_FLOAT, MAX_INT, MIN_FLOAT, MIN_INT, MITER, MODEL, MODELVIEW, MOVE, MULTIPLY, NEAREST_NEIGHBOR, 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
ConstructorsConstructorDescriptionPShader()Creates a shader program using the specified vertex and fragment shaders. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanprotected booleanprotected booleanvoidbind()Initializes (if needed) and binds the shader program.protected voidprotected voidbooleanbound()Returns true if the shader is bound, false otherwise.protected booleancheckPolyType(int type) protected booleancompile()protected booleanprotected booleanprotected voidprotected booleanprotected 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 intprotected static intgetShaderType(String[] source, int defaultType) protected intgetType()protected intgetUniformLoc(String name) Returns the ID location of the uniform parameter given its name.protected booleanprotected booleanhasType()protected booleanvoidinit()protected booleanprotected booleanprotected booleanprotected voidprotected voidvoidvoidvoidvoidvoidvoidvoidvoidvoidvoidvoidvoidvoidSets the uniform variables inside the shader to modify the effect while the program is running.voidvoidvoidvoidvoidvoidvoidvoidvoidvoidprotected 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 voidprotected 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 voidprotected 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 booleanprotected booleanvoidunbind()Unbinds the shader program.protected voidprotected voidprotected voidupdateFloatBuffer(float[] vec) protected voidupdateIntBuffer(int[] vec) protected voidvalidate()
-
Field Details
-
POINT
protected static final int POINT- See Also:
-
LINE
protected static final int LINE- See Also:
-
POLY
protected static final int POLY- See Also:
-
COLOR
protected static final int COLOR- See Also:
-
LIGHT
protected static final int LIGHT- See Also:
-
TEXTURE
protected static final int TEXTURE- See Also:
-
TEXLIGHT
protected static final int TEXLIGHT- See Also:
-
pointShaderAttrRegexp
-
pointShaderInRegexp
-
lineShaderAttrRegexp
-
lineShaderInRegexp
-
pointShaderDefRegexp
-
lineShaderDefRegexp
-
colorShaderDefRegexp
-
lightShaderDefRegexp
-
texShaderDefRegexp
-
texlightShaderDefRegexp
-
polyShaderDefRegexp
-
triShaderAttrRegexp
-
quadShaderAttrRegexp
-
parent
-
-
currentPG
-
pgl
-
context
protected int context -
type
protected int type -
glProgram
public int glProgram -
glVertex
public int glVertex -
glFragment
public int glFragment -
vertexURL
-
fragmentURL
-
vertexFilename
-
fragmentFilename
-
vertexShaderSource
-
fragmentShaderSource
-
bound
protected boolean bound -
uniformValues
-
textures
-
texUnits
-
intBuffer
-
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
-
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 Details
-
PShader
public PShader() -
PShader
-
PShader
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
- Parameters:
vertURL- network location of the vertex shaderfragURL- network location of the fragment shader
-
PShader
-
-
Method Details
-
setVertexShader
-
setVertexShader
-
setVertexShader
-
setFragmentShader
-
setFragmentShader
-
setFragmentShader
-
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
Sets the uniform variables inside the shader to modify the effect while the program is running.- Parameters:
name- the name of the uniform variable to modifyx- first component of the variable to modify
-
set
- 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
- 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
- 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
-
set
-
set
-
set
-
set
- 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
-
set
-
set
-
set
-
set
-
set
- Parameters:
ncoords- number of coordinates per element, max 4
-
set
-
set
-
set
-
set
-
set
- Parameters:
mat- matrix of values
-
set
-
set
- Parameters:
use3x3- enforces the matrix is 3 x 3
-
set
- 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
Returns the ID location of the attribute parameter given its name.- Parameters:
name- String- Returns:
- int
-
getUniformLoc
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
-
setUniformImpl
-
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() -
compileFragmentShader
protected boolean compileFragmentShader() -
dispose
protected void dispose() -
getShaderType
-
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
-
loadAttributes
protected void loadAttributes() -
loadUniforms
protected void loadUniforms() -
setCommonUniforms
protected void setCommonUniforms() -
bindTyped
protected void bindTyped() -
unbindTyped
protected void unbindTyped() -
setTexture
-
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)
-