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 int
protected boolean
protected static final int
protected int
protected static String
protected int
protected PGraphicsOpenGL
protected int
protected int
protected FloatBuffer
protected String
protected String[]
protected URL
int
int
int
protected IntBuffer
protected static final int
protected int
protected int
protected int
protected int
protected int
protected int
protected static String
protected int
protected int
protected static final int
protected static String
protected static String
protected static String
protected boolean
protected boolean
protected int
protected int
protected int
protected int
protected PApplet
protected int
protected PGL
protected static final int
protected static String
protected static String
protected static String
protected static final int
protected static String
protected int
protected int
protected PGraphicsOpenGL
protected int
protected static String
protected int
protected int
protected int
protected int
protected float[]
protected int
protected static final int
protected static String
protected int
protected int
protected static String
protected Texture
protected static final int
protected int
protected int
protected int
protected static String
protected int
protected HashMap<String,
PShader.UniformValue> protected String
protected int
protected String[]
protected URL
protected int
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, MACOS, 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
ConstructorsConstructorDescriptionPShader()
Creates a shader program using the specified vertex and fragment shaders. -
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
protected boolean
protected boolean
void
bind()
Initializes (if needed) and binds the shader program.protected void
protected void
boolean
bound()
Returns true if the shader is bound, false otherwise.protected boolean
checkPolyType
(int type) protected boolean
compile()
protected boolean
protected boolean
protected void
protected boolean
protected void
create()
protected void
dispose()
protected void
draw
(int idxId, int count, int offset) protected int
getAttributeLoc
(String name) Returns the ID location of the attribute parameter given its name.protected int
protected static int
getShaderType
(String[] source, int defaultType) protected int
getType()
protected int
getUniformLoc
(String name) Returns the ID location of the uniform parameter given its name.protected boolean
protected boolean
hasType()
protected boolean
void
init()
protected boolean
protected boolean
protected boolean
protected void
protected void
void
void
void
void
void
void
void
void
void
void
void
void
void
Sets the uniform variables inside the shader to modify the effect while the program is running.void
void
void
void
void
void
void
void
void
void
protected void
setAmbientAttribute
(int vboId, int size, int type, int stride, int offset) protected void
setAttributeVBO
(int loc, int vboId, int size, int type, boolean normalized, int stride, int offset) protected void
setColorAttribute
(int vboId, int size, int type, int stride, int offset) protected void
protected void
setEmissiveAttribute
(int vboId, int size, int type, int stride, int offset) void
setFragmentShader
(String fragFilename) void
setFragmentShader
(String[] fragSource) void
setFragmentShader
(URL fragURL) protected void
setLineAttribute
(int vboId, int size, int type, int stride, int offset) protected void
setNormalAttribute
(int vboId, int size, int type, int stride, int offset) protected void
setPointAttribute
(int vboId, int size, int type, int stride, int offset) protected void
protected void
setShininessAttribute
(int vboId, int size, int type, int stride, int offset) protected void
setSpecularAttribute
(int vboId, int size, int type, int stride, int offset) protected void
setTexcoordAttribute
(int vboId, int size, int type, int stride, int offset) protected void
setTexture
(Texture tex) protected void
setType
(int type) protected void
setUniformImpl
(String name, int type, Object value) protected void
setUniformMatrix
(int loc, float[] mat) protected void
setUniformTex
(int loc, Texture tex) protected void
setUniformValue
(int loc, float x) protected void
setUniformValue
(int loc, float x, float y) protected void
setUniformValue
(int loc, float x, float y, float z) protected void
setUniformValue
(int loc, float x, float y, float z, float w) protected void
setUniformValue
(int loc, int x) protected void
setUniformValue
(int loc, int x, int y) protected void
setUniformValue
(int loc, int x, int y, int z) protected void
setUniformValue
(int loc, int x, int y, int z, int w) protected void
setUniformVector
(int loc, float[] vec, int ncoords, int length) protected void
setUniformVector
(int loc, int[] vec, int ncoords, int length) 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.protected void
setVertexAttribute
(int vboId, int size, int type, int stride, int offset) void
setVertexShader
(String vertFilename) void
setVertexShader
(String[] vertSource) void
setVertexShader
(URL vertURL) protected boolean
protected boolean
void
unbind()
Unbinds the shader program.protected void
protected void
protected void
updateFloatBuffer
(float[] vec) protected void
updateIntBuffer
(int[] vec) protected void
validate()
-
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)
-