Skip to content

Commit

Permalink
fallback to webgl1
Browse files Browse the repository at this point in the history
  • Loading branch information
mathis committed Oct 25, 2023
1 parent a45754a commit 22baf4e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion client/src/gl/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface Context {
viewport: Viewport
}

export let gl: WebGL2RenderingContext
export let gl: WebGL2RenderingContext | WebGLRenderingContext
export let shader: Shader
export let renderer: Renderer
export let viewport: Viewport
Expand Down
4 changes: 2 additions & 2 deletions client/src/gl/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { TW_VERT, TW_FRAG } from './shaders'
import { mat4 } from 'gl-matrix'

export class Renderer {
gl: WebGL2RenderingContext
gl: WebGL2RenderingContext | WebGLRenderingContext
shader: Shader
proj: mat4

constructor(canvas: HTMLCanvasElement) {
this.gl = canvas.getContext('webgl2', { antialias: false })!
this.gl = canvas.getContext('webgl', { antialias: false })!

this.proj = mat4.create()

Expand Down
12 changes: 7 additions & 5 deletions client/src/gl/shader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ type ProgLocations = {
unifs: { [k in typeof UniformLocations[number]]: WebGLUniformLocation | null }
}

type WebGLCtx = WebGL2RenderingContext | WebGLRenderingContext

export class Shader {
prog: WebGLProgram
vert: WebGLShader
frag: WebGLShader
locs: ProgLocations

constructor(gl: WebGL2RenderingContext, vertSrc: string, fragSrc: string) {
constructor(gl: WebGLCtx, vertSrc: string, fragSrc: string) {
this.prog = gl.createProgram()!
this.vert = Shader.makeShader(gl, vertSrc, gl.VERTEX_SHADER)
this.frag = Shader.makeShader(gl, fragSrc, gl.FRAGMENT_SHADER)
Expand All @@ -34,7 +36,7 @@ export class Shader {
this.initUniforms(gl)
}

private initLocations(gl: WebGL2RenderingContext) {
private initLocations(gl: WebGLCtx) {
const locs: ProgLocations = {
attrs: {
aPosition: gl.getAttribLocation(this.prog, 'aPosition'),
Expand All @@ -53,17 +55,17 @@ export class Shader {
return locs
}

private initAttributes(gl: WebGL2RenderingContext) {
private initAttributes(gl: WebGLCtx) {
gl.enableVertexAttribArray(this.locs.attrs.aPosition)
}

private initUniforms(gl: WebGL2RenderingContext) {
private initUniforms(gl: WebGLCtx) {
gl.uniform4fv(this.locs.unifs.uColorMask, [1.0, 1.0, 1.0, 1.0])
gl.uniform1i(this.locs.unifs.uTexCoord, 0)
gl.uniform1i(this.locs.unifs.uVertexColor, 0)
}

private static makeShader(gl: WebGL2RenderingContext, src: string, typ: number) {
private static makeShader(gl: WebGLCtx, src: string, typ: number) {
const shader = gl.createShader(typ)!
gl.shaderSource(shader, src)
gl.compileShader(shader)
Expand Down

0 comments on commit 22baf4e

Please sign in to comment.