BufferObject

Introduction

Object containing the newInstance and extend functions for vtkOpenGLBufferObject.

Methods

bind

Binds the buffer object.

Returns

Type Description
boolean Whether the binding was successful.

extend

Extends the given object with the properties and methods of vtkOpenGLBufferObject.

Argument Type Required Description
publicAPI Yes The public API to extend.
model Yes The model to extend.
initialValues Yes The initial values to apply.

getError

Retrieves the error message, if any.

Returns

Type Description
string The error message.

newInstance

Creates a new instance of vtkOpenGLBufferObject with the given initial values.

Argument Type Required Description
initialValues Yes The initial values to use.

Returns

Type Description
vtkOpenGLBufferObject The new instance.

release

Releases the buffer object.

Returns

Type Description
boolean Whether the release was successful.

releaseGraphicsResources

Releases graphics resources associated with the buffer object.

setOpenGLRenderWindow

Sets the OpenGL render window.

Argument Type Required Description
renWin Yes The render window to set.

upload

Uploads data to the buffer object.

Argument Type Required Description
data Yes The data to be uploaded.
type Yes The type of the data.

Returns

Type Description
boolean Whether the upload was successful.

Source

Constants.d.ts
export declare enum ObjectType {
ARRAY_BUFFER = 0,
ELEMENT_ARRAY_BUFFER = 1,
TEXTURE_BUFFER = 2,
}

declare const _default: {
ObjectType: typeof ObjectType;
};

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

export default {
ObjectType,
};
index.d.ts
import { ObjectType } from './Constants';
import { vtkAlgorithm, vtkObject } from '../../../interfaces';

/**
* Interface for initial values of BufferObject
*/
export interface IBufferObjectInitialValues {
objectType?: ObjectType;
context?: WebGLRenderingContext | WebGL2RenderingContext;
allocatedGPUMemoryInBytes?: number;
}

/**
* Interface for OpenGL Buffer Object
*/
export interface vtkOpenGLBufferObject extends vtkObject {
/**
* Uploads data to the buffer object.
* @param data The data to be uploaded.
* @param type The type of the data.
* @returns {boolean} Whether the upload was successful.
*/
upload(data: any, type: any): boolean;

/**
* Binds the buffer object.
* @returns {boolean} Whether the binding was successful.
*/
bind(): boolean;

/**
* Releases the buffer object.
* @returns {boolean} Whether the release was successful.
*/
release(): boolean;

/**
* Releases graphics resources associated with the buffer object.
*/
releaseGraphicsResources(): void;

/**
* Sets the OpenGL render window.
* @param renWin The render window to set.
*/
setOpenGLRenderWindow(renWin: any): void;

/**
* Retrieves the error message, if any.
* @returns {string} The error message.
*/
getError(): string;
}

/**
* Extends the given object with the properties and methods of vtkOpenGLBufferObject.
* @param publicAPI The public API to extend.
* @param model The model to extend.
* @param initialValues The initial values to apply.
*/
export function extend(
publicAPI: object,
model: object,
initialValues?: IBufferObjectInitialValues
): void;

/**
* Creates a new instance of vtkOpenGLBufferObject with the given initial values.
* @param initialValues The initial values to use.
* @returns {vtkOpenGLBufferObject} The new instance.
*/
export function newInstance(
initialValues?: IBufferObjectInitialValues
): vtkOpenGLBufferObject;

/**
* Object containing the newInstance and extend functions for vtkOpenGLBufferObject.
*/
export declare const vtkOpenGLBufferObject: {
newInstance: typeof newInstance;
extend: typeof extend;
};
export default vtkOpenGLBufferObject;
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
);
model.allocatedGPUMemoryInBytes = data.length * data.BYTES_PER_ELEMENT;
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;
model.allocatedGPUMemoryInBytes = 0;
}
};

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,
allocatedGPUMemoryInBytes: 0,
};

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

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

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

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

vtkOpenGLBufferObject(publicAPI, model);
}

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

export const newInstance = macro.newInstance(extend);

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

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