Skip to content

Commit 2bf3702

Browse files
committed
fix(Glyph3DMapper): shift+scale for large coords
1 parent 06b05f9 commit 2bf3702

File tree

1 file changed

+46
-2
lines changed
  • Sources/Rendering/OpenGL/Glyph3DMapper

1 file changed

+46
-2
lines changed

Sources/Rendering/OpenGL/Glyph3DMapper/index.js

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import vtkHardwareSelector from 'vtk.js/Sources/Rendering/OpenGL/HardwareSelecto
77
import vtkProperty from 'vtk.js/Sources/Rendering/Core/Property';
88
import vtkOpenGLPolyDataMapper from 'vtk.js/Sources/Rendering/OpenGL/PolyDataMapper';
99
import vtkShaderProgram from 'vtk.js/Sources/Rendering/OpenGL/ShaderProgram';
10+
import { computeCoordShiftAndScale } from 'vtk.js/Sources/Rendering/OpenGL/CellArrayBufferObject/helpers';
1011

1112
import { registerOverride } from 'vtk.js/Sources/Rendering/OpenGL/ViewNodeFactory';
13+
import { primTypes } from '../Helper';
1214

1315
const { vtkErrorMacro } = macro;
1416
const { Representation } = vtkProperty;
@@ -18,6 +20,19 @@ const { PassTypes } = vtkHardwareSelector;
1820
const StartEvent = { type: 'StartEvent' };
1921
const EndEvent = { type: 'EndEvent' };
2022

23+
const MAT4_BYTE_SIZE = 64;
24+
const MAT4_ELEMENT_COUNT = 16;
25+
26+
function applyShiftScaleToMat(mat, shift, scale) {
27+
// the translation component of a 4x4 column-major matrix
28+
mat[12] = (mat[12] - shift[0]) * scale[0];
29+
mat[13] = (mat[13] - shift[1]) * scale[1];
30+
mat[14] = (mat[14] - shift[2]) * scale[2];
31+
mat[0] *= scale[0];
32+
mat[5] *= scale[1];
33+
mat[10] *= scale[2];
34+
}
35+
2136
// ----------------------------------------------------------------------------
2237
// vtkOpenGLSphereMapper methods
2338
// ----------------------------------------------------------------------------
@@ -630,9 +645,14 @@ function vtkOpenGLGlyph3DMapper(publicAPI, model) {
630645
};
631646

632647
publicAPI.buildBufferObjects = (ren, actor) => {
648+
const garray = model.renderable.getMatrixArray();
649+
650+
const pts = model.renderable.getInputData(0).getPoints();
651+
const { useShiftAndScale, coordShift, coordScale } =
652+
computeCoordShiftAndScale(pts);
653+
633654
if (model.hardwareSupport) {
634655
// update the buffer objects if needed
635-
const garray = model.renderable.getMatrixArray();
636656
const narray = model.renderable.getNormalArray();
637657
const carray = model.renderable.getColorArray();
638658
if (!model.matrixBuffer) {
@@ -645,6 +665,19 @@ function vtkOpenGLGlyph3DMapper(publicAPI, model) {
645665
model.pickBuffer = vtkBufferObject.newInstance();
646666
model.pickBuffer.setOpenGLRenderWindow(model._openGLRenderWindow);
647667
}
668+
669+
if (useShiftAndScale) {
670+
const buf = garray.buffer;
671+
for (
672+
let ptIdx = 0;
673+
ptIdx < garray.byteLength;
674+
ptIdx += MAT4_BYTE_SIZE
675+
) {
676+
const mat = new Float32Array(buf, ptIdx, MAT4_ELEMENT_COUNT);
677+
applyShiftScaleToMat(mat, coordShift, coordScale);
678+
}
679+
}
680+
648681
if (
649682
model.renderable.getBuildTime().getMTime() >
650683
model.glyphBOBuildTime.getMTime()
@@ -674,7 +707,18 @@ function vtkOpenGLGlyph3DMapper(publicAPI, model) {
674707
model.glyphBOBuildTime.modified();
675708
}
676709
}
677-
return superClass.buildBufferObjects(ren, actor);
710+
711+
superClass.buildBufferObjects(ren, actor);
712+
713+
// apply shift + scale to primitives AFTER vtkOpenGLPolyDataMapper.buildBufferObjects
714+
// so that the Glyph3DMapper gets the last say in the shift + scale
715+
if (useShiftAndScale) {
716+
for (let i = primTypes.Start; i < primTypes.End; i++) {
717+
model.primitives[i]
718+
.getCABO()
719+
.setCoordShiftAndScale(coordShift, coordScale);
720+
}
721+
}
678722
};
679723
}
680724

0 commit comments

Comments
 (0)