2020-06-10 13:38:04 +08:00
|
|
|
class WebGLUtils {
|
2020-06-15 20:08:21 +08:00
|
|
|
public static getWebGL(): WebGLRenderingContext {
|
2020-06-10 16:25:39 +08:00
|
|
|
if (egret.WebGLUtils.checkCanUseWebGL())
|
|
|
|
|
return document.querySelector("canvas").getContext("webgl");
|
|
|
|
|
|
|
|
|
|
throw new Error("cannot get webgl");
|
2020-06-10 13:38:04 +08:00
|
|
|
}
|
|
|
|
|
|
2020-06-15 20:08:21 +08:00
|
|
|
public static drawUserIndexPrimitives<T>(primitiveType: number, vertexData: T[], vertexOffset: number,
|
|
|
|
|
numVertices: number, indexData: number[], indexOffset: number, primitiveCount: number) {
|
2020-06-10 13:38:04 +08:00
|
|
|
let GL = this.getWebGL();
|
|
|
|
|
|
|
|
|
|
GL.bindBuffer(GL.ARRAY_BUFFER, 0);
|
|
|
|
|
this.checkGLError();
|
|
|
|
|
GL.bindBuffer(GL.ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
|
this.checkGLError();
|
|
|
|
|
|
2020-06-15 20:08:21 +08:00
|
|
|
GL.drawElements(primitiveType,
|
|
|
|
|
this.getElementCountArray(primitiveType, primitiveCount),
|
|
|
|
|
GL.UNSIGNED_SHORT,
|
2020-06-10 13:38:04 +08:00
|
|
|
indexOffset * 2);
|
|
|
|
|
this.checkGLError();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-15 20:08:21 +08:00
|
|
|
private static getElementCountArray(primitiveType: number, primitiveCount: number) {
|
2020-06-10 13:38:04 +08:00
|
|
|
let GL = this.getWebGL();
|
2020-06-15 20:08:21 +08:00
|
|
|
switch (primitiveType) {
|
2020-06-10 13:38:04 +08:00
|
|
|
case GL.LINES:
|
|
|
|
|
return primitiveCount * 2;
|
|
|
|
|
case GL.LINE_STRIP:
|
|
|
|
|
return primitiveCount + 1;
|
|
|
|
|
case GL.TRIANGLES:
|
|
|
|
|
return primitiveCount * 3;
|
|
|
|
|
case GL.TRIANGLE_STRIP:
|
|
|
|
|
return primitiveCount + 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new Error("not support");
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-15 20:08:21 +08:00
|
|
|
public static checkGLError() {
|
2020-06-10 13:38:04 +08:00
|
|
|
let GL = this.getWebGL();
|
|
|
|
|
let error = GL.getError();
|
2020-06-15 20:08:21 +08:00
|
|
|
if (error != GL.NO_ERROR) {
|
2020-06-10 13:38:04 +08:00
|
|
|
throw new Error("GL.GetError() returned" + error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|