getUniformLocation method

Returns a WebGLUniformLocation object for the location of a uniform variable within a WebGLProgram object.

IE11

 

Syntax

var retVal = WebGLRenderingContext.getUniformLocation(program, name);

Parameters

program [in]

Type: WebGLProgram

The program object to query.

name [in]

Type: String

A string containing the name of the uniform variable.

Return value

Type: WebGLUniformLocation

The WebGLUniformLocation for the name. Returns null if a WebGL error occurs or if name starts with a reserved WebGL prefix.

Remarks

WebGL ErrorDescription
gl.INVALID_VALUEIf program is not generated by WebGL.
gl.INVALID_OPERATION
  • If program is not a program object.
  • if program isn't successfully linked.

 

Examples

The following example uses getUniformLocation for points in either a line or a photo rendering app. For more info on this example, see Get started with WebGL.


//  Clear color buffer and set it to light gray
    gl.clearColor(1.0, 1.0, 1.0, 0.5);
    gl.clear(this.gl.COLOR_BUFFER_BIT);
            
// This draws either the grid or the photo for stretching
    if (document.getElementById("renderLines").checked)
    {
      gl.bindBuffer(gl.ARRAY_BUFFER, this.texCoordBuffer2);

      gl.useProgram(this.lineprogram);

      gl.uniform2fv(gl.getUniformLocation(this.lineprogram, "p1"), p1);
      gl.uniform2fv(gl.getUniformLocation(this.lineprogram, "p2"), p2);

      gl.vertexAttribPointer(this.texCoordLocation2, 2, gl.FLOAT, false, 0, 0);

      gl.enableVertexAttribArray(this.texCoordLocation2);

      gl.drawArrays(gl.LINES, 0, resolution * resolution * 10);
    }
    else
    { 
      gl.bindBuffer(gl.ARRAY_BUFFER, this.texCoordBuffer);
      
      gl.useProgram(this.pictureprogram);


      gl.uniform2fv(gl.getUniformLocation(this.pictureprogram, "p1"), p1);
      gl.uniform2fv(gl.getUniformLocation(this.pictureprogram, "p2"), p2);

      gl.vertexAttribPointer(this.texCoordLocation, 2, gl.FLOAT, false, 0, 0);

      gl.enableVertexAttribArray(this.texCoordLocation);


      gl.drawArrays(gl.TRIANGLES, 0, resolution * resolution * 2 * 3);
    }


See also

WebGLRenderingContext

 

 

Show: