             segmentStartPos_eye, endPosCut_eye, fragmentColor);
         finalColor = finalColor + fragmentColor * (1.0f - finalColor.a);
       }
-      position = fromEyeToNdcSpace(endPos_eye);
-      outputColor = clamp(finalColor, vec4(0.0), vec4(1.0));
+      setPositionAndDepthFor(layerIndex, endPos_eye);
+      setColorForLayer(layerIndex, clamp(finalColor, vec4(0.0), vec4(1.0)));
       finalColor = vec4(0);
       fragmentColor = vec4(0);
       segmentStartPos_eye = endPosCut_eye;
+
+      ++layerIndex;
     }
 
     if (activeObjectCount > 0)


uniform sampler3D volumeSampler;

const float POSITION_NOT_SET = -2;

#define transferFunctionRowCount 64.0f

struct VolumeData

  float endDistance = dot(world, layerPlanes[layerIndex]);
  while (endDistance < 0)
  {
    vec4 newPosition = viewMatrix *
        vec4(world.xyz - endDistance * layerPlanes[layerIndex].xyz, 1.0f);
    setPositionAndDepthFor(layerIndex, newPosition);
    setColorForLayer(layerIndex, vec4(0));
    ++layerIndex;
    endDistance = dot(world, layerPlanes[layerIndex]);
