Repository navigation
Expand file tree
/
Copy pathshader-program.js
More file actions
170 lines (147 loc) · 4.59 KB
/
Copy pathshader-program.js
File metadata and controls
170 lines (147 loc) · 4.59 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
export class ShaderProgram {
constructor(gl){
this.vs = undefined;
this.fs = undefined;
this.gl = gl;
this.uniforms = {};
this.attributes = {};
this.program = undefined;
this.verticies = undefined;
this.indices = undefined;
this.buffers = {};
this.texture = undefined;
this.vao = undefined;
this.fbo = undefined;
}
create_fbo() {
this.fbo = this.gl.createFramebuffer();
return this;
}
get_fbo() {
return this.fbo;
}
create_vao() {
this.vao = this.gl.createVertexArray();
return this;
}
get_vao() {
return this.vao;
}
create_texture() {
this.texture = this.gl.createTexture();
return this;
}
get_texture() {
if (this.texture === undefined) {
throw new Error("Failed to get texture. Call create_texture() first.");
}
return this.texture;
}
get_indices() {
return this.indices;
}
set_indices(indices) {
this.indices = indices;
return this;
}
set_verticies(verts) {
this.verticies = verts;
return this;
}
get_verticies() {
return this.verticies;
}
create_buffer(key) {
this.buffers[key] = this.gl.createBuffer();
return this;
}
get_buffer(key) {
if (!(key in this.buffers)) {
throw new Error("Failed to get buffer. Call create_buffer(key) first.");
}
return this.buffers[key];
}
add_vertex_shader(source) {
this.vs = this.compile_shader(source, this.gl.VERTEX_SHADER);
return this;
}
add_fragment_shader(source) {
this.fs = this.compile_shader(source, this.gl.FRAGMENT_SHADER);
return this;
}
build() {
this.program = this.link_program();
return this;
}
get_uniform(uniform) {
if (!(uniform in this.uniforms)) {
throw new Error(`Failed to get uniform: ${uniform}.`);
}
return this.uniforms[uniform];
}
get_attribute(attr) {
if (!(attr in this.attributes)) {
throw new Error(`Failed to get attribute: ${attr}.`);
}
return this.attributes[attr];
}
get_program() {
return this.program;
}
build_uniforms(uniforms) {
if (this.program === undefined) {
throw new Error("Failed to get uniforms: missing program. Call build() before getting uniforms.");
}
uniforms.forEach(name => {
this.uniforms[name] = this.gl.getUniformLocation(this.program, name);
});
return this;
}
build_attributes(attrs) {
if (this.program === undefined) {
throw new Error("Failed to get attributes: missing program. Call build() before getting attributes.");
}
attrs.forEach(name => {
this.attributes[name] = this.gl.getAttribLocation(this.program, name);
});
return this;
}
compile_shader(source, type) {
const shader = this.gl.createShader(type);
this.gl.shaderSource(shader, source);
this.gl.compileShader(shader);
if (!this.gl.getShaderParameter(shader, this.gl.COMPILE_STATUS)) {
console.error(this.gl.getShaderInfoLog(shader));
const shaderType = this.shader_type_to_string(shader);
this.gl.deleteShader(shader);
throw new Error(`Failed to compile ${shaderType} shader.`);
}
return shader;
}
link_program() {
if (this.vs === undefined) {
throw new Error("Failed to link program: missing vertex shader. Call add_vertex_shader() before build.");
} else if (this.fs === undefined) {
throw new Error("Failed to link program: missing fragment shader. Call add_fragment_shader() before build.");
}
const program = this.gl.createProgram();
this.gl.attachShader(program, this.vs);
this.gl.attachShader(program, this.fs);
this.gl.linkProgram(program);
if (!this.gl.getProgramParameter(program, this.gl.LINK_STATUS)) {
console.error("Failed to link program:", this.gl.getProgramInfoLog(program));
throw new Error("Linking program has failed");
}
return program;
}
shader_type_to_string(type) {
switch(type) {
case this.gl.FRAGMENT_SHADER:
return "FRAGMENT_SHADER";
case this.gl.VERTEX_SHADER:
return "VERTEX_SHADER";
default:
return "UNKNOWN";
}
}
}