diff options
Diffstat (limited to 'src/view/Renderer.ts')
-rw-r--r-- | src/view/Renderer.ts | 102 |
1 files changed, 51 insertions, 51 deletions
diff --git a/src/view/Renderer.ts b/src/view/Renderer.ts index 9b7bfe3..ae3c6e7 100644 --- a/src/view/Renderer.ts +++ b/src/view/Renderer.ts @@ -1,85 +1,62 @@ import {mat4} from 'gl-matrix'; - -import vertexShaderSrc from './default.vs'; import fragmentShaderSrc from './default.fs'; - +import vertexShaderSrc from './default.vs'; 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; - } + private viewportLoc!: WebGLUniformLocation; + private translateLoc!: WebGLUniformLocation; - 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 + "'"); + constructor(private canvas: HTMLCanvasElement) { + this.gl = this.mkContext(); - return ret; - } + this.initShaders(); - 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 + "'"); + this.gl.clearColor(0.0, 0.0, 0.0, 1.0); - return ret; + this.setSize(); } - createBuffer(): WebGLBuffer { - let ret = this.gl.createBuffer(); + public createBuffer(): WebGLBuffer { + const 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); - } + private mkContext(): WebGLRenderingContext { + const gl = ( + this.canvas.getContext('webgl') || this.canvas.getContext('experimental-webgl') + ) as WebGLRenderingContext|null; + if (!gl) + throw new Error('unable to initialize WebGL context'); - return shader; + return gl; } private initShaders(): void { - let shaderProgram = this.gl.createProgram(); + const 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); - + const vertexShader = this.compileShader(this.gl.VERTEX_SHADER, vertexShaderSrc); + const 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); + const err = this.gl.getProgramInfoLog(shaderProgram); this.gl.deleteShader(vertexShader); this.gl.deleteShader(fragmentShader); @@ -102,8 +79,8 @@ export default class Renderer { } private setSize(): void { - let w = this.canvas.width; - let h = this.canvas.height; + const w = this.canvas.width; + const h = this.canvas.height; this.gl.viewport(0, 0, w, h); this.gl.clear(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT); @@ -115,13 +92,36 @@ export default class Renderer { this.gl.uniform2f(this.translateLoc, -5.0, -5.0); } - constructor(private canvas: HTMLCanvasElement) { - this.gl = this.mkContext(); + private getAttribLocation(program: WebGLProgram, name: string): number { + const ret = this.gl.getAttribLocation(program, name); + if (ret < 0) + throw new Error("unable to get location of attribute '" + name + "'"); - this.initShaders(); + return ret; + } - this.gl.clearColor(0.0, 0.0, 0.0, 1.0); + private getUniformLocation(program: WebGLProgram, name: string): WebGLUniformLocation { + const ret = this.gl.getUniformLocation(program, name); + if (!ret) + throw new Error("unable to get location of uniform '" + name + "'"); - this.setSize(); + return ret; + } + + private compileShader(type: number, src: string): WebGLShader { + const 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)) { + const err = this.gl.getShaderInfoLog(shader); + this.gl.deleteShader(shader); + throw new Error('Unable to compile shader: ' + err); + } + + return shader; } } |