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

93.75% Statements 135/144
72.72% Branches 40/55
87.5% Functions 7/8
94.36% Lines 134/142

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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368                                1x               5x     5x   5x 5x 5x 5x     5x 5x 5x   5x         5x           5x               5x             5x           5x 5x     5x 5x   5x                                                                                       5x           5x                       5x 5x   5x     5x 6x             5x                               6x 6x       6x   6x 6x 6x       4x       2x       6x     5x 6x   6x 6x   6x 6x     6x 6x           6x       6x 6x           5x   5x 5x   5x       5x 5x   5x   5x 5x 5x 5x   5x 5x   5x       3x     5x 5x 5x 5x 4x 4x 4x 4x 4x 4x 4x   4x 1x 1x   5x   5x   5x   5x 5x 5x         5x 5x 5x 167x 167x 153x     167x 167x 167x 167x 167x 167x 167x 160x 160x 160x 160x 160x     167x 167x 167x 167x 167x 167x 167x 160x 160x 160x 160x     167x 167x 167x 167x 167x 167x 167x 160x 160x 160x 160x       5x 5x 5x 4x     5x               1x         5x     5x     5x         1x             1x  
import { mat4 } from 'gl-matrix';
import { ObjectType } from 'vtk.js/Sources/Rendering/OpenGL/BufferObject/Constants';
 
import * as macro from 'vtk.js/Sources/macros';
 
import vtkBufferObject from 'vtk.js/Sources/Rendering/OpenGL/BufferObject';
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
 
import vtkShaderProgram from 'vtk.js/Sources/Rendering/OpenGL/ShaderProgram';
import vtkOpenGLPolyDataMapper from 'vtk.js/Sources/Rendering/OpenGL/PolyDataMapper';
 
import vtkSphereMapperVS from 'vtk.js/Sources/Rendering/OpenGL/glsl/vtkSphereMapperVS.glsl';
import vtkPolyDataFS from 'vtk.js/Sources/Rendering/OpenGL/glsl/vtkPolyDataFS.glsl';
 
import { registerOverride } from 'vtk.js/Sources/Rendering/OpenGL/ViewNodeFactory';
 
const { vtkErrorMacro } = macro;
 
// ----------------------------------------------------------------------------
// vtkOpenGLSphereMapper methods
// ----------------------------------------------------------------------------
 
function vtkOpenGLSphereMapper(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkOpenGLSphereMapper');
 
  // Capture 'parentClass' api for internal use
  const superClass = { ...publicAPI };
 
  publicAPI.getShaderTemplate = (shaders, ren, actor) => {
    shaders.Vertex = vtkSphereMapperVS;
    shaders.Fragment = vtkPolyDataFS;
    shaders.Geometry = '';
  };
 
  publicAPI.replaceShaderValues = (shaders, ren, actor) => {
    let VSSource = shaders.Vertex;
    let FSSource = shaders.Fragment;
 
    VSSource = vtkShaderProgram.substitute(VSSource, '//VTK::Camera::Dec', [
      'uniform mat4 VCPCMatrix;\n',
      'uniform mat4 MCVCMatrix;',
    ]).result;
 
    FSSource = vtkShaderProgram.substitute(FSSource, '//VTK::PositionVC::Dec', [
      'varying vec4 vertexVCVSOutput;',
    ]).result;
 
    // we create vertexVC below, so turn off the default
    // implementation
    FSSource = vtkShaderProgram.substitute(
      FSSource,
      '//VTK::PositionVC::Impl',
      ['vec4 vertexVC = vertexVCVSOutput;\n']
    ).result;
 
    // for lights kit and positional the VCPC matrix is already defined
    // so don't redefine it
    const replacement = [
      'uniform float invertedDepth;\n',
      'uniform int cameraParallel;\n',
      'varying float radiusVCVSOutput;\n',
      'varying vec3 centerVCVSOutput;\n',
      'uniform mat4 VCPCMatrix;\n',
    ];
    FSSource = vtkShaderProgram.substitute(
      FSSource,
      '//VTK::Normal::Dec',
      replacement
    ).result;
 
    let fragString = '';
    Iif (model.context.getExtension('EXT_frag_depth')) {
      fragString = 'gl_FragDepthEXT = (pos.z / pos.w + 1.0) / 2.0;\n';
    }
    if (model._openGLRenderWindow.getWebgl2()) {
      fragString = 'gl_FragDepth = (pos.z / pos.w + 1.0) / 2.0;\n';
    }
    FSSource = vtkShaderProgram.substitute(FSSource, '//VTK::Depth::Impl', [
      // compute the eye position and unit direction
      '  vec3 EyePos;\n',
      '  vec3 EyeDir;\n',
      '  if (cameraParallel != 0) {\n',
      '    EyePos = vec3(vertexVC.x, vertexVC.y, vertexVC.z + 3.0*radiusVCVSOutput);\n',
      '    EyeDir = vec3(0.0,0.0,-1.0); }\n',
      '  else {\n',
      '    EyeDir = vertexVC.xyz;\n',
      '    EyePos = vec3(0.0,0.0,0.0);\n',
      '    float lengthED = length(EyeDir);\n',
      '    EyeDir = normalize(EyeDir);\n',
      // we adjust the EyePos to be closer if it is too far away
      // to prevent floating point precision noise
      '    if (lengthED > radiusVCVSOutput*3.0) {\n',
      '      EyePos = vertexVC.xyz - EyeDir*3.0*radiusVCVSOutput; }\n',
      '    }\n',
 
      // translate to Sphere center
      '  EyePos = EyePos - centerVCVSOutput;\n',
      // scale to radius 1.0
      '  EyePos = EyePos/radiusVCVSOutput;\n',
      // find the intersection
      '  float b = 2.0*dot(EyePos,EyeDir);\n',
      '  float c = dot(EyePos,EyePos) - 1.0;\n',
      '  float d = b*b - 4.0*c;\n',
      '  vec3 normalVCVSOutput = vec3(0.0,0.0,1.0);\n',
      '  if (d < 0.0) { discard; }\n',
      '  else {\n',
      '    float t = (-b - invertedDepth*sqrt(d))*0.5;\n',
 
      // compute the normal, for unit sphere this is just
      // the intersection point
      '    normalVCVSOutput = invertedDepth*normalize(EyePos + t*EyeDir);\n',
      // compute the intersection point in VC
      '    vertexVC.xyz = normalVCVSOutput*radiusVCVSOutput + centerVCVSOutput;\n',
      '    }\n',
      // compute the pixel's depth
      // ' normalVCVSOutput = vec3(0,0,1);\n'
      '  vec4 pos = VCPCMatrix * vertexVC;\n',
      fragString,
    ]).result;
 
    // Strip out the normal line -- the normal is computed as part of the depth
    FSSource = vtkShaderProgram.substitute(
      FSSource,
      '//VTK::Normal::Impl',
      ''
    ).result;
 
    Iif (model.haveSeenDepthRequest) {
      // special depth impl
      FSSource = vtkShaderProgram.substitute(FSSource, '//VTK::ZBuffer::Impl', [
        'if (depthRequest == 1) {',
        'float computedZ = (pos.z / pos.w + 1.0) / 2.0;',
        'float iz = floor(computedZ * 65535.0 + 0.1);',
        'float rf = floor(iz/256.0)/255.0;',
        'float gf = mod(iz,256.0)/255.0;',
        'gl_FragData[0] = vec4(rf, gf, 0.0, 1.0); }',
      ]).result;
    }
 
    shaders.Vertex = VSSource;
    shaders.Fragment = FSSource;
 
    superClass.replaceShaderValues(shaders, ren, actor);
  };
 
  publicAPI.setMapperShaderParameters = (cellBO, ren, actor) => {
    if (
      cellBO.getCABO().getElementCount() &&
      (model.VBOBuildTime > cellBO.getAttributeUpdateTime().getMTime() ||
        cellBO.getShaderSourceTime().getMTime() >
          cellBO.getAttributeUpdateTime().getMTime()) &&
      cellBO.getProgram().isAttributeUsed('offsetMC')
    ) {
      Iif (
        !cellBO.getVAO().addAttributeArray(
          cellBO.getProgram(),
          cellBO.getCABO(),
          'offsetMC',
          12, // 12:this->VBO->ColorOffset+sizeof(float)
          cellBO.getCABO().getStride(),
          model.context.FLOAT,
          2,
          false
        )
      ) {
        vtkErrorMacro("Error setting 'offsetMC' in shader VAO.");
      }
    }
 
    if (cellBO.getProgram().isUniformUsed('invertedDepth')) {
      cellBO
        .getProgram()
        .setUniformf('invertedDepth', model.invert ? -1.0 : 1.0);
    }
    if (cellBO.getProgram().isUniformUsed('scaleFactor')) {
      // apply scaling factor only if a scale array has been provided.
      const poly = model.currentInput;
      const pointData = poly.getPointData();
      if (
        model.renderable.getScaleArray() != null &&
        pointData.hasArray(model.renderable.getScaleArray())
      ) {
        cellBO
          .getProgram()
          .setUniformf('scaleFactor', model.renderable.getScaleFactor());
      } else {
        cellBO.getProgram().setUniformf('scaleFactor', 1.0);
      }
    }
 
    superClass.setMapperShaderParameters(cellBO, ren, actor);
  };
 
  publicAPI.setCameraShaderParameters = (cellBO, ren, actor) => {
    const program = cellBO.getProgram();
 
    const cam = ren.getActiveCamera();
    const keyMats = model.openGLCamera.getKeyMatrices(ren);
 
    if (program.isUniformUsed('VCPCMatrix')) {
      program.setUniformMatrix('VCPCMatrix', keyMats.vcpc);
    }
 
    if (program.isUniformUsed('MCVCMatrix')) {
      Iif (!actor.getIsIdentity()) {
        const actMats = model.openGLActor.getKeyMatrices();
        const tmp4 = new Float64Array(16);
        mat4.multiply(tmp4, keyMats.wcvc, actMats.mcwc);
        program.setUniformMatrix('MCVCMatrix', tmp4);
      } else {
        program.setUniformMatrix('MCVCMatrix', keyMats.wcvc);
      }
    }
 
    if (program.isUniformUsed('cameraParallel')) {
      cellBO
        .getProgram()
        .setUniformi('cameraParallel', cam.getParallelProjection());
    }
  };
 
  publicAPI.getOpenGLMode = (rep, type) => model.context.TRIANGLES;
 
  publicAPI.buildBufferObjects = (ren, actor) => {
    const poly = model.currentInput;
 
    Iif (poly === null) {
      return;
    }
 
    model.renderable.mapScalars(poly, 1.0);
    const c = model.renderable.getColorMapColors();
 
    const vbo = model.primitives[model.primTypes.Tris].getCABO();
 
    const pointData = poly.getPointData();
    const points = poly.getPoints();
    const numPoints = points.getNumberOfPoints();
    const pointArray = points.getData();
 
    const pointSize = 5; // x,y,z,orientation1,orientation2
    let scales = null;
 
    if (
      model.renderable.getScaleArray() != null &&
      pointData.hasArray(model.renderable.getScaleArray())
    ) {
      scales = pointData.getArray(model.renderable.getScaleArray()).getData();
    }
 
    let colorData = null;
    let colorComponents = 0;
    let packedUCVBO = null;
    if (c) {
      colorComponents = c.getNumberOfComponents();
      vbo.setColorOffset(0);
      vbo.setColorBOStride(4);
      colorData = c.getData();
      packedUCVBO = new Uint8Array(3 * numPoints * 4);
      if (!vbo.getColorBO()) {
        vbo.setColorBO(vtkBufferObject.newInstance());
      }
      vbo.getColorBO().setOpenGLRenderWindow(model._openGLRenderWindow);
    } else if (vbo.getColorBO()) {
      vbo.setColorBO(null);
    }
    vbo.setColorComponents(colorComponents);
 
    const packedVBO = new Float32Array(pointSize * numPoints * 3);
 
    vbo.setStride(pointSize * 4);
 
    const cos30 = Math.cos(vtkMath.radiansFromDegrees(30.0));
    let pointIdx = 0;
    let colorIdx = 0;
 
    //
    // Generate points and point data for sides
    //
    let vboIdx = 0;
    let ucIdx = 0;
    for (let i = 0; i < numPoints; ++i) {
      let radius = model.renderable.getRadius();
      if (scales) {
        radius = scales[i];
      }
 
      pointIdx = i * 3;
      packedVBO[vboIdx++] = pointArray[pointIdx++];
      packedVBO[vboIdx++] = pointArray[pointIdx++];
      packedVBO[vboIdx++] = pointArray[pointIdx++];
      packedVBO[vboIdx++] = -2.0 * radius * cos30;
      packedVBO[vboIdx++] = -radius;
      if (colorData) {
        colorIdx = i * colorComponents;
        packedUCVBO[ucIdx++] = colorData[colorIdx];
        packedUCVBO[ucIdx++] = colorData[colorIdx + 1];
        packedUCVBO[ucIdx++] = colorData[colorIdx + 2];
        packedUCVBO[ucIdx++] = colorData[colorIdx + 3];
      }
 
      pointIdx = i * 3;
      packedVBO[vboIdx++] = pointArray[pointIdx++];
      packedVBO[vboIdx++] = pointArray[pointIdx++];
      packedVBO[vboIdx++] = pointArray[pointIdx++];
      packedVBO[vboIdx++] = 2.0 * radius * cos30;
      packedVBO[vboIdx++] = -radius;
      if (colorData) {
        packedUCVBO[ucIdx++] = colorData[colorIdx];
        packedUCVBO[ucIdx++] = colorData[colorIdx + 1];
        packedUCVBO[ucIdx++] = colorData[colorIdx + 2];
        packedUCVBO[ucIdx++] = colorData[colorIdx + 3];
      }
 
      pointIdx = i * 3;
      packedVBO[vboIdx++] = pointArray[pointIdx++];
      packedVBO[vboIdx++] = pointArray[pointIdx++];
      packedVBO[vboIdx++] = pointArray[pointIdx++];
      packedVBO[vboIdx++] = 0.0;
      packedVBO[vboIdx++] = 2.0 * radius;
      if (colorData) {
        packedUCVBO[ucIdx++] = colorData[colorIdx];
        packedUCVBO[ucIdx++] = colorData[colorIdx + 1];
        packedUCVBO[ucIdx++] = colorData[colorIdx + 2];
        packedUCVBO[ucIdx++] = colorData[colorIdx + 3];
      }
    }
 
    vbo.setElementCount(vboIdx / pointSize);
    vbo.upload(packedVBO, ObjectType.ARRAY_BUFFER);
    if (c) {
      vbo.getColorBO().upload(packedUCVBO, ObjectType.ARRAY_BUFFER);
    }
 
    model.VBOBuildTime.modified();
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Inheritance
  vtkOpenGLPolyDataMapper.extend(publicAPI, model, initialValues);
 
  // Object methods
  vtkOpenGLSphereMapper(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkOpenGLSphereMapper');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };
 
// Register ourself to OpenGL backend if imported
registerOverride('vtkSphereMapper', newInstance);