Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize

attachShader method

Attaches a WebGLShader object to a WebGLProgram object.

IE11

 

Syntax

WebGLRenderingContext.attachShader(program, shader);

Parameters

program [in]

Type: WebGLProgram

The WebGLProgram object created using the createProgram method.

shader [in]

Type: WebGLShader

The WebGLShader object to attach.

Return value

This method does not return a value.

Remarks

WebGL errorDescription
gl.INVALID_OPERATION
  • If program isn't a program object.
  • If shader isn't a shader object.
  • If shader is already attached to program, or another shader object of the same type is already attached.
gl.INVALID_VALUEIf either program or shader isn't a value generated by WebGL.

 

A shader can be attached to a program before or after the code is loaded into the shader object and compiled. Only one shader object of each type can be attached to the same program object, but the same shader object can be attached to multiple program objects.

Both a vertex and a fragment shader are required in order to link the program.

Examples

This clip creates a program and attaches a vertex and a fragment shaders. See Get started with WebGL for more info.


function loadProgram(gl, vertexShader, fragmentShader)
{
  // create a progam object
  var program = gl.createProgram();

  // attach the two shaders 
  gl.attachShader(program, vertexShader);
  gl.attachShader(program, fragmentShader);

  // link everything 
  gl.linkProgram(program);

  // Check the link status
  var linked = gl.getProgramParameter(program, gl.LINK_STATUS);
  if (!linked) {

    // An error occurred while linking
    var lastError = gl.getProgramInfoLog(program);
    console.warn("Error in program linking:" + lastError);

    gl.deleteProgram(program);
    return null;
  }

  // if all is well, return the program object
  return program;
};


See also

WebGLRenderingContext

 

 

Show:
© 2015 Microsoft