Skip to content

Commit

Permalink
refactor(webgl): fix #200, extract private initBuffer()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Feb 10, 2020
1 parent a8a4c87 commit 9ab496e
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions packages/webgl/src/buffer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TypedArray } from "@thi.ng/api";
import { AttribPool } from "@thi.ng/vector-pools";
import { IndexBufferSpec, IWebGLBuffer } from "./api/buffers";
import { ModelAttributeSpecs, ModelSpec } from "./api/model";
import { ModelAttributeSpec, ModelAttributeSpecs, ModelSpec } from "./api/model";
import { isGL2Context } from "./checks";
import { error } from "./error";

Expand Down Expand Up @@ -83,26 +83,27 @@ export const compileModel = (
return spec;
};

const initBuffer = (
gl: WebGLRenderingContext,
src: ModelAttributeSpec | IndexBufferSpec,
type: GLenum,
mode: GLenum
) => {
if (src.buffer) {
src.data && src.buffer.set(<any>src.data);
} else {
src.buffer = new WebGLArrayBuffer(gl, src.data, type, mode);
}
};

const compileAttribs = (
gl: WebGLRenderingContext,
attribs: ModelAttributeSpecs,
mode: GLenum
) => {
if (attribs) {
for (let id in attribs) {
if (attribs.hasOwnProperty(id)) {
const attr = attribs[id];
if (attr.buffer) {
attr.data && attr.buffer.set(attr.data);
} else {
attr.buffer = new WebGLArrayBuffer(
gl,
attr.data,
gl.ARRAY_BUFFER,
mode
);
}
}
initBuffer(gl, attribs[id], gl.ARRAY_BUFFER, mode);
}
}
return attribs;
Expand All @@ -114,16 +115,7 @@ export const compileIndices = (
mode: GLenum = gl.STATIC_DRAW
) => {
if (index) {
if (index.buffer) {
index.data && index.buffer.set(index.data);
} else {
index.buffer = new WebGLArrayBuffer(
gl,
index.data,
gl.ELEMENT_ARRAY_BUFFER,
mode
);
}
initBuffer(gl, index, gl.ELEMENT_ARRAY_BUFFER, mode);
}
return index;
};
Expand Down

0 comments on commit 9ab496e

Please sign in to comment.