-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShaderProgram.js
85 lines (67 loc) · 2.06 KB
/
ShaderProgram.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"use strict";
function ShaderProgram(vertexCode, fragmentCode)
{
this.errors = []
var vertexShader = this._compileShader(gl.VERTEX_SHADER, vertexCode)
var fragmentShader = this._compileShader(gl.FRAGMENT_SHADER, fragmentCode)
if (!vertexShader || !fragmentShader)
return
this.program = gl.createProgram()
gl.attachShader(this.program, vertexShader)
gl.attachShader(this.program, fragmentShader)
gl.linkProgram(this.program)
gl.deleteShader(vertexShader)
gl.deleteShader(fragmentShader)
if (!gl.getProgramParameter(this.program, gl.LINK_STATUS))
{
this.errors.push("Link failed")
}
}
ShaderProgram.prototype.destroy = function()
{
gl.deleteProgram(this.program)
}
ShaderProgram.prototype.bind = function()
{
gl.useProgram(this.program)
}
ShaderProgram.prototype.getAttributeLocation = function(name)
{
return gl.getAttribLocation(this.program, name);
}
ShaderProgram.prototype.bindAttribLocation = function(index, name)
{
return gl.bindAttribLocation(this.program, index, name);
}
ShaderProgram.prototype.setFloatUniform = function(name, value)
{
var location = gl.getUniformLocation(this.program, name)
gl.uniform1f(location, value)
}
ShaderProgram.prototype.setVec2Uniform = function(name, value)
{
var location = gl.getUniformLocation(this.program, name)
gl.uniform2f(location, value[0], value[1])
}
ShaderProgram.prototype.setVec3Uniform = function(name, value)
{
var location = gl.getUniformLocation(this.program, name)
gl.uniform3f(location, value[0], value[1], value[2])
}
ShaderProgram.prototype.setMat4Uniform = function(name, value)
{
var location = gl.getUniformLocation(this.program, name)
gl.uniformMatrix4fv(location, false, value);
}
ShaderProgram.prototype._compileShader = function(type, code)
{
var shader = gl.createShader(type)
gl.shaderSource(shader, code)
gl.compileShader(shader)
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS))
{
this.errors.push(gl.getShaderInfoLog(shader))
return null
}
return shader
}