89 lines
2.7 KiB
TypeScript
89 lines
2.7 KiB
TypeScript
import {setProgramInfo} from './init';
|
|
|
|
/**
|
|
* Initialize a shader program, so WebGL knows how to draw our data
|
|
* @param {any} context the program context
|
|
* @param {any} shaderProgram reference to the shader program to init
|
|
* @param {any} fragmentShader fragment shader object reference
|
|
* @param {any} vertexShader vertex shader object reference
|
|
* @param {string} vsSource the vertex shader source
|
|
* @param {string} fsSource the fragment shader source
|
|
* @return {any} the new shader program
|
|
*/
|
|
export function initShaderProgram(context: any,
|
|
shaderProgram: any,
|
|
fragmentShader: any,
|
|
vertexShader: any,
|
|
vsSource: string,
|
|
fsSource: string) {
|
|
vertexShader = loadShader(context.gl,
|
|
context.gl.VERTEX_SHADER, vsSource);
|
|
fragmentShader = loadShader(context.gl,
|
|
context.gl.FRAGMENT_SHADER, fsSource);
|
|
|
|
// Create the shader program
|
|
shaderProgram = context.gl.createProgram();
|
|
context.gl.attachShader(shaderProgram, vertexShader);
|
|
context.gl.attachShader(shaderProgram, fragmentShader);
|
|
context.gl.linkProgram(shaderProgram);
|
|
|
|
// If creating the shader program failed, alert
|
|
if (!context.gl.getProgramParameter(shaderProgram,
|
|
context.gl.LINK_STATUS)) {
|
|
alert('Unable to initialize the shader program: ' +
|
|
context.gl.getProgramInfoLog(shaderProgram));
|
|
shaderProgram = null;
|
|
}
|
|
return shaderProgram;
|
|
}
|
|
|
|
/**
|
|
* load a GL shader
|
|
* @param {any} gl the WebGL context
|
|
* @param {any} type type of shader to load
|
|
* @param {string} source source code of shader
|
|
* @return {any} the loaded shader
|
|
*/
|
|
export function loadShader(gl: any, type: any, source: string) {
|
|
const shader = gl.createShader(type);
|
|
// Send the source to the shader object
|
|
gl.shaderSource(shader, source);
|
|
// Compile the shader program
|
|
gl.compileShader(shader);
|
|
// See if it compiled successfully
|
|
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
alert('An error occurred compiling the shaders: ' +
|
|
gl.getShaderInfoLog(shader));
|
|
gl.deleteShader(shader);
|
|
return null;
|
|
}
|
|
return shader;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {any} context the program context
|
|
* @param {any} programInfo programInfo reference to update
|
|
* @param {any} shaderProgram shaderProgram reference to update
|
|
* @param {any} shaders the current shaders reference
|
|
* @param {string} fsSource new fragment shader source
|
|
* @param {string} vsSource current vsSource
|
|
* @return {Array<any>} the updated programInfo and shaderProgram
|
|
*/
|
|
export function changeFragmentShader(context: any,
|
|
programInfo: any,
|
|
shaderProgram: any,
|
|
shaders: any,
|
|
fsSource: string,
|
|
vsSource: string) {
|
|
context.gl.deleteShader(shaders.frag);
|
|
shaderProgram = initShaderProgram(context,
|
|
shaderProgram,
|
|
shaders.frag,
|
|
shaders.vert,
|
|
vsSource,
|
|
fsSource);
|
|
programInfo = setProgramInfo(context, shaderProgram);
|
|
return [programInfo, shaderProgram];
|
|
}
|