summaryrefslogtreecommitdiffstats
path: root/src/view/renderer
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2018-10-24 23:05:13 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2018-10-24 23:05:13 +0200
commitebc56db63c054702ad910987aa666d7cfb52d5cc (patch)
tree1fc08dcb6b84edd9102770d20bfe351fa02cc3f9 /src/view/renderer
parent9770eaf432bacff9de1b9911adef1db368b8cc70 (diff)
downloadrpgedit-ebc56db63c054702ad910987aa666d7cfb52d5cc.tar
rpgedit-ebc56db63c054702ad910987aa666d7cfb52d5cc.zip
Move shader initialization out of renderer
Avoid public fields and ! overrides, make almost all fields readonly.
Diffstat (limited to 'src/view/renderer')
-rw-r--r--src/view/renderer/Renderer.ts100
-rw-r--r--src/view/renderer/Shaders.ts79
-rw-r--r--src/view/renderer/default.fs8
-rw-r--r--src/view/renderer/default.vs13
4 files changed, 200 insertions, 0 deletions
diff --git a/src/view/renderer/Renderer.ts b/src/view/renderer/Renderer.ts
new file mode 100644
index 0000000..6e3897d
--- /dev/null
+++ b/src/view/renderer/Renderer.ts
@@ -0,0 +1,100 @@
+import {mat4} from 'gl-matrix';
+
+import Shaders from './Shaders';
+
+export default class Renderer {
+ private readonly gl: WebGLRenderingContext;
+ private readonly shaders: Shaders;
+ private readonly viewport: mat4 = mat4.create();
+
+ constructor(private readonly canvas: HTMLCanvasElement) {
+ this.gl = this.mkContext();
+
+ this.shaders = new Shaders(this.gl);
+
+ this.gl.clearColor(0.0, 0.0, 0.0, 1.0);
+
+ this.setSize();
+ }
+
+ public createBuffer(): WebGLBuffer {
+ const ret = this.gl.createBuffer();
+ if (!ret)
+ throw new Error('unable to create buffer');
+
+ return ret;
+ }
+
+ public getContext(): WebGLRenderingContext {
+ return this.gl;
+ }
+
+ public getVertexPosLoc(): number {
+ return this.shaders.vertexPosLoc;
+ }
+
+ public getTextureCoordLoc(): number {
+ return this.shaders.textureCoordLoc;
+ }
+
+ public getSamplerLoc(): WebGLUniformLocation {
+ return this.shaders.samplerLoc;
+ }
+
+ private mkContext(): WebGLRenderingContext {
+ const gl = (
+ this.canvas.getContext('webgl') || this.canvas.getContext('experimental-webgl')
+ );
+ if (!gl)
+ throw new Error('unable to initialize WebGL context');
+
+ return gl;
+ }
+
+ private setSize(): void {
+ 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);
+
+ mat4.identity(this.viewport);
+ mat4.scale(this.viewport, this.viewport, [2 * 64 / w, -2 * 64 / h, 1.0]);
+ this.gl.uniformMatrix4fv(this.shaders.viewportLoc, false, this.viewport);
+
+ this.gl.uniform2f(this.shaders.translateLoc, -5.0, -5.0);
+ }
+
+ 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 + "'");
+
+ return ret;
+ }
+
+ 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 + "'");
+
+ 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;
+ }
+}
diff --git a/src/view/renderer/Shaders.ts b/src/view/renderer/Shaders.ts
new file mode 100644
index 0000000..da75a18
--- /dev/null
+++ b/src/view/renderer/Shaders.ts
@@ -0,0 +1,79 @@
+import fragmentShaderSrc from './default.fs';
+import vertexShaderSrc from './default.vs';
+
+export default class Shaders {
+ public readonly viewportLoc: WebGLUniformLocation;
+ public readonly translateLoc: WebGLUniformLocation;
+
+ public readonly vertexPosLoc: number;
+ public readonly textureCoordLoc: number;
+ public readonly samplerLoc: WebGLUniformLocation;
+
+ constructor(private readonly gl: WebGLRenderingContext) {
+ const shaderProgram = this.gl.createProgram();
+ if (!shaderProgram)
+ throw new Error('Unable to create shader program');
+
+ 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)) {
+ const 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 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;
+ }
+
+ 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 + "'");
+
+ return ret;
+ }
+
+ 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 + "'");
+
+ return ret;
+ }
+}
diff --git a/src/view/renderer/default.fs b/src/view/renderer/default.fs
new file mode 100644
index 0000000..351fed7
--- /dev/null
+++ b/src/view/renderer/default.fs
@@ -0,0 +1,8 @@
+varying highp vec2 vTextureCoord;
+
+uniform sampler2D uSampler;
+
+
+void main(void) {
+ gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.x, vTextureCoord.y));
+}
diff --git a/src/view/renderer/default.vs b/src/view/renderer/default.vs
new file mode 100644
index 0000000..4715a17
--- /dev/null
+++ b/src/view/renderer/default.vs
@@ -0,0 +1,13 @@
+attribute vec2 aVertexPos;
+attribute vec2 aTextureCoord;
+
+uniform mat4 uViewport;
+uniform vec2 uTranslate;
+
+varying highp vec2 vTextureCoord;
+
+
+void main(void) {
+ gl_Position = uViewport * vec4(aVertexPos + uTranslate, 0.0, 1.0);
+ vTextureCoord = aTextureCoord;
+}