I do not understand the concept of ray tracing through DDA very well, and I cannot understand the error in my code that does not allow voxels to be displayed correctly from a 3D texture. I kind of tried to do this from the tutorial to create a DDA for 2D graphics, and tried to transfer it to 3D, but it doesn’t work. It is possible, and most likely likely, that I implemented DDA incorrectly, and incorrectly get the voxel value from the 3D texture. I would also like to apply 3 vectors to calculations – the displacement of the model in world coordinates, rotation by Pitch -> Yaw -> Roll, and scaling the model along three axes.
Here is my glsl function:
vec3 trace_dda_voxel(Ray cam_ray, vec3 pos, vec3 rot, vec3 size, sampler3D tex_voxel, vec3 tex_size, float max_dist) {
vec3 ro = cam_ray.pos;
vec3 rd = cam_ray.dir;
vec3 ray_unit_step_size = vec3(
sqrt(1.0 + (rd.y / rd.x) * (rd.y / rd.x) + (rd.z / rd.x) * (rd.z / rd.x)),
sqrt(1.0 + (rd.x / rd.y) * (rd.x / rd.y) + (rd.z / rd.y) * (rd.z / rd.y)),
sqrt(1.0 + (rd.x / rd.z) * (rd.x / rd.z) + (rd.y / rd.z) * (rd.y / rd.z))
);
vec3 map_check = floor(ro);
vec3 ray_length_1d;
vec3 step;
float traveled_distance = 0.0;
if (rd.x < 0.0) {
step.x = -1.0;
ray_length_1d.x = (ro.x - float(map_check.x)) * ray_unit_step_size.x;
} else {
step.x = 1.0;
ray_length_1d.x = (float(map_check.x + 1) - ro.x) * ray_unit_step_size.x;
}
if (rd.y < 0.0) {
step.y = -1.0;
ray_length_1d.y = (ro.y - float(map_check.y)) * ray_unit_step_size.y;
} else {
step.y = 1.0;
ray_length_1d.y = (float(map_check.y + 1) - ro.y) * ray_unit_step_size.y;
}
if (rd.z < 0.0) {
step.z = -1.0;
ray_length_1d.z = (ro.z - float(map_check.z)) * ray_unit_step_size.z;
} else {
step.z = 1.0;
ray_length_1d.z = (float(map_check.z + 1) - ro.z) * ray_unit_step_size.z;
}
while(traveled_distance < max_dist) {
if (ray_length_1d.x < ray_length_1d.y && ray_length_1d.x < ray_length_1d.z) {
map_check.x += step.x;
traveled_distance = ray_length_1d.x;
ray_length_1d.x += ray_unit_step_size.x;
} else if (ray_length_1d.y < ray_length_1d.z) {
map_check.y += step.y;
traveled_distance = ray_length_1d.y;
ray_length_1d.y += ray_unit_step_size.y;
} else {
map_check.z += step.z;
traveled_distance = ray_length_1d.z;
ray_length_1d.z += ray_unit_step_size.z;
}
int vox_id = int(texture(u_tex_voxel, map_check/tex_size).r * 255.0);
if (vox_id == 255) return vec3(1.0);
}
return vec3(0.0);
}
And here’s what I get: