Skip to content

Commit

Permalink
fix(Rendering): improve the termination of vol ren rays
Browse files Browse the repository at this point in the history
Better handling of the end condition of rays where the
opacity peaks right where the ray ends. Solve by taking
a last residual step on webgl2. Improve for both webgl1
and webgl2 with better starting loaction and step calculation.

Fix naming of openGLRenderWindow in GenericRenderWindow
  • Loading branch information
Ken Martin committed Aug 9, 2018
1 parent a6b5943 commit 85bfcfa
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
16 changes: 8 additions & 8 deletions Sources/Rendering/Misc/GenericRenderWindow/index.js
Expand Up @@ -21,13 +21,13 @@ function vtkGenericRenderWindow(publicAPI, model) {
model.renderer = vtkRenderer.newInstance();
model.renderWindow.addRenderer(model.renderer);

// OpenGlRenderWindow
model.openGlRenderWindow = vtkOpenGLRenderWindow.newInstance();
model.renderWindow.addView(model.openGlRenderWindow);
// OpenGLRenderWindow
model.openGLRenderWindow = vtkOpenGLRenderWindow.newInstance();
model.renderWindow.addView(model.openGLRenderWindow);

// Interactor
model.interactor = vtkRenderWindowInteractor.newInstance();
model.interactor.setView(model.openGlRenderWindow);
model.interactor.setView(model.openGLRenderWindow);
model.interactor.initialize();

// Expose background
Expand All @@ -41,7 +41,7 @@ function vtkGenericRenderWindow(publicAPI, model) {
if (model.container) {
const dims = model.container.getBoundingClientRect();
const devicePixelRatio = window.devicePixelRatio || 1;
model.openGlRenderWindow.setSize(
model.openGLRenderWindow.setSize(
Math.floor(dims.width * devicePixelRatio),
Math.floor(dims.height * devicePixelRatio)
);
Expand All @@ -53,7 +53,7 @@ function vtkGenericRenderWindow(publicAPI, model) {
// Handle DOM container relocation
publicAPI.setContainer = (el) => {
if (model.container) {
model.openGlRenderWindow.setContainer(model.container);
model.openGLRenderWindow.setContainer(model.container);
model.interactor.unbindEvents(model.container);
}

Expand All @@ -62,7 +62,7 @@ function vtkGenericRenderWindow(publicAPI, model) {

// Bind to new container
if (model.container) {
model.openGlRenderWindow.setContainer(model.container);
model.openGLRenderWindow.setContainer(model.container);
model.interactor.bindEvents(model.container);
}
};
Expand Down Expand Up @@ -94,7 +94,7 @@ export function extend(publicAPI, model, initialValues = {}) {
macro.get(publicAPI, model, [
'renderWindow',
'renderer',
'openGlRenderWindow',
'openGLRenderWindow',
'interactor',
'container',
]);
Expand Down
14 changes: 9 additions & 5 deletions Sources/Rendering/OpenGL/glsl/vtkVolumeFS.glsl
Expand Up @@ -194,15 +194,19 @@ void main()
dot(endVC, vPlaneNormal0),
dot(endVC, vPlaneNormal2),
dot(endVC, vPlaneNormal4));
vec3 vdelta = endvpos - vpos;
float numSteps = length(vdelta) / sampleDistance;
vdelta = vdelta / numSteps;

// 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);
vec3 vdelta = endvpos - vpos;
vpos = vpos + normalize(vdelta)*(0.01 + 0.98*jitter)*sampleDistance;

// update vdelta post jitter
vdelta = endvpos - vpos;
float numSteps = length(vdelta) / sampleDistance;
vdelta = vdelta / float(numSteps);

vec4 color = vec4(0.0, 0.0, 0.0, 0.0);
int count = int(numSteps - 0.2); // end slightly inside
int count = int(numSteps - 0.05); // end slightly inside

vec3 ijk = vpos * vVCToIJK;
vdelta = vdelta * vVCToIJK;
Expand Down
42 changes: 37 additions & 5 deletions Sources/Rendering/OpenGL/glsl/vtkVolumeFS2.glsl
Expand Up @@ -163,18 +163,22 @@ void main()
dot(endVC, vPlaneNormal0),
dot(endVC, vPlaneNormal2),
dot(endVC, vPlaneNormal4));

// start slightly inside and apply some jitter
float jitter = texture2D(jtexture, gl_FragCoord.xy/32.0).r;
vec3 vdelta = endvpos - vpos;
vpos = vpos + normalize(vdelta)*(0.01 + 0.98*jitter)*sampleDistance;

// update vdelta post jitter
vdelta = endvpos - vpos;
float numSteps = length(vdelta) / sampleDistance;
vdelta = vdelta / float(numSteps);

// 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;
vdelta = vdelta * vVCToIJK;
int count = int(numSteps - 0.2); // end slightly inside
int count = int(numSteps - 0.05); // end slightly inside
for (int i = 0; i < //VTK::MaximumSamplesValue ; ++i)
{
// compute the scalar
Expand All @@ -197,8 +201,36 @@ void main()

color = color + vec4(tcolor.rgb*tcolor.a, tcolor.a)*mix;
if (color.a > 0.99) { color.a = 1.0; break; }
if (i >= count) { break; }
ijk += vdelta;
if (i >= count)
{
break;
}
}

if (color.a < 0.99)
{
float residual = numSteps - float(count);
ijk += (residual - 1.0)*vdelta;

// compute the scalar
scalar = texture(texture1, ijk).r;

// now map through opacity and color
vec4 tcolor = texture2D(ctexture, vec2(scalar * cscale + cshift, 0.5));
tcolor.a = residual*texture2D(otexture, vec2(scalar * oscale + oshift, 0.5)).r;

// compute the normal if needed
//VTK::Normal::Impl

// handle gradient opacity
//VTK::GradientOpacity::Impl

// handle lighting
//VTK::Light::Impl

float mix = (1.0 - color.a);
color = color + vec4(tcolor.rgb*tcolor.a, tcolor.a)*mix;
}

gl_FragData[0] = vec4(color.rgb/color.a, color.a);
Expand Down

0 comments on commit 85bfcfa

Please sign in to comment.