/*
* File: SimpleShader.js
*
* Implements a SimpleShader object.
*
*/
/*jslint node: true, vars: true */
/*global gEngine: false, document: false, alert: false */
/* find out more about jslint: http://www.jslint.com/help.html */
"use strict"; // Operate in Strict mode such that variables must be declared before used!
//
// constructor of SimpleShader object
function SimpleShader(vertexShaderID, fragmentShaderID) {
// instance variables
// Convention: all instance variables: mVariables
this.mCompiledShader = null; // reference to the compiled shader in webgl context
this.mShaderVertexPositionAttribute = null; // reference to SquareVertexPosition within the shader
var gl = gEngine.Core.getGL();
// start of constructor code
//
// Step A: load and compile vertex and fragment shaders
var vertexShader = this._loadAndCompileShader(vertexShaderID, gl.VERTEX_SHADER);
var fragmentShader = this._loadAndCompileShader(fragmentShaderID, gl.FRAGMENT_SHADER);
// Step B: Create and link the shaders into a program.
this.mCompiledShader = gl.createProgram();
gl.attachShader(this.mCompiledShader, vertexShader);
gl.attachShader(this.mCompiledShader, fragmentShader);
gl.linkProgram(this.mCompiledShader);
// Step C: check for error
if (!gl.getProgramParameter(this.mCompiledShader, gl.LINK_STATUS)) {
alert("Error linking shader");
return null;
}
// Step D: Gets a reference to the aSquareVertexPosition attribute within the shaders.
this.mShaderVertexPositionAttribute = gl.getAttribLocation(
this.mCompiledShader, "aSquareVertexPosition");
}
//
//
// Access to the compiled shader
SimpleShader.prototype.getShader = function () { return this.mCompiledShader; };
// Activate the shader for rendering
SimpleShader.prototype.activateShader = function () {
var gl = gEngine.Core.getGL();
gl.useProgram(this.mCompiledShader);
// Step E: Activates the vertex buffer loaded in EngineCore_VertexBuffer.js
gl.bindBuffer(gl.ARRAY_BUFFER, gEngine.VertexBuffer.getGLVertexRef());
// Step F: Describe the characteristic of the vertex position attribute
gl.vertexAttribPointer(this.mShaderVertexPositionAttribute,
3, // each element is a 3-float (x,y.z)
gl.FLOAT, // data type is FLOAT
false, // if the content is normalized vectors
0, // number of bytes to skip in between elements
0); // offsets to the first element\
gl.enableVertexAttribArray(this.mShaderVertexPositionAttribute);
};
//-- end of public methods
//
//
//**-----------------------------------
// Private methods not mean to call by outside of this object
// naming convention: starts with an "_"
// **------------------------------------
//
// Returns a compiled shader from a shader in the dom.
// The id is the id of the script in the html tag.
SimpleShader.prototype._loadAndCompileShader = function (id, shaderType) {
var shaderText, shaderSource, compiledShader;
var gl = gEngine.Core.getGL();
// Step A: Get the shader source from index.html
shaderText = document.getElementById(id);
shaderSource = shaderText.firstChild.textContent;
// Step B: Create the shader based on the shader type: vertex or fragment
compiledShader = gl.createShader(shaderType);
// Step C: Compile the created shader
gl.shaderSource(compiledShader, shaderSource);
gl.compileShader(compiledShader);
// Step D: check for errors and return results (null if error)
// The log info is how shader compilation errors are typically displayed.
// This is useful for debugging the shaders.
if (!gl.getShaderParameter(compiledShader, gl.COMPILE_STATUS)) {
alert("A shader compiling error occurred: " + gl.getShaderInfoLog(compiledShader));
}
return compiledShader;
};
//-- end of private methods
//