summaryrefslogtreecommitdiffstats
path: root/src/view/renderer
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2018-12-08 12:39:18 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2018-12-08 12:39:18 +0100
commitb3950330e3351437f153c6c1debb3821d6e28864 (patch)
tree0b381b523045bd59cd679825a11976a45813fc24 /src/view/renderer
parent439dcf391784ea3abb61473c74b9c27fcd9fdc2d (diff)
downloadrpgedit-b3950330e3351437f153c6c1debb3821d6e28864.tar
rpgedit-b3950330e3351437f153c6c1debb3821d6e28864.zip
Create Electron app
Diffstat (limited to 'src/view/renderer')
-rw-r--r--src/view/renderer/renderer.ts99
-rw-r--r--src/view/renderer/shaders.ts79
-rw-r--r--src/view/renderer/shaders/default.fs10
-rw-r--r--src/view/renderer/shaders/default.vs13
4 files changed, 0 insertions, 201 deletions
diff --git a/src/view/renderer/renderer.ts b/src/view/renderer/renderer.ts
deleted file mode 100644
index 93f8589..0000000
--- a/src/view/renderer/renderer.ts
+++ /dev/null
@@ -1,99 +0,0 @@
-import { Shaders } from './shaders';
-
-import { mat4, vec2 } from 'gl-matrix';
-
-export class Renderer {
- public readonly coordScale = 32;
- private readonly viewScale = 2;
-
- private readonly gl: WebGLRenderingContext;
- private readonly shaders: Shaders;
-
- private readonly center: vec2 = vec2.create();
- private readonly translation: vec2 = vec2.create();
- 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.gl.enable(this.gl.BLEND);
- this.gl.blendFunc(this.gl.ONE, this.gl.ONE_MINUS_SRC_ALPHA);
- this.gl.pixelStorei(this.gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 1);
-
- 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;
- }
-
- public setCenter(v: vec2|number[]) {
- this.snapToGrid(this.center, v);
- }
-
- public setTranslation(v: vec2|number[]) {
- vec2.sub(this.translation, v, this.center);
- this.snapToGrid(this.translation, this.translation);
- this.gl.uniform2fv(this.shaders.translateLoc, this.translation);
- }
-
- public clear(): void {
- this.gl.clear(this.gl.COLOR_BUFFER_BIT);
-
- this.setTranslation([0, 0]);
- }
-
- public snapToGrid(out: vec2, a: vec2|number[]): void {
- vec2.scale(out, a, this.coordScale);
- vec2.round(out, out);
- vec2.scale(out, out, 1 / this.coordScale);
- }
-
- 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.clear();
-
- const scale = this.viewScale * this.coordScale;
-
- mat4.identity(this.viewport);
- mat4.scale(this.viewport, this.viewport, [2 * scale / w, -2 * scale / h, 1.0]);
- this.gl.uniformMatrix4fv(this.shaders.viewportLoc, false, this.viewport);
- }
-}
diff --git a/src/view/renderer/shaders.ts b/src/view/renderer/shaders.ts
deleted file mode 100644
index 8fd1fda..0000000
--- a/src/view/renderer/shaders.ts
+++ /dev/null
@@ -1,79 +0,0 @@
-import fragmentShaderSrc from './shaders/default.fs';
-import vertexShaderSrc from './shaders/default.vs';
-
-export 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/shaders/default.fs b/src/view/renderer/shaders/default.fs
deleted file mode 100644
index 2c8ad15..0000000
--- a/src/view/renderer/shaders/default.fs
+++ /dev/null
@@ -1,10 +0,0 @@
-precision highp float;
-
-varying vec2 vTextureCoord;
-
-uniform sampler2D uSampler;
-
-
-void main(void) {
- gl_FragColor = texture2D(uSampler, vTextureCoord);
-}
diff --git a/src/view/renderer/shaders/default.vs b/src/view/renderer/shaders/default.vs
deleted file mode 100644
index 4715a17..0000000
--- a/src/view/renderer/shaders/default.vs
+++ /dev/null
@@ -1,13 +0,0 @@
-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;
-}