Newer
Older
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
/**
* Utilities for WebGL contexts
*/
export class WebGLUtils {
context: WebGLRenderingContext;
constructor(context: WebGLRenderingContext) {
this.context = context;
}
/**
* Create WebGL texture
*
* Originates from Pxls
*
* @returns WebGL texture
*/
createTexture() {
const texture = this.context.createTexture();
this.context.bindTexture(this.context.TEXTURE_2D, texture);
this.context.texParameteri(
this.context.TEXTURE_2D,
this.context.TEXTURE_WRAP_S,
this.context.CLAMP_TO_EDGE
);
this.context.texParameteri(
this.context.TEXTURE_2D,
this.context.TEXTURE_WRAP_T,
this.context.CLAMP_TO_EDGE
);
this.context.texParameteri(
this.context.TEXTURE_2D,
this.context.TEXTURE_MIN_FILTER,
this.context.NEAREST
);
this.context.texParameteri(
this.context.TEXTURE_2D,
this.context.TEXTURE_MAG_FILTER,
this.context.NEAREST
);
return texture;
}
/**
* Create WebGL program
*
* Originates from Pxls
*
* @param vertexSource
* @param fragmentSource
* @returns WebGL program
*/
createProgram(vertexSource: string, fragmentSource: string) {
// i believe null is only returned when webgl context is destroyed?
// maybe add proper handling here
const program = this.context.createProgram()!;
this.context.attachShader(
program,
this.createShader(this.context.VERTEX_SHADER, vertexSource)
);
this.context.attachShader(
program,
this.createShader(this.context.FRAGMENT_SHADER, fragmentSource)
);
this.context.linkProgram(program);
if (!this.context.getProgramParameter(program, this.context.LINK_STATUS)) {
throw new Error(
`Failed to link WebGL template program:\n\n${this.context.getProgramInfoLog(program)}`
);
}
return program;
}
/**
* Create WebGL shader
*
* Originates from Pxls
*
* @param type
* @param source
* @returns WebGL shader
*/
createShader(type: number, source: string) {
// i believe null is only returned when webgl context is destroyed?
// maybe add proper handling here
const shader = this.context.createShader(type)!;
this.context.shaderSource(shader, source);
this.context.compileShader(shader);
if (!this.context.getShaderParameter(shader, this.context.COMPILE_STATUS)) {
throw new Error(
`Failed to compile WebGL template shader:\n\n${this.context.getShaderInfoLog(shader)}`
);
}
return shader;
}
}