2

I am working on volumetric raycasting and I am having a hard time finding the way to calculate the step size so that every step, the ray would step to a new voxel in my fragment shader GLSL.

I have a 3D box of dimension which doesn't have equal dimension on all side (x,y,z) and I already have that value and I also have a vec3 ray direction.

I need to know the step size for the ray or normalized ray to traverse from the starting and end of the hitting point in the cube.

From the axis-aligned box intersection, I know the tmin and tmax.

I know the code of AABI is irrelevant but I am adding this for any reference if needed.

vec2 boxIntersection(vec3 ray_direction2, float origin[3]){
  
  float boxmin[3] = float[3](0.0, 0.0, 0.0);
  float boxmax[3] = float[3](1.0, 1.0, 1.0);
  vec3 invdir = 1.0/ray_direction2;

  float inv_raydirection[3] = float[3](invdir.x, invdir.y, invdir.z);
  for(int i=0; i<3; i++ ){
    float t1 = (boxmin[i]-origin[i])*inv_raydirection[i];
    float t2 = (boxmax[i]-origin[i])*inv_raydirection[i];

    tmin = min(tmin, min(t1, t2));
    tmax = max(tmax, max(t1, t2));

    if(tmax>max(tmin,0.0)){
      return vec2(tmin, tmax);
    }
    else{
      discard;
    }
  }

Pravin Poudel
  • 1,433
  • 3
  • 16
  • 38
  • thanks for the response, I have updated my question. Could you please help me with my problem !!! – Pravin Poudel Aug 16 '20 at 16:40
  • 1
    If voxels, not frags?, You need 3 vectors, 1 for each plane (x,y,z). Each vector has length to step one voxel plane. With 3 starting coords (one on entry plane other two back to closest other plane before entry) you step the 3 vector planes looking for the nearest new plane crossing to get the next voxel, Only step plane forward when used as closest plane crossing (many caveats) .,Or adapt Bresenham's line algorithm. The 3 plane vectors replaced with error (y,z) as `vec2`, and error accumulator as `vec2` (y,z) Will give vox x coord and number voxs till next plane y,or z – Blindman67 Aug 17 '20 at 17:07
  • I have been reading multiple papers for that and I think i understand what you are saying. Could you please make an effort to put more words in the answer so that I and other who will come to this problem in the future can learn more and if possible illustratively. Thanks a lot for your response. Much appreciated !!! – Pravin Poudel Aug 19 '20 at 03:56
  • maybe this [simple GLSL 3D Voxel back ray tracer](https://stackoverflow.com/a/48092685/2521214) will help its for cube voxels so you just need to adjust scale for the different sizes... its just 3D port of this [2D ray caster](https://stackoverflow.com/a/47251071/2521214) where at the end the image explains how it works – Spektre Oct 05 '20 at 08:07

0 Answers0