4 Commits

Author SHA1 Message Date
bc263cfcb9 uusin 2025-07-31 20:15:39 +03:00
efac210beb poistettu valot 2025-07-31 18:16:10 +03:00
b1f6fab663 fix 2025-07-31 18:03:01 +03:00
aa949ba2a7 uus fft 2025-07-31 16:27:44 +03:00
4 changed files with 81 additions and 364 deletions

View File

@ -1,78 +1,28 @@
#include "fft.h"
#include <math.h>
static float window[FFT_SIZE];
constexpr float PI = 3.14159;
constexpr float M_PI = 3.14159;
// In-place FFT on array of Complex numbers
void fft(Complex* x, int N, Complex* buffer) {
if (N <= 1) return;
Complex* even = buffer;
Complex* odd = buffer + N / 2;
// Call once before use
void init_hamming_window() {
for (int i = 0; i < FFT_SIZE; i++) {
window[i] = 0.54f - 0.46f * cosf(2.0f * (float)M_PI * i / (FFT_SIZE - 1));
}
}
static unsigned int bit_reverse(unsigned int x, int log2n) {
unsigned int n = 0;
for (int i = 0; i < log2n; i++) {
n <<= 1;
n |= (x & 1);
x >>= 1;
}
return n;
}
void compute_fft(float* time_data, float* freq_out) {
static float real[FFT_SIZE];
static float imag[FFT_SIZE];
int log2n = 0;
for (int t = FFT_SIZE; t > 1; t >>= 1) ++log2n;
// Apply Hamming window
for (int i = 0; i < FFT_SIZE; i++) {
real[i] = time_data[i] * window[i];
imag[i] = 0.0f;
for (int i = 0; i < N / 2; ++i) {
even[i] = x[i * 2];
odd[i] = x[i * 2 + 1];
}
// Bit reversal
for (int i = 0; i < FFT_SIZE; ++i) {
int j = bit_reverse(i, log2n);
if (j > i) {
float tmp_re = real[i], tmp_im = imag[i];
real[i] = real[j]; imag[i] = imag[j];
real[j] = tmp_re; imag[j] = tmp_im;
}
fft(even, N / 2, buffer + N); // deeper even
fft(odd, N / 2, buffer + N + N / 2); // deeper odd
for (int k = 0; k < N / 2; ++k) {
double angle = -2 * PI * k / N;
Complex twiddle(cos(angle), sin(angle));
Complex t = twiddle * odd[k];
x[k] = even[k] + t;
x[k + N / 2] = even[k] - t;
}
// Cooley-Tukey FFT
for (int s = 1; s <= log2n; ++s) {
int m = 1 << s;
for (int k = 0; k < FFT_SIZE; k += m) {
for (int j = 0; j < m / 2; ++j) {
int t = k + j;
int u = t + m / 2;
float angle = -2.0f * (float)M_PI * j / m;
float w_real = cosf(angle);
float w_imag = sinf(angle);
float re = w_real * real[u] - w_imag * imag[u];
float im = w_real * imag[u] + w_imag * real[u];
real[u] = real[t] - re;
imag[u] = imag[t] - im;
real[t] += re;
imag[t] += im;
}
}
}
for (int i = 0; i < FFT_SIZE / 2; ++i) {
float mag = sqrtf(real[i] * real[i] + imag[i] * imag[i]) / FFT_SIZE;
float db = 20.0f * log10f(mag + 1e-6f); // Decibels
float normalized = (db + 60.0f) / 60.0f; // [0,1]
freq_out[i] = mag;
}
}
}

View File

@ -3,5 +3,16 @@
#include <math.h>
#define FFT_SIZE 2048
void init_hamming_window();
void compute_fft(float* time_data, float* freq_out);
// Simple complex number struct
struct Complex {
float re, im;
Complex(float r = 0, float i = 0) : re(r), im(i) {}
Complex operator+(const Complex& o) const { return { re + o.re, im + o.im }; }
Complex operator-(const Complex& o) const { return { re - o.re, im - o.im }; }
Complex operator*(const Complex& o) const {
return { re * o.re - im * o.im, re * o.im + im * o.re };
}
};
void fft(Complex* x, int N, Complex* buffer);

View File

@ -16,17 +16,14 @@
#define USE_AUDIO 1
#define NO_UNIFORMS 0
#define SHAPES 16
#define SHAPES_TEX_SIZE (SHAPES * SHAPES * 4)
#define SHAPES_TOTAL (SHAPES * SHAPES)
#define MAX_DISTANCE 100.f
#include "definitions.h"
#if OPENGL_DEBUG
#include "debug.h"
#endif
#include "glext.h"
#include "fft.h"
#pragma data_seg(".shader")
#include "shaders/fragment.inl"
#if POST_PASS
@ -34,8 +31,6 @@
#include "shaders/post.inl"
#endif
#include "fft.h"
#pragma data_seg(".pids")
// static allocation saves a few bytes
static int pidMain;
@ -44,6 +39,11 @@ static int pidPost;
#ifndef EDITOR_CONTROLS
#pragma code_seg(".main")
// FFT buffers
static Complex signal[FFT_SIZE];
static Complex buffer[3 * FFT_SIZE];
static float fft_uniform[FFT_SIZE / 4];
void entrypoint(void)
#else
#include "editor.h"
@ -112,7 +112,7 @@ int __cdecl main(int argc, char* argv[])
track.play();
double position = 0.0;
#endif
static float syncs[1 + SU_NUMSYNCS];
long playCursor = 0;
long lastPlayCursor = -1;
volatile float maximum = 0.0; // Helper variable to calculate maximum fft output for normalization
@ -123,30 +123,6 @@ int __cdecl main(int argc, char* argv[])
// Play sound
direct_sound_buffer->Play(0, 0, 0);
static float syncs[1 + SU_NUMSYNCS];
// Init FFT
init_hamming_window();
// FFT buffers
static float fft_input[FFT_SIZE];
static float fft_output[FFT_SIZE / 2]; // Magnitudes
static float fft_uniform[FFT_SIZE / 4];
static float shapesData[SHAPES_TEX_SIZE];
// main note effect
boolean beenPlaying = false;
const int SHAPES_SIZE = 15;
float shapeIncrement = 0.15f;
int currentShape = 0; // mark location which shape we are currently building
boolean isPlaying = false;
float lastPlayPos = 0;
float lastNote = 0.0f;
struct Vec3 {
float x, y, z;
};
@ -156,18 +132,6 @@ int __cdecl main(int argc, char* argv[])
PFNGLUNIFORM1IPROC glUniform1i = ((PFNGLUNIFORM1IPROC)wglGetProcAddress("glUniform1i"));
PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation = ((PFNGLGETUNIFORMLOCATIONPROC)wglGetProcAddress("glGetUniformLocation"));
GLuint ShapesTex;
glGenTextures(1, &ShapesTex);
glBindTexture(GL_TEXTURE_2D, ShapesTex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, SHAPES, SHAPES, 0, GL_RGBA, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
const ULONGLONG targetIntervalMs = 1000 / 60; // For 60 FPS FFT updates
do
@ -221,7 +185,7 @@ int __cdecl main(int argc, char* argv[])
{
SUsample* samples = (SUsample*)audio_ptr;
for (int i = 0; i < FFT_SIZE; ++i) {
fft_input[i] = (float)samples[i];
signal[i] = Complex((float)samples[i], 0.0);
}
}
@ -229,91 +193,29 @@ int __cdecl main(int argc, char* argv[])
}
// Calculate FFT
compute_fft(fft_input, fft_output);
fft(signal, FFT_SIZE, buffer);
// Normalize output
for (int i = 0; i < (FFT_SIZE / 4); i++)
{
float gain = 50.0f;
float alpha = 0.10f; // "Hidastaa" FFT:n piikkej<EFBFBD>
float threshhold = 0.015f; // Alin arvo mik<69> p<><70>stet<65><74>n shaderille (v<>hent<6E><74> "noisea")
float x_t = (fft_output[i] < threshhold) ? 0.f : fft_output[i] * gain;
float gain = 0.05f;
float alpha = 0.10f; // "Hidastaa" FFT:n piikkejä
float threshhold = 0.00015f; // Alin arvo mik<69> p<><70>stet<65><74>n shaderille (v<>hent<6E><74> "noisea")
// float magnitude = sqrt(signal[i].re * signal[i].re + signal[i].im * signal[i].im); // signal strength
float magnitude = (float)signal[i].re;
float x_t = (magnitude < threshhold) ? 0.f : magnitude * gain;
// Exponential smoothing kaava
// s(t) = alpha*x(t)+(1-alpha)*s(t-1)
fft_uniform[i] = alpha * (x_t)+(1 - alpha) * fft_uniform[i];
}
}
syncs[0] = (float)playCursor / (SU_SAMPLE_RATE * SU_CHANNEL_COUNT * SU_SAMPLE_SIZE); // Aika sekunteina.
for (int i = 0; i < SU_NUMSYNCS; ++i)
{
syncs[i + 1] = syncBuf[(playCursor / (2 * sizeof(SUsample)) >> 8) * SU_NUMSYNCS + i];
}
//////////////////////////////////////////////////////
// Shape builder
//////////////////////////////////////////////////////
// if sound is playing, start a shape, if shape is already started - add length
// if sound has stopped, end shape
// if shape is finished, move shape forward
float captureSync = syncs[5];
bool shapeJustFinished = false;
bool shapeJustStarted = false;
// Detect note change to start a new shape
bool noteChanged = (captureSync != lastNote) ? TRUE : FALSE;
lastNote = captureSync;
// If note changed and value is significant, start new shape
if (noteChanged) {
int index = currentShape * 4;
// Reset and activate current shape
shapesData[index + 0] = 0.0f; // x start
shapesData[index + 1] = captureSync; // y from audio
shapesData[index + 2] = 1.0f; // z (unused or length)
shapesData[index + 3] = 1.0f; // active
// Advance to next shape slot (circular)
currentShape = (currentShape + 1) % SHAPES_TOTAL;
}
// Move active shapes
for (int i = 0; i < SHAPES_TOTAL; ++i) {
int index = i * 4;
// Check if shape is active
if (shapesData[index + 3] > 0.5f) {
// Move shape right
shapesData[index + 0] += shapeIncrement;
// Update y (optional, reflect live audio)
//shapesData[index + 1] = captureSync;
// Optional: grow z value to show duration
//shapesData[index + 2] += shapeIncrement * 0.2f;
// If x exceeds max distance, deactivate and reset
if (shapesData[index + 0] > MAX_DISTANCE) {
shapesData[index + 0] = 0.0f;
shapesData[index + 1] = 0.0f;
shapesData[index + 2] = 0.0f;
shapesData[index + 3] = 0.0f; // inactive
}
}
}
// Bind and update u_ShapesTex
glUniform1i(glGetUniformLocation(pidMain, "u_ShapesTex"), 0);
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, ShapesTex);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, SHAPES, SHAPES, GL_RGBA, GL_FLOAT, shapesData);
glUniform1fvProc(0, SU_NUMSYNCS + 1, syncs);
glUniform1fvProc(20, FFT_SIZE / 4, fft_uniform);

View File

@ -6,22 +6,16 @@ const float TAU = (2. * PI);
const float PHI = sqrt(5.) * 0.5 + 0.5;
layout(location = 0) uniform float syncs[11];
layout(location = 20) uniform float fft_output[512]; // FFT_SIZE / 4
layout(location = 600) uniform vec3 shapes[15]; // shapes - x = horizontal position, y = vertical position, z = length
layout(location = 700) uniform vec3 test; // shapes test
//layout(binding = 1) uniform sampler2D u_hexGridTex; //uniform sampler2D u_fft_texture;
layout(binding = 0) uniform sampler2D u_ShapesTex; // uniform sampler2D shapes texture
// float u_time = syncs[0];
vec3 palette(float t){
vec3 a=vec3(0.46,0.2,0.94);
vec3 b=vec3(0.66,0.64,0.77);
vec3 c=vec3(0.91,0.62,0.97);
vec3 d=vec3(0.26,0.2,0.84);
return a+b*cos(6.28318*(c*t+d));
vec3 palette(float t) {
vec3 a = vec3(0.46, 0.2, 0.94);
vec3 b = vec3(0.66, 0.64, 0.77);
vec3 c = vec3(0.91, 0.62, 0.97);
vec3 d = vec3(0.26, 0.2, 0.84);
return a + b * cos(6.28318 * (c * t + d));
}
vec2 getUV() {
const vec2 scale = vec2(0.00104166667, 0.00185185185);
return gl_FragCoord.xy * scale - 1.0;
@ -110,21 +104,29 @@ vec2 opU(vec2 d1, vec2 d2) {
return (d1.x < d2.x) ? d1 : d2;
}
// float opU( float d1, float d2 ) { return -max( -d1, -d2 ); }
float noyce(vec2 axial) {
if(mod(floor(syncs[0]), 2.) == 0.) {
return mix(noise(axial, 0.1), noise(axial, 0.2), sin(syncs[0] * 2.));
}
return mix(noise(axial, 0.2), noise(axial, 0.1), sin(syncs[0] * 2.));
}
vec2 mapScene(in vec3 p) {
float res = p.y;
float mat = 0.;
float hexRadius = 0.85;
float hexRadius = 0.83;
vec3 hexpos = vec3(p.x, p.y - 2.5, p.z);
HexData hex = hexTile(hexpos, 1.5);
HexData hex = hexTile(hexpos, 1.1);
float distFromCenter = hexDistance(hex.axial);
int fftIndex = int(clamp(distFromCenter + 1.0, 0.0, 511.0));
float fftVal = fft_output[fftIndex];
float hexHeight = 1.0 + fftVal * 4.0;
float noise = noyce(hex.axial);
// float hexHeight = 1.0 + fftVal * 5.0 + noise;
float hexHeight = 1.0 + noise;
// Rotate individual hex tiles if needed
vec3 r = hex.local;
@ -136,28 +138,6 @@ vec2 mapScene(in vec3 p) {
res = (d1 < res) ? d1 : res;
/* const float gridSize = 16.;
for(float j = 0.; j < gridSize; j++) {
for(float i = 0.; i < gridSize; i++) {
ivec2 texSize = textureSize(u_ShapesTex, 0);
vec2 texCoord = (vec2(i, j)) / vec2(texSize);
vec4 shapeData = texture(u_ShapesTex, texCoord); // RGBA: x, y, length, active
float shapeActive = shapeData.a;
if(shapeActive < 0.5)
continue;
vec3 shapePos = p - vec3(-70.0 + (shapeData.x * 2.), 12. + (shapeData.y * 80.), 0.0);
float a = sdSphere(shapePos, 0.8);
res = min(res, a);
if(res == a) {
mat = 1.0;
}
}
}*/
return vec2(res, mat);
}
@ -247,140 +227,41 @@ vec3 addPointLight(vec3 lightPos, vec3 lightColor, float intensity, vec3 worldPo
return (diffuse + specular * fresnel) * shadow;
}
float getAmbientOcc(vec3 p, vec3 n) {
float occ = 0.;
float weight = 1.;
for(int i = 0; i < 8; i++) {
float len = 0.01 + 0.02 * float(i * i);
float dist = mapScene(p + n * len).x;
occ += (len - dist) * weight;
weight *= 0.85;
}
return 1.0 - clamp(0.6 * occ, 0., 1.);
}
vec3 applyFog(vec3 col, float t, vec3 rd, vec3 lightDir, float fogAmount) {
float syncsBass = clamp((syncs[1] + syncs[2] + syncs[3]), 0., 1.);
float fogAmount2 = 1.0 - exp(-t * fogAmount);
float sunAmount = max(dot(rd, lightDir), 0.0);
float sunAmount = max(dot(rd, lightDir), 1.0);
// highlight color
vec3 fogColor = mix(vec3(0.2706, 0.2706, 0.2863), vec3(0.2314, 0.2314, 0.2314), // Main color
pow(sunAmount, syncsBass * 1.0));
return mix(col, fogColor, fogAmount2);
}
// Point light with no shadow and radius based falloff
vec3 addPointLightNoShadow(vec3 lightPos, vec3 lightColor, float intensity, float radius, vec3 worldPos, vec3 viewDir, vec3 normal, float roughness) {
// Light vector from surface to light
vec3 lightDir = lightPos - worldPos;
float lightDistance = length(lightDir);
lightDir = normalize(lightDir);
// Attenuation (radius based falloff)
float attenuation = clamp(intensity - lightDistance * lightDistance / (radius * radius), 0.0, 1.0);
// Diffuse lighting (Lambert)
float NdotL = max(dot(normal, lightDir), 0.0);
vec3 diffuse = lightColor * NdotL * attenuation;
// Specular lighting (Blinn-Phong)
vec3 halfDir = normalize(lightDir + (-viewDir));
float NdotH = max(dot(normal, halfDir), 0.0);
float shininess = mix(128.0, 8.0, roughness); // Convert roughness to shininess
vec3 specular = lightColor * pow(NdotH, shininess) * attenuation;
// Fresnel effect
vec3 F0 = vec3(0.04); // Base reflectance for dielectrics
vec3 fresnel = F0 + (1.0 - F0) * pow(clamp(1.0 - max(dot(halfDir, lightDir), 0.0), 0.0, 1.0), 5.0);
// Soft shadows
//float shadow = softshadow(worldPos + normal * 0.01, lightDir, 0.02, lightDistance, 4.0);
// Combine diffuse and specular with shadow
return diffuse + specular * fresnel;
}
/*vec3 addPointLight(vec3 lightPos, vec3 lightColor, float intensity, vec3 worldPos, vec3 viewDir, vec3 normal) {
vec3 lightDir = normalize(lightPos - worldPos);
float lightDistance = length(lightPos - worldPos);
// Attenuation
float attenuation = intensity / (1.0 + 0.1 * lightDistance + 0.01 * lightDistance * lightDistance);
// Diffuse
float NdotL = max(dot(normal, lightDir), 0.0);
// Specular (Blinn-Phong)
vec3 halfDir = normalize(lightDir - viewDir);
float NdotH = max(dot(normal, halfDir), 0.0);
float specular = pow(NdotH, 32.0);
// Shadow
float shadow = softshadow(worldPos + normal * 0.01, lightDir, 0.01, lightDistance, 8.0);
return lightColor * (NdotL + specular * 0.5) * attenuation * shadow;
}
*/
vec3 shading(vec3 p, vec3 n, vec3 dir, float material) {
float shininess = 0.01;
float shininess = 0.0;
vec3 outMaterial = vec3(0.);
if(material == 0.) {
outMaterial = vec3(0.4941, 0.4941, 0.4941);
outMaterial = palette(p.y * 0.1);
if(syncs[0] > 5.) {
outMaterial = vec3(0.5);
}
outMaterial = mix(palette(p.y * 0.1), vec3(0.5), abs(sin(syncs[0] * 0.2)));
shininess = 0.6;
}
if(material == 1.) {
outMaterial = vec3(0.5);
shininess = 0.6;
} else if(material == 1.) {
outMaterial = vec3(0.6196, 0.6118, 0.6118);
shininess = .7;
} else if(material == 2.) {
outMaterial = vec3(0.3255, 0.4784, 0.3255);
shininess = .2;
} else if(material == 3.) {
outMaterial = vec3(0.2471, 0.3059, 0.6314);
shininess = 1.0;
} else if(material == 4.) {
outMaterial = vec3(0.9961, 1.0, 0.9922);
shininess = .1;
} else if(material == 5.) {
outMaterial = vec3(0.9961, 1.0, 0.9922);
shininess = .3;
}
vec3 lights = vec3(0.);
lights += addPointLight(vec3(0., 20.0, 10.), vec3(0.77, 0.26, 0.73), 15.0, p, dir, n, shininess);
lights += addPointLight(vec3(-10., 20.0, -10.), vec3(0.18, 0.61, 0.86), 15., p, dir, n, shininess);
// LIGHTS IN HEX GRID PATTERN
/*
float gridsize = 16.0; // or GRID if you want full size
vec3 p = vec3(0.);
for(float j = 0.; j < gridsize; j++) {
for(float i = 0.; i < gridsize; i++) {
ivec2 texSize = textureSize(u_hexGridTex, 0);
vec2 texCoord = (vec2(i, j)) / vec2(texSize);
vec4 hexData = texture(u_hexGridTex, texCoord); // RGBA: x, y, z, dist
vec3 hexPos = hexData.rgb;
float hexDist = hexData.a;
// Optionally use hexDist for ripple effect with FFT
int index = clamp(int((hexDist / 34.)*512.), 0, 511);
float hexSize = getScaledFFT(index, 15. ,0.);
//float a = sdHex(p - hexPos, 1.0 + hexSize, 0.0);
lights += addPointLightNoShadow(vec3(hexPos.x, (hexPos.y + hexSize) + 2., hexPos.z), palette(hexSize* 1.5), clamp(hexSize * 5., 0., 1.), 3.4 ,v, dir, n, shininess);
}
}
*/
vec3 lightDir = vec3(0., 2., 3);
//float sun_dif = clamp(dot(n, lightDir), 0., 1.);
//float shadow = softshadow(v + n * 0.01, lightDir, .01, 30., 18.);
//lights += vec3(0.6431, 0.7804, 0.8588) * sun_dif * shadow * occ;
float ind = clamp(dot(n, normalize(lightDir * vec3(.0, 1.0, -2.0))), 0.0, 1.0);
lights += vec3(0.08, 0.62, 0.75) * ind * 0.8;
@ -438,7 +319,7 @@ vec3 render(vec2 uv) {
}
//glow from the bottom
vec3 bGlowColor = palette(syncs[0] * .5); // color change
vec3 bGlowColor = palette(syncs[0] * .075); // color change
float bGlowDistance = 0.3;
vec3 p = camPos + t.x * rayDir;
@ -457,31 +338,4 @@ void main() {
vec3 finalColor = render(getUV());
finalColor = postProcess(finalColor);
o = vec4(finalColor, 1.);
}
/*
vec3 render2(vec2 uv, float time) {
vec3 res = vec3(.0);
float pos = syncs[5];
if (uv.x >= pos && uv.x <= pos+0.01 ) {
res += vec3(1.);
}
//if (uv.x >= 0.0 && uv.x <= 0.01 ) {
// res += vec3(abs(syncs[4]*2));
//}
//res += vec3(0.1, 0.2, 0.3) * abs(syncs[2]*1.2);
return res;
}
void main() {
vec2 uv = gl_FragCoord.xy * 2. / vec2(1920,1080);
vec3 col = render2(uv, u_time);
o = vec4(col,1.0);
}
*/
}