Skip to content

Instantly share code, notes, and snippets.

@tmpvar
Last active April 25, 2022 17:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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;
}
};
@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