UNPKG

gl2d

Version:

2D graphics package for WebGL

135 lines 5.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * Creates an array buffer with the specified size. * @param gl the WebGL context. * @param size the size of the array buffer, or the initial data for the buffer. * @param usage one of gl.STATIC_DRAW (often used, seldom changed), gl.DYNAMIC_DRAW (often used, often changed), or gl.STREAM_DRAW (seldom used). */ function createArrayBuffer(gl, size, usage) { if (usage === void 0) { usage = gl.STATIC_DRAW; } var buffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, buffer); gl.bufferData(gl.ARRAY_BUFFER, size, usage); return buffer; } exports.createArrayBuffer = createArrayBuffer; /** * Creates an element buffer with the specified size. * @param gl the WebGL context. * @param size the size of the element buffer, or the initial data for the buffer. * @param usage one of gl.STATIC_DRAW (often used, seldom changed), gl.DYNAMIC_DRAW (often used, often changed), or gl.STREAM_DRAW (seldom used). */ function createElementBuffer(gl, size, usage) { if (usage === void 0) { usage = gl.STATIC_DRAW; } var buffer = gl.createBuffer(); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, size, usage); return buffer; } exports.createElementBuffer = createElementBuffer; /** * Creates a program from 2 shaders. * @param gl the WebGL context. * @param vertexShaderSource string containing code for the vertex shader. * @param fragmentShaderSource string containing code for the fragment shader. * @returns the program. */ function createProgramFromSources(gl, vertexShaderSource, fragmentShaderSource) { // Compile vertex and fragment shader var vs = compileShader(gl, vertexShaderSource, gl.VERTEX_SHADER); var fs = compileShader(gl, fragmentShaderSource, gl.FRAGMENT_SHADER); // Create program and return return createProgramFromShaders(gl, vs, fs); } exports.createProgramFromSources = createProgramFromSources; ; /** * Creates a program from 2 shaders. * @param gl rhe WebGL context. * @param vertexShader a compiled vertex shader. * @param fragmentShader a compiled fragment shader. * @returns the program. */ function createProgramFromShaders(gl, vertexShader, fragmentShader) { // create a program. var program = gl.createProgram(); // attach the shaders. gl.attachShader(program, vertexShader); gl.attachShader(program, fragmentShader); // link the program. gl.linkProgram(program); // Check if it linked. var success = gl.getProgramParameter(program, gl.LINK_STATUS); if (!success) { // something went wrong with the link throw ("program filed to link:" + gl.getProgramInfoLog(program)); } return program; } exports.createProgramFromShaders = createProgramFromShaders; ; /** * Creates and compiles a shader. * @param gl the WebGL Context. * @param shaderSource the GLSL source code for the shader. * @param shaderType the type of shader, VERTEX_SHADER or FRAGMENT_SHADER. * @returns the shader. */ function compileShader(gl, shaderSource, shaderType) { // Create the shader object var shader = gl.createShader(shaderType); // Set the shader source code. gl.shaderSource(shader, shaderSource); // Compile the shader gl.compileShader(shader); // Check if it compiled var success = gl.getShaderParameter(shader, gl.COMPILE_STATUS); if (!success) { // Something went wrong during compilation; get the error throw "could not compile shader:" + gl.getShaderInfoLog(shader); } return shader; } exports.compileShader = compileShader; /** * Gets the location of each of the uniforms associated with the specified program. */ function getUniformLocations(gl, program, renamed) { var uniforms = {}; if (renamed) { for (var name_1 in renamed) { uniforms[name_1] = gl.getUniformLocation(program, renamed[name_1]); } } else { var count = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS); for (var i = 0; i < count; i++) { var name_2 = gl.getActiveUniform(program, i).name; uniforms[name_2] = gl.getUniformLocation(program, name_2); } } return uniforms; } exports.getUniformLocations = getUniformLocations; /** * Gets the location of each of the attributes associated with the specified program. */ function getAttributeLocations(gl, program, renamed) { var attribs = {}; if (renamed) { for (var name_3 in renamed) { attribs[name_3] = gl.getAttribLocation(program, renamed[name_3]); } } else { var count = gl.getProgramParameter(program, gl.ACTIVE_ATTRIBUTES); for (var i = 0; i < count; i++) { var name_4 = gl.getActiveAttrib(program, i).name; attribs[name_4] = gl.getAttribLocation(program, name_4); } } return attribs; } exports.getAttributeLocations = getAttributeLocations; //# sourceMappingURL=util.js.map