web_g_engine factory method complete, should be capable of rendering
This commit is contained in:
@@ -1,19 +1,101 @@
|
||||
namespace WebGEngine {
|
||||
class Engine {
|
||||
private _GL: WebGL2RenderingContext;
|
||||
private _Program: WebGLProgram;
|
||||
private _Shaders: Array<WebGLShader>;
|
||||
private _DrawMode: EngineDrawMode = EngineDrawMode.TEXTURE;
|
||||
|
||||
constructor(canvas: HTMLCanvasElement) {
|
||||
let context = canvas.getContext('webgl2');
|
||||
if (!context) throw new Error(`Failed to get webgl2 context from ${canvas}`);
|
||||
|
||||
this._GL = context;
|
||||
this.init();
|
||||
constructor(gl: WebGL2RenderingContext) {
|
||||
this._GL = gl;
|
||||
}
|
||||
|
||||
private init(): void {}
|
||||
/**
|
||||
* Factory method: Loads all shaders compiles and links them
|
||||
* @async
|
||||
* @public
|
||||
* @param config - Engine configuration
|
||||
* @returns Promise<Engine> that resolver when the engine is done initializing\
|
||||
*/
|
||||
public static async Initialize(config: EngineConfig) {
|
||||
const GL = config.canvas.getContext('webgl2');
|
||||
if (!GL) throw new Error('WebGL2 not supported!');
|
||||
|
||||
const shaderSources: ShaderSrc[] = [];
|
||||
|
||||
try {
|
||||
const [vertSource, fragSource] = await Promise.all([
|
||||
fetch(config.vertexShaderUrl).then((r) => r.text()),
|
||||
fetch(config.fragmentShaderUrl).then((r) => r.text())
|
||||
]);
|
||||
|
||||
shaderSources.push(
|
||||
{ type: GL.VERTEX_SHADER, src: vertSource, url: config.vertexShaderUrl },
|
||||
{ type: GL.FRAGMENT_SHADER, src: fragSource, url: config.fragmentShaderUrl }
|
||||
);
|
||||
} catch (error) {
|
||||
throw new Error(`${error} Filed to fetch the shaders`);
|
||||
}
|
||||
|
||||
if (config.additionalShaders) {
|
||||
}
|
||||
|
||||
const ENGINE = new Engine(GL);
|
||||
ENGINE._Program = GL.createProgram()!;
|
||||
|
||||
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))
|
||||
throw new Error(
|
||||
`Failed to link program ${ENGINE._Program} - Logs: ${GL.getProgramInfoLog(ENGINE._Program)}`
|
||||
);
|
||||
GL.useProgram(ENGINE._Program);
|
||||
return ENGINE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile a single shader from source
|
||||
* @param type - WebGL Shader type (e.g., gl.VERTEX_SHADER)
|
||||
* @param src - Source code in GLSL
|
||||
* @param url - Source url - used for debbugging (optional)
|
||||
* @returns - Compiled {WebGLShader}
|
||||
* @private
|
||||
*/
|
||||
private compileShader(type: number, src: string, url?: string) {
|
||||
const shader = this._GL.createShader(type);
|
||||
if (!shader) throw new Error(`Failed to create a new shader of type ${type}`);
|
||||
|
||||
this._GL.shaderSource(shader, src);
|
||||
this._GL.compileShader(shader);
|
||||
|
||||
if (!this._GL.getShaderParameter(shader, this._GL.COMPILE_STATUS)) {
|
||||
this._GL.deleteShader(shader);
|
||||
throw new Error(
|
||||
`Failed to compile shader from source ${src} located at ${url || 'unknown'} - Logs: ${this._GL.getShaderInfoLog(shader)}`
|
||||
);
|
||||
}
|
||||
|
||||
return shader;
|
||||
}
|
||||
|
||||
public get getGL(): WebGL2RenderingContext {
|
||||
return this._GL;
|
||||
}
|
||||
|
||||
public get getProgram(): WebGLProgram {
|
||||
return 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,
|
||||
@@ -21,16 +103,17 @@ namespace WebGEngine {
|
||||
}
|
||||
|
||||
interface ShaderSrc {
|
||||
type: number,
|
||||
|
||||
type: number;
|
||||
src: string;
|
||||
url: string;
|
||||
}
|
||||
|
||||
interface EngineConfig {
|
||||
canvas: HTMLCanvasElement,
|
||||
vertexShader: string,
|
||||
fragmentShader: string,
|
||||
additionalShaders?:
|
||||
|
||||
canvas: HTMLCanvasElement;
|
||||
vertexShaderUrl: string;
|
||||
fragmentShaderUrl: string;
|
||||
additionalShaders: Array<{ type: number; url: string }>;
|
||||
attributes: Record<string, number>;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user