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
|
import {mat4} from 'gl-matrix';
import vertexShaderSrc from './default.vs';
import fragmentShaderSrc from './default.fs';
export default class Renderer {
public gl: WebGLRenderingContext;
public vertexPosLoc!: number;
public textureCoordLoc!: number;
private viewportLoc!: WebGLUniformLocation;
private translateLoc!: WebGLUniformLocation;
public samplerLoc!: WebGLUniformLocation;
private viewport: mat4 = mat4.create();
private mkContext(): WebGLRenderingContext {
let gl = (this.canvas.getContext('webgl') || this.canvas.getContext('experimental-webgl')) as WebGLRenderingContext|null;
if (!gl)
throw new Error('unable to initialize WebGL context');
return gl;
}
getAttribLocation(program: WebGLProgram, name: string): number {
let ret = this.gl.getAttribLocation(program, name);
if (ret < 0)
throw new Error('unable to get location of attribute \'' + name + '\'');
return ret;
}
getUniformLocation(program: WebGLProgram, name: string): WebGLUniformLocation {
let ret = this.gl.getUniformLocation(program, name);
if (!ret)
throw new Error('unable to get location of uniform \'' + name + '\'');
return ret;
}
createBuffer(): WebGLBuffer {
let ret = this.gl.createBuffer();
if (!ret)
throw new Error('unable to create buffer');
return ret;
}
compileShader(type: number, src: string): WebGLShader {
let shader = this.gl.createShader(type);
if (!shader)
throw new Error('Unable to create shader');
this.gl.shaderSource(shader, src);
this.gl.compileShader(shader);
if (!this.gl.getShaderParameter(shader, this.gl.COMPILE_STATUS)) {
let err = this.gl.getShaderInfoLog(shader);
this.gl.deleteShader(shader);
throw new Error('Unable to compile shader: ' + err);
}
return shader;
}
private initShaders(): void {
let shaderProgram = this.gl.createProgram();
if (!shaderProgram)
throw new Error('Unable to create shader program');
let vertexShader = this.compileShader(this.gl.VERTEX_SHADER, vertexShaderSrc);
let fragmentShader = this.compileShader(this.gl.FRAGMENT_SHADER, fragmentShaderSrc);
this.gl.attachShader(shaderProgram, vertexShader);
this.gl.attachShader(shaderProgram, fragmentShader);
this.gl.linkProgram(shaderProgram);
if (!this.gl.getProgramParameter(shaderProgram, this.gl.LINK_STATUS)) {
let err = this.gl.getProgramInfoLog(shaderProgram);
this.gl.deleteShader(vertexShader);
this.gl.deleteShader(fragmentShader);
this.gl.deleteProgram(shaderProgram);
throw new Error('Unable to link shader: ' + err);
}
this.gl.useProgram(shaderProgram);
this.vertexPosLoc = this.getAttribLocation(shaderProgram, 'aVertexPos');
this.gl.enableVertexAttribArray(this.vertexPosLoc);
this.textureCoordLoc = this.getAttribLocation(shaderProgram, 'aTextureCoord');
this.gl.enableVertexAttribArray(this.textureCoordLoc);
this.viewportLoc = this.getUniformLocation(shaderProgram, 'uViewport');
this.translateLoc = this.getUniformLocation(shaderProgram, 'uTranslate');
this.samplerLoc = this.getUniformLocation(shaderProgram, 'uSampler');
}
private setSize(): void {
let w = this.canvas.width;
let h = this.canvas.height;
this.gl.viewport(0, 0, w, h);
this.gl.clear(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT);
mat4.identity(this.viewport);
mat4.scale(this.viewport, this.viewport, [2 * 64 / w, -2 * 64 / h, 1.0]);
this.gl.uniformMatrix4fv(this.viewportLoc, false, this.viewport);
this.gl.uniform2f(this.translateLoc, -5.0, -5.0);
}
constructor(private canvas: HTMLCanvasElement) {
this.gl = this.mkContext();
this.initShaders();
this.gl.clearColor(0.0, 0.0, 0.0, 1.0);
this.setSize();
}
}
|