All files / Sources/Rendering/OpenGL/Camera index.js

100% Statements 38/38
91.66% Branches 11/12
100% Functions 5/5
100% Lines 38/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135                          215x   215x 912x 456x   456x 456x         215x 704x 352x 352x           352x               215x 215x 215x 215x   215x   4250x             239x   239x 239x       239x   239x   239x       239x   239x           239x 239x     4250x               1x                   215x     215x   215x 215x     215x               215x     215x         1x             1x  
import { mat3, mat4 } from 'gl-matrix';
 
import * as macro from 'vtk.js/Sources/macros';
import vtkViewNode from 'vtk.js/Sources/Rendering/SceneGraph/ViewNode';
 
import { registerOverride } from 'vtk.js/Sources/Rendering/OpenGL/ViewNodeFactory';
 
// ----------------------------------------------------------------------------
// vtkOpenGLCamera methods
// ----------------------------------------------------------------------------
 
function vtkOpenGLCamera(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkOpenGLCamera');
 
  publicAPI.buildPass = (prepass) => {
    if (prepass) {
      model._openGLRenderer =
        publicAPI.getFirstAncestorOfType('vtkOpenGLRenderer');
      model._openGLRenderWindow = model._openGLRenderer.getParent();
      model.context = model._openGLRenderWindow.getContext();
    }
  };
 
  // Renders myself
  publicAPI.opaquePass = (prepass) => {
    if (prepass) {
      const tsize = model._openGLRenderer.getTiledSizeAndOrigin();
      model.context.viewport(
        tsize.lowerLeftU,
        tsize.lowerLeftV,
        tsize.usize,
        tsize.vsize
      );
      model.context.scissor(
        tsize.lowerLeftU,
        tsize.lowerLeftV,
        tsize.usize,
        tsize.vsize
      );
    }
  };
  publicAPI.translucentPass = publicAPI.opaquePass;
  publicAPI.zBufferPass = publicAPI.opaquePass;
  publicAPI.opaqueZBufferPass = publicAPI.opaquePass;
  publicAPI.volumePass = publicAPI.opaquePass;
 
  publicAPI.getKeyMatrices = (ren) => {
    // has the camera changed?
    if (
      ren !== model.lastRenderer ||
      model._openGLRenderWindow.getMTime() > model.keyMatrixTime.getMTime() ||
      publicAPI.getMTime() > model.keyMatrixTime.getMTime() ||
      ren.getMTime() > model.keyMatrixTime.getMTime() ||
      model.renderable.getMTime() > model.keyMatrixTime.getMTime()
    ) {
      mat4.copy(model.keyMatrices.wcvc, model.renderable.getViewMatrix());
 
      mat3.fromMat4(model.keyMatrices.normalMatrix, model.keyMatrices.wcvc);
      mat3.invert(
        model.keyMatrices.normalMatrix,
        model.keyMatrices.normalMatrix
      );
      mat4.transpose(model.keyMatrices.wcvc, model.keyMatrices.wcvc);
 
      const aspectRatio = model._openGLRenderer.getAspectRatio();
 
      mat4.copy(
        model.keyMatrices.vcpc,
        model.renderable.getProjectionMatrix(aspectRatio, -1, 1)
      );
      mat4.transpose(model.keyMatrices.vcpc, model.keyMatrices.vcpc);
 
      mat4.multiply(
        model.keyMatrices.wcpc,
        model.keyMatrices.vcpc,
        model.keyMatrices.wcvc
      );
 
      model.keyMatrixTime.modified();
      model.lastRenderer = ren;
    }
 
    return model.keyMatrices;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  context: null,
  lastRenderer: null,
  keyMatrixTime: null,
  keyMatrices: null,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Inheritance
  vtkViewNode.extend(publicAPI, model, initialValues);
 
  model.keyMatrixTime = {};
  macro.obj(model.keyMatrixTime);
 
  // values always get set by the get method
  model.keyMatrices = {
    normalMatrix: new Float64Array(9),
    vcpc: new Float64Array(16),
    wcvc: new Float64Array(16),
    wcpc: new Float64Array(16),
  };
 
  // Build VTK API
  macro.setGet(publicAPI, model, ['context', 'keyMatrixTime']);
 
  // Object methods
  vtkOpenGLCamera(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };
 
// Register ourself to OpenGL backend if imported
registerOverride('vtkCamera', newInstance);