Skip to content

Instantly share code, notes, and snippets.

@anirul
Last active October 9, 2020 06:42
Show Gist options
  • Save anirul/3875d6571a9fc22868a1f6e02374dedf to your computer and use it in GitHub Desktop.
Save anirul/3875d6571a9fc22868a1f6e02374dedf to your computer and use it in GitHub Desktop.
// This code generate a kernel for SSAO.
#include <cmath>
#include <iostream>
#include <vector>
#include <random>
#include <algorithm>
namespace glm {
class vec3
{
public:
float x, y, z;
vec3(float a, float b, float c) : x(a), y(b), z(c) {}
vec3& operator*=(const float f)
{
x *= f;
y *= f;
z *= f;
return *this;
}
};
vec3 normalize(const vec3& v)
{
float length = std::sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
vec3 norm(v.x / length, v.y / length, v.z / length);
return norm;
}
}
int main()
{
// Noise and kernel.
std::default_random_engine generator;
std::uniform_real_distribution<float> random_length(-1.0, 1.0);
std::uniform_real_distribution<float> random_unit(0.0, 1.0);
for (unsigned int i = 0; i < 64; ++i)
{
glm::vec3 sample(
random_length(generator),
random_length(generator),
random_unit(generator));
sample = glm::normalize(sample);
sample *= random_unit(generator);
float scale = static_cast<float>(i) / 64.0f;
// Scale sample s.t. they are more aligned to the center of the
// kernel
scale = std::lerp(0.1f, 1.0f, scale * scale);
sample *= scale;
[](const glm::vec3& v) {
std::cout << "{" << "\n";
std::cout << "\t\"x\": \"" << v.x << "\",\n";
std::cout << "\t\"y\": \"" << v.y << "\",\n";
std::cout << "\t\"z\": \"" << v.z << "\"\n";
std::cout << "},\n";
}(sample);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment