Skip to content
Snippets Groups Projects
Select Git revision
  • 9aed84e8b11d027cc8226e889c8e9bad95f63efb
  • master default protected
2 results

main.o

Blame
  • WebglInstance.js 3.69 KiB
    /**
     * Object that contains the WebGl context and some utilities
     * @param {Element} canvas HTML Canvas object where our 3D scene will be drawn
     * @constructor
     * 	- Get WebGl context and store it in the attribute "gl"
     * 	- Set the clear color of the canvas
     * 	- Enable the depth test
     */
    function WebglInstance(canvas) {
    	this.gl = getWebGLContext(canvas);
    	if (!this.gl)
    		throw 'Failed to get the rendering context for WebglInstance';
    
    	// Set the clear color and enable the depth test
    	this.gl.clearColor(0.0, 0.0, 0.0, 1.0);
    	this.gl.enable(this.gl.DEPTH_TEST);
    }
    
    /**
     * Initialize and build the shader program based on the vertex and fragment shader
     * @param {string} vertexShader Containing the vertex shader
     * @param {string} fragmentShader Containing the fragment shader
     */
    WebglInstance.prototype.initShaders = function(vertexShader, fragmentShader) {
    	if (!initShaders(this.gl, vertexShader, fragmentShader))
    		throw 'Failed to initialize shaders.';
    };
    
    /**
     * Write the vertex coordinates and colors to the buffer object
     * @param {YourLetter} shape Object containing 3 attributes: vertices, indices and colors (see YourLetter.js)
     */
    WebglInstance.prototype.writeObject = function(shape) {
    	// Pass the shape positions to the vertex shader
    	this.writeArrayBuffer(new Float32Array([...shape.vertices]), 3, this.gl.FLOAT, 'a_Position');
    
    	// Pass the colors to the vertex shader
    	this.writeArrayBuffer(new Float32Array([...shape.colors]), 3, this.gl.FLOAT, 'a_Color');
    
    	// Write the indices to the buffer object
    	const indexBuffer = this.gl.createBuffer();
    	if (!indexBuffer)
    		throw 'Failed to createBuffer';
    
    	// Write the indices to the buffer object
    	this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
    	this.gl.bufferData(this.gl.ELEMENT_ARRAY_BUFFER, new Uint8Array([...shape.indices]), this.gl.STATIC_DRAW);
    
    	this.indicesCount = shape.indices.length;
    };
    
    /**
     * Write array to the vertex object buffer
     * @param {Float32Array} array Data to write
     * @param {GLint} size Size of each vertex (3 in our case xyz for positions and rgb for colors
     * @param {GLenum} type Type of each value in the array
     * @param {string} attribute Attribute variable name
     */
    WebglInstance.prototype.writeArrayBuffer = function(array, size, type, attribute) {
    	const buffer = this.gl.createBuffer();   // Create a buffer object
    	if (!buffer)
    		throw 'Failed to create the buffer object';
    
    	// Write date into the buffer object
    	this.gl.bindBuffer(this.gl.ARRAY_BUFFER, buffer);
    	this.gl.bufferData(this.gl.ARRAY_BUFFER, array, this.gl.STATIC_DRAW);
    	// Assign the buffer object to the attribute variable
    	const a_attribute = this.gl.getAttribLocation(this.gl.program, attribute);
    	if (a_attribute < 0)
    		throw 'Failed to get the storage location of ' + attribute;
    
    	this.gl.vertexAttribPointer(a_attribute, size, type, false, 0, 0);
    	// Enable the assignment of the buffer object to the attribute variable
    	this.gl.enableVertexAttribArray(a_attribute);
    };
    
    /**
     * Bind and pass a matrix to a uniform varibale of the vertex shader
     * @param {Matrix4} matrix Data to be passed to the uniform
     * @param {string} u_uniform Uniform variable name
     */
    WebglInstance.prototype.writeUniformMatrix = function(matrix, u_uniform) {
    	// TODO:
    	// 	- Get the location of the uniform variable in the shader program. Hint: use getUniformLocation() function)
    	// 	- Pass the matrix to the uniform variable. Hint: use uniformMatrix4fv() function)
    };
    
    /**
     * Clear the output buffer
     */
    WebglInstance.prototype.clear = function() {
    	this.gl.clear(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT);
    };
    
    /**
     * Render from array buffer data
     */
    WebglInstance.prototype.draw = function() {
    	this.gl.drawElements(this.gl.TRIANGLES, this.indicesCount, this.gl.UNSIGNED_BYTE, 0);
    };