-
Notifications
You must be signed in to change notification settings - Fork 0
/
shaderManager.js
58 lines (49 loc) · 1.39 KB
/
shaderManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"use strict";
var ShaderManager = function( )
{
this.program;
this.shaders = [ ];
};
ShaderManager.prototype.createShader = function( source, shaderType )
{
var shader = this.compileShader ( source, shaderType );
this.shaders.push( shader );
};
ShaderManager.prototype.compileShader = function( shaderSource, shaderType )
{
if( shaderSource === null )
this.throwError( shaderSource + "null or undefined");
var compiledShader;
compiledShader = gl.createShader( shaderType );
gl.shaderSource( compiledShader, shaderSource );
gl.compileShader( compiledShader );
if (!gl.getShaderParameter( compiledShader, gl.COMPILE_STATUS ))
{
this.throwError("Error compiling shader:"
+ gl.getShaderInfoLog(compiledShader)
+ shaderSource );
}
return compiledShader;
};
ShaderManager.prototype.createProg = function( )
{
this.program = gl.createProgram();
for (var i = 0; i < this.shaders.length; i++)
gl.attachShader(this.program, this.shaders[i]);
};
ShaderManager.prototype.linkProg = function( )
{
gl.linkProgram(this.program);
if (!gl.getProgramParameter(this.program, gl.LINK_STATUS))
this.throwError("Error in program linking:" + gl.getProgramInfoLog(this.mCompiledShader));
};
ShaderManager.prototype.useProg = function()
{
gl.useProgram(this.program);
};
ShaderManager.prototype.throwError = function( message )
{
alert("ERROR");
console.warn( message );
throw "SHADER ERROR";
};