From f9a8dc2b692ba53f39ed33877bd14c29d25fec27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teemu=20J=C3=A4rvisalo?= Date: Thu, 31 Jul 2025 20:11:54 +0300 Subject: [PATCH] partikkeli valot --- src/main.cpp | 55 +++++++++------- src/shaders/fragment.frag | 53 +++++++++++---- src/shaders/fragment.inl | 134 ++++++++++++++++++-------------------- 3 files changed, 136 insertions(+), 106 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 2cccc31..0407fb9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -37,6 +37,31 @@ static int pidMain; static int pidPost; // static HDC hDC; +#define GRID 32 +#define HEX_TEX_SIZE (GRID * GRID * 4) // RGBA: 4 floats per texel + +#define SHAPES 16 +#define SHAPES_TEX_SIZE (SHAPES * SHAPES * 4) +#define MAX_DISTANCE 100.f + +#define PARTICLES 16 +#define PARTICLES_TEX_SIZE (PARTICLES * PARTICLES * 4) +#define PARTICLES_DISTANCE 80.f + + +static float syncs[1 + SU_NUMSYNCS]; + +// 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 particlesData[PARTICLES_TEX_SIZE]; +static float particlesData2[PARTICLES * PARTICLES]; // additional value for particle distance +static float shapesData[SHAPES_TEX_SIZE]; + +static float hexGridData[HEX_TEX_SIZE]; + #ifndef EDITOR_CONTROLS #pragma code_seg(".main") void entrypoint(void) @@ -118,14 +143,11 @@ 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]; + @@ -135,16 +157,7 @@ int __cdecl main(int argc, char* argv[]) float x, y, z; }; -#define GRID 32 -#define HEX_TEX_SIZE (GRID * GRID * 4) // RGBA: 4 floats per texel -#define SHAPES 16 -#define SHAPES_TEX_SIZE (SHAPES * SHAPES * 4) -#define MAX_DISTANCE 100.f - -#define PARTICLES 16 -#define PARTICLES_TEX_SIZE (PARTICLES * PARTICLES * 4) -#define PARTICLES_DISTANCE 80.f PFNGLUNIFORM1FVPROC glUniform1fvProc = ((PFNGLUNIFORM1FVPROC)wglGetProcAddress("glUniform1fv")); PFNGLACTIVETEXTUREPROC glActiveTexture = ((PFNGLACTIVETEXTUREPROC)wglGetProcAddress("glActiveTexture")); @@ -184,15 +197,14 @@ int __cdecl main(int argc, char* argv[]) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - static float shapesData[SHAPES_TEX_SIZE]; + int currentShape = 0; // mark location which shape we are currently building float shapeIncrement = 0.15f; float shapeLastNote = 0.0f; - static float particlesData[PARTICLES_TEX_SIZE]; - static float particlesData2[PARTICLES*PARTICLES]; // additional value for particle distance + int currentParticle = 0; // mark location which shape we are currently building - float particleIncrement = 1.0f; + float particleIncrement = 3.0f; float particleLastNote = 0.0f; do @@ -396,19 +408,12 @@ int __cdecl main(int argc, char* argv[]) //glUniform3fvProc(600, 1, flatShapes); // array of shapes //glUniform3fvProc(700, 1, test); // test shape - glUniform1fvProc(0, SU_NUMSYNCS + 1, syncs); glUniform1fvProc(20, FFT_SIZE / 4, fft_uniform); - - - ////////////////////////////////////////////////////// // Hex Grid Builder ////////////////////////////////////////////////////// - - static float hexGridData[HEX_TEX_SIZE]; - float gap = 0.3f; diff --git a/src/shaders/fragment.frag b/src/shaders/fragment.frag index ec9e867..4ca6627 100644 --- a/src/shaders/fragment.frag +++ b/src/shaders/fragment.frag @@ -172,9 +172,9 @@ vec2 mapScene(in vec3 p) { } } */ - + /* float maxRadius = 40.; // Circle radius - float sphereRadius = 0.7; + float sphereRadius = 0.3; int index = 0; float gridSize3 = 16.; @@ -192,12 +192,6 @@ vec2 mapScene(in vec3 p) { // Direction from center to initial circle position (in XY plane) vec3 dir = normalize(vec3(cos(particleStartPos), 0.0, sin(particleStartPos))); // XZ direction - // Compute current radius: shrinking over time - //float r = particleDist; - //if(particleDist > maxRadius) { - // particleDist = maxRadius; - //} - float r = max(maxRadius - particleData.z, -maxRadius); float offsetFactor = 15.; @@ -211,9 +205,7 @@ vec2 mapScene(in vec3 p) { index++; } } - - //res = opU(res, vec2(sdSphere(p - vec3(.0, 4., .0), 1.0), .1)); - + */ return res; } @@ -422,6 +414,45 @@ vec3 shading(vec3 v, vec3 n, vec3 dir, float material) { } */ + // LIGHTS WITH PARTICLES + float maxRadius = 40.; // Circle radius + float sphereRadius = 0.7; + int index = 0; + float particleHeight = 4.; + + float gridSize3 = 16.; + for(float j = 0; j < gridSize3; j++) { + for(float i = 0; i < gridSize3; i++) { + vec2 texCoord = (vec2(i, j) + 0.5) / float(gridSize3); + vec4 particleData = texture(u_ParticlesTex, texCoord); // RGBA: x, y, distance, active + float particleActive = particleData.a; + + if(particleData.z < 0.5) continue; + + // Map param to angle + float particleStartPos = hash(vec2(j, i)) * 2.0 * PI; + + // Direction from center to initial circle position (in XY plane) + vec3 dir = normalize(vec3(cos(particleStartPos), 0.0, sin(particleStartPos))); // XZ direction + + float r = max(maxRadius - particleData.z, -maxRadius); + + float offsetFactor = 15.; + vec3 particleOffset = vec3(hash(vec2(i, j)) * offsetFactor , particleHeight, hash(vec2(j, i)) * offsetFactor); // particle offset + + // Final object position = center (offset) + radial movement + vec3 center = particleOffset; // Circle center + vec3 objPos = center + dir * r; // Object slides inward + //res = opU(res, vec2(sdSphere(p - objPos, sphereRadius), 1.)); + + lights += addPointLight(objPos, palette(i + j), 3.0, v, dir, n, shininess); + //lights += addPointLightNoShadow(objPos, palette(i + j), 10. , 20. ,v, dir, n, shininess); + + index++; + } + } + + vec3 lightDir = vec3(0., 1., -3); //float sun_dif = clamp(dot(n, lightDir), 0., 1.); //float shadow = softshadow(v + n * 0.01, lightDir, .01, 30., 18.); diff --git a/src/shaders/fragment.inl b/src/shaders/fragment.inl index 8780ad5..38a7609 100644 --- a/src/shaders/fragment.inl +++ b/src/shaders/fragment.inl @@ -23,118 +23,112 @@ const char *fragment_frag = "{" "return fract(sin(dot(v,vec2(127.1,311.7)))*43758.5453123);" "}" - "vec2 t(vec2 f,vec2 i)" - "{" - "return f.x6e2)" + "f=v+x*i;" + "vec2 m=t(f);" + "i+=m.x;" + "e=m.y;" + "if(i>6e2)" "break;" - "if(g.x<.001*l)" + "if(m.x<.001*i)" "{" "y=1.;" "break;" "}" "}" - "return vec3(l,m,y);" + "return vec3(i,e,y);" "}" - "float t(vec3 v,vec3 f,float i)" + "float t(vec3 v,vec3 i,float f)" "{" - "float l=1.,m=.02;" + "float m=1.,y=.02;" "for(int n=0;n<6;n++)" "{" - "if(m>i)" + "if(y>f)" "break;" - "float g=t(v+m*f).x;" - "l=min(l,g/(4.*m));" - "m+=clamp(g,.1,.8);" - "if(l<-1.)" + "float d=t(v+y*i).x;" + "m=min(m,d/(4.*y));" + "y+=clamp(d,.1,.8);" + "if(m<-1.)" "break;" "}" - "l=max(l,-1.);" - "return.25*(1.+l)*(1.+l)*(2.-l);" + "m=max(m,-1.);" + "return.25*(1.+m)*(1.+m)*(2.-m);" "}" "vec3 x(vec3 v)" "{" - "vec2 f=vec2(.01,0);" - "return normalize(vec3(t(v+f.xyy).x-t(v-f.xyy).x,t(v+f.yxy).x-t(v-f.yxy).x,t(v+f.yyx).x-t(v-f.yyx).x));" + "vec2 m=vec2(.01,0);" + "return normalize(vec3(t(v+m.xyy).x-t(v-m.xyy).x,t(v+m.yxy).x-t(v-m.yxy).x,t(v+m.yyx).x-t(v-m.yyx).x));" "}" - "vec3 t(vec3 v,vec3 l,vec3 f,vec3 m,float i)" + "vec3 t(vec3 v,vec3 m,float i,vec3 d,vec3 f,vec3 x,float y)" "{" - "vec3 g=vec3(.77,.26,.73);" - "v-=l;" + "v-=d;" "float n=length(v);" "v=normalize(v);" - "float x=30./(1.+.09*n+.032*n*n),y=max(dot(m,v),0.);" + "i/=1.+.09*n+.032*n*n;" + "float g=max(dot(x,v),0.);" "f=normalize(v-f);" "vec3 e=vec3(.04);" "e+=(1.-e)*pow(clamp(1.-max(dot(f,v),0.),0.,1.),5.);" - "n=t(l+m*.01,v,n);" - "return(g*y*x+g*pow(max(dot(m,f),0.),mix(128.,8.,i))*x*e)*n;" + "n=t(d+x*.01,v,n);" + "return(m*g*i+m*pow(max(dot(x,f),0.),mix(128.,8.,y))*i*e)*n;" "}" - "vec3 t(vec3 v,vec3 f,vec3 m,float l)" + "vec3 t(vec3 v,vec3 f,vec3 i,float m)" "{" - "float i=.01;" + "float y=.01;" "vec3 n=vec3(0);" - "if(l==0.)" - "n=vec3(.8314,.2941,.2941),i=.1;" - "else if(l==1.)" - "n=vec3(.6196,.6118,.6118),i=.7;" - "else if(l==2.)" - "n=vec3(.3255,.4784,.3255),i=.2;" - "else if(l==3.)" - "n=vec3(.2471,.3059,.6314),i=1.;" - "else if(l==4.)" - "n=vec3(.9961,1,.9922),i=.1;" - "else if(l==5.)" - "n=vec3(.9961,1,.9922),i=.3;" - "v=vec3(0)+t(vec3(0,40,-10),v,m,f,i)+t(vec3(0,20,15),v,m,f,i)+vec3(.08,.62,.75)*clamp(dot(f,normalize(vec3(0,1,-3)*vec3(0,-1,-2))),0.,1.)*.8;" - "return n*max(vec3(0),v);" + "if(m==0.)" + "n=vec3(.8314,.2941,.2941),y=.1;" + "else if(m==1.)" + "n=vec3(.6196,.6118,.6118),y=.7;" + "else if(m==2.)" + "n=vec3(.3255,.4784,.3255),y=.2;" + "else if(m==3.)" + "n=vec3(.2471,.3059,.6314),y=1.;" + "else if(m==4.)" + "n=vec3(.9961,1,.9922),y=.1;" + "else if(m==5.)" + "n=vec3(.9961,1,.9922),y=.3;" + "vec3 e=vec3(0)+t(vec3(0,40,-10),vec3(.77,.26,.73),30.,v,i,f,y)+t(vec3(0,20,15),vec3(.77,.26,.73),30.,v,i,f,y);" + "int g=0;" + "for(float m=0;m<16.;m++)" + "for(float i=0;i<16.;i++)" + "{" + "vec2 n=(vec2(i,m)+.5)/float(16.);" + "vec4 x=texture(d,n);" + "if(x.z<.5)" + "continue;" + "float s=t(vec2(m,i))*2.*acos(-1.);" + "vec3 c=normalize(vec3(cos(s),0,sin(s))),l=vec3(t(vec2(i,m))*15.,4,t(vec2(m,i))*15.);" + "e+=t(l+c*max(40.-x.z,-40.),vec3(.46,.2,.94)+vec3(.66,.64,.77)*cos(6.28318*(vec3(.91,.62,.97)*(i+m)+vec3(.26,.2,.84))),3.,v,c,f,y);" + "g++;" + "}" + "e+=vec3(.08,.62,.75)*clamp(dot(f,normalize(vec3(0,1,-3)*vec3(0,-1,-2))),0.,1.)*.8;" + "return n*max(vec3(0),e);" "}" - "vec3 t(vec2 f,vec3 v)" + "vec3 t(vec2 m,vec3 v)" "{" "v=normalize(vec3(0,10,0)-v);" - "vec3 i=normalize(cross(vec3(0,1,0),v));" - "return normalize(v+f.x*i+f.y*cross(v,i));" + "vec3 y=normalize(cross(vec3(0,1,0),v));" + "return normalize(v+m.x*y+m.y*cross(v,y));" "}" "vec3 x(vec2 v)" "{" - "vec3 f=vec3(-10,20,-20),l=t(v,f),i=vec3(0),n=vec3(0);" - "f=t(f,l,n);" + "vec3 f=vec3(-10,20,-20),y=t(v,f),i=vec3(0),n=vec3(0);" + "f=t(f,y,n);" "if(f.x>0.)" "{" "vec3 v=x(n);" - "i=t(n,v,l,f.y);" + "i=t(n,v,y,f.y);" "}" - "return i*exp(-f.x*.01)+mix(i,mix(vec3(.2667,.2941,.3451),vec3(.302,.3176,.3725),pow(max(dot(l,vec3(0,-.5,1.8)),0.),clamp(m[1]+m[2]+m[3],0.,1.)*5.)),1.-exp(-f.x*.01))*(1.-exp(-f.x*.01));" + "return i*exp(-f.x*.01)+mix(i,mix(vec3(.2667,.2941,.3451),vec3(.302,.3176,.3725),pow(max(dot(y,vec3(0,-.5,1.8)),0.),clamp(m[1]+m[2]+m[3],0.,1.)*5.)),1.-exp(-f.x*.01))*(1.-exp(-f.x*.01));" "}" "void main()" "{"