1 Commits

Author SHA1 Message Date
15afab09cb aaltogeneraattori(wip) 2025-07-19 18:38:36 +03:00
7 changed files with 318 additions and 207 deletions

View File

@ -606,6 +606,7 @@ shader_minifier.exe -v -o src\shaders\post.inl src\shaders\post.frag</Command>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Heavy Release|Win32'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Heavy Release|Win32'">true</ExcludedFromBuild>
</ClInclude> </ClInclude>
<ClInclude Include="src\waveGenerator.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="src\editor.cpp"> <ClCompile Include="src\editor.cpp">
@ -621,6 +622,7 @@ shader_minifier.exe -v -o src\shaders\post.inl src\shaders\post.frag</Command>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Snapshot|Win32'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Snapshot|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
</ClCompile> </ClCompile>
<ClCompile Include="src\waveGenerator.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="src\shaders\fragment.frag"> <None Include="src\shaders\fragment.frag">

View File

@ -5,6 +5,7 @@
<ClCompile Include="src\song.cpp" /> <ClCompile Include="src\song.cpp" />
<ClCompile Include="src\editor.cpp" /> <ClCompile Include="src\editor.cpp" />
<ClCompile Include="src\fft.cpp" /> <ClCompile Include="src\fft.cpp" />
<ClCompile Include="src\waveGenerator.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="src\debug.h" /> <ClInclude Include="src\debug.h" />
@ -19,6 +20,7 @@
<ClInclude Include="src\editor.h" /> <ClInclude Include="src\editor.h" />
<ClInclude Include="src\sointu\song.h" /> <ClInclude Include="src\sointu\song.h" />
<ClInclude Include="src\fft.h" /> <ClInclude Include="src\fft.h" />
<ClInclude Include="src\waveGenerator.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="src\shaders\post.inl"> <None Include="src\shaders\post.inl">

View File

@ -30,6 +30,7 @@
#endif #endif
#include "fft.h" #include "fft.h"
#include "waveGenerator.h"
#pragma data_seg(".pids") #pragma data_seg(".pids")
// static allocation saves a few bytes // static allocation saves a few bytes
@ -127,41 +128,8 @@ int __cdecl main(int argc, char* argv[])
static float fft_output[FFT_SIZE / 2]; // Magnitudes static float fft_output[FFT_SIZE / 2]; // Magnitudes
static float fft_uniform[FFT_SIZE / 4]; static float fft_uniform[FFT_SIZE / 4];
// main note effect static float wave[WAVE_SIZE];
boolean beenPlaying = false; static float wave_uniform[WAVE_SIZE];
const int SHAPES_SIZE = 15;
float shapeIncrement = 0.02f;
/**
* Definition of shape with 3 parameters in a vec3
*
* h position, v position, length
*
*/
float vec3ShapeArray[5][3] = {
{ 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f }
};
float test[3] = { 0.0f, 0.1f, 0.0f }; // test float of one shape
int currentShape = 0; // mark location which shape we are currently building
boolean isPlaying = false;
float lastPlayPos = 0;
float lastNote = 0.0f;
/*GLfloat shapes[5][3] = {
{ 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f }
};*/
do do
{ {
@ -238,72 +206,28 @@ int __cdecl main(int argc, char* argv[])
syncs[i + 1] = syncBuf[(playCursor / (2 * sizeof(SUsample)) >> 8) * 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];
if (captureSync >= 0.001f) {
isPlaying = true;
}
if (isPlaying) {
vec3ShapeArray[currentShape][1] = captureSync;
vec3ShapeArray[currentShape][2] = vec3ShapeArray[currentShape][2] + shapeIncrement;
test[0] = captureSync; // y position
test[1] = test[1] + shapeIncrement; // x position
test[2] = 0.3f; // length
}
// when note changes -- reset
if (lastNote != captureSync) {
test[1] = 0.1f;
test[2] = 0.0f;
lastNote = captureSync;
}
// shape mover, if the shape isnt the current one - move it
for (int i = 0; i < SHAPES_SIZE; ++i) {
if (i != currentShape) {
vec3ShapeArray[i][0] = vec3ShapeArray[i][0] + shapeIncrement;
}
}
beenPlaying = isPlaying;
// go through the array and start from the first when all shapes have been used
if (beenPlaying) {
if (currentShape <= SHAPES_SIZE) {
++currentShape;
}
else {
currentShape = 0;
}
}
float flatShapes[15] = {
vec3ShapeArray[0][0], vec3ShapeArray[0][1], vec3ShapeArray[0][2],
vec3ShapeArray[1][0], vec3ShapeArray[1][1], vec3ShapeArray[1][2],
vec3ShapeArray[2][0], vec3ShapeArray[2][1], vec3ShapeArray[2][2],
vec3ShapeArray[3][0], vec3ShapeArray[3][1], vec3ShapeArray[3][2],
vec3ShapeArray[4][0], vec3ShapeArray[4][1], vec3ShapeArray[4][2]
};
PFNGLUNIFORM3FVPROC glUniform3fvProc = ((PFNGLUNIFORM3FVPROC)wglGetProcAddress("glUniform3fv"));
glUniform3fvProc(10, 3, flatShapes); // array of shapes
glUniform3fvProc(40, 1, test); // test shape
PFNGLUNIFORM1FVPROC glUniform1fvProc = ((PFNGLUNIFORM1FVPROC)wglGetProcAddress("glUniform1fv")); PFNGLUNIFORM1FVPROC glUniform1fvProc = ((PFNGLUNIFORM1FVPROC)wglGetProcAddress("glUniform1fv"));
glUniform1fvProc(0, SU_NUMSYNCS + 1, syncs); glUniform1fvProc(0, SU_NUMSYNCS + 1, syncs);
glUniform1fvProc(8, FFT_SIZE / 4, fft_uniform); glUniform1fvProc(8, FFT_SIZE / 4, fft_uniform);
/******************
* WAVE GENERATOR
*******************/
waveGenerator(syncs[4], wave);
for (int i = 0; i < (WAVE_SIZE); i++)
{
float gain = 1.2f;
float alpha = 0.15f; // "Hidastaa" FFT:n piikkej<65>
float threshhold = 0.1f; // Alin arvo mik<69> p<><70>stet<65><74>n shaderille (v<>hent<6E><74> "noisea")
float x_t = wave[i] * gain;
// Exponential smoothing kaava
// s(t) = alpha*x(t)+(1-alpha)*s(t-1)
wave_uniform[i] = (x_t < threshhold) ? 0.f : alpha * (x_t)+(1 - alpha) * wave_uniform[i];
}
glUniform1fvProc(1000, WAVE_SIZE, wave);
glRects(-1, -1, 1, 1); glRects(-1, -1, 1, 1);
//syncs[0] = -syncs[0]; //syncs[0] = -syncs[0];
@ -341,5 +265,11 @@ int __cdecl main(int argc, char* argv[])
lastPlayCursor = playCursor; lastPlayCursor = playCursor;
} while(!GetAsyncKeyState(VK_ESCAPE)); } while(!GetAsyncKeyState(VK_ESCAPE));
ExitProcess(0); ExitProcess(0);
} }

View File

@ -6,13 +6,13 @@ const float TAU = (2. * PI);
const float PHI = sqrt(5.) * 0.5 + 0.5; const float PHI = sqrt(5.) * 0.5 + 0.5;
layout(location = 0) uniform float syncs[7]; layout(location = 0) uniform float syncs[7];
layout(location = 8) uniform float fft_output[512]; // FFT_SIZE / 4 layout(location = 8) 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 = 1000) uniform float wave[512]; // FFT_SIZE / 4
layout(location = 700) uniform vec3 test; // shapes test
float u_time = syncs[0]; float u_time = syncs[0];
vec2 getUV() { vec2 getUV(vec2 offset) {
const vec2 scale = vec2(0.00104166667, 0.00185185185); vec2 uv = 2.0 * ((gl_FragCoord.xy + offset *0.5) / vec2(1920,1080) - 0.5);
return gl_FragCoord.xy * scale - 1.0; uv.x *= 1920/1080;
return uv;
} }
float noise(in vec2 xy, in float seed) { float noise(in vec2 xy, in float seed) {
@ -32,21 +32,26 @@ float sdHex(vec3 pos, float i, float angle) {
return d1; return d1;
} }
float sdSphere(vec3 p, float r) {
return length(p)-r;
}
mat2 rot2D(float angle) {
float s = sin(angle), c = cos(angle);
return mat2(c, -s, s, c);
}
float getScaledFFT(int index, float scale, float offset) { float getScaledFFT(int index, float scale, float offset) {
// Clamp index to valid range // Clamp index to valid range
index = clamp(index, 0, 511); index = clamp(index, 0, 511);
// Get raw FFT value // Get raw FFT value
float raw = fft_output[index]; float raw = wave[index];
// Apply logarithmic scaling: log(1 + value * scale) + offset // Apply logarithmic scaling: log(1 + value * scale) + offset
return log(1.0 + raw * scale) + offset; return log(1.0 + raw * scale) + offset;
} }
float sdSphere(vec3 p, float r){
return length(p) -r;
}
// Modify your mapScene function // Modify your mapScene function
vec2 mapScene(in vec3 p) { vec2 mapScene(in vec3 p) {
float mat = 0.; float mat = 0.;
@ -58,9 +63,42 @@ vec2 mapScene(in vec3 p) {
float rippleFreq = 1.0; float rippleFreq = 1.0;
float rippleDecay = 0.25; float rippleDecay = 0.25;
// big sphere
a = sdSphere(vec3(p.x, p.y - 16., p.z - 10.), 2.);
d = min(d,a);
if (d==a) {
mat = 1.;
}
// rotating spheres
vec3 q = vec3(p.x, p.y - 16., p.z - 10.);
q.xy = rot2D(u_time*2.)*q.xy;
float smallSphereDist = 4.;
float smallSphereSize = 0.2;
a = sdSphere(vec3(q.x + smallSphereDist, q.y, q.z), smallSphereSize),
d = min(d,a);
if (d==a) mat = 1.0;
a = sdSphere(vec3(q.x - smallSphereDist, q.y, q.z), smallSphereSize),
d = min(d,a);
if (d==a) mat = 1.0;
a = sdSphere(vec3(q.x, q.y + smallSphereDist, q.z), smallSphereSize),
d = min(d,a);
if (d==a) mat = 1.0;
a = sdSphere(vec3(q.x, q.y - smallSphereDist, q.z), smallSphereSize),
d = min(d,a);
if (d==a) mat = 1.0;
// Hexagonal grid // Hexagonal grid
float hexGap = 0.2; float hexGap = 0.2;
/*
for(float j = 0.; j < 16.; j++) { for(float j = 0.; j < 16.; j++) {
vec3 po = p; vec3 po = p;
po += vec3((1.6 + hexGap) * 8, -5., -(1.88 + hexGap) * 10); po += vec3((1.6 + hexGap) * 8, -5., -(1.88 + hexGap) * 10);
@ -91,14 +129,6 @@ vec2 mapScene(in vec3 p) {
} }
} }
} }
*/
// main note effect shapes
a = sdSphere(vec3(p.x + test.y, p.y + test.x, p.z), test.z + 2.);
d = min(d, a);
if (d == a) {
mat = 1.;
}
return vec2(d, mat); return vec2(d, mat);
} }
@ -112,7 +142,7 @@ vec3 castRay(vec3 ro, vec3 rd, inout vec3 pos) {
pos = ro + rd * t; pos = ro + rd * t;
vec2 res = mapScene(pos); vec2 res = mapScene(pos);
// Increase step size multiplier for faster marching // Increase step size multiplier for faster marching
t += res.x; t += res.x * 1.2;
mat = res.y; mat = res.y;
if(t > 100.) { // Reduced max distance if(t > 100.) { // Reduced max distance
break; break;
@ -261,7 +291,7 @@ vec3 postProcess(vec3 col) {
//col += 0.075*clamp(vec3(0.5*random, 0.5*random2, 0.5*random), 0.02, 1.); // dither //col += 0.075*clamp(vec3(0.5*random, 0.5*random2, 0.5*random), 0.02, 1.); // dither
// Normalized pixel coordinates (from 0 to 1) // Normalized pixel coordinates (from 0 to 1)
vec2 screenCoord = getUV(); vec2 screenCoord = getUV(vec2(0., 0.)); //gl_FragCoord.xy / u_resolution.xy;
// Vignette // Vignette
float radius = 0.8; float radius = 0.8;
@ -292,7 +322,7 @@ vec3 getCameraRay(vec2 uv, vec3 camPos, vec3 camTarget, float fov) {
vec3 camRight = normalize(cross(vec3(0.0, 1.0, 0.0), camForward)); vec3 camRight = normalize(cross(vec3(0.0, 1.0, 0.0), camForward));
vec3 camUp = normalize(cross(camForward, camRight)); vec3 camUp = normalize(cross(camForward, camRight));
vec3 rayDir = normalize(uv.x * camRight + uv.y * camUp + camForward * fov); vec3 rayDir = normalize(uv.x * camRight + uv.y * camUp + camForward);
return rayDir; return rayDir;
} }
@ -300,11 +330,11 @@ vec3 getCameraRay(vec2 uv, vec3 camPos, vec3 camTarget, float fov) {
// Camera positioning function // Camera positioning function
vec3 getCameraPosition(float time, int cameraMode) { vec3 getCameraPosition(float time, int cameraMode) {
vec3 camPos; vec3 camPos;
camPos = vec3(0.0, 30.0, -10.0); camPos = vec3(0.0, 10.0, -10.0);
if(cameraMode == 1) { if(cameraMode == 1) {
// Orbiting camera // Orbiting camera
vec3 camTarget = vec3(0.0, 0.0, -20.0); vec3 camTarget = vec3(0.0, 50.0, -20.0);
float orbitRadius = 20.0; float orbitRadius = 20.0;
float orbitSpeed = 0.2; float orbitSpeed = 0.2;
float orbitHeight = 10.0; float orbitHeight = 10.0;
@ -318,6 +348,8 @@ vec3 getCameraPosition(float time, int cameraMode) {
// First person style movement // First person style movement
float walkSpeed = 2.0; float walkSpeed = 2.0;
camPos = vec3(sin(time * walkSpeed) * 0.1, 8.0 + sin(time * walkSpeed * 2.0) * 0.05, time * 0.5); camPos = vec3(sin(time * walkSpeed) * 0.1, 8.0 + sin(time * walkSpeed * 2.0) * 0.05, time * 0.5);
} else if(cameraMode == 4) {
camPos = vec3(0., 30., 25.);
} }
return camPos; return camPos;
@ -326,7 +358,7 @@ vec3 getCameraPosition(float time, int cameraMode) {
// Main camera function that combines everything // Main camera function that combines everything
vec3 setupCamera(vec2 uv, float time, int positionMode) { vec3 setupCamera(vec2 uv, float time, int positionMode) {
vec3 camPos = getCameraPosition(time, positionMode); vec3 camPos = getCameraPosition(time, positionMode);
vec3 camTarget = vec3(0.0, -1.0, 10.0); // Adjust target as needed vec3 camTarget = vec3(0.0, 10.0, 10.0); // Adjust target as needed
float fov = 1.; float fov = 1.;
return getCameraRay(uv, camPos, camTarget, fov); return getCameraRay(uv, camPos, camTarget, fov);
@ -337,7 +369,7 @@ vec3 render(vec2 uv) {
// Choose camera modes: // Choose camera modes:
// Position: 0=static, 1=orbit, 2=smooth, 3=walk // Position: 0=static, 1=orbit, 2=smooth, 3=walk
// Ray: 0=standard, 1=zoom, 2=dof // Ray: 0=standard, 1=zoom, 2=dof
int positionMode = 2; // Static int positionMode = 4; // Static
vec3 rayDir = setupCamera(uv, u_time, positionMode); vec3 rayDir = setupCamera(uv, u_time, positionMode);
vec3 camPos = getCameraPosition(u_time, positionMode); vec3 camPos = getCameraPosition(u_time, positionMode);
@ -356,7 +388,7 @@ vec3 render(vec2 uv) {
void main() { void main() {
vec3 finalColor = render(getUV()); vec3 finalColor = render(getUV(vec2(0., 0.)));
//finalColor = postProcess(finalColor); //finalColor = postProcess(finalColor);

View File

@ -1,131 +1,182 @@
// Generated with Shader Minifier 1.5.1 (https://github.com/laurentlb/Shader_Minifier/) // Generated with Shader Minifier 1.5.1 (https://github.com/laurentlb/Shader_Minifier/)
#ifndef FRAGMENT_INL_ #ifndef FRAGMENT_INL_
# define FRAGMENT_INL_ # define FRAGMENT_INL_
# define VAR_fft_output "f" # define VAR_fft_output "l"
# define VAR_o "i" # define VAR_o "i"
# define VAR_shapes "C"
# define VAR_syncs "m" # define VAR_syncs "m"
# define VAR_test "k" # define VAR_wave "n"
const char *fragment_frag = const char *fragment_frag =
"#version 460\n" "#version 460\n"
"precision mediump float;" "precision mediump float;"
"out vec4 i;" "out vec4 i;"
"const float n=2.*acos(-1.),v=sqrt(5.)*.5+.5;" "const float f=2.*acos(-1.),v=sqrt(5.)*.5+.5;"
"layout(location=0)uniform float m[7];" "layout(location=0)uniform float m[7];"
"layout(location=8)uniform float f[512];" "layout(location=8)uniform float l[512];"
"layout(location=600)uniform vec3 C[15];" "layout(location=1000)uniform float n[512];"
"layout(location=700)uniform vec3 k;" "float x=m[0];"
"float l=m[0];" "vec2 t()"
"vec2 t(vec3 v)"
"{" "{"
"float n=0.,i=1e9,m=length(vec3(v.x+k.y,v.y+k.x,v.z))-k.z-2.;" "vec2 i=2.*((gl_FragCoord.xy+vec2(0)*.5)/vec2(1920,1080)-.5);"
"i=min(i,m);" "i.x*=1;"
"if(i==m)" "return i;"
"n=1.;"
"return vec2(i,n);"
"}" "}"
"vec3 t(vec3 v,vec3 i,inout vec3 n)" "float t(vec3 v,vec2 i)"
"{" "{"
"float f=0.,r=0.,m=0.;" "v=abs(v);"
"for(int e=0;e<30;e++)" "return max(v.y-i.y,max(v.x*sqrt(3.)*.5+v.z*.5,v.z)-i.x);"
"}"
"float t(vec3 v,float i)"
"{"
"return length(v)-i;"
"}"
"mat2 e()"
"{"
"float v=x*2.,i=sin(v);"
"v=cos(v);"
"return mat2(v,-i,i,v);"
"}"
"float e(int v)"
"{"
"v=clamp(v,0,511);"
"float i=n[v];"
"return log(1.+i*15.);"
"}"
"vec2 e(vec3 v)"
"{"
"float i=0.,f=1e9,m=0.;"
"vec2 n=vec2(7);"
"m=t(vec3(v.x,v.y-16.,v.z-10.),2.);"
"f=min(f,m);"
"if(f==m)"
"i=1.;"
"vec3 l=vec3(v.x,v.y-16.,v.z-10.);"
"l.xy=e()*l.xy;"
"m=t(vec3(l.x+4.,l.yz),.2),f=min(f,m);"
"if(f==m)"
"i=1.;"
"m=t(vec3(l.x-4.,l.yz),.2),f=min(f,m);"
"if(f==m)"
"i=1.;"
"m=t(vec3(l.x,l.y+4.,l.z),.2),f=min(f,m);"
"if(f==m)"
"i=1.;"
"m=t(vec3(l.x,l.y-4.,l.z),.2),f=min(f,m);"
"if(f==m)"
"i=1.;"
"for(float l=0.;l<16.;l++)"
"{" "{"
"n=v+i*f;" "vec3 x=v+vec3(1.8*8,-5,-2.08*10)+vec3(0,0,2.08*l);"
"vec2 l=t(n);" "for(float v=0.;v<16.;v++)"
"f+=l.x;"
"r=l.y;"
"if(f>1e2)"
"break;"
"if(l.x<.001*f)"
"{" "{"
"m=1.;" "x=mod(v,2.)==0.?"
"x-vec3(1.8,0,1):"
"x+vec3(-1.8,0,1);"
"int y=int(length(vec2(v,l)-n.xy));"
"m=t(x,vec2(.86,1.+e(y)*2.));"
"f=min(f,m);"
"if(f==m)"
"i=1.;"
"}"
"}"
"return vec2(f,i);"
"}"
"vec3 e(vec3 v,vec3 i,inout vec3 f)"
"{"
"float m=0.,l=0.,y=0.;"
"for(int r=0;r<30;r++)"
"{"
"f=v+i*m;"
"vec2 n=e(f);"
"m+=n.x*1.2;"
"l=n.y;"
"if(m>1e2)"
"break;"
"if(n.x<.001*m)"
"{"
"y=1.;"
"break;" "break;"
"}" "}"
"}" "}"
"if(f>1e2)" "if(m>1e2)"
"f=0.;" "m=0.;"
"return vec3(f,r,m);" "return vec3(m,l,y);"
"}" "}"
"float t(vec3 v,vec3 f,float n)" "float e(vec3 v,vec3 m,float i)"
"{" "{"
"float i=1.,m=.02;" "float f=1.,l=.02;"
"for(int e=0;e<6;e++)" "for(int r=0;r<6;r++)"
"{" "{"
"if(m>n)" "if(l>i)"
"break;" "break;"
"float l=t(v+m*f).x;" "float x=e(v+l*m).x;"
"i=min(i,l/(4.*m));" "f=min(f,x/(4.*l));"
"m+=clamp(l,.1,.8);" "l+=clamp(x,.1,.8);"
"if(i<-1.)" "if(f<-1.)"
"break;" "break;"
"}" "}"
"i=max(i,-1.);" "f=max(f,-1.);"
"return.25*(1.+i)*(1.+i)*(2.-i);" "return.25*(1.+f)*(1.+f)*(2.-f);"
"}" "}"
"vec3 e(vec3 v)" "vec3 t(vec3 v)"
"{" "{"
"vec2 i=vec2(.01,0);" "vec2 i=vec2(.01,0);"
"return normalize(vec3(t(v+i.xyy).x-t(v-i.xyy).x,t(v+i.yxy).x-t(v-i.yxy).x,t(v+i.yyx).x-t(v-i.yyx).x));" "return normalize(vec3(e(v+i.xyy).x-e(v-i.xyy).x,e(v+i.yxy).x-e(v-i.yxy).x,e(v+i.yyx).x-e(v-i.yyx).x));"
"}" "}"
"vec3 e(vec3 v,vec3 i,vec3 l,vec3 f,vec3 m,float n)" "vec3 e(vec3 v,vec3 f,vec3 m,vec3 i,vec3 x,float l)"
"{" "{"
"v-=l;" "v-=m;"
"float e=length(v);" "float r=length(v);"
"v=normalize(v);" "v=normalize(v);"
"float y=3./(1.+.09*e+.032*e*e),r=max(dot(m,v),0.);" "float n=3./(1.+.09*r+.032*r*r),y=max(dot(x,v),0.);"
"f=normalize(v-f);" "i=normalize(v-i);"
"vec3 x=vec3(.04);" "vec3 z=vec3(.04);"
"x+=(1.-x)*pow(clamp(1.-max(dot(f,v),0.),0.,1.),5.);" "z+=(1.-z)*pow(clamp(1.-max(dot(i,v),0.),0.,1.),5.);"
"e=t(l+m*.01,v,e);" "r=e(m+x*.01,v,r);"
"return(i*r*y+i*pow(max(dot(m,f),0.),mix(128.,8.,n))*y*x)*e;" "return(f*y*n+f*pow(max(dot(x,i),0.),mix(128.,8.,l))*n*z)*r;"
"}" "}"
"vec3 e(vec3 v,vec3 i,vec3 f,float n)" "vec3 e(vec3 v,vec3 f,vec3 i,float m)"
"{" "{"
"float m=.01;" "float l=.01;"
"vec3 l=vec3(0);" "vec3 x=vec3(0);"
"if(n==0.)" "if(m==0.)"
"l=vec3(.8314,.2941,.2941),m=.1;" "x=vec3(.8314,.2941,.2941),l=.1;"
"else if(n==1.)" "else if(m==1.)"
"l=vec3(.6196,.6118,.6118),m=.7;" "x=vec3(.6196,.6118,.6118),l=.7;"
"else if(n==2.)" "else if(m==2.)"
"l=vec3(.3255,.4784,.3255),m=.2;" "x=vec3(.3255,.4784,.3255),l=.2;"
"else if(n==3.)" "else if(m==3.)"
"l=vec3(.2471,.3059,.6314),m=1.;" "x=vec3(.2471,.3059,.6314),l=1.;"
"else if(n==4.)" "else if(m==4.)"
"l=vec3(.9961,1,.9922),m=.1;" "x=vec3(.9961,1,.9922),l=.1;"
"else if(n==5.)" "else if(m==5.)"
"l=vec3(.9961,1,.9922),m=.3;" "x=vec3(.9961,1,.9922),l=.3;"
"v=vec3(0)+e(vec3(-10,10,0),vec3(.77,.26,.73),v,f,i,m)+e(vec3(0,10,-5),vec3(.08,.62,.75),v,f,i,m)+e(vec3(0,25,0),vec3(.5137,.1961,.7725),v,f,i,m)+vec3(.08,.62,.75)*clamp(dot(i,normalize(vec3(0,1,-3)*vec3(0,-1,-2))),0.,1.)*.8;" "v=vec3(0)+e(vec3(-10,10,0),vec3(.77,.26,.73),v,i,f,l)+e(vec3(0,10,-5),vec3(.08,.62,.75),v,i,f,l)+e(vec3(0,25,0),vec3(.5137,.1961,.7725),v,i,f,l)+vec3(.08,.62,.75)*clamp(dot(f,normalize(vec3(0,1,-3)*vec3(0,-1,-2))),0.,1.)*.8;"
"return l*max(vec3(0),v);" "return x*max(vec3(0),v);"
"}" "}"
"vec3 e(vec2 v,vec3 i)" "vec3 e(vec2 v,vec3 f)"
"{" "{"
"i=normalize(vec3(0,-1,10)-i);" "f=normalize(vec3(0,10,10)-f);"
"vec3 m=normalize(cross(vec3(0,1,0),i));" "vec3 m=normalize(cross(vec3(0,1,0),f));"
"return normalize(v.x*m+v.y*normalize(cross(i,m))+i);" "return normalize(v.x*m+v.y*normalize(cross(f,m))+f);"
"}" "}"
"vec3 e()" "vec3 d()"
"{" "{"
"vec3 i;" "return vec3(0,30,25);"
"{"
"float v=l*.06;"
"i=vec3(sin(v)*15.,30.+cos(v*.5)*5.,cos(v)*15.);"
"}"
"return i;"
"}" "}"
"vec3 e(vec2 v)" "vec3 d(vec2 v)"
"{" "{"
"vec3 i=e(v,e()),m=vec3(.102,.2431,.3412),n=vec3(0),l=t(e(),i,n);" "vec3 f=e(v,d()),i=vec3(.102,.2431,.3412),m=vec3(0),l=e(d(),f,m);"
"if(l.x>0.)" "if(l.x>0.)"
"{" "{"
"vec3 v=e(n);" "vec3 v=t(m);"
"m=e(n,v,i,l.y);" "i=e(m,v,f,l.y);"
"}" "}"
"return m;" "return i;"
"}" "}"
"void main()" "void main()"
"{" "{"
"vec3 v=e(gl_FragCoord.xy*vec2(.00104166667,.00185185185)-1.);" "vec3 v=d(t());"
"i=vec4(v,1);" "i=vec4(v,1);"
"}"; "}";

87
src/waveGenerator.cpp Normal file
View File

@ -0,0 +1,87 @@
#include "waveGenerator.h"
#include <cmath>
const float DECAY_FACTOR = 0.80f; // Amplitude decay per iteration
const float PI = 3.14159265f;
const float WAVE_LENGTH = 3.0f;
const float WAVE_SPEED = 0.5f;
const int speed = 1;
float waveOffset = 0.0f;
float temp[WAVE_SIZE] = { 0.0f }; // Temp buffer for propagation
//float wave[WAVE_SIZE] = { 0.0f }; // Wave buffer
float clamp(float value) {
if (value > 4.f) {
value = 4.f;
}
if (value < 0.f) {
value = 0.f;
}
return value;
}
// Generate a sine wave into the beginning of the array
void generateSineWave(float amplitude, float* wave) {
for (int i = 0; i < WAVE_SIZE; ++i) {
float sine = std::sin(2.0f * PI * i / WAVE_LENGTH);
float sample = 1.0f + sine;
wave[i] = clamp(sample * amplitude);
}
}
// Shift wave and apply decay
void propagateWave(float* wave) {
for (int i = WAVE_SIZE - 1; i >= speed; --i) {
wave[i] = wave[i - speed] * DECAY_FACTOR;
}
// Decay the front section that no longer receives new values
for (int i = 0; i < speed; ++i) {
wave[i] = wave[i] * DECAY_FACTOR;
}
}
/*
void propagateWave(float* wave) {
waveOffset += WAVE_SPEED;
for (int i = 0; i < WAVE_SIZE; ++i) {
float srcIndex = i - waveOffset;
if (srcIndex < 0 || srcIndex >= WAVE_SIZE - 1) {
temp[i] = 0.0f;
}
else {
int index0 = srcIndex - (srcIndex < 0 && srcIndex != (int)srcIndex);
int index1 = index0 + 1;
float t = srcIndex - index0;
float value = (1.0f - t) * wave[index0] + t * wave[index1];
temp[i] = clamp(value * DECAY_FACTOR);
}
}
//// Copy temp back to wave
for (int i = 0; i < WAVE_SIZE; ++i) {
wave[i] = temp[i];
}
//// Prevent offset overflow
if (waveOffset >= 1.0f) {
waveOffset -= waveOffset - (waveOffset < 0 && srcIndex != (int)srcIndex);
}
}
*/
void waveGenerator(float sound, float* wave) {
generateSineWave(sound, wave);
propagateWave(wave);
}

7
src/waveGenerator.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
#include <cmath>
#define WAVE_SIZE 512
void generateSineWave(float amplitude, float* wave);
void propagateWave(float* wave);
void waveGenerator(float sound, float* wave);