From a5e69edc5a6f1a95618c04e214d39b397577d796 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 11 Sep 2017 23:24:13 +0200 Subject: Code structuring Also, use a fixed-size canvas for now. --- src/view/Renderer.ts | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/view/Scene.ts | 41 +++++++++++++++++ src/view/default.fs | 3 ++ src/view/default.vs | 8 ++++ 4 files changed, 176 insertions(+) create mode 100644 src/view/Renderer.ts create mode 100644 src/view/Scene.ts create mode 100644 src/view/default.fs create mode 100644 src/view/default.vs (limited to 'src/view') diff --git a/src/view/Renderer.ts b/src/view/Renderer.ts new file mode 100644 index 0000000..556cfe3 --- /dev/null +++ b/src/view/Renderer.ts @@ -0,0 +1,124 @@ +import {mat4} from 'gl-matrix'; + + +class Renderer { + public gl: WebGLRenderingContext; + + public vertexPosLoc: number; + private viewportLoc: WebGLUniformLocation; + private translateLoc: WebGLUniformLocation; + + private viewport: mat4 = mat4.create(); + private translate: 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, require('./default.vs')); + let fragmentShader = this.compileShader(this.gl.FRAGMENT_SHADER, require('./default.fs')); + + + 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, 'vertexPos'); + this.gl.enableVertexAttribArray(this.vertexPosLoc); + + this.viewportLoc = this.getUniformLocation(shaderProgram, 'viewport'); + this.translateLoc = this.getUniformLocation(shaderProgram, 'translate'); + } + + 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, [64 / w, 64 / h, 1.0]); + this.gl.uniformMatrix4fv(this.viewportLoc, false, this.viewport); + + mat4.identity(this.translate); + this.gl.uniformMatrix4fv(this.translateLoc, false, this.translate); + } + + constructor(private canvas: HTMLCanvasElement) { + this.gl = this.mkContext(); + + this.initShaders(); + + this.gl.clearColor(0.0, 0.0, 0.0, 1.0); + this.gl.enable(this.gl.DEPTH_TEST); + + this.setSize(); + } +} + +export default Renderer; diff --git a/src/view/Scene.ts b/src/view/Scene.ts new file mode 100644 index 0000000..0de7546 --- /dev/null +++ b/src/view/Scene.ts @@ -0,0 +1,41 @@ +import Renderer from './Renderer'; + +class Scene { + private triangleVertexPositionBuffer: WebGLBuffer; + private squareVertexPositionBuffer: WebGLBuffer; + + constructor(private r: Renderer) { + this.triangleVertexPositionBuffer = r.createBuffer(); + r.gl.bindBuffer(r.gl.ARRAY_BUFFER, this.triangleVertexPositionBuffer); + const triangleVertices = [ + -1.5, 1.0, + -2.5, -1.0, + -0.5, -1.0, + ]; + r.gl.bufferData(r.gl.ARRAY_BUFFER, new Float32Array(triangleVertices), r.gl.STATIC_DRAW); + + this.squareVertexPositionBuffer = r.createBuffer(); + r.gl.bindBuffer(r.gl.ARRAY_BUFFER, this.squareVertexPositionBuffer); + const squareVertices = [ + 2.5, 1.0, + 0.5, 1.0, + 2.5, -1.0, + 0.5, -1.0, + ]; + r.gl.bufferData(r.gl.ARRAY_BUFFER, new Float32Array(squareVertices), r.gl.STATIC_DRAW); + } + + draw(): void { + this.r.gl.clear(this.r.gl.COLOR_BUFFER_BIT | this.r.gl.DEPTH_BUFFER_BIT); + + this.r.gl.bindBuffer(this.r.gl.ARRAY_BUFFER, this.triangleVertexPositionBuffer); + this.r.gl.vertexAttribPointer(this.r.vertexPosLoc, 2, this.r.gl.FLOAT, false, 0, 0); + this.r.gl.drawArrays(this.r.gl.TRIANGLES, 0, 3); + + this.r.gl.bindBuffer(this.r.gl.ARRAY_BUFFER, this.squareVertexPositionBuffer); + this.r.gl.vertexAttribPointer(this.r.vertexPosLoc, 2, this.r.gl.FLOAT, false, 0, 0); + this.r.gl.drawArrays(this.r.gl.TRIANGLE_STRIP, 0, 4); + } +} + +export default Scene; diff --git a/src/view/default.fs b/src/view/default.fs new file mode 100644 index 0000000..7a085b6 --- /dev/null +++ b/src/view/default.fs @@ -0,0 +1,3 @@ +void main(void) { + gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +} diff --git a/src/view/default.vs b/src/view/default.vs new file mode 100644 index 0000000..7c4eaeb --- /dev/null +++ b/src/view/default.vs @@ -0,0 +1,8 @@ +attribute vec2 vertexPos; + +uniform mat4 viewport; +uniform mat4 translate; + +void main(void) { + gl_Position = viewport * translate * vec4(vertexPos, 0.0, 1.0); +} -- cgit v1.2.3