Skip to content

Commit

Permalink
Merge pull request #413 from Kitware/add_jitter
Browse files Browse the repository at this point in the history
feat(Rendering): add jitter to volume rendering
  • Loading branch information
Ken Martin committed Nov 4, 2017
2 parents f9c4333 + 25b86e9 commit d620129
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Examples/Applications/VolumeViewer/index.js
Expand Up @@ -110,7 +110,8 @@ function createViewer(container, fileContentAsText) {
mapper.setSampleDistance(sampleDistance);
actor.getProperty().setRGBTransferFunction(0, lookupTable);
actor.getProperty().setScalarOpacity(0, piecewiseFunction);
actor.getProperty().setInterpolationTypeToFastLinear();
// actor.getProperty().setInterpolationTypeToFastLinear();
actor.getProperty().setInterpolationTypeToLinear();

// For better looking volume rendering
// - distance in world coordinates a scalar opacity of 1.0
Expand Down
23 changes: 22 additions & 1 deletion Sources/Rendering/OpenGL/VolumeMapper/index.js
Expand Up @@ -11,7 +11,7 @@ import vtkShaderProgram from 'vtk.js/Sources/Rendering/OpenGL/ShaderProgram'
import vtkVertexArrayObject from 'vtk.js/Sources/Rendering/OpenGL/VertexArrayObject';
import vtkViewNode from 'vtk.js/Sources/Rendering/SceneGraph/ViewNode';
import { Representation } from 'vtk.js/Sources/Rendering/Core/Property/Constants';
import { Filter } from 'vtk.js/Sources/Rendering/OpenGL/Texture/Constants';
import { Wrap, Filter } from 'vtk.js/Sources/Rendering/OpenGL/Texture/Constants';
import { InterpolationType } from 'vtk.js/Sources/Rendering/Core/VolumeProperty/Constants';

import vtkVolumeVS from 'vtk.js/Sources/Rendering/OpenGL/glsl/vtkVolumeVS.glsl';
Expand Down Expand Up @@ -57,6 +57,8 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
model.opacityTexture.setContext(model.context);
model.lightingTexture.setWindow(model.openGLRenderWindow);
model.lightingTexture.setContext(model.context);
model.jitterTexture.setWindow(model.openGLRenderWindow);
model.jitterTexture.setContext(model.context);
model.framebuffer.setWindow(model.openGLRenderWindow);

model.openGLVolume = publicAPI.getFirstAncestorOfType('vtkOpenGLVolume');
Expand Down Expand Up @@ -595,6 +597,8 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
model.colorTexture.getTextureUnit());
program.setUniformi('otexture',
model.opacityTexture.getTextureUnit());
program.setUniformi('jtexture',
model.jitterTexture.getTextureUnit());

const volInfo = model.scalarTexture.getVolumeInfo();
const sscale = volInfo.max - volInfo.min;
Expand Down Expand Up @@ -779,6 +783,7 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
model.scalarTexture.activate();
model.opacityTexture.activate();
model.colorTexture.activate();
model.jitterTexture.activate();
if (actor.getProperty().getShade() ||
actor.getProperty().getUseGradientOpacity(0)) {
model.lightingTexture.activate();
Expand All @@ -794,6 +799,7 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
model.scalarTexture.deactivate();
model.colorTexture.deactivate();
model.opacityTexture.deactivate();
model.jitterTexture.deactivate();
if (actor.getProperty().getShade() ||
actor.getProperty().getUseGradientOpacity(0)) {
model.lightingTexture.deactivate();
Expand Down Expand Up @@ -923,6 +929,17 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {

const vprop = actor.getProperty();

if (!model.jitterTexture.getHandle()) {
const oTable = new Uint8Array(32 * 32);
for (let i = 0; i < 32 * 32; ++i) {
oTable[i] = 255.0 * Math.random();
}
model.jitterTexture.setMinificationFilter(Filter.LINEAR);
model.jitterTexture.setMagnificationFilter(Filter.LINEAR);
model.jitterTexture.create2DFromRaw(32, 32, 1,
VtkDataTypes.UNSIGNED_CHAR, oTable);
}

// rebuild opacity tfun?
const ofun = vprop.getScalarOpacity(0);
const opacityFactor = model.renderable.getSampleDistance() /
Expand Down Expand Up @@ -1035,6 +1052,7 @@ const DEFAULT_VALUES = {
colortextureString: null,
lightingTexture: null,
lightingTextureString: null,
jitterTexture: null,
tris: null,
framebuffer: null,
copyShader: null,
Expand Down Expand Up @@ -1067,6 +1085,9 @@ export function extend(publicAPI, model, initialValues = {}) {
model.opacityTexture = vtkOpenGLTexture.newInstance();
model.colorTexture = vtkOpenGLTexture.newInstance();
model.lightingTexture = vtkOpenGLTexture.newInstance();
model.jitterTexture = vtkOpenGLTexture.newInstance();
model.jitterTexture.setWrapS(Wrap.REPEAT);
model.jitterTexture.setWrapT(Wrap.REPEAT);
model.framebuffer = vtkOpenGLFramebuffer.newInstance();

model.idxToView = mat4.create();
Expand Down
8 changes: 6 additions & 2 deletions Sources/Rendering/OpenGL/glsl/vtkVolumeFS.glsl
Expand Up @@ -50,6 +50,9 @@ uniform sampler2D ctexture;
uniform float cshift;
uniform float cscale;

// jitter texture
uniform sampler2D jtexture;

// some 3D texture values
uniform sampler2D texture1;
uniform float sampleDistance;
Expand Down Expand Up @@ -195,8 +198,9 @@ void main()
float numSteps = length(vdelta) / sampleDistance;
vdelta = vdelta / numSteps;

// start slightly inside
vpos = vpos + vdelta*0.1;
// start slightly inside and apply some jitter
float jitter = texture2D(jtexture, gl_FragCoord.xy/32.0).r;
vpos = vpos + vdelta*(0.01 + 0.98*jitter);
vec4 color = vec4(0.0, 0.0, 0.0, 0.0);
int count = int(numSteps - 0.2); // end slightly inside

Expand Down
8 changes: 6 additions & 2 deletions Sources/Rendering/OpenGL/glsl/vtkVolumeFS2.glsl
Expand Up @@ -50,6 +50,9 @@ uniform sampler2D ctexture;
uniform float cshift;
uniform float cscale;

// jitter texture
uniform sampler2D jtexture;

// some 3D texture values
uniform highp sampler3D texture1;
uniform float sampleDistance;
Expand Down Expand Up @@ -164,8 +167,9 @@ void main()
float numSteps = length(vdelta) / sampleDistance;
vdelta = vdelta / numSteps;

// start slightly inside
vpos = vpos + vdelta*0.1;
// start slightly inside and apply some jitter
float jitter = texture2D(jtexture, gl_FragCoord.xy/32.0).r;
vpos = vpos + vdelta*(0.01 + 0.98*jitter);
vec4 color = vec4(0.0, 0.0, 0.0, 0.0);

vec3 ijk = vpos * vVCToIJK;
Expand Down

0 comments on commit d620129

Please sign in to comment.