|
|
|
@@ -1,9 +1,12 @@
|
|
|
|
|
namespace WebGEngine {
|
|
|
|
|
class Engine {
|
|
|
|
|
import type Vector2 from '$lib/vector';
|
|
|
|
|
|
|
|
|
|
export namespace WebG {
|
|
|
|
|
export class Engine {
|
|
|
|
|
private _GL: WebGL2RenderingContext;
|
|
|
|
|
private _Program: WebGLProgram;
|
|
|
|
|
private _Shaders: Array<WebGLShader>;
|
|
|
|
|
private _Program!: WebGLProgram;
|
|
|
|
|
private _Shaders: Array<WebGLShader> = Array<WebGLShader>();
|
|
|
|
|
private _DrawMode: EngineDrawMode = EngineDrawMode.TEXTURE;
|
|
|
|
|
private _PosBuffer!: WebGLBuffer;
|
|
|
|
|
|
|
|
|
|
constructor(gl: WebGL2RenderingContext) {
|
|
|
|
|
this._GL = gl;
|
|
|
|
@@ -36,25 +39,41 @@ namespace WebGEngine {
|
|
|
|
|
throw new Error(`${error} Filed to fetch the shaders`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (config.additionalShaders) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ENGINE = new Engine(GL);
|
|
|
|
|
ENGINE._Program = GL.createProgram()!;
|
|
|
|
|
ENGINE._Program = GL.createProgram();
|
|
|
|
|
if (!ENGINE._Program) throw new Error(`Failed to create a new program ${ENGINE._Program}`);
|
|
|
|
|
|
|
|
|
|
for (const { type, src, url } of shaderSources) {
|
|
|
|
|
const shader = ENGINE.compileShader(type, src, url);
|
|
|
|
|
|
|
|
|
|
ENGINE._Shaders.push(shader);
|
|
|
|
|
|
|
|
|
|
GL.attachShader(ENGINE._Program, shader);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GL.linkProgram(ENGINE._Program);
|
|
|
|
|
if (GL.getProgramParameter(ENGINE._Program, GL.LINK_STATUS))
|
|
|
|
|
ENGINE._GL.linkProgram(ENGINE._Program);
|
|
|
|
|
if (!GL.getProgramParameter(ENGINE._Program, GL.LINK_STATUS))
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Failed to link program ${ENGINE._Program} - Logs: ${GL.getProgramInfoLog(ENGINE._Program)}`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const pos = GL.createBuffer();
|
|
|
|
|
ENGINE._PosBuffer = pos;
|
|
|
|
|
ENGINE._GL.bindBuffer(ENGINE._GL.ARRAY_BUFFER, pos);
|
|
|
|
|
|
|
|
|
|
GL.enableVertexAttribArray(0);
|
|
|
|
|
GL.bindBuffer(GL.ARRAY_BUFFER, pos);
|
|
|
|
|
GL.vertexAttribPointer(0, 2, GL.FLOAT, false, 0, 0);
|
|
|
|
|
|
|
|
|
|
GL.clearColor(0, 0, 0, 0);
|
|
|
|
|
GL.clear(GL.COLOR_BUFFER_BIT);
|
|
|
|
|
|
|
|
|
|
GL.viewport(0, 0, GL.canvas.width, GL.canvas.height);
|
|
|
|
|
|
|
|
|
|
GL.useProgram(ENGINE._Program);
|
|
|
|
|
|
|
|
|
|
let resUniLoc = GL.getUniformLocation(ENGINE._Program, 'u_resolution');
|
|
|
|
|
GL.uniform2f(resUniLoc, GL.canvas.width, GL.canvas.height);
|
|
|
|
|
return ENGINE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -83,23 +102,52 @@ namespace WebGEngine {
|
|
|
|
|
return shader;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get getGL(): WebGL2RenderingContext {
|
|
|
|
|
public get GL(): WebGL2RenderingContext {
|
|
|
|
|
return this._GL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get getProgram(): WebGLProgram {
|
|
|
|
|
public get Program(): WebGLProgram {
|
|
|
|
|
return this._Program;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public setDrawMode(mode: EngineDrawMode) {
|
|
|
|
|
this._DrawMode = mode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public draw(args: DrawCallArgs) {
|
|
|
|
|
const positions: Array<number> = [];
|
|
|
|
|
for (const v of args.vertices) {
|
|
|
|
|
positions.push(v.x, v.y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._GL.bindBuffer(this._GL.ARRAY_BUFFER, this._PosBuffer);
|
|
|
|
|
this._GL.bufferData(this._GL.ARRAY_BUFFER, new Float32Array(positions), this._GL.STATIC_DRAW);
|
|
|
|
|
|
|
|
|
|
this._GL.drawArrays(this._DrawMode, 0, args.vertices.length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public destroy() {
|
|
|
|
|
for (const shader of this._Shaders) {
|
|
|
|
|
this._GL.detachShader(this._Program, shader);
|
|
|
|
|
this._GL.deleteShader(shader);
|
|
|
|
|
}
|
|
|
|
|
this._GL.deleteBuffer(this._PosBuffer);
|
|
|
|
|
this._GL.deleteProgram(this._Program);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Enum for setting the draw mode of the engine
|
|
|
|
|
* @enum {0} - Texture, the default form of rendering, either a texture or a color
|
|
|
|
|
*/
|
|
|
|
|
enum EngineDrawMode {
|
|
|
|
|
TEXTURE = 0,
|
|
|
|
|
WIREFRAME = 1,
|
|
|
|
|
VERTEX = 2
|
|
|
|
|
export enum EngineDrawMode {
|
|
|
|
|
TEXTURE = 5, //WebGL2RenderingContext.TRIANGLE_STRIP
|
|
|
|
|
WIREFRAME = 2, //WebGL2RenderingContext.LINES
|
|
|
|
|
VERTEX = 0 //WebGL2RenderingContext.POINTS
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface DrawCallArgs {
|
|
|
|
|
vertices: Array<Vector2>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface ShaderSrc {
|
|
|
|
@@ -108,13 +156,19 @@ namespace WebGEngine {
|
|
|
|
|
url: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface EngineConfig {
|
|
|
|
|
/**
|
|
|
|
|
* {Engine} class factory method dependency that passes all the nesesary data for engine initialization
|
|
|
|
|
* @param canvas - A {HTMLCanvasElement}
|
|
|
|
|
* @param vertexShaderUrl - A string url to the primary vertex shader file
|
|
|
|
|
* @param fragmentShaderUrl - A string utl to the primary fragment shader file
|
|
|
|
|
* @param attributes - Shader attributes passed into and mapped to the program
|
|
|
|
|
* @param uniforms - Shader uniforms passed into and mapped to the program
|
|
|
|
|
*/
|
|
|
|
|
export interface EngineConfig {
|
|
|
|
|
canvas: HTMLCanvasElement;
|
|
|
|
|
vertexShaderUrl: string;
|
|
|
|
|
fragmentShaderUrl: string;
|
|
|
|
|
additionalShaders: Array<{ type: number; url: string }>;
|
|
|
|
|
attributes: Record<string, number>;
|
|
|
|
|
attributes?: Record<string, number>;
|
|
|
|
|
uniforms?: Record<string, number>;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default WebGEngine;
|
|
|
|
|