2

I have tried to make better quality of my volume ray casting algorithm. I have set a smaller step of raycast (quality is better), but it causes problem. It is on pictures below (black areas where they shouldnt be).

I am using RGB cube to get direction of ray in volume. I think, i have the same algorithm like there: volume rendering (using glsl) with ray casting algorithm

Have anybody some ideas, where could be a problem? I need to resolve this, because deadline of my diplom thesis is to close:( I realy don't know, why it doesnt work:(

enter image description here enter image description here

EDIT: I cant show there my all code (it could be problem, if i will supply it before hand it in school). But the key code to going throught the volume:

  // All variables neede to rays
  vec3 rayDirection = texture2D(backFaceCube, texCoo).xyz - varcolor.xyz;
  float lenRay = length(rayDirection);
  vec3 normDir = normalize(rayDirection);
  float d = qualitySteps; //quality steps is size of steps defined by user -> example: 0.01, 0.001, 0.0001 etc.
  vec3 step = normDir * d;
  float lenStep = length(step);
  float accumulatedLength = 0.0;

and then in cycle:

      posInCube.xyz += step;
      accumulatedLength += lenStep;

      ...
      ...
      ...

      if(accumulatedLength >= lenRay || accumulatedColor.a > 1.0 ) {
         break;
      }

EDIT2:(sorry but like comment it was too long)

Yes, the texture is noisy...i have tried to delete the condition with alpha: if(accumulatedColor.a > 1.0), but the result is same. I think that there is some direct correlation with length of ray and size of step. I tried many combination and i have found these things. If step is big, i am able to go throught all volume, but if it is small, than i am realy not able to go throught volume (maybe). If step is extremely big, than i can see mirroved object (it can be caused by repeating texture if i go out of the texture on GPU). If step is too small, than i am able to mapped only small part of texture -> it seems, that ray is too short, but in reality he isnt. Questins are, why mapping of 3D coordinates to 2D texture is wrong and depend on size of step..

Community
  • 1
  • 1
Xrew
  • 173
  • 13
  • did you fix it by any chance? if so, is the code open source? I'm really interested in this.thanks – Nicolas Feb 09 '15 at 20:08

1 Answers1

1

Can you please supply the code for your fragment shader?

Are you traversing the whole vector from front to end position? Here's an example shader (the code might contain some errors since I just wrote it from the top of my head. I unfortunately can't test the code on my computer at the moment):

in vec2 texCoord;
out vec4 outColor;
uniform float stepSize;
uniform int numSteps;
uniform sampler2d frontTexture;
uniform sampler2d backTexture;
uniform sampler3d volumeTexture;
uniform sampler1d transferTexture; // Density to RGB

void main()
{
    vec4 color = vec4(0.0);
    vec3 startPosition = texture(frontTexture, texCoord);
    vec3 endPosition = texture(backTexture, texCoord);
    vec3 delta = normalize(startPosition - endPosition) * stepSize;
    vec3 position = startPosition;

    for (int i = 0; i < numSteps; ++i)
    {
        float density = texture(volumeTexture, position).r;
        vec3 voxelColor = texture(transferTexture, density);

        // Sampling distance correction
        color.a = 1.0 - pow((1.0 - color.a), stepSize * 500.0);

        // Front to back blending (no shading done)
        color.rgb = color.rgb + (1.0 - color.a) * voxelColor.a * voxelColor.rgb;
        color.a = color.a + (1.0 - color.a) * voxelColor.a;

        if (color.a >= 1.0)
        {
            break;
        }

        // Advance
        position += direction;

        if (position.x > 1.0 || position.y > 1.0 || position.z > 1.0)
        {
            break;
        }
    }

    outColor = color;
}
Glass
  • 11
  • 1
  • I have posted part of my shader above. I think, that your solution is exactly the same like mine...I have no idea what iam doing wrong :( thank you ! :-) – Xrew Apr 30 '13 at 08:11
  • It seems, that if the size of step is smaller, than len of ray is smaller too. Question is, why? I simulate 3D texture in fragment by 2D texture (WebGL doesnt have 3D textures). could be there some problem with size of step? – Xrew Apr 30 '13 at 11:40
  • Do you have a noisy texture? According to your description you get the second screenshot if your step size is smaller. If the step size is smaller you would calculate more samples in the same distance. If you have a noisy texture, this might cause your alpha value to go above 1.0 very early (thus the dark areas?). Are you using any sampling distance correction like my code above? Just as a sidenote, in your sample code lenStep is the same value as qualityStep. Length of a normalized vector is 1, so `lenStep = length(step) = length(normDir * d) = d * length(normDir) = d * 1 = qualityStep`. – Glass May 01 '13 at 04:42
  • Response is in EDIT2. It was too long :( – Xrew May 01 '13 at 11:46