Skip to content

Instantly share code, notes, and snippets.

@RedstoneWizard08
Created January 5, 2024 19:32
Show Gist options
  • Save RedstoneWizard08/1f7a936dd00893561e8e40ac91e3f3cc to your computer and use it in GitHub Desktop.
Save RedstoneWizard08/1f7a936dd00893561e8e40ac91e3f3cc to your computer and use it in GitHub Desktop.
Perlin noise on a sphere!!!
// Needs:
// - PerlinNoise (https://github.com/Reputeless/PerlinNoise)
// - GLM
// - >= C++ 11 (I used C++ 20)
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <glm/glm.hpp>
#include <glm/ext.hpp>
#include "PerlinNoise.hpp"
#define radians(v) v * (M_PI / 180)
#define degrees(v) v * (180 / M_PI)
using namespace std;
using namespace siv;
using namespace glm;
vec3 rotate(vec3 v, vec3 k, float theta) {
float cos_theta = cos(theta);
float sin_theta = sin(theta);
return (v * cos_theta) + (cross(k, v) * sin_theta) + (k * dot(k, v)) * (1 - cos_theta);
}
vec3 solve(float radius, float angle, float z) {
float x = radius * cos(radians(angle));
float y = radius * sin(radians(angle));
return vec3(x, y, z);
}
int main() {
const float radius = 4.0f;
const float resolution = 64.0;
const PerlinNoise::seed_type seed = 123456u;
const PerlinNoise perlin { seed };
float change = 360.0f / resolution;
for (int i = 0; i < resolution; i++) {
vec3 zPoint = solve(radius, change * i, 0.0f);
for (int j = 0; j < resolution; j++) {
vec3 pos = solve(zPoint.y, change * j, zPoint.x);
float y_angle = change * j;
float z_angle = change * i;
double noise = perlin.octave3D_01(pos.x, pos.y, pos.z, 4);
vec3 noise_vec = vec3(0.0f, noise, 0.0f);
const vec3 y_up = vec3(0.0f, 1.0f, 0.0f);
const vec3 z_up = vec3(0.0f, 0.0f, 1.0f);
noise_vec = rotate(noise_vec, z_up, y_angle);
noise_vec = rotate(noise_vec, y_up, z_angle);
pos = pos + noise_vec;
printf("%f, %f, %f\n", pos.x, pos.y, pos.z);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment