| 1 | class GLRenderer |
| 2 | { |
| 3 | constructor (canvas, vertexShaderSource = "default_vert", fragmentShaderSource = "default_frag") |
| 4 | { |
| 5 | this.canvas = canvas; |
| 6 | this.gl = canvas.getContext('webgl'); |
| 7 | |
| 8 | if (!this.gl) |
| 9 | { |
| 10 | console.error('WebGL not supported, falling back on experimental-webgl'); |
| 11 | this.gl = canvas.getContext('experimental-webgl'); |
| 12 | } |
| 13 | |
| 14 | if (!this.gl) |
| 15 | { |
| 16 | throw new Error('Your browser does not support WebGL'); |
| 17 | } |
| 18 | |
| 19 | this.program = null; |
| 20 | this.positionLocation = null; |
| 21 | this.texCoordLocation = null; |
| 22 | this.matrixLocation = null; |
| 23 | |
| 24 | this.uniformLocations = {}; |
| 25 | |
| 26 | // Create buffers for vertex positions and texture coordinates |
| 27 | this.positionBuffer = this.gl.createBuffer(); |
| 28 | this.texCoordBuffer = this.gl.createBuffer(); |
| 29 | |
| 30 | // Default vertices for a quad |
| 31 | this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.positionBuffer); |
| 32 | const positions = [ |
| 33 | -0.5, -0.5, |
| 34 | 0.5, -0.5, |
| 35 | -0.5, 0.5, |
| 36 | 0.5, 0.5, |
| 37 | ]; |
| 38 | this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(positions), this.gl.STATIC_DRAW); |
| 39 | |
| 40 | // Default texture coordinates |
| 41 | this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.texCoordBuffer); |
| 42 | const texCoords = [ |
| 43 | 0.0, 0.0, |
| 44 | 1.0, 0.0, |
| 45 | 0.0, 1.0, |
| 46 | 1.0, 1.0, |
| 47 | ]; |
| 48 | this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(texCoords), this.gl.STATIC_DRAW); |
| 49 | |
| 50 | // Asynchronously load shaders |
| 51 | this.initShaders(vertexShaderSource, fragmentShaderSource); |
| 52 | } |
| 53 | |
| 54 | async initShaders (vertexShaderSource, fragmentShaderSource) |
| 55 | { |
| 56 | // Once shaders are loaded, compile them and create the program |
| 57 | this.program = this.createProgram(game_shaders[vertexShaderSource], game_shaders[fragmentShaderSource]); |
| 58 | |
| 59 | const numUniforms = this.gl.getProgramParameter(this.program, this.gl.ACTIVE_UNIFORMS); |
| 60 | for (let i = 0; i < numUniforms; i++) |
| 61 | { |
| 62 | const uniformInfo = this.gl.getActiveUniform(this.program, i); |
| 63 | if (uniformInfo) |
| 64 | { |
| 65 | const { name } = uniformInfo; |
| 66 | const location = this.gl.getUniformLocation(this.program, name); |
| 67 | if (location) |
| 68 | { |
| 69 | this.uniformLocations[name] = location; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // Get the locations of attributes and uniforms after the program is created |
| 75 | this.positionLocation = this.gl.getAttribLocation(this.program, 'a_position'); |
| 76 | this.texCoordLocation = this.gl.getAttribLocation(this.program, 'a_texCoord'); |
| 77 | this.matrixLocation = this.gl.getUniformLocation(this.program, 'u_matrix'); |
| 78 | } |
| 79 | |
| 80 | createShader (type, source) |
| 81 | { |
| 82 | const shader = this.gl.createShader(type); |
| 83 | this.gl.shaderSource(shader, source); |
| 84 | this.gl.compileShader(shader); |
| 85 | if (!this.gl.getShaderParameter(shader, this.gl.COMPILE_STATUS)) |
| 86 | { |
| 87 | console.error('An error occurred compiling the shaders:', this.gl.getShaderInfoLog(shader)); |
| 88 | this.gl.deleteShader(shader); |
| 89 | return null; |
| 90 | } |
| 91 | return shader; |
| 92 | } |
| 93 | |
| 94 | createProgram (vertexShaderSource, fragmentShaderSource) |
| 95 | { |
| 96 | const vertexShader = this.createShader(this.gl.VERTEX_SHADER, vertexShaderSource); |
| 97 | const fragmentShader = this.createShader(this.gl.FRAGMENT_SHADER, fragmentShaderSource); |
| 98 | const program = this.gl.createProgram(); |
| 99 | this.gl.attachShader(program, vertexShader); |
| 100 | this.gl.attachShader(program, fragmentShader); |
| 101 | this.gl.linkProgram(program); |
| 102 | if (!this.gl.getProgramParameter(program, this.gl.LINK_STATUS)) |
| 103 | { |
| 104 | console.error('Unable to initialize the shader program:', this.gl.getProgramInfoLog(program)); |
| 105 | this.gl.deleteProgram(program); |
| 106 | return null; |
| 107 | } |
| 108 | return program; |
| 109 | } |
| 110 | |
| 111 | loadTexture (image) |
| 112 | { |
| 113 | const texture = this.gl.createTexture(); |
| 114 | this.gl.bindTexture(this.gl.TEXTURE_2D, texture); |
| 115 | this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.gl.RGBA, this.gl.UNSIGNED_BYTE, image); |
| 116 | this.gl.generateMipmap(this.gl.TEXTURE_2D); |
| 117 | return texture; |
| 118 | } |
| 119 | |
| 120 | bindTexture (texture, unit) |
| 121 | { |
| 122 | this.gl.activeTexture(unit); |
| 123 | this.gl.bindTexture(this.gl.TEXTURE_2D, texture); |
| 124 | } |
| 125 | |
| 126 | setUniform (name, value) |
| 127 | { |
| 128 | const location = this.uniformLocations[name]; |
| 129 | if (location !== undefined) |
| 130 | { |
| 131 | if (value instanceof Float32Array) |
| 132 | { |
| 133 | this.gl.uniformMatrix4fv(location, false, value); |
| 134 | } else if (typeof value === 'number') |
| 135 | { |
| 136 | this.gl.uniform1f(location, value); |
| 137 | } else if (Array.isArray(value)) |
| 138 | { |
| 139 | if (value.length === 2) |
| 140 | { |
| 141 | this.gl.uniform2fv(location, value); |
| 142 | } else if (value.length === 3) |
| 143 | { |
| 144 | this.gl.uniform3fv(location, value); |
| 145 | } else if (value.length === 4) |
| 146 | { |
| 147 | this.gl.uniform4fv(location, value); |
| 148 | } |
| 149 | } else if (value.unit) |
| 150 | { |
| 151 | // This is for textures, so we use a specific uniform location for texture unit |
| 152 | this.gl.uniform1i(location, value.unit); |
| 153 | } |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | console.warn(`Uniform location for ${name} not found`); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | render (texture, position, scale, angle, canvasWidth, canvasHeight, uniforms = {}) |
| 162 | { |
| 163 | if (!this.program) |
| 164 | { |
| 165 | console.warn('Shaders not initialized yet.'); |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | this.gl.clear(this.gl.COLOR_BUFFER_BIT); |
| 170 | |
| 171 | this.gl.useProgram(this.program); |
| 172 | |
| 173 | // Convert from world coordinates to WebGL normalized device coordinates |
| 174 | const normalizedPositionX = (position.x / canvasWidth) * 2 - 1; |
| 175 | const normalizedPositionY = 1 - (position.y / canvasHeight) * 2; // Flip Y-axis |
| 176 | |
| 177 | // Bind buffers for vertices and texture coordinates |
| 178 | this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.positionBuffer); |
| 179 | this.gl.enableVertexAttribArray(this.positionLocation); |
| 180 | this.gl.vertexAttribPointer(this.positionLocation, 2, this.gl.FLOAT, false, 0, 0); |
| 181 | |
| 182 | this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.texCoordBuffer); |
| 183 | this.gl.enableVertexAttribArray(this.texCoordLocation); |
| 184 | this.gl.vertexAttribPointer(this.texCoordLocation, 2, this.gl.FLOAT, false, 0, 0); |
| 185 | |
| 186 | // Set the transformation matrix (using normalized position) |
| 187 | const matrix = this.createTransformationMatrix( |
| 188 | { x: normalizedPositionX, y: normalizedPositionY }, |
| 189 | scale, |
| 190 | angle |
| 191 | ); |
| 192 | this.setUniform('u_matrix', matrix); |
| 193 | |
| 194 | // Bind the texture |
| 195 | if (texture) |
| 196 | { |
| 197 | this.bindTexture(texture, this.gl.TEXTURE0); |
| 198 | this.setUniform('u_texture', { unit: 0 }); |
| 199 | } |
| 200 | |
| 201 | // Set any additional uniforms |
| 202 | Object.entries(uniforms).forEach(([name, value]) => |
| 203 | { |
| 204 | this.setUniform(name, value); |
| 205 | }); |
| 206 | |
| 207 | // Draw the sprite |
| 208 | this.gl.drawArrays(this.gl.TRIANGLE_STRIP, 0, 4); |
| 209 | } |
| 210 | |
| 211 | createTransformationMatrix (position, scale, angle) |
| 212 | { |
| 213 | const rad = angle * Math.PI / 180; |
| 214 | const cos = Math.cos(rad); |
| 215 | const sin = Math.sin(rad); |
| 216 | |
| 217 | // Apply rotation, scaling, and translation |
| 218 | const matrix = new Float32Array([ |
| 219 | cos * scale.x, -sin * scale.y, 0, 0, |
| 220 | sin * scale.x, cos * scale.y, 0, 0, |
| 221 | 0, 0, 1, 0, |
| 222 | position.x, position.y, 0, 1 |
| 223 | ]); |
| 224 | |
| 225 | return matrix; |
| 226 | } |
| 227 | } |