import { Game } from '../core'; import { Color } from '../math'; import { ShaderProgram } from '../shader/program'; import { TextureAtlas } from '../texture'; import { RendererConfig } from './renderer.config'; /** * Core WebGL Renderer; utilised by the EntityManager to defer the rendering of Entities to the Canvas * * Handles every aspect of WebGL API interaction; including the construction and maintenance of Shaders and VBOs, and the rendering of game * objects by way of a per-render-call configuration object * * Designed to operate entirely on outside configuration, so as to enable the EntityManager to implement abstracted optimisations for things * like vertex management, buffering and shader switching * * @see Game * @see EntityManager */ export declare class Renderer { /** The WebGLRenderingContext retrieved from the Canvas */ private readonly gl; /** Game background Color, in its GL-friendly Float32Array form */ private readonly backgroundColor; /** A maintained list of shader program specifications; mapped by their name for simple management and usage */ private readonly shaderPrograms; /** Active shader program specification; used for frame-to-frame optimisation of shader switching */ private activeShaderProgram; /** A maintained list of VBO handles; mapped by their name for simple managment and usage */ private readonly vbos; /** Active VBO name; used for frame-to-frame optimisation of VBO switching and vertexAttribPointer() calls */ private activeVBOName; /** A maintained list of texture specifications; mapped by their name for simple management and usage */ private readonly textures; /** Active texture specification; used for render-to-render optimisation of texture switching */ private activeTexture; /** Current rendering mode; used for differentiating some rendering functionality between 2D and 3D States */ private mode; /** Reference to the Game the Renderer belongs to */ private game; /** * Constructor. Retrieve and store the Game the Renderer belongs to, then perform one-time setup of the Canvas context * * @param game the Game the Renderer belongs to * @param clearColor the Game's background color, to be set as the gl clearColor once on init */ constructor(game: Game, clearColor: Color); /** * Getter for the active texture unit, used for configuring Sampler2Ds in shaders */ get activeTextureUnit(): number; /** * Clear the drawing buffer with the appropriate bitmask, account for depth buffer if we're rendering in 3D */ clearScreen(): void; /** * Create and store a VBO with a given name to be used as a buffering target and vertex source later on * * @param name the name of the VBO */ createVBO(name: string): void; /** * Delete a VBO with a given name to release memory no longer required by the application * * @param name the name of the VBO */ deleteVBO(name: string): void; /** * Initialise and store a shader program with the given ShaderProgram specification, performing one-time retrieval of its attribute and * uniform locations to persist in the ShaderProgramSpec map * * @param shader the ShaderProgram specification */ createShaderProgram(shader: ShaderProgram): void; /** * Create a texture from an image with a given source * * // TODO lots of opportunity in here for configuration on a per-texture basis * // TODO texParameter*() * * @param textureAtlas the TextureAtlas representing the texture to load */ createTexture(textureAtlas: TextureAtlas): void; /** * Extra WebGL state configuation for specific 2D and 3D related state * * // TODO make this configurable */ setRenderingMode(mode: '2D' | '3D'): void; /** * Generic rendering method; using the information in a given RendererConfig, render some Entities * * @param config the RendererConfig specifying what and how to render */ render(config: RendererConfig): void; /** * Called as part of Game destroy(); clean up after ourselves * * // TODO incomplete, part of the first-working-version of the destroy() solution (see TODO/general) */ destroy(): void; /** * One-time WebGL global state configuration, where configurations apply to both 2D and 3D rendering modes * * // TODO make this configurable */ private init; /** * Internal-use single-shader source compilation routine for registering and compiling the individual Vertex and Fragment aspects of a * ShaderProgram * * @param type the gl numerical type of the shader to create; either gl.VERTEX_SHADER or gl.FRAGMENT_SHADER * @param src the source of the shader to compile * * @returns the compiled WebGLShader */ private compileShader; /** * Perform one-time setup of a new shader program by retrieving information about its attribute and uniform locations and storing the * results for later use in the ShaderProgramSpec map * * @param program the WebGLProgram to initialise * @param spec the ShaderProgram that was used to create the program */ private initializeShaderProgram; /** * Internal-use shader switching routine; make the shader program, specified by name, active for draw calls * * @param name the name of the shader program to make active */ private useShaderProgram; /** * Internal-use VBO switching routine; make the VBO, specified by a configuration, active for draw calls * * (re)buffer the vertices specified in the VBOConfig if its 'changed' flag is set, indicating either that it's new or that its vertex * list has changed since the last time it was drawn * * @param vbo the VBOConfig representing the VBO to make active */ private useVBO; /** * Internal-use texture switching routine; make the texture, specified by name, active for draw calls * * @param name the name of the texture to make active */ private useTexture; /** * Internal-use uniform upload routine; used in uploading uniform values as necessary in render() * * Encapsulates and limits the implicit relationship between the UniformType enum and associated uniform*() methods * * @param location the WebGLUniformLocation of the uniform to upload * @param type the UniformType of the uniform to upload * @param value the value of the uniform to upload */ private loadUniform; /** * Check if a given Image is sourced from the same origin as that which Aura is running on, used as a check for including the * 'crossOrigin' field on images loaded as textures, thereby enabling cross-origin textures * * @param image the Image * @param url the source URL of the Image * * @returns whether or not the image is sourced from the same origin */ private imageIsSameOrigin; }