Skip to content

Commit

Permalink
feat(webgl): add DrawMode enums
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Mar 9, 2020
1 parent c742fae commit 5adaa23
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
12 changes: 11 additions & 1 deletion packages/webgl/src/api/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import type { IndexBufferSpec, IWebGLBuffer } from "./buffers";
import type { AttribBufferData, IShader, UniformValues } from "./shader";
import type { ITexture } from "./texture";

export const enum DrawMode {
POINTS = 0,
LINES = 1,
LINE_LOOP = 2,
LINE_STRIP = 3,
TRIANGLES = 4,
TRIANGLE_STRIP = 5,
TRIANGLE_FAN = 6
}

export type ModelAttributeSpecs = IObjectOf<ModelAttributeSpec>;

export interface ModelSpec {
Expand Down Expand Up @@ -41,7 +51,7 @@ export interface ModelSpec {
/**
* WebGL draw mode. Defaults to `TRIANGLES`
*/
mode?: GLenum;
mode?: DrawMode;
/**
* Number of vertices/indices to draw
*/
Expand Down
9 changes: 7 additions & 2 deletions packages/webgl/src/buffer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { AttribPool } from "@thi.ng/vector-pools";
import {
DrawMode,
ModelAttributeSpec,
ModelAttributeSpecs,
ModelSpec
} from "./api/model";
import { isGL2Context } from "./checks";
import { error } from "./error";
import type { TypedArray } from "@thi.ng/api";
import type { IndexBufferSpec, IWebGLBuffer } from "./api/buffers";
import type { ModelAttributeSpec, ModelAttributeSpecs, ModelSpec } from "./api/model";

export class WebGLArrayBuffer<T extends TypedArray> implements IWebGLBuffer<T> {
gl: WebGLRenderingContext;
Expand Down Expand Up @@ -78,7 +83,7 @@ export const compileModel = (
}
spec.instances && compileAttribs(gl, spec.instances.attribs, mode);
compileIndices(gl, spec.indices, mode);
spec.mode == null && (spec.mode = gl.TRIANGLES);
spec.mode == null && (spec.mode = DrawMode.TRIANGLES);
// TODO auto-create VAO & inject into model spec?
return spec;
};
Expand Down
3 changes: 2 additions & 1 deletion packages/webgl/src/geo/cube.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ModelSpec } from "../api/model";
import { DrawMode, ModelSpec } from "../api/model";

export interface CubeOpts {
size: number;
Expand All @@ -21,6 +21,7 @@ export const cube = (opts?: Partial<CubeOpts>) => {
},
uniforms: {},
shader: <any>null,
mode: DrawMode.TRIANGLES,
num: 36
};
opts.normal && (spec.attribs.normal = {
Expand Down
4 changes: 2 additions & 2 deletions packages/webgl/src/geo/quad.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ModelSpec } from "../api/model";
import { DrawMode, ModelSpec } from "../api/model";

export const quad = (uv = true): ModelSpec => ({
attribs: {
Expand All @@ -17,6 +17,6 @@ export const quad = (uv = true): ModelSpec => ({
},
uniforms: {},
shader: <any>null,
mode: 5, // TRIANGLE_STRIP,
mode: DrawMode.TRIANGLE_STRIP,
num: 4
});

0 comments on commit 5adaa23

Please sign in to comment.