BufferObject

OpenGL buffer object

OpenGL buffer object to store index, geometry and/or attribute data on the GPU.

getType()

Get the type of the buffer object.

setType(type)

Set the type of the buffer object.

getHandle()

Get the handle of the buffer object (actual buffer object returned by WebGL).

isReady()

Determine if the buffer object is ready to be used.

generateBuffer

Generate the the opengl buffer for this handle.

upload(data, type)

Upload data to the buffer object. The internal buffer object type must match the type or be
uninitialized. The data param must be a typed array and contain tightly packed values
accessible by the index operator ([]).

bind()

Bind the buffer object ready for rendering. Note: Only one ARRAY_BUFFER and one
ELEMENT_ARRAY_BUFFER may be bound at any time.

release()

Release the buffer. This should be done after rendering is complete.

releaseGraphicsResources()

Release any graphics resources that are being consumed by this class.

getError()

Return a string describing errors.

Source

Constants.js
export const ObjectType = {
ARRAY_BUFFER: 0,
ELEMENT_ARRAY_BUFFER: 1,
TEXTURE_BUFFER: 2,
};

export default {
ObjectType,
};
index.js
import macro from 'vtk.js/Sources/macros';
import Constants from 'vtk.js/Sources/Rendering/OpenGL/BufferObject/Constants';

const { ObjectType } = Constants;

// ----------------------------------------------------------------------------
// Global methods
// ----------------------------------------------------------------------------

// ----------------------------------------------------------------------------
// Static API
// ----------------------------------------------------------------------------

export const STATIC = {};

// ----------------------------------------------------------------------------
// vtkOpenGLBufferObject methods
// ----------------------------------------------------------------------------

function vtkOpenGLBufferObject(publicAPI, model) {
// Set our className
model.classHierarchy.push('vtkOpenGLBufferObject');

// Class-specific private functions
function convertType(type) {
switch (type) {
case ObjectType.ELEMENT_ARRAY_BUFFER:
return model.context.ELEMENT_ARRAY_BUFFER;
case ObjectType.TEXTURE_BUFFER:
if ('TEXTURE_BUFFER' in model.context) {
return model.context.TEXTURE_BUFFER;
}
/* eslint-disable no-fallthrough */
// Intentional fallthrough in case there is no TEXTURE_BUFFER in WebGL
case ObjectType.ARRAY_BUFFER:
default:
return model.context.ARRAY_BUFFER;
/* eslint-enable no-fallthrough */
}
}

let internalType = null;
let internalHandle = null;
let dirty = true;
let error = '';

// Public API methods
publicAPI.getType = () => internalType;

publicAPI.setType = (value) => {
internalType = value;
};

publicAPI.getHandle = () => internalHandle;
publicAPI.isReady = () => dirty === false;

publicAPI.generateBuffer = (type) => {
const objectTypeGL = convertType(type);
if (internalHandle === null) {
internalHandle = model.context.createBuffer();
internalType = type;
}
return convertType(internalType) === objectTypeGL;
};

publicAPI.upload = (data, type) => {
// buffer, size, type
const alreadyGenerated = publicAPI.generateBuffer(type);
if (!alreadyGenerated) {
error = 'Trying to upload array buffer to incompatible buffer.';
return false;
}
model.context.bindBuffer(convertType(internalType), internalHandle);
model.context.bufferData(
convertType(internalType),
data,
model.context.STATIC_DRAW
);
dirty = false;
return true;
};

publicAPI.bind = () => {
if (!internalHandle) {
return false;
}
model.context.bindBuffer(convertType(internalType), internalHandle);
return true;
};

publicAPI.release = () => {
if (!internalHandle) {
return false;
}
model.context.bindBuffer(convertType(internalType), null);
return true;
};

publicAPI.releaseGraphicsResources = () => {
if (internalHandle !== null) {
model.context.bindBuffer(convertType(internalType), null);
model.context.deleteBuffer(internalHandle);
internalHandle = null;
}
};

publicAPI.setOpenGLRenderWindow = (rw) => {
if (model._openGLRenderWindow === rw) {
return;
}
publicAPI.releaseGraphicsResources();
model._openGLRenderWindow = rw;
model.context = null;
if (rw) {
model.context = model._openGLRenderWindow.getContext();
}
};

publicAPI.getError = () => error;
}

// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------

const DEFAULT_VALUES = {
objectType: ObjectType.ARRAY_BUFFER,
// _openGLRenderWindow: null,
context: null,
};

// ----------------------------------------------------------------------------

export function extend(publicAPI, model, initialValues = {}) {
Object.assign(model, DEFAULT_VALUES, initialValues);

// Object methods
macro.obj(publicAPI, model);

macro.get(publicAPI, model, ['_openGLRenderWindow']);
macro.moveToProtected(publicAPI, model, ['openGLRenderWindow']);

vtkOpenGLBufferObject(publicAPI, model);
}

// ----------------------------------------------------------------------------

export const newInstance = macro.newInstance(extend);

// ----------------------------------------------------------------------------

export default { newInstance, extend, ...STATIC, ...Constants };