Ejemplo n.º 1
0
void main()
{
  //Calculate the ray direction using viewport information
  vec3 rayDirection = frag_worldpos - RayOrigin;
  rayDirection = normalize(rayDirection);
  
  //Cube ray intersection test
  vec3 invR = 1.0 / rayDirection;
  vec3 tbot = invR * (volumeMin - RayOrigin);
  vec3 ttop = invR * (volumeMax - RayOrigin);
  
  //Now sort all elements of tbot and ttop to find the two min and max elements
  vec3 tmin = min(ttop, tbot); //Closest planes
  vec2 t = max(tmin.xx, tmin.yz); //Out of the closest planes, find the last to be entered (collision point)
  float tnear = max(t.x, t.y);//...
  
  //If the viewpoint is penetrating the volume, make sure to only cast the ray
  //from the eye position, not behind it
  tnear = max(0.0, tnear);
  
  //Now work out when the ray will leave the volume
  vec3 tmax = max(ttop, tbot); //Distant planes
  t = min(tmax.xx, tmax.yz);//Find the first plane to be exited
  float tfar = min(t.x, t.y);//...
  
  //Check what the screen depth is to make sure we don't sample the
  //volume past any standard GL objects
  float bufferDepth = texelFetch(DepthTexture, ivec2(gl_FragCoord.xy), 0).r;
  float depth = recalcZCoord(bufferDepth);
  tfar = min(depth, tfar);
  
  //We need to calculate the ray's starting position. We add a random
  //fraction of the stepsize to the original starting point to dither
  //the output
  float starting_offset = tnear; 

  if (DitherRay != 0)
    starting_offset += StepSize * fract(sin(gl_FragCoord.x * 12.9898 + gl_FragCoord.y * 78.233) * 43758.5453);

  vec3 rayPos = RayOrigin + rayDirection * starting_offset;
  
  //The color accumulation variable
  vec4 color = vec4(0.0, 0.0, 0.0, 0.0);
  
  //Start the sampling by initialising the ray variables. We take the
  //first sample ready to integrate to the next sample
  vec4 first_sample = texture(DataTexture, (rayPos + 1.0) * 0.5);

  float lastsamplea = first_sample.a;
  vec4 lastTransfer = texture(IntTransferTexture, lastsamplea);
  vec3 lastnorm = first_sample.xyz * 2.0 - vec3(1.0);
  float lastnorm_length = length(lastnorm);
  lastnorm = (lastnorm_length == 0) ? -rayDirection : lastnorm / lastnorm_length;

  //Make this into the ray position step vector
  rayDirection *= StepSize;

  rayPos += rayDirection;

  for (float length = tfar - tnear; length > 0.0;
       length -= StepSize, rayPos += rayDirection)
    {
      //Grab the volume sample
      vec4 sample = texture(DataTexture, (rayPos - volumeMin) * invVolumeDimensions);
      float delta = sample.a - lastsamplea;
      vec4 transfer = texture(IntTransferTexture, sample.a);
      float deltaT = transfer.a - lastTransfer.a;
      vec3 deltaK = transfer.rgb - lastTransfer.rgb;

      vec4 src;
      if (delta == 0.0)
	{ //Special case where the integration breaks down, just use the constant val.
	  src = texture(TransferTexture, sample.a);
	  src.a = (1.0 - exp( - StepSize * src.a));
	}
      else
	{
	  /*Pre-Integrated color calc*/
	  float opacity = 1.0 - exp( - deltaT * StepSize / delta);
	  vec3 color = abs(deltaK) / (abs(deltaT) + 1.0e-10);
	  src = vec4(color, opacity);
	}

      lastTransfer = transfer;
      lastsamplea = sample.a;

      ////////////Lighting calculations
      //We perform all the calculations in the eye space, The normal
      //from the previous step is used, as it is the normal in the
      //direction the ray entered the volume.
      vec3 norm = (ViewMatrix * vec4(lastnorm, 0.0)).xyz;      
      src.rgb = calcLighting((ViewMatrix * vec4(rayPos,1.0)).xyz, norm, src.rgb);

      //Update the lastnormal with the new normal, if it is valid
      norm = sample.xyz * 2.0 - vec3(1.0);
      //Test if we've got a bad normal and need to reuse the old one
      float sqrnormlength = dot(norm,norm);
      norm /= sqrt(sqrnormlength);
      if (sqrnormlength >= 0.01)
	lastnorm = norm;

      ///////////Front to back blending
      src.rgb *= src.a;
      color = (1.0 - color.a) * src + color;
  
      //We only accumulate up to 0.95 alpha (the blending never
      //reaches 1).
      if (color.a >= 0.95)
  	{
  	  //We have to renormalize the color by the alpha value (see
  	  //below)
  	  color.rgb /= color.a;
  	  //Set the alpha to one to make sure the pixel is not transparent
  	  color.a = 1.0;
  	  break;
  	}
    }
  /*We must renormalize the color by the alpha value. For example, if
  our ray only hits just one white voxel with a alpha of 0.5, we will have
  
  src.rgb = vec4(1,1,1,0.5)
  
  src.rgb *= src.a; 
  //which gives, src.rgb = 0.5 * src.rgb = vec4(0.5,0.5,0.5,0.5)
  
  color = (1.0 - color.a) * src + color;
  //which gives, color = (1.0 - 0) * vec4(0.5,0.5,0.5,0.5) + vec4(0,0,0,0) = vec4(0.5,0.5,0.5,0.5)
  
  So the final color of the ray is half way between white and black, but the voxel it hit was white!
  The solution is to divide by the alpha, as this is the "amount of color" added to color.
  */
  color.rgb /= float(color.a == 0.0) + color.a;
  color_out = color;
});
Ejemplo n.º 2
0
void main()
{
  //Calculate the ray direction using viewport information
  vec3 rayDirection;
  rayDirection.x = 2.0 * gl_FragCoord.x / WindowSize.x - 1.0;
  rayDirection.y = 2.0 * gl_FragCoord.y / WindowSize.y - 1.0;
  rayDirection.y *= WindowSize.y / WindowSize.x;
  rayDirection.z = -FocalLength;
  rayDirection = (vec4(rayDirection, 0.0) * ViewMatrix).xyz;
  rayDirection = normalize(rayDirection);

  //Cube ray intersection test
  vec3 invR = 1.0 / rayDirection;
  vec3 boxMin = vec3(-1.0,-1.0,-1.0);
  vec3 boxMax = vec3( 1.0, 1.0, 1.0);
  vec3 tbot = invR * (boxMin - RayOrigin);
  vec3 ttop = invR * (boxMax - RayOrigin);
  
  //Now sort all elements of tbot and ttop to find the two min and max elements
  vec3 tmin = min(ttop, tbot); //Closest planes
  vec2 t = max(tmin.xx, tmin.yz); //Out of the closest planes, find the last to be entered (collision point)
  float tnear = max(t.x, t.y);//...

  //If the viewpoint is penetrating the volume, make sure to only cast the ray
  //from the eye position, not behind it
  if (tnear < 0.0) tnear = 0.0;

  //Now work out when the ray will leave the volume
  vec3 tmax = max(ttop, tbot); //Distant planes
  t = min(tmax.xx, tmax.yz);//Find the first plane to be exited
  float tfar = min(t.x, t.y);//...

  //Check what the screen depth is to make sure we don't sample the
  //volume past any standard GL objects
  float bufferDepth = texture2D(DepthTexture, gl_FragCoord.xy / WindowSize.xy).r;
  float depth = recalcZCoord(bufferDepth);
  if (tfar > depth) tfar = depth;
  
  //This value is used to ensure that changing the step size does not
  //change the visualization as the alphas are renormalized using it.
  //For more information see the loop below where it is used
  const float baseStepSize = 0.01;

  //We need to calculate the ray's starting position. We add a random
  //fraction of the stepsize to the original starting point to dither
  //the output
  float random = DitherRay * fract(sin(gl_FragCoord.x * 12.9898 + gl_FragCoord.y * 78.233) * 43758.5453); 
  vec3 rayPos = RayOrigin + rayDirection * (tnear + StepSize * random);

  //The color accumulation variable
  vec4 color = vec4(0.0, 0.0, 0.0, 0.0);
  
  //We store the last valid normal, incase we hit a homogeneous region
  //and need to reuse it, but at the start we have no normal
  vec3 lastnorm = vec3(0,0,0); 

  for (float length = tfar - tnear; length > 0.0; 
       length -= StepSize, rayPos.xyz += rayDirection * StepSize)
    {
      //Grab the volume sample
      vec4 sample = texture3D(DataTexture, (rayPos + 1.0) * 0.5);

      //Sort out the normal data
      vec3 norm = sample.xyz * 2.0 - 1.0;
      //Test if we've got a bad normal and need to reuse the old one
      if (dot(norm,norm) < 0.5) norm = lastnorm; 
      //Store the current normal
      lastnorm = norm; 

      //Calculate the color of the voxel using the transfer function
      vec4 src = texture1D(TransferTexture, sample.a);
      
      //This corrects the transparency change caused by changing step
      //size. All alphas are defined for a certain base step size
      src.a = 1.0 - pow((1.0 - src.a), StepSize / baseStepSize);

      ////////////Lighting calculations
      //We perform all the calculations in the model (untransformed)
      //space.
      vec3 lightDir = normalize(LightPosition - rayPos);
      float lightNormDot = dot(normalize(norm), lightDir);
      
      //Diffuse lighting
      float diffTerm =  max(0.5 * lightNormDot + 0.5, 0.5);
      //Quadratic falloff of the diffusive term
      diffTerm *= diffTerm;

      //We either use diffusive lighting plus an ambient, or if its
      //disabled (DiffusiveLighting = 0), we just use the original
      //color.
      vec3 ambient = vec3(0.1,0.1,0.1);
      src.rgb *= DiffusiveLighting * (diffTerm + ambient) + (1.0 - DiffusiveLighting);
      
      //Specular lighting term
      //This is enabled if (SpecularLighting == 1)
      vec3 ReflectedRay = reflect(lightDir, norm);
      src.rgb += SpecularLighting
	* (lightNormDot > 0) //Test to ensure that specular is only
	//applied to front facing voxels
	* vec3(1.0,1.0,1.0) * pow(max(dot(ReflectedRay, rayDirection), 0.0), 96.0);
      
      ///////////Front to back blending
      src.rgb *= src.a;
      color = (1.0 - color.a) * src + color;

      //We only accumulate up to 0.95 alpha (the front to back
      //blending never reaches 1). 
      if (color.a >= 0.95)
	{
	  //We have to renormalize the color by the alpha value (see
	  //below)
	  color.rgb /= color.a;
	  //Set the alpha to one to make sure the pixel is not transparent
	  color.a = 1.0;
	  break;
	}
    }
  /*We must renormalize the color by the alpha value. For example, if
  our ray only hits just one white voxel with a alpha of 0.5, we will have

  src.rgb = vec4(1,1,1,0.5)

  src.rgb *= src.a; 
  //which gives, src.rgb = 0.5 * src.rgb = vec4(0.5,0.5,0.5,0.5)

  color = (1.0 - color.a) * src + color;
  //which gives, color = (1.0 - 0) * vec4(0.5,0.5,0.5,0.5) + vec4(0,0,0,0) = vec4(0.5,0.5,0.5,0.5)

  So the final color of the ray is half way between white and black, but the voxel it hit was white!
  The solution is to divide by the alpha, as this is the "amount of color" added to color.
  */
  color.rgb /= (color.a == 0.0) +  color.a;
  gl_FragColor = color;
});