diff --git a/src/main.cpp b/src/main.cpp index 28ecfb5..8ea282e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -270,8 +270,8 @@ int __cdecl main(int argc, char* argv[]) for (int i = 0; i < (FFT_SIZE / 4); i++) { float gain = 50.0f; - float alpha = 0.10f; // "Hidastaa" FFT:n piikkejä - float threshhold = 0.05f; // Alin arvo mikä päästetään shaderille (vähentää "noisea") + float alpha = 0.10f; // "Hidastaa" FFT:n piikkej� + float threshhold = 0.05f; // Alin arvo mik� p��stet��n shaderille (v�hent�� "noisea") float x_t = fft_output[i] * gain; // Exponential smoothing kaava // s(t) = alpha*x(t)+(1-alpha)*s(t-1) @@ -301,12 +301,12 @@ int __cdecl main(int argc, char* argv[]) bool shapeJustStarted = false; // Detect note change to start a new shape - bool noteChanged = (captureSync - lastNote) * (captureSync - lastNote) > 0.0001f; + bool noteChanged = (captureSync != lastNote) ? TRUE : FALSE; lastNote = captureSync; // If note changed and value is significant, start new shape - if (captureSync > 0.001f && noteChanged) { + if (noteChanged) { int index = currentShape * 4; // Reset and activate current shape @@ -345,10 +345,10 @@ int __cdecl main(int argc, char* argv[]) } // Bind and update u_ShapesTex - glActiveTexture(GL_TEXTURE0 + 2); + 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); - glUniform1i(glGetUniformLocation(pidMain, "u_ShapesTex"), 2); /* @@ -371,58 +371,6 @@ int __cdecl main(int argc, char* argv[]) 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; - - 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; - } - } - - // Bind and update u_hexGridTex - glActiveTexture(GL_TEXTURE0 + 1); - glBindTexture(GL_TEXTURE_2D, hexGridTex); - glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, GRID, GRID, GL_RGBA, GL_FLOAT, hexGridData); - glUniform1i(glGetUniformLocation(pidMain, "u_hexGridTex"), 1); - glRects(-1, -1, 1, 1); // render "post process" using the opengl backbuffer diff --git a/src/shaders/fragment.frag b/src/shaders/fragment.frag index 80d6046..b927812 100644 --- a/src/shaders/fragment.frag +++ b/src/shaders/fragment.frag @@ -8,7 +8,7 @@ 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 -layout(binding = 1) uniform sampler2D u_hexGridTex; //uniform sampler2D u_fft_texture; +//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]; @@ -183,7 +183,8 @@ vec2 mapScene(in vec3 p) { float gridSize2 = 16.; for(float j = 0.; j < gridSize2; j++) { for(float i = 0.; i < gridSize2; i++) { - vec2 texCoord = (vec2(i, j) + 0.5) / float(gridSize2); + 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; @@ -213,55 +214,55 @@ vec3 castRay(vec3 ro, vec3 rd, inout vec3 pos) { const float Z_REPEAT_DIST = 1.; vec2 res; - // Raymarching - for (int i = 0; i < 50; i++) { - pos = ro + rd * t; - res = mapScene(pos); // Get distance to objects - ad = abs(res.x); - mat = res.y; - if (t > tmax) break; - if (ad < tolerance*(t*0.125 + 1.0)) { - hit = 1.0; - break; - } - t += res.x; // "march" the ray - } - t -= Z_REPEAT_DIST*1.; - - for( int i=0; i<30; i++ ) - { - vec3 pos2 = ro + rd * t; - res = mapScene(pos2); // get distance to objects - ad = abs(res.x); - mat = res.y; - if (ad < (tolerance)) - { - hit = 1.0; - pos = pos2; - break; - } - if (t > tmax) break; - t += min(d.x, Z_REPEAT_DIST/2.0); // "march" the ray - } +// // Raymarching +// for (int i = 0; i < 50; i++) { +// pos = ro + rd * t; +// res = mapScene(pos); // Get distance to objects +// ad = abs(res.x); +// mat = res.y; +// if (t > tmax) break; +// if (ad < tolerance*(t*0.125 + 1.0)) { +// hit = 1.0; +// break; +// } +// t += res.x; // "march" the ray +// } +// t -= Z_REPEAT_DIST*1.; +// +// for( int i=0; i<30; i++ ) +// { +// vec3 pos2 = ro + rd * t; +// res = mapScene(pos2); // get distance to objects +// ad = abs(res.x); +// mat = res.y; +// if (ad < (tolerance)) +// { +// hit = 1.0; +// pos = pos2; +// break; +// } +// if (t > tmax) break; +// t += min(d.x, Z_REPEAT_DIST/2.0); // "march" the ray +// } //if (t >= tmax) { // t= - 1.0; // hit = 0.; //} //Reduced from 40 to 24 steps -// for(int i = 0; i < 30; i++) { -// pos = ro + rd * t; -// vec2 res = mapScene(pos); -// // Increase step size multiplier for faster marching -// t += res.x; -// mat = res.y; -// if(t > 400.) { // Reduced max distance -// break; -// } -// if(res.x < 0.00001 * (t*0.00125 + 1.0)) { // Less precise hit detection -// hit = 1.; -// break; -// } -// } + for(int i = 0; i < 30; i++) { + pos = ro + rd * t; + vec2 res = mapScene(pos); + // Increase step size multiplier for faster marching + t += res.x; + mat = res.y; + if(t > 400.) { // Reduced max distance + break; + } + if(res.x < 0.00001 * (t*0.00125 + 1.0)) { // Less precise hit detection + hit = 1.; + break; + } + } // This will break fog effect //if (t > 100.) // t = 0.; diff --git a/src/shaders/fragment.inl b/src/shaders/fragment.inl index 67ff9d0..223e5a9 100644 --- a/src/shaders/fragment.inl +++ b/src/shaders/fragment.inl @@ -1,193 +1,175 @@ // Generated with Shader Minifier 1.5.1 (https://github.com/laurentlb/Shader_Minifier/) #ifndef FRAGMENT_INL_ # define FRAGMENT_INL_ -# define VAR_fft_output "H" -# define VAR_o "f" -# define VAR_shapes "a" +# define VAR_fft_output "f" +# define VAR_o "v" +# define VAR_shapes "H" # define VAR_syncs "m" # define VAR_test "l" -# define VAR_u_ShapesTex "x" -# define VAR_u_hexGridTex "k" +# define VAR_u_ShapesTex "a" const char *fragment_frag = "#version 460\n" "precision mediump float;" - "out vec4 f;" - "const float i=2.*acos(-1.),v=sqrt(5.)*.5+.5;" + "out vec4 v;" + "const float i=2.*acos(-1.),n=sqrt(5.)*.5+.5;" "layout(location=0)uniform float m[12];" - "layout(location=20)uniform float H[512];" - "layout(location=600)uniform vec3 a[15];" + "layout(location=20)uniform float f[512];" + "layout(location=600)uniform vec3 H[15];" "layout(location=700)uniform vec3 l;" - "layout(binding=1)uniform sampler2D k;" - "layout(binding=0)uniform sampler2D x;" - "float n=m[0];" - "mat2 s()" + "layout(binding=0)uniform sampler2D a;" + "float g=m[0];" + "mat2 t()" "{" - "float v=sin(.5),x=cos(.5);" - "return mat2(x,-v,v,x);" + "float v=sin(.5),a=cos(.5);" + "return mat2(a,-v,v,a);" "}" - "float s(vec3 v,vec2 x)" + "float t(vec3 v,vec2 i)" "{" "v=abs(v);" - "return max(v.y-x.y,max(v.x*sqrt(3.)*.5+v.z*.5,v.z)-x.x);" + "return max(v.y-i.y,max(v.x*sqrt(3.)*.5+v.z*.5,v.z)-i.x);" "}\n" "#define zclamp(a)max(a,0.0)\n" "struct HexData{vec3 local;vec2 axial;};" - "HexData s(vec3 v)" + "HexData t(vec3 v)" "{" - "float x=sqrt(3.)/3.*v.x-1./3.*v.z,f=2./3.*v.z,m=round(x),l=round(f),k=round(-x-f),n=abs(m-x),a=abs(l-f);" - "x=abs(k+x+f);" - "if(n>a&&n>x)" - "m=-l-k;" - "else if(a>x)" - "l=-m-k;" - "x=sqrt(3.)*(m+l*.5);" - "f=1.5*l;" - "HexData i;" - "i.local=v-vec3(x,0,f);" - "i.axial=vec2(m,l);" - "return i;" + "float i=sqrt(3.)/3.*v.x-1./3.*v.z,f=2./3.*v.z,a=round(i),x=round(f),l=round(-i-f),m=abs(a-i),n=abs(x-f);" + "i=abs(l+i+f);" + "if(m>n&&m>i)" + "a=-x-l;" + "else if(n>i)" + "x=-a-l;" + "i=sqrt(3.)*(a+x*.5);" + "f=1.5*x;" + "HexData r;" + "r.local=v-vec3(i,0,f);" + "r.axial=vec2(a,x);" + "return r;" "}" "struct HexData{vec3 local;vec2 axial;};" - "float s(vec2 v)" + "float t(vec2 v)" "{" - "float x=v.x,f=v.y;" - "return max(abs(x),max(abs(f),abs(-x-f)));" + "float i=v.x,a=v.y;" + "return max(abs(i),max(abs(a),abs(-i-a)));" "}" - "vec2 t(vec3 v)" + "vec2 s(vec3 v)" "{" - "vec2 f=vec2(v.y,0);" - "HexData m=s(vec3(v.x,v.y-2.5,v.z));" - "float i=1.+H[int(clamp(s(m.axial)+1.,0.,511.))]*3.;" - "vec3 l=m.local;" - "l.xz*=s();" - "i=s(vec3(l.x,l.y-i/2,l.z),vec2(.83,i/2));" - "f=min(f,vec2(i,0));" - "for(float i=0.;i<16.;i++)" + "vec2 i=vec2(v.y,0);" + "HexData m=t(vec3(v.x,v.y-2.5,v.z));" + "float l=1.+f[int(clamp(t(m.axial)+1.,0.,511.))]*3.;" + "vec3 r=m.local;" + "r.xz*=t();" + "l=t(vec3(r.x,r.y-l/2,r.z),vec2(.83,l/2));" + "i=min(i,vec2(l,0));" + "for(float f=0.;f<16.;f++)" "for(float l=0.;l<16.;l++)" "{" - "vec2 m=(vec2(l,i)+.5)/float(16.);" - "vec4 n=texture(x,m);" - "if(n.w<.5)" + "ivec2 m=textureSize(a,0);" + "vec2 n=vec2(l,f)/vec2(m);" + "vec4 r=texture(a,n);" + "if(r.w<.5)" "continue;" - "f=min(f,vec2(length(v-vec3(-70.+n.x*2.,12.+n.y*80.,0))-.8,1));" + "i=min(i,vec2(length(v-vec3(-70.+r.x*2.,12.+r.y*80.,0))-.8,1));" "}" - "return f;" + "return i;" "}" - "vec3 s(vec3 v,vec3 x,inout vec3 f)" + "vec3 s(vec3 v,vec3 i,inout vec3 f)" "{" - "float i=0.,l=0.;" - "vec3 m;" - "float n=0.,k;" - "vec2 a;" - "for(int m=0;m<50;m++)" - "{" - "f=v+x*n;" - "a=t(f);" - "k=abs(a.x);" - "i=a.y;" - "if(n>4e2)" - "break;" - "if(k<.001*(n*.125+1.))" - "{" - "l=1.;" - "break;" - "}" - "n+=a.x;" - "}" - "n-=1.;" + "float l=0.,a=0.,x=0.;" "for(int r=0;r<30;r++)" "{" - "vec3 s=v+x*n;" - "a=t(s);" - "k=abs(a.x);" - "i=a.y;" - "if(k<.001)" + "f=v+i*x;" + "vec2 m=s(f);" + "x+=m.x;" + "l=m.y;" + "if(x>4e2)" + "break;" + "if(m.x<1e-5*(x*.00125+1.))" "{" - "l=1.;" - "f=s;" + "a=1.;" "break;" "}" - "if(n>4e2)" - "break;" - "n+=min(m.x,.5);" "}" - "return vec3(n,i,l);" + "return vec3(x,l,a);" "}" - "float s(vec3 v,vec3 x,float f)" + "float s(vec3 v,vec3 i,float f)" "{" - "float i=1.,l=.02;" - "for(int m=0;m<6;m++)" + "float l=1.,a=.02;" + "for(int r=0;r<6;r++)" "{" - "if(l>f)" + "if(a>f)" "break;" - "float n=t(v+l*x).x;" - "i=min(i,n/(4.*l));" - "l+=clamp(n,.1,.8);" - "if(i<-1.)" + "float m=s(v+a*i).x;" + "l=min(l,m/(4.*a));" + "a+=clamp(m,.1,.8);" + "if(l<-1.)" "break;" "}" - "i=max(i,-1.);" - "return.25*(1.+i)*(1.+i)*(2.-i);" + "l=max(l,-1.);" + "return.25*(1.+l)*(1.+l)*(2.-l);" "}" - "vec3 p(vec3 v)" + "vec3 x(vec3 v)" "{" "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));" + "v=vec3(s(v+i.xyy).x-s(v-i.xyy).x,s(v+i.yxy).x-s(v-i.yxy).x,s(v+i.yyx).x-s(v-i.yyx).x);" + "return normalize(v);" "}" - "vec3 p(vec3 v,vec3 m,vec3 i,vec3 x,float f)" + "vec3 s(vec3 v,vec3 l,vec3 i,vec3 a,float f)" "{" - "vec3 l=vec3(.77,.26,.73);" - "v-=m;" - "float n=length(v);" + "vec3 m=vec3(.77,.26,.73);" + "v-=l;" + "float r=length(v);" "v=normalize(v);" - "float a=30./(1.+.09*n+.032*n*n),k=max(dot(x,v),0.);" + "float x=30./(1.+.09*r+.032*r*r),n=max(dot(a,v),0.);" "i=normalize(v-i);" - "vec3 r=vec3(.04);" - "r+=(1.-r)*pow(clamp(1.-max(dot(i,v),0.),0.,1.),5.);" - "n=s(m+x*.01,v,n);" - "return(l*k*a+l*pow(max(dot(x,i),0.),mix(128.,8.,f))*a*r)*n;" + "vec3 e=vec3(.04);" + "e+=(1.-e)*pow(clamp(1.-max(dot(i,v),0.),0.,1.),5.);" + "r=s(l+a*.01,v,r);" + "return(m*n*x+m*pow(max(dot(a,i),0.),mix(128.,8.,f))*x*e)*r;" "}" - "vec3 p(vec3 v,vec3 i,vec3 x,float f)" + "vec3 s(vec3 v,vec3 a,vec3 i,float l)" "{" - "float l=.01;" + "float f=.01;" "vec3 m=vec3(0);" - "if(f==0.)" - "m=vec3(.8314,.2941,.2941),l=.1;" - "else if(f==1.)" - "m=vec3(.6196,.6118,.6118),l=.7;" - "else if(f==2.)" - "m=vec3(.3255,.4784,.3255),l=.2;" - "else if(f==3.)" - "m=vec3(.2471,.3059,.6314),l=1.;" - "else if(f==4.)" - "m=vec3(.9961,1,.9922),l=.1;" - "else if(f==5.)" - "m=vec3(.9961,1,.9922),l=.3;" - "v=vec3(0)+p(vec3(0,40,-10),v,x,i,l)+p(vec3(0,20,15),v,x,i,l)+vec3(.08,.62,.75)*clamp(dot(i,normalize(vec3(0,1,-3)*vec3(0,-1,-2))),0.,1.)*.8;" - "return m*max(vec3(0),v);" + "if(l==0.)" + "m=vec3(.8314,.2941,.2941),f=.1;" + "else if(l==1.)" + "m=vec3(.6196,.6118,.6118),f=.7;" + "else if(l==2.)" + "m=vec3(.3255,.4784,.3255),f=.2;" + "else if(l==3.)" + "m=vec3(.2471,.3059,.6314),f=1.;" + "else if(l==4.)" + "m=vec3(.9961,1,.9922),f=.1;" + "else if(l==5.)" + "m=vec3(.9961,1,.9922),f=.3;" + "vec3 r=vec3(0);" + "r+=s(vec3(0,40,-10),v,i,a,f);" + "r+=s(vec3(0,20,15),v,i,a,f);" + "r+=vec3(.08,.62,.75)*clamp(dot(a,normalize(vec3(0,1,-3)*vec3(0,-1,-2))),0.,1.)*.8;" + "return m*max(vec3(0),r);" "}" - "vec3 p(vec2 v,vec3 f)" + "vec3 s(vec2 i,vec3 v)" "{" - "f=normalize(vec3(0,10,0)-f);" - "vec3 l=normalize(cross(vec3(0,1,0),f));" - "return normalize(f+v.x*l+v.y*cross(f,l));" + "v=normalize(vec3(0,10,0)-v);" + "vec3 f=normalize(cross(vec3(0,1,0),v));" + "return normalize(v+i.x*f+i.y*cross(v,f));" "}" - "vec3 p(vec2 v)" + "vec3 s(vec2 v)" "{" - "vec3 f=vec3(-20,20,-20),l=p(v,f),i=vec3(0),x=vec3(0);" - "f=s(f,l,x);" - "if(f.x>0.)" + "vec3 i=vec3(-20,20,-20),a=s(v,i),l=vec3(0),f=vec3(0);" + "i=s(i,a,f);" + "if(i.x>0.)" "{" - "vec3 v=p(x);" - "i=p(x,v,l,f.y);" + "vec3 v=x(f);" + "l=s(f,v,a,i.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 l*exp(-i.x*.01)+mix(l,mix(vec3(.2667,.2941,.3451),vec3(.302,.3176,.3725),pow(max(dot(a,vec3(0,-.5,1.8)),0.),clamp(m[1]+m[2]+m[3],0.,1.)*5.)),1.-exp(-i.x*.01))*(1.-exp(-i.x*.01));" "}" "void main()" "{" - "vec3 v=p(gl_FragCoord.xy*vec2(.00104166667,.00185185185)-1.);" - "f=vec4(v,1);" + "vec3 l=s(gl_FragCoord.xy*vec2(.00104166667,.00185185185)-1.);" + "v=vec4(l,1);" "}"; #endif // FRAGMENT_INL_