Documentation generator: JsDoc Toolkit 2.4.0
Template: Codeview 1.2
Generated on: 2012-11-19 0:05

Class SpiderGL.WebGL.Program

The SpiderGL.WebGL.Program is a wrapper for WebGLProgram objects.

Class Summary
Constructor Attributes Constructor Name and Description
 
SpiderGL.WebGL.Program(gl, options)
Creates a SpiderGL.WebGL.Program.
Field Summary
Field Attributes Field Name and Description
 
Gets/Sets if the program will be linked automatically whenever shaders are added or removed, or vertex attribute indices change.
<static>  
SpiderGL.WebGL.Program.DEFAULT_AUTO_LINK
Default value for SpiderGL.WebGL.Program#autoLink.
<readonly>  
Tests if the program is linked.
<readonly>  
Tests if the program is ready to use.
<readonly>  
log
Gets the program info log.
<static>  
SpiderGL.WebGL.Program.TARGET
Dummy WebGL target for programs.
Fields borrowed from class SpiderGL.WebGL.ObjectGL:
gl, handle, isValid, target
Fields borrowed from class SpiderGL.Core.ObjectBase:
uid

Method Summary

Method Attributes Method Name and Description
 
addShaders(shaders, link)
Attaches the provided shaders to the program.
 
bind()
Binds the wrapped WebGLProgram with WebGLRenderingContext.useProgram().
 
Destroys the WebGLProgram.
 
Gets the vertex attributes binding indices.
 
Gets the vertex attributes informations.
 
Gets vertex attributes names.
 
Gets the attached shaders.
 
Gets the program uniforms informations.
 
Gets uniforms names.
 
Gets the values of the program uniforms.
 
hasShader(shader)
Tests whether the passed shader is attached to the program.
 
link()
Links the program.
 
removeShaders(shaders, link)
Detaches the provided shaders to the program.
 
setAttributes(attributes)
Sets the indices of vertex shader attributes.
 
setUniforms(uniforms)
Sets the program uniforms.
<static>  
SpiderGL.WebGL.Program.unbind(gl)
WebGLProgram unbinding.
 
Binds the "null" program with WebGLRenderingContext.useProgram(null).
 
Validates the program with the current attribute indices, uniforms and WebGLRenderingContext state It is performed using WebGLRenderingContext.validateProgram().

Class Detail

SpiderGL.WebGL.Program(gl, options)
Creates a SpiderGL.WebGL.Program. SpiderGL.WebGL.Program is a wrapper for WebGLProgram objects.
var vertexShader   = new SpiderGL.WebGL.VertexShader   (gl, {source: vertexShaderSrc  });
var fragmentShader = new SpiderGL.WebGL.FragmentShader (gl, {source: fragmentShaderSrc});

var program = new SpiderGL.WebGL.Program(gl, {
	autoLink : true, // if true, the program is automatically linked whenever shaders are added or removed, or whenever attribute indices are changed.
	shaders  : [vertexShader, fragmentShader],
	attributes : {
		aPosition : 0, // the vertex shader aPosition attribute will be bound to the vertex attribute at index 0
		aNormal   : 1  // the vertex shader aNormal attribute will be bound to the vertex attribute at index 1
	},
	uniforms : {
		uDiffuseMap  : 0,  // index of the texture unit for diffuse color textures
		uScaleFactor : 2.0
	}
};

// uniforms can also be set when the program is not bound
program.setUniforms({
	uModelViewProjection : getMVP(),
	uShininess           : getShininess()
});

program.bind();
// render
Parameters:
{WebGLRenderingContext} gl
A WebGLRenderingContext hijacked with SpiderGL.WebGL.Context.hijack.
{object} options Optional
Optional parameters.
{WebGLProgram} options.handle Optional
A WebGLProgram. If present, this object will be used as the wrapped WebGLProgram and the attached shaders will be queried. Otherwise a new one will be created.
{bool} options.autoLink Optional, Default: SpiderGL.WebGL.Program.DEFAULT_AUTO_LINK
If true, the program will be linked automatically whenever shaders are added or removed, or vertex attribute indices change.
{array} options.shaders Optional
An array of SpiderGL.WebGL.Shader objects to attach to the program.
{object} options.attributes Optional
An object where each property has the name of a vertex shader attribute and whose value is the attribute index to wich the vertex attribute will be bound.
{object} options.uniforms Optional
An object where each property has the name of a program uniform and whose value is the uniform value.
See:
setShaders
setAttributes
setUniforms
autoLink
SpiderGL.WebGL.Shader
SpiderGL.WebGL.ObjectGL

Field Detail

Gets/Sets if the program will be linked automatically whenever shaders are added or removed, or vertex attribute indices change.
See:
link
Default value for SpiderGL.WebGL.Program#autoLink.
Default Value:
true
<readonly> {bool} isLinked
Tests if the program is linked.
See:
isReady
<readonly> {bool} isReady
Tests if the program is ready to use. A program is considered ready if it is succesfully linked.
See:
isLinked
<readonly> {bool} log
Gets the program info log.
<static> {number} SpiderGL.WebGL.Program.TARGET
Dummy WebGL target for programs. It is equal to WebGLRenderingContext.NONE and is provided only for completeness with other WebGL wrappers.
Default Value:
WebGLRenderingContext.NONE

Method Detail

  • {bool} addShaders(shaders, link)
    Attaches the provided shaders to the program. If link is not specified and autoLink is true, the program is automatically linked.
    Parameters:
    {array|SpiderGL.WebGL.Shader} shaders
    An array of SpiderGL.WebGL.Shader or a single SpiderGL.WebGL.Shader to attach.
    {bool} link Optional
    If specified, overrides the value of autoLink.
    Returns:
    {bool} If the program has been linked, returns whether the program is linked, otherwise always returns true.
    See:
    isLinked
  • bind()
    Binds the wrapped WebGLProgram with WebGLRenderingContext.useProgram().
    See:
    unbind
  • destroy()
    Destroys the WebGLProgram. After destruction, the handle is set to null and this object should not be used anymore.
    See:
    SpiderGL.WebGL.ObjectGL#destroy
  • {object} getAttributesIndices()
    Gets the vertex attributes binding indices.
    Returns:
    {object} An object with one property for each active vertex attribute. The name of the property is the name of the attribute in the vertex shader and its value is a non-negative integer representing the attribute bind index.
    See:
    getAttributesInfo
    setAttributes
  • {object} getAttributesInfo()
    Gets the vertex attributes informations.
    Returns:
    {object} An object where each property has the name of a vertex shader attribute and whose value is an object containing attribute information. The attribute index is in the "location" property.
    See:
    getAttributesIndices
    setAttributes
  • {array} getAttributesNames()
    Gets vertex attributes names.
    Returns:
    {array} An array containing the names of all active vertex shader attributes.
    See:
    getAttributesIndices
    getAttributesInfo
  • {array} getShaders()
    Gets the attached shaders.
    Returns:
    {array} An array with the attached SpiderGL.WebGL.Shader objects.
  • {object} getUniformsInfo()
    Gets the program uniforms informations.
    Returns:
    {object} An object where each property has the name of a program uniform and whose value is an object containing uinformation.
  • {array} getUniformsNames()
    Gets uniforms names.
    Returns:
    {array} An array containing the names of all active uniforms.
    See:
    getUniformsValues
    getUniformsInfo
  • {object} getUniformsValues()
    Gets the values of the program uniforms.
    Returns:
    {object} An object with one property for each active uniform. The name of the property is the name of the uniform and its value is the uniform value, which can be a number, an array or a typed array.
  • {bool} hasShader(shader)
    Tests whether the passed shader is attached to the program.
    Parameters:
    {SpiderGL.WebGL.Shader} shader
    The shader to test for attachment.
    Returns:
    {bool} If the shader is attached, false otherwise.
  • Links the program.
    Returns:
    {bool} True if the program has been succesfully linked, false otherwise.
  • {bool} removeShaders(shaders, link)
    Detaches the provided shaders to the program. If link is not specified and autoLink is true, the program is automatically linked.
    Parameters:
    {array|SpiderGL.WebGL.Shader} shaders
    An array of SpiderGL.WebGL.Shader or a single SpiderGL.WebGL.Shader to detach.
    {bool} link Optional
    If specified, overrides the value of autoLink.
    Returns:
    {bool} If the program has been linked, returns whether the program is linked, otherwise always returns true.
  • {bool} setAttributes(attributes)
    Sets the indices of vertex shader attributes. Only the recognized attributes (i.e. the active attributes in vertex shaders) will be set. The attribute binding indices will take effect only when the program is linked again. If autoLink is true, the program is automatically linked.
    var vertexShaderSrc = "" +
    	"..." + 
    	"attribute vec3 aPosition; \n" +
    	"attribute vec3 aNormal;   \n" +
    	"...";
    
    // setup the program (attribute indices can also be set at construction)
    // ...
    
    // set attributes indices;
    // if autoLink is true, the program will be automatically linked;
    // otherwise it must be explicitly linked with program.link()
    program.setAttributes({
    	aPosition : 3,  // bind attribute aPosition to vertex attribute 3
    	aNormal   : 1   // bind attribute aNormal to vertex attribute 1
    	aColor    : 2   // this attribute is not set because it is not an active attribute
    });
    Parameters:
    {object} attributes
    The attributes to set. For each attribute index to set, the object must contain a property whose name is the name of the vertex attribute and whose value is a non-negative integer specifying the attribute bind index.
    Returns:
    {bool} True if the attributes have been set, false otherwise.
    See:
    getAttributesIndices
    getAttributesInfo
    setUniforms
    link
    autoLink
  • {bool} setUniforms(uniforms)
    Sets the program uniforms. Only the recognized uniforms are set.
    var vertexShaderSrc = "" +
    	"..." + 
    	"uniform mat4  uMVP;      \n" +
    	"uniform float uScale;    \n" +
    	"uniform vec3  uLightPos; \n" +
    	"...";
    
    // setup the program (uniforms can also be set at construction)
    // ...
    
    // set uniform values;
    program.setUniforms({
    	uMVP      : getModelViewProjection(),
    	uScale    : 2.3,
    	uLightPos : [0, 0.5, 4.7], // can be a typed array: new Float32Array([0, 0.5, 4.7])
    	uOther    : 1.0 // this uniform is not set because it is not an active uniform
    });
    Parameters:
    {object} uniforms
    An object where each property has the name of the uniform to set and whose value is the uniform value.
    Returns:
    {bool} True if the uniforms have been set succesfully, false otherwise.
  • <static> SpiderGL.WebGL.Program.unbind(gl)
    WebGLProgram unbinding. This function binds the null program with WebGLRenderingContext.useProgram(null).
    Parameters:
    {WebGLRenderingContext} gl
    A WebGLRenderingContext.
  • unbind()
    Binds the "null" program with WebGLRenderingContext.useProgram(null). This method is provided only for simmetry with bind and is not relative to the object state.
    See:
    bind
  • {bool} validate()
    Validates the program with the current attribute indices, uniforms and WebGLRenderingContext state It is performed using WebGLRenderingContext.validateProgram().
    Returns:
    {bool} True if the program has been succesfully validated, false otherwise.