Skip to content

Instantly share code, notes, and snippets.

@sergof sergof/triangle_sample.cpp
Last active Feb 13, 2019

Embed
What would you like to do?
/* Quasi random point sequence
* based on: http://extremelearning.com.au/a-simple-method-to-construct-isotropic-quasirandom-blue-noise-point-sequences/ */
void r2(float *x, float *y, uint32_t i, float jitter)
{
assert(i > 0);
assert(jitter >= 0.0f);
float phi = 1.324717957244746f;
float sqrtpi = 1.7724538509055159f;
float a0 = 1.0f / phi;
float a1 = 1.0f / (phi*phi);
float d0 = 0.76f;
float i0 = 0.7f;
/* based on: http://marc-b-reynolds.github.io/shf/2017/09/27/LPRNS.html */
uint32_t c = i;
c ^= c >> 15; c *= 0x85ebca77; /* XXHASH prime */
c ^= c >> 13; c *= 0xc2b2ae3d; /* XXHASH prime */
float n0 = (1.0f / 4294967295.0f) * (float)(c * 741103597);
float n1 = (1.0f / 4294967295.0f) * (float)(c * 1597334677);
float f = (float)i;
jitter = (jitter * sqrtpi * d0) / (4.0f * sqrtf(f - i0));
*x = fmodf(a0 * f + jitter * n0, 1.0f);
*y = fmodf(a1 * f + jitter * n1, 1.0f);
}
/* methods to pick points inside a triangle based on "Generating Random Points in Triangles" form Graphics Gems 1 */
void draw_samples_parallelogram(vec3 v0, vec3 v1, vec3 v2, int n, float jitter, uint32_t color)
{
/* we could sort the triangle corners based on their angles for somewhat better results */
for (int i = 1; i <= n; i++) {
float u;
float w;
r2(&u, &w, i, jitter);
if (u + w > 1.0f) {
u = 1.0f-u;
w = 1.0f-w;
}
vec3 p = (1 - u - w) * v0 + u * v1 + w * v2;
draw_point(Renderer, p, color);
}
}
void draw_samples_fibonacci(vec3 v0, vec3 v1, vec3 v2, int n, float jitter, uint32_t color)
{
for (int i = 1; i <= n; i++) {
float u;
float w;
r2(&u, &w, i, jitter);
float su = sqrtf(u);
u = 1.0f - su;
w = w * su;
vec3 p = (1 - u - w) * v0 + u * v1 + w * v2;
draw_point(Renderer, p, color);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.