export enum ShaderType {
|
|
VERTEX,
|
|
FRAGMENT,
|
|
}
|
|
|
|
export const createShader = (
|
|
gl: WebGLRenderingContext,
|
|
type: ShaderType,
|
|
source: string
|
|
): WebGLShader | null => {
|
|
const SHADER_TYPE_TO_GL_TYPE = {
|
|
[ShaderType.VERTEX]: gl.VERTEX_SHADER,
|
|
[ShaderType.FRAGMENT]: gl.FRAGMENT_SHADER,
|
|
};
|
|
const shader = gl.createShader(SHADER_TYPE_TO_GL_TYPE[type]);
|
|
|
|
gl.shaderSource(shader, source);
|
|
gl.compileShader(shader);
|
|
|
|
const success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
|
|
if (!success) {
|
|
console.error(gl.getShaderInfoLog(shader));
|
|
gl.deleteShader(shader);
|
|
return null;
|
|
}
|
|
|
|
return shader;
|
|
};
|
|
|
|
export const createProgram = (
|
|
gl: WebGLRenderingContext,
|
|
vertexShader: WebGLShader,
|
|
fragmentShader: WebGLShader
|
|
): WebGLProgram | null => {
|
|
const program = gl.createProgram();
|
|
|
|
gl.attachShader(program, vertexShader);
|
|
gl.attachShader(program, fragmentShader);
|
|
gl.linkProgram(program);
|
|
|
|
const success = gl.getProgramParameter(program, gl.LINK_STATUS);
|
|
if (!success) {
|
|
console.error(gl.getProgramInfoLog(program));
|
|
gl.deleteProgram(program);
|
|
return null;
|
|
}
|
|
|
|
return program;
|
|
};
|