WebGL Textures & Vertices

Beginner's Guide

Page Thirty Eight

WebGL API createProgram()

Method createProgram() requires no parameters and returns an empty WebGLProgram object. Method getProgram() creates a program reference with a call to the WebGL API method createProgram(). The following listing demonstrates creating an empty WebGLProgram.

var p = gl.createProgram();

Listing 30: WebGL API createProgram()

WebGL API attachShader(WebGLProgram, WebGLShader)

Method attachShader() requires two parameters. The first parameter is a WebGLProgram. The second parameter is a WebGLShader.

The following listing demonstrates attaching the fragment shader, within getProgram(). The parameter p is a WebGLProgram.

gl.attachShader
(
 p, 
 shaderF
);

Listing 31: Attach Fragment Shader

The following listing demonstrates attaching the vertex shader, within getProgram(). The parameter p is a WebGLProgram.

gl.attachShader
(
 p, 
 shaderV
);

Listing 32: Attach Vertex Shader

WebGL API linkProgram(WebGLProgram)

WebGL API method linkProgram() returns nothing. The only parameter to linkProgram() is a WebGLProgram with two shaders attached. linkProgram() links the two shaders to the WebGLProgram. The following listing demonstrates linking the program.

gl.linkProgram
(
 p
);

Listing 33: WebGL API linkProgram(WebGLProgram)

WebGL API useProgram(WebGLProgram)

WebGL API method useProgram() assigns the WebGLProgram for use. Future drawing operations access shaders linked to the assigned program. The following listing demonstrates calling useProgram(WebGLProgram) within method getProgram().

gl.useProgram
(
 p
);

Listing 34: WebGL API useProgram(WebGLProgram)

Finally getProgram() returns the WebGLProgram or null. If the value returned from getProgram() is null, then the GLControl constructor displays an error message and exits. Otherwise the GLControl constructor retains a reference to the WebGLProgram in the property named program.

See the source code for method getProgram().

WebGL Beginner's Guide Introduction WebGL Beginner's Guide
Copyright © 2015 Seven Thunder Software. All Rights Reserved.