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/macro';
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
default:
/* eslint-enable no-fallthrough */
case ObjectType.ARRAY_BUFFER:
return model.context.ARRAY_BUFFER;
}
}

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']);

vtkOpenGLBufferObject(publicAPI, model);
}

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

export const newInstance = macro.newInstance(extend);

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

export default Object.assign({ newInstance, extend }, STATIC, Constants);