Skip to content

Instantly share code, notes, and snippets.

@tmpvar
Last active October 11, 2024 00:25
Show Gist options
  • Save tmpvar/437363604061ac0cfd1bdd8c41dc5bbe to your computer and use it in GitHub Desktop.
Save tmpvar/437363604061ac0cfd1bdd8c41dc5bbe to your computer and use it in GitHub Desktop.
Branchless 3D DDA cursor
#include <glm/glm.hpp>
using namespace glm;
struct DDACursor {
vec3 mask;
vec3 mapPos;
vec3 rayStep;
vec3 rayDir;
vec3 rayPos;
vec3 sideDist;
vec3 deltaDist;
DDACursor(){}
DDACursor(vec3 pos, vec3 rayDir) {
rayPos = pos;
rayDir = rayDir;
mapPos = glm::floor(pos);
deltaDist = glm::abs(vec3(glm::length(rayDir)) / rayDir);
rayStep = glm::sign(rayDir);
vec3 p = (mapPos - pos);
sideDist = (
rayStep * p + (rayStep * 0.5f) + 0.5f
) * deltaDist;
compute_mask();
}
void compute_mask() {
mask = glm::step(sideDist, vec3(sideDist.y, sideDist.z, sideDist.x)) *
glm::step(sideDist, vec3(sideDist.z, sideDist.x, sideDist.y));
}
void step() {
compute_mask();
sideDist += mask * deltaDist;
mapPos += mask * rayStep;
rayPos += mask * rayStep;
}
};
@Avokadoen
Copy link

Avokadoen commented Apr 25, 2022

@tmpvar
Copy link
Author

tmpvar commented Apr 25, 2022

correct!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment