diff --git a/src/main.cpp b/src/main.cpp index f94a067..435c64a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -130,7 +130,7 @@ int __cdecl main(int argc, char* argv[]) // main note effect boolean beenPlaying = false; const int SHAPES_SIZE = 15; - float shapeIncrement = 0.02f; + float shapeIncrement = 0.15f; /** * Definition of shape with 3 parameters in a vec3 @@ -163,6 +163,14 @@ int __cdecl main(int argc, char* argv[]) { 0.0f, 0.0f, 0.0f } };*/ + + struct Vec3 { + float x, y, z; + }; + +#define GRID 32 +#define HEX_TEX_SIZE (GRID * GRID * 4) // RGBA: 4 floats per texel + do { direct_sound_buffer->GetCurrentPosition((DWORD*)&playCursor, NULL); @@ -245,31 +253,32 @@ int __cdecl main(int argc, char* argv[]) // 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 = 0.0; + + float captureSync = syncs[5]; if (captureSync >= 0.001f) { isPlaying = true; } - //if (isPlaying) { - vec3ShapeArray[currentShape][1] = captureSync; - vec3ShapeArray[currentShape][2] = vec3ShapeArray[currentShape][2] + shapeIncrement; + if (isPlaying) { + // vec3ShapeArray[currentShape][1] = captureSync; + // vec3ShapeArray[currentShape][2] = vec3ShapeArray[currentShape][2] + shapeIncrement; test[0] = test[0] + shapeIncrement; // x position test[1] = captureSync; // y position test[2] = 0.3f; // length - //} + } // when note changes -- reset if (lastNote != captureSync) { + test[0] = 0.0f; 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; @@ -287,7 +296,7 @@ int __cdecl main(int argc, char* argv[]) currentShape = 0; } } - + float flatShapes[15] = { vec3ShapeArray[0][0], vec3ShapeArray[0][1], vec3ShapeArray[0][2], vec3ShapeArray[1][0], vec3ShapeArray[1][1], vec3ShapeArray[1][2], @@ -297,17 +306,73 @@ int __cdecl main(int argc, char* argv[]) }; PFNGLUNIFORM3FVPROC glUniform3fvProc = ((PFNGLUNIFORM3FVPROC)wglGetProcAddress("glUniform3fv")); - glUniform3fvProc(10, 3, flatShapes); // array of shapes - glUniform3fvProc(40, 1, test); // test shape - + glUniform3fvProc(600, 1, flatShapes); // array of shapes + glUniform3fvProc(700, 1, test); // test shape + PFNGLUNIFORM1FVPROC glUniform1fvProc = ((PFNGLUNIFORM1FVPROC)wglGetProcAddress("glUniform1fv")); glUniform1fvProc(0, SU_NUMSYNCS + 1, syncs); - glUniform1fvProc(8, FFT_SIZE / 4, fft_uniform); + glUniform1fvProc(20, FFT_SIZE / 4, fft_uniform); glRects(-1, -1, 1, 1); - //syncs[0] = -syncs[0]; - //glUniform1fvProc(0, SU_NUMSYNCS + 1, syncs); + PFNGLACTIVETEXTUREPROC glActiveTexture = ((PFNGLACTIVETEXTUREPROC)wglGetProcAddress("glActiveTexture")); + PFNGLUNIFORM1IPROC glUniform1i = ((PFNGLUNIFORM1IPROC)wglGetProcAddress("glUniform1i")); + PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation = ((PFNGLGETUNIFORMLOCATIONPROC)wglGetProcAddress("glGetUniformLocation")); + + static float hexGridData[HEX_TEX_SIZE]; + + + float gap = 0.3f; + + float cellW = 1.6f + 0.2f; // flat-to-flat width + float cellH = (sqrtf(3.0f) / 2.0f) * cellW; // height based on regular hex geometry + + float horizontalSpacing = 0.75f * cellW * (1.0f + gap); // 3/4 of width per column + float verticalSpacing = cellH * (1.0f + gap); // full height per row + + float rippleCenterX = 7.0f; + float rippleCenterY = 7.0f; + + Vec3 gridOffset = { horizontalSpacing * (GRID - 1) * 0.24f, -5.0f, verticalSpacing * (GRID - 1) * 0.24f }; + + for (int j = 0; j < GRID; ++j) { + for (int i = 0; i < GRID; ++i) { + // Offset every other column vertically + float offsetZ = (i % 2 == 0) ? 0.0f : 0.5f * verticalSpacing; + + float x = i * horizontalSpacing; + float z = j * verticalSpacing + offsetZ; + float y = 0.0f; + + Vec3 hexPos = { x - gridOffset.x, y - gridOffset.y, z - gridOffset.z }; + + float dx = (float)i - rippleCenterX; + float dy = (float)j - rippleCenterY; + float hexDist = sqrtf(dx * dx + dy * dy); + + int idx = (j * GRID + i) * 4; + hexGridData[idx + 0] = hexPos.x; + hexGridData[idx + 1] = hexPos.y; + hexGridData[idx + 2] = hexPos.z; + hexGridData[idx + 3] = hexDist; + } + } + + GLuint hexGridTex; + glGenTextures(1, &hexGridTex); + glBindTexture(GL_TEXTURE_2D, hexGridTex); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, GRID, GRID, 0, GL_RGBA, GL_FLOAT, hexGridData); + + + 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); + + glActiveTexture(GL_TEXTURE0 + 2); + glUniform1i(glGetUniformLocation(pidMain, "u_hexGridTex"), 2); + // render "post process" using the opengl backbuffer #if POST_PASS diff --git a/src/shaders/fragment.frag b/src/shaders/fragment.frag index fc20ffd..2ecf85f 100644 --- a/src/shaders/fragment.frag +++ b/src/shaders/fragment.frag @@ -8,8 +8,17 @@ layout(location = 0) uniform float syncs[12]; 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 +uniform sampler2D u_hexGridTex; //uniform sampler2D u_fft_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)); +} + vec2 getUV() { const vec2 scale = vec2(0.00104166667, 0.00185185185); return gl_FragCoord.xy * scale - 1.0; @@ -114,9 +123,32 @@ vec2 mapScene(in vec3 p) { } */ // main note effect shapes - res = opU( res, vec2( sdSphere(p- vec3(2.0 + test.x, 4. + test.y, 0.0), 2. ), 1.)); + res = opU( res, vec2( sdSphere(p- vec3(2.0 + (test.x * 2.), 12. + (test.y * 10.), 0.0), 0.2 ), 1.)); + float gridsize = 16.0; // or GRID if you want full size + //float hexGap = 0.2; + + + 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 = fft_output[index] * 5.0; + float a = sdHex(p - hexPos, 1.0 + hexSize, 0.0); + res = min(res, vec2(a, 1.)); + + } + } + return res; } @@ -205,6 +237,44 @@ vec3 addPointLight(vec3 lightPos, vec3 lightColor, float intensity, vec3 worldPo return (diffuse + specular * fresnel) * shadow; } +vec3 addPointLightNoShadow(vec3 lightPos, vec3 lightColor, float intensity, 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); + + float a = 0.; + float minLight = 0.01; + float radius = 3.4; + float b = 1.0 / (radius*radius * minLight); + + // Attenuation (quadratic 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); @@ -230,7 +300,7 @@ vec3 addPointLight(vec3 lightPos, vec3 lightColor, float intensity, vec3 worldPo vec3 shading(vec3 v, vec3 n, vec3 dir, float material) { float shininess = 0.01; - vec3 outMaterial = vec3(0.0, 0.0, 0.0); + vec3 outMaterial = vec3(0.); if(material == 0.) { outMaterial = vec3(0.8314, 0.2941, 0.2941); @@ -254,10 +324,30 @@ vec3 shading(vec3 v, vec3 n, vec3 dir, float material) { vec3 lights = vec3(0.); lights += addPointLight(vec3(0., 40.0, -10.), vec3(0.77, 0.26, 0.73), 30.0, v, dir, n, shininess); - lights += addPointLight(vec3(0., 6.0, -5.0), vec3(0.08, 0.62, 0.75), 20.0, v, dir, n, shininess); - //lights += addPointLight(vec3(0., 25.0, 0.0), vec3(0.5137, 0.1961, 0.7725), 3.0, v, dir, n, shininess); + //lights += addPointLight(vec3(0., 20.0, 15.), vec3(0.77, 0.26, 0.73), 30.0, v, dir, n, shininess); + + + 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 = fft_output[index] * 5.0; + //float a = sdHex(p - hexPos, 1.0 + hexSize, 0.0); + + lights += addPointLightNoShadow(vec3(hexPos.x, (hexPos.y + hexSize) + 3., hexPos.z), palette(hexSize* 1.5), clamp(hexSize * 3., 0., 1.) ,v, dir, n, shininess); + } + } - lights += addPointLight(vec3(0., 20.0, 15.), vec3(0.77, 0.26, 0.73), 30.0, v, dir, n, shininess); vec3 lightDir = vec3(0., 1., -3); //float sun_dif = clamp(dot(n, lightDir), 0., 1.); @@ -318,7 +408,7 @@ vec3 getCameraRayDir(vec2 uv, vec3 camPos, vec3 camTarget, float fov) vec3 render(vec2 uv) { - vec3 camPos = vec3(0.0, 20.0, -10.0); + vec3 camPos = vec3(-10.0, 20.0, -20.0); vec3 camTarget = vec3(0.0, -1.0, 10.0); // Adjust target as needed float fov = 1.0; @@ -337,8 +427,36 @@ vec3 render(vec2 uv) { return col; } + void main() { vec3 finalColor = render(getUV()); //finalColor = postProcess(finalColor); o = vec4(finalColor, 1.); -} \ No newline at end of file +} + +/* +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); +} +*/ \ No newline at end of file diff --git a/src/shaders/fragment.inl b/src/shaders/fragment.inl index 3848972..b39d7ff 100644 --- a/src/shaders/fragment.inl +++ b/src/shaders/fragment.inl @@ -1,122 +1,162 @@ // Generated with Shader Minifier 1.5.1 (https://github.com/laurentlb/Shader_Minifier/) #ifndef FRAGMENT_INL_ # define FRAGMENT_INL_ -# define VAR_fft_output "m" +# define VAR_fft_output "i" # define VAR_o "f" -# define VAR_shapes "C" -# define VAR_syncs "i" +# define VAR_shapes "s" +# define VAR_syncs "m" # define VAR_test "k" +# define VAR_u_hexGridTex "d" const char *fragment_frag = "#version 460\n" "precision mediump float;" "out vec4 f;" "const float n=2.*acos(-1.),v=sqrt(5.)*.5+.5;" - "layout(location=0)uniform float i[12];" - "layout(location=20)uniform float m[512];" - "layout(location=600)uniform vec3 C[15];" + "layout(location=0)uniform float m[12];" + "layout(location=20)uniform float i[512];" + "layout(location=600)uniform vec3 s[15];" "layout(location=700)uniform vec3 k;" - "float F=i[0];" - "vec2 t(vec2 i,vec2 k)" + "uniform sampler2D d;" + "float C=m[0];" + "float t(vec3 v,vec2 i)" "{" - "return i.x1e2)" + "if(d>1e2)" "break;" - "if(m.x<.001*k)" + "if(m.x<.001*d)" "{" "y=1.;" "break;" "}" "}" - "if(k>1e2)" - "k=0.;" - "return vec3(k,n,y);" + "if(d>1e2)" + "d=0.;" + "return vec3(d,n,y);" "}" "float t(vec3 v,vec3 i,float f)" "{" - "float k=1.,y=.02;" - "for(int e=0;e<6;e++)" + "float d=1.,l=.02;" + "for(int n=0;n<6;n++)" "{" - "if(y>f)" + "if(l>f)" "break;" - "float n=t(v+y*i).x;" - "k=min(k,n/(4.*y));" - "y+=clamp(n,.1,.8);" - "if(k<-1.)" + "float m=t(v+l*i).x;" + "d=min(d,m/(4.*l));" + "l+=clamp(m,.1,.8);" + "if(d<-1.)" "break;" "}" - "k=max(k,-1.);" - "return.25*(1.+k)*(1.+k)*(2.-k);" + "d=max(d,-1.);" + "return.25*(1.+d)*(1.+d)*(2.-d);" "}" "vec3 e(vec3 v)" "{" - "vec2 k=vec2(.01,0);" - "return normalize(vec3(t(v+k.xyy).x-t(v-k.xyy).x,t(v+k.yxy).x-t(v-k.yxy).x,t(v+k.yyx).x-t(v-k.yyx).x));" + "vec2 i=vec2(.01,0);" + "v=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(v);" "}" - "vec3 e(vec3 v,vec3 k,float i,vec3 m,vec3 f,vec3 n,float y)" + "vec3 e(vec3 v,vec3 f,vec3 d,float i)" "{" - "v-=m;" - "float e=length(v);" - "v=normalize(v);" - "i/=1.+.09*e+.032*e*e;" - "float F=max(dot(n,v),0.);" - "f=normalize(v-f);" + "vec3 m=vec3(.77,.26,.73),l=vec3(0,40,-10)-v;" + "float n=length(l);" + "l=normalize(l);" + "float y=30./(1.+.09*n+.032*n*n),c=max(dot(d,l),0.);" + "f=normalize(l-f);" "vec3 r=vec3(.04);" - "r+=(1.-r)*pow(clamp(1.-max(dot(f,v),0.),0.,1.),5.);" - "e=t(m+n*.01,v,e);" - "return(k*F*i+k*pow(max(dot(n,f),0.),mix(128.,8.,y))*i*r)*e;" + "r+=(1.-r)*pow(clamp(1.-max(dot(f,l),0.),0.,1.),5.);" + "n=t(v+d*.01,l,n);" + "return(m*c*y+m*pow(max(dot(d,f),0.),mix(128.,8.,i))*y*r)*n;" "}" - "vec3 e(vec3 v,vec3 i,vec3 f,float k)" + "vec3 e(vec3 v,vec3 i,float d,vec3 f,vec3 n,vec3 m,float l)" + "{" + "v-=f;" + "float r=length(v);" + "v=normalize(v);" + "r=clamp(d-r*r/11.56,0.,1.);" + "d=max(dot(m,v),0.);" + "f=normalize(v-n);" + "n=vec3(.04);" + "v=n+(1.-n)*pow(clamp(1.-max(dot(f,v),0.),0.,1.),5.);" + "return i*d*r+i*pow(max(dot(m,f),0.),mix(128.,8.,l))*r*v;" + "}" + "vec3 t(vec3 v,vec3 f,vec3 m,float l)" "{" "float n=.01;" "vec3 y=vec3(0);" - "if(k==0.)" + "if(l==0.)" "y=vec3(.8314,.2941,.2941),n=.1;" - "else if(k==1.)" + "else if(l==1.)" "y=vec3(.6196,.6118,.6118),n=.7;" - "else if(k==2.)" + "else if(l==2.)" "y=vec3(.3255,.4784,.3255),n=.2;" - "else if(k==3.)" + "else if(l==3.)" "y=vec3(.2471,.3059,.6314),n=1.;" - "else if(k==4.)" + "else if(l==4.)" "y=vec3(.9961,1,.9922),n=.1;" - "else if(k==5.)" + "else if(l==5.)" "y=vec3(.9961,1,.9922),n=.3;" - "v=vec3(0)+e(vec3(0,40,-10),vec3(.77,.26,.73),30.,v,f,i,n)+e(vec3(0,6,-5),vec3(.08,.62,.75),20.,v,f,i,n)+e(vec3(0,20,15),vec3(.77,.26,.73),30.,v,f,i,n)+vec3(.08,.62,.75)*clamp(dot(i,normalize(vec3(0,1,-3)*vec3(0,-1,-2))),0.,1.)*.8;" - "return y*max(vec3(0),v);" + "vec3 r=vec3(0);" + "r+=e(v,m,f,n);" + "for(float l=0.;l<16.;l++)" + "for(float y=0.;y<16.;y++)" + "{" + "ivec2 c=textureSize(d,0);" + "vec2 x=vec2(y,l)/vec2(c);" + "vec4 t=texture(d,x);" + "vec3 k=t.xyz;" + "float C=i[clamp(int(t.w/34.*512.),0,511)]*5.;" + "r+=e(vec3(k.x,k.y+C+3.,k.z),vec3(.46,.2,.94)+vec3(.66,.64,.77)*cos(6.28318*(C*1.5*vec3(.91,.62,.97)+vec3(.26,.2,.84))),clamp(C*3.,0.,1.),v,m,f,n);" + "}" + "r+=vec3(.08,.62,.75)*clamp(dot(f,normalize(vec3(0,1,-3)*vec3(0,-1,-2))),0.,1.)*.8;" + "return y*max(vec3(0),r);" "}" - "vec3 e(vec2 k,vec3 v)" + "vec3 e(vec2 v,vec3 f)" "{" - "v=normalize(vec3(0,-1,10)-v);" - "vec3 n=normalize(cross(vec3(0,1,0),v));" - "return normalize(v+k.x*n+k.y*cross(v,n));" + "f=normalize(vec3(0,-1,10)-f);" + "vec3 n=normalize(cross(vec3(0,1,0),f));" + "return normalize(f+v.x*n+v.y*cross(f,n));" "}" "vec3 e(vec2 v)" "{" - "vec3 k=vec3(0,20,-10),n=e(v,k),f=vec3(0),y=vec3(0);" - "k=t(k,n,y);" - "if(k.x>0.)" + "vec3 f=vec3(-10,20,-20),n=e(v,f),l=vec3(0),d=vec3(0);" + "f=t(f,n,d);" + "if(f.x>0.)" "{" - "vec3 v=e(y);" - "f=e(y,v,n,k.y);" + "vec3 v=e(d);" + "l=t(d,v,n,f.y);" "}" - "return f;" + "return l;" "}" "void main()" "{" diff --git a/src/sointu/song.h b/src/sointu/song.h index b963fed..13ff655 100644 --- a/src/sointu/song.h +++ b/src/sointu/song.h @@ -13,7 +13,7 @@ #define SU_LENGTH_IN_PATTERNS 108 #define SU_LENGTH_IN_ROWS (SU_LENGTH_IN_PATTERNS*SU_PATTERN_SIZE) #define SU_SAMPLES_PER_ROW (SU_SAMPLE_RATE*60/(SU_BPM*SU_ROWS_PER_BEAT)) -#define SU_NUMSYNCS 12 +#define SU_NUMSYNCS 11 #define SU_SYNCBUFFER_LENGTH ((SU_LENGTH_IN_SAMPLES+255)>>8)*SU_NUMSYNCS #include