WebGL Textures & Vertices

Beginner's Guide

Page Thirty Seven

Compile & Link a Program

The method getProgram() called by the GLControl constructor, receives no parameters and returns either a WebGLProgram object or null. The method getProgram() creates an empty WebGLProgram, attaches two WebGLShader objects to the WebGLProgram, links the program and assigns the program for use.

First getProgram() calls getShader(String, Number) twice, to create and compile two WebGLShader objects. The first call prepares a fragment shader. The second call prepares a vertex shader.

The following listing demonstrates obtaining a reference to the fragment shader. The first parameter shader-f represents the shader's id within the current Web page. The WebGL constant FRAGMENT_SHADER represents the type of shader to compile.

var shaderF = this.getShader
(
 "shader-f", 
 gl.FRAGMENT_SHADER
);       

Listing 28: getShader(String, FRAGMENT_SHADER)

The following listing demonstrates obtaining a reference to the vertex shader. The first parameter shader-v represents the shader's id within the current Web page. The WebGL constant VERTEX_SHADER represents the type of shader to compile.

var shaderV = this.getShader
(
 "shader-v", 
 gl.VERTEX_SHADER
);       

Listing 29: getShader(String, VERTEX_SHADER)

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