Skip to content

Instantly share code, notes, and snippets.

@gaziya
Created February 19, 2017 16:24
Show Gist options
  • Save gaziya/6771d2a3a9c5b801251d7b396f57c3c0 to your computer and use it in GitHub Desktop.
Save gaziya/6771d2a3a9c5b801251d7b396f57c3c0 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"vsh = \"\"\"\n",
"#version 330\n",
"\n",
"uniform vec2 resolution;\n",
"uniform float time;\n",
"\n",
"out float vPartsID;\n",
"out float vGroupID;\n",
"out vec3 vEye;\n",
"out vec3 vPos;\n",
"out mat3 vMat;\n",
"out vec3 vLight;\n",
"out float vLen;\n",
"out vec2 vResolution;\n",
"\n",
"#define SLICE_NUM 30.0\n",
"#define X vec3(1,0,0)\n",
"#define Y vec3(0,1,0)\n",
"#define Z vec3(0,0,1)\n",
"#define PI\t3.1415\n",
"#define PI2 6.2832\n",
"#define PIH 1.5708\n",
"\n",
"float TIME,ID;\n",
"\n",
"mat2 rotate(in float a)\n",
"{\n",
"\treturn mat2(cos(a), sin(a), -sin(a), cos(a));\n",
"}\n",
"\n",
"mat4 moveTo(in vec3 p)\n",
"{\n",
" mat4 m = mat4(1);\n",
" m[3].xyz = p;\n",
" return m;\n",
"}\n",
"\n",
"mat4 moveX(in float x)\n",
"{\n",
" return moveTo(vec3(x,0,0));\n",
"}\n",
"\n",
"mat4 moveY(in float y)\n",
"{\n",
" return moveTo(vec3(0,y,0));\n",
"}\n",
"\n",
"mat4 rotate4(in vec3 axis, in float theta)\n",
"{\n",
" axis = normalize(axis);\n",
" float x = axis.x, y = axis.y, z = axis.z, s = sin(theta), c = cos(theta), o = 1.0-c;\n",
" return mat4(\n",
" o*x*x+c,o*x*y+z*s,o*z*x-y*s,0,\n",
" o*x*y-z*s,o*y*y+c,o*y*z+x*s,0,\n",
" o*z*x+y*s,o*y*z-x*s,o*z*z+c,0,\n",
" 0,0,0,1);\n",
"}\n",
"\n",
"mat4 rotate4X(float a)\n",
"{\n",
" return mat4(1,0,0,0,0,cos(a),sin(a),0,0,-sin(a),cos(a),0,0,0,0,1);\n",
"}\n",
"\n",
"mat4 rotate4Y(float a)\n",
"{\n",
" return mat4(cos(a),0,-sin(a),0,0,1,0,0,sin(a),0,cos(a),0,0,0,0,1);\n",
"}\n",
"\n",
"mat3 rotate3(in vec3 axis, in float theta)\n",
"{\n",
" mat4 m4 = rotate4(axis, theta);\n",
" return mat3(m4[0].xyz,m4[1].xyz,m4[2].xyz);\n",
"}\n",
"\n",
"mat3 rotate3X(in float theta)\n",
"{\n",
" return rotate3(X, theta);\n",
"}\n",
"\n",
"mat3 matchTo(in vec3 a, in vec3 b)\n",
"{\n",
" a = normalize(a);\n",
" b = normalize(b);\n",
" return rotate3(cross(b,a), acos(dot(a,b)));\n",
"}\n",
"\n",
"mat4 matchTo4(in vec3 a, in vec3 b)\n",
"{\n",
" a = normalize(a);\n",
" b = normalize(b);\n",
" return rotate4(cross(b,a), acos(dot(a,b)));\n",
"}\n",
"\n",
"float hash(float n)\n",
"{\n",
" return fract(sin(n) * 43758.5453123);\n",
"}\n",
"\n",
"vec2 hashVector(in float n) \n",
"{\n",
" float a =sin(mod(n,PI2))*PI2;\n",
" return vec2(cos(a),sin(a)); \n",
"} \n",
"\n",
"float scene(in float t1, in float t2)\n",
"{\n",
" return smoothstep(t1,t2,time); \n",
"}\n",
"\n",
"float ezing(in float t)\n",
"{\n",
" return pow(sin(t * PIH), 0.3);\n",
"}\n",
"\n",
"vec2 randomWalk(float t)\n",
"{\n",
" vec2 p = vec2(0);\n",
" float n = 23532.682;\n",
" t *= 0.15;\n",
" for (int i=0; i<6; i++)\n",
" {\n",
" p += hashVector(n) * cos(t)+ hashVector(n*2.356) * sin(t);\n",
" n +=12.78; t *= 1.36456;\n",
" }\n",
" return p;\n",
"}\n",
"\n",
"vec3 sphere(in float n, in float r)\n",
"{\n",
" float id = floor(n/49.0);\n",
" n = mod(n,49.0);\n",
" vec3 p = vec3(floor(n/7.0), mod(n, 7.0), 0)/7.0*2.0-1.0;\n",
" p.z = (mod(id,2.0)<0.5)?1.0:-1.0;\n",
" p = (id>3.5)?p.yzx:(id>1.5)?p.zxy:p;\n",
" return r * normalize(p);\n",
"}\n",
"\n",
"vec3 torusSpin(in float n, in float rH, in float rV, in float spinV, in float spinH)\n",
"{\n",
" float t = PI2 * n / 294.0;\n",
" float vi = floor(spinV);\n",
" vec3 p = rV*mix(\n",
" vec3(rotate(t*vi)* vec2(1,0), 0.0),\n",
" vec3(rotate(t*(vi+1.0))* vec2(1,0), 0.0),\n",
" fract(spinV)\n",
" );\n",
" p.x+= rH;\n",
" float hi = floor(spinH);\n",
" return mix(\n",
" vec3(rotate(t*hi)*p.xz,p.y).xzy,\n",
" vec3(rotate(t*(hi+1.0))*p.xz,p.y).xzy,\n",
" fract(spinH)\n",
" );\n",
"}\n",
"\n",
"vec4 hex_star(in float modelID, in float bbID, in vec3 localPos, in vec3 eye, in vec3 light)\n",
"{\n",
" vec3 worldPos;\n",
" mat3 localMat;\n",
" float sn = (modelID-0.5)*2.0;\n",
" if (time <40.0)\n",
" { \n",
" worldPos = sphere(bbID, 5.0);\n",
" localMat = matchTo(Z, worldPos);\n",
" float n = hash((atan(worldPos.x,worldPos.z)+5.)*(worldPos.y+3.));\n",
" float s0 = scene(43.0,26.0);\n",
" worldPos *= mix(\n",
" vec3(1),\n",
" vec3(\n",
" sin(3.*time+sin(time*2.0)),\n",
" sin(time*3.2),\n",
" cos(3.0*time)\n",
" )*0.3+1.0,\n",
" s0\n",
" );\n",
" worldPos.x += sn * 25.0 * scene(25.0,0.0);\n",
" worldPos *= max(1.0,\n",
" sn * 10.0*n*sin(time + 2.0*n*sin(3.0*n*time)) *\n",
" s0\n",
" );\n",
" worldPos.x +=\n",
" sn*3.0*\n",
" sin(time + 2.0*sin(time*3.0 + 5.0*sin(time*5.0)))*\n",
" pow(sin(time),3.5)*\n",
" s0;\n",
" sn *= 2.0;\n",
" } \n",
" else if (time <55.0)\n",
" {\n",
" vec3 spherePos = sphere(bbID, 5.0);\n",
" vec3 torusPos =\n",
" torusSpin(\n",
" bbID, \n",
" mix(0.0, 6.0 + 2.0*sin(time*3.0),scene(60.0, 65.0)),\n",
" 6.0,\n",
" sin(time*0.2)*8.0,\n",
" 1.0\n",
" );\n",
" float s0 = scene(47.0, 55.0);\n",
" localMat = matchTo(Z, mix(spherePos, torusPos, s0));\n",
" worldPos = mix(\n",
" spherePos +\n",
" sn* vec3(15,0,0) * ezing(scene(40.0, 41.0)) +\n",
" spherePos * 15.0 * ezing(scene(44.0, 50.0)),\n",
" torusPos + sn * vec3(8,0,0),\n",
" s0\n",
" );\n",
" sn *= mix(1.0, 3.0,scene(52.0,55.0));\n",
" }\n",
" else if (time <72.0)\n",
" { \n",
" worldPos = \n",
" torusSpin(\n",
" bbID,\n",
" mix(0.0, 6.0 + 2.0*sin(time*3.0),scene(60.0, 65.0)),\n",
" 6.0,\n",
" sin(time*0.2)*8.0,\n",
" 1.0\n",
" );\n",
" localMat = matchTo(Z, worldPos);\n",
" worldPos.x += sn * mix(8.0,10.0*sin(time),scene(58.0,65.0));\n",
" sn *= 3.0;\n",
" }\n",
" else if (time <100.0)\n",
" { \n",
" worldPos = 12.0 * mix(\n",
" vec3(randomWalk((bbID+time*10.)*0.3),randomWalk(0.2*(bbID+time*10.)+50.).x)*0.5,\n",
" torusSpin(bbID,0.8,0.5,sin(time*0.5)*8.0, 3.),\n",
" sin(time*0.3)*0.5 +0.5\n",
" );\n",
" localMat = matchTo(Z, worldPos);\n",
" worldPos.x += sn * 10.0 * sin(time*0.5 + 3.0 * sin(time * 0.5));\n",
" sn *= 2.0;\n",
" }\n",
" else\n",
" {\n",
" worldPos = 12.0 *\n",
" torusSpin(\n",
" bbID,\n",
" 1.0,\n",
" 0.8,\n",
" sin(time*0.2+2.0*sin(time*0.5))*8.0,\n",
" 3.+1.5*sin(time*0.2+2.0*sin(time*0.5))\n",
" );\n",
" localMat = matchTo(Z, worldPos);\n",
" worldPos.x += sn;\n",
" sn=2.0;\n",
" } \n",
" \n",
" if(time> 120.0)\n",
" {\n",
" worldPos *= \n",
" mix(1.0, 0.3, scene(130.0,139.0)) *\n",
" mix(1.0, 0.1, ezing(scene(140.0,141.0))) *\n",
" mix(1.0, 500.0, ezing(scene(141.2,143.0)));\n",
" }\n",
" worldPos *= rotate3X(sn * sin(time*0.2+2.0*sin(time*0.5)));\n",
" mat3 worldMat = rotate3(Y, 5.0*sin(time*0.02 + 3.5* sin(time*0.06)));\n",
" worldPos = worldMat*worldPos;\n",
" vPos = localMat*(localPos*worldMat);\n",
" vLight = localMat*(light*worldMat);\n",
" return vec4(matchTo(eye-worldPos, Z)*localPos + worldPos, 1.0); \n",
"}\n",
"\n",
"mat4 rootMat()\n",
"{\n",
" return moveTo(vec3(0));\n",
"}\n",
"\n",
"mat4 waistMat(in mat4 m)\n",
"{\n",
" return m *rotate4Y(0.3*sin(TIME))* moveTo(vec3(0,0.8,0.2));\n",
"}\n",
"\n",
"mat4 breastMat(in mat4 m)\n",
"{\n",
" return m * moveTo(vec3(0,0.8,0.2));\n",
"}\n",
"\n",
"mat4 neckMat(in mat4 m)\n",
"{\n",
" return m * rotate4Y(-0.3*sin(TIME)) * moveY(0.7);\n",
"}\n",
"\n",
"mat4 headMat(in mat4 m)\n",
"{\n",
" return m * moveTo(vec3(0,0.25,0.05));\n",
"}\n",
"\n",
"mat4 hipMat(in mat4 m)\n",
"{\n",
" return m * rotate4Y(-0.1*sin(TIME))*moveY(-0.4);\n",
"}\n",
"\n",
"mat4 leftShoulderMat(in mat4 m)\n",
"{\n",
" return m * moveTo(vec3(-0.6,-0.1,0));\n",
"}\n",
"\n",
"mat4 rightShoulderMat(in mat4 m)\n",
"{\n",
" return m * moveTo(vec3(0.6,-0.1,0));\n",
"}\n",
"\n",
"mat4 leftUpperArmMat(in mat4 m)\n",
"{\n",
" vec3 p = normalize(vec3(1,-1,0));\n",
" p *= (hash(ID)<0.2)?rotate3X(TIME):rotate3X(sin(TIME));\n",
" return m * matchTo4(X,p) * moveX(-1.2);\n",
"}\n",
"\n",
"mat4 rightUpperArmMat(in mat4 m)\n",
"{\n",
" vec3 p = normalize(vec3(1,0.2,0));\n",
" p *= (hash(ID)>0.8)?rotate3X(-TIME):rotate3X(sin(-TIME));\n",
" return m * rotate4(cross(p,X),dot(X,p)) * moveX(1.2);\n",
"}\n",
"\n",
"mat4 leftLowerArmMat(in mat4 m)\n",
"{\n",
" return m * rotate4Y(1.0) * moveX(-1.0);\n",
"}\n",
"\n",
"mat4 rightLowerArmMat(in mat4 m)\n",
"{\n",
" return m * rotate4Y(-1.0) * moveX(1.0);\n",
"}\n",
"\n",
"mat4 leftCrotchMat(in mat4 m)\n",
"{\n",
" return m * moveX(-0.5);\n",
"}\n",
"\n",
"mat4 rightCrotchMat(in mat4 m)\n",
"{\n",
" return m * moveX(0.5);\n",
"}\n",
"\n",
"mat4 leftUpperLegMat(in mat4 m)\n",
"{\n",
" return m * rotate4X(sin(TIME)) * moveY(-1.5);\n",
"}\n",
"\n",
"mat4 rightUpperLegMat(in mat4 m)\n",
"{\n",
" return m * rotate4X(-sin(TIME)) * moveY(-1.5);\n",
"}\n",
"\n",
"mat4 leftLowerLegMat(in mat4 m)\n",
"{\n",
" return m * rotate4X(0.3*sin(TIME*2.0)+0.8) * moveY(-1.6);\n",
"}\n",
"\n",
"mat4 rightLowerLegMat(in mat4 m)\n",
"{\n",
" return m * rotate4X(0.3*sin(TIME*2.0)+0.8) * moveY(-1.6);\n",
"}\n",
"\n",
"mat4 waistMat(){return waistMat(rootMat());}\n",
"mat4 breastMat(){return breastMat(waistMat());}\n",
"mat4 neckMat(){return neckMat(breastMat());}\n",
"mat4 headMat(){return headMat(neckMat());}\n",
"mat4 hipMat(){return hipMat(rootMat());}\n",
"mat4 leftShoulderMat(){return leftShoulderMat(breastMat());}\n",
"mat4 rightShoulderMat(){return rightShoulderMat(breastMat());}\n",
"mat4 leftUpperArmMat(){return leftUpperArmMat(leftShoulderMat());}\n",
"mat4 rightUpperArmMat(){return rightUpperArmMat(rightShoulderMat());}\n",
"mat4 leftLowerArmMat(){return leftLowerArmMat(leftUpperArmMat());}\n",
"mat4 rightLowerArmMat(){return rightLowerArmMat(rightUpperArmMat());}\n",
"mat4 leftCrotchMat(){return leftCrotchMat(hipMat());}\n",
"mat4 rightCrotchMat(){return rightCrotchMat(hipMat());}\n",
"mat4 leftUpperLegMat(){return leftUpperLegMat(leftCrotchMat());}\n",
"mat4 rightUpperLegMat(){return rightUpperLegMat(rightCrotchMat());}\n",
"mat4 leftLowerLegMat(){return leftLowerLegMat(leftUpperLegMat());}\n",
"mat4 rightLowerLegMat(){return rightLowerLegMat(rightUpperLegMat());}\n",
"\n",
"vec4 ragdoll(in float modelID, in float bbID, in vec3 localPos, in vec3 eye, in vec3 light)\n",
"{\n",
" ID = modelID + 123.456;\n",
" TIME = time*4.0+hash(ID*12.34)*3.0;\n",
" \n",
" int partsID = 0;\n",
" for (int i = 0; i < 13; i++) \n",
" {\n",
" float num = step(5.0, float(i))+1.0;\n",
" if (num > bbID) break;\n",
" bbID -= num ;\n",
" partsID++;\n",
" }\n",
" mat4 a,b;\n",
" if (partsID == 0) {a = rootMat(); b = waistMat(a);}\n",
" else if (partsID == 1) {a = waistMat(); b = breastMat(a);}\n",
" else if (partsID == 2) {a = breastMat(); b = neckMat(a);}\n",
" else if (partsID == 3) {a = neckMat(); b = headMat(a);}\n",
" else if (partsID == 4) {a = rootMat(); b = hipMat(a);}\n",
" else if (partsID == 5) {a = leftShoulderMat(); b = leftUpperArmMat(a);}\n",
" else if (partsID == 6) {a = rightShoulderMat(); b = rightUpperArmMat(a);}\n",
" else if (partsID == 7) {a = leftUpperArmMat(); b = leftLowerArmMat(a);}\n",
" else if (partsID == 8) {a = rightUpperArmMat(); b = rightLowerArmMat(a);}\n",
" else if (partsID == 9) {a = leftCrotchMat(); b = leftUpperLegMat(a);}\n",
" else if (partsID == 10) {a = rightCrotchMat(); b = rightUpperLegMat(a);}\n",
" else if (partsID == 11) {a = leftUpperLegMat(); b = leftLowerLegMat(a);}\n",
" else if (partsID == 12) {a = rightUpperLegMat(); b = rightLowerLegMat(a);}\n",
" float t = time + float(modelID)*0.4;\n",
" vec3 worldPos = vec3(randomWalk(t)*10.0,0).xzy;\n",
" worldPos.z += (scene(15.0,0.0)+scene(135.0,155.0))*100.0;\n",
" worldPos.y = max(5.0-pow(length(worldPos.xz)*0.3,2.0),0.0);\n",
" vec2 delta = normalize(randomWalk(t+0.001) - randomWalk(t-0.001));\n",
" mat4 worldMat = moveTo(worldPos) * rotate4Y(acos(dot(vec2(0,1),delta)) * sign(cross(vec3(delta,0),Y).z));\n",
" a = worldMat *a;\n",
" b = worldMat *b;\n",
" vec3 p1 = (a*vec4(0,0,0,1)).xyz;\n",
" vec3 p2 = (b*vec4(0,0,0,1)).xyz;\n",
" vec3 p12 = (p2-p1);\n",
" worldPos = mix(p1, p2, (bbID+0.5)/ (step(9., float(partsID))+1.));\n",
" localPos = matchTo(eye-worldPos, Z) * localPos + worldPos;\n",
" vLight = (vec4(light,1)*a).xyz;\n",
" vLen = length(p12)/2.0;\n",
" vMat = matchTo(Z,(vec4(p12,1)*a).xyz);\n",
" vPos = (vec4(localPos-mix(p1, p2, 0.5),1)*a).xyz;\n",
" vPartsID =float(partsID);\n",
" return vec4(localPos, 1.0); \n",
"}\n",
"\n",
"void main()\n",
"{\n",
" float vertexID=abs(3.0-mod(float(gl_VertexID),6.0)),polyID=floor(float(gl_VertexID)/6.0);\n",
" vec2 coord = vec2(mod(vertexID,2.0), floor(vertexID/2.0))*2.0-1.0;\n",
" vec3 eye = vec3(0,10,35),light = normalize(vec3(1)),w = normalize(eye),u=normalize(cross(Y,w)),v=normalize(cross(w,u));\n",
" mat4 pvMat = \n",
" mat4(2.5/(resolution.x/resolution.y),0,0,0,0,2.5,0,0,0,0,-99.1/98.9,-1,0,0,-19.8/98.9,1)*\n",
" transpose(mat4(u,-dot(u,eye),v,-dot(v,eye),w,-dot(w,eye),0,0,0,1));\n",
"\n",
" int groupFaceNum[4] = int[](1,8820,8820,9450);\n",
" float groupID = 0.0;\n",
" for (int i = 0; i < 4; i++) \n",
" {\n",
" if (groupFaceNum[i] > int(polyID)) break;\n",
" polyID -= float(groupFaceNum[i]);\n",
" groupID++;\n",
" }\n",
" vGroupID = groupID; \n",
"\n",
" if (groupID<0.5) \n",
" { \n",
" gl_Position = vec4(coord, 0.999, 1);\n",
" } \n",
" else if (groupID<2.5) \n",
" {\n",
" gl_Position = pvMat * \n",
" hex_star(\n",
" groupID-1.0, \n",
" floor(polyID/SLICE_NUM), \n",
" vec3(coord, 1.0 - 2.0 * fract(polyID/SLICE_NUM)),\n",
" eye, light\n",
" );\n",
" }\n",
" else \n",
" {\n",
" gl_Position = pvMat * \n",
" ragdoll(\n",
" floor(polyID/(SLICE_NUM*21.)), \n",
" mod(floor(polyID/SLICE_NUM), 21.),\n",
" vec3(coord, 1.0 - 2.0 * fract(polyID/SLICE_NUM)) * 0.8, \n",
" eye, light\n",
" );\n",
" } \n",
" vEye = eye;\n",
" vResolution = resolution;\n",
"}\n",
"\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"fsh = \"\"\"\n",
"#version 330\n",
"\n",
"in vec2 vResolution;\n",
"in float vPartsID;\n",
"in float vGroupID;\n",
"in vec3 vPos;\n",
"in vec3 vEye;\n",
"in mat3 vMat;\n",
"in vec3 vLight;\n",
"in float vLen;\n",
"out vec4 fragColor;\n",
"\n",
"#define rad37 0.64577\n",
"#define rad60 1.0472\n",
"\n",
"float smin(float a, float b, float k)\n",
"{\n",
" return -log(exp(-k*a )+exp(-k*b))/k;\n",
"}\n",
"\n",
"float smax(in float a, in float b, in float k)\n",
"{\n",
" return log(exp(a/k)+exp(b/k))*k;\n",
"}\n",
"\n",
"float sabs(in float n, in float k)\n",
"{\n",
" return n-2.0*smin(0.0,n,k); \n",
"}\n",
"\n",
"vec2 sfold(in vec2 p, in float a)\n",
"{\n",
" p.x = abs(p.x);\n",
" vec2 v = vec2(cos(a), sin(a));\n",
" for(int i = 0; i < 3; i++) \n",
" {\t\n",
" \tp -= 2.0 * smin(0.0, dot(p, v),45.) * v;\n",
" \tv = normalize(vec2(v.x - 1.0, v.y));\n",
" }\n",
" \treturn p; \n",
"}\n",
"\n",
"float deStar(in vec3 p)\n",
"{\n",
" p.xy =sfold(p.xy, rad37);\n",
" return smax(dot(vec2(sabs(p.x,25.),p.y), \n",
" normalize(vec2(1.8, 1)))-0.5,abs(p.z)-0.3,0.05)\n",
" +0.05 * smoothstep(0.45, 0.25, length(p));\n",
"}\n",
"\n",
"float deHex(in vec3 p)\n",
"{\n",
" p.xy =sfold(p.xy, rad60);\n",
" return smax(p.y-0.7,abs(p.z)-0.3,0.05)\n",
" +0.05 * smoothstep(0.35, 0.15, p.y);\n",
"}\n",
"\n",
"float deRagdoll(in vec3 p)\n",
"{\n",
" p = vMat*p;\n",
" float x=clamp(p.z,-vLen,vLen);\n",
" float t = x/vLen;\n",
" vec2 sec =\n",
" (vPartsID < 0.5)? vec2(0.02):\n",
" (vPartsID < 1.5)? vec2(0.15,0.35+0.1*t):\n",
" (vPartsID < 2.5)? vec2(0):\n",
" (vPartsID < 3.5)? vec2(0.15):\n",
" (vPartsID < 4.5)? vec2(0.15,0.2):\n",
" (vPartsID < 6.5)? vec2(0.12-t*0.02):\n",
" (vPartsID < 8.5)? vec2(0.1):\n",
" (vPartsID < 10.5)? vec2(0.12-t*0.05):\n",
" vec2(0.1);\n",
" return length(max(abs(vec2(length(p.zy-vec2(x,0)),p.x))-sec,0.0))-0.2;\n",
"}\n",
"\n",
"float map(in vec3 p)\n",
"{\n",
" return (vGroupID<1.5)? deHex(p):\n",
" (vGroupID<2.5)? deStar(p):\n",
" deRagdoll(p);\n",
"}\n",
"\n",
"vec3 calcNormal(in vec3 pos)\n",
"{\n",
" vec2 e = vec2(1.0,-1.0)*0.002;\n",
" return normalize(\n",
" e.xyy*map(pos+e.xyy)+e.yyx*map(pos+e.yyx)+ \n",
" e.yxy*map(pos+e.yxy)+e.xxx*map(pos+e.xxx)\n",
" );\n",
"}\n",
"\n",
"void main() \n",
"{\n",
" vec2 uv = gl_FragCoord.xy / vResolution;\n",
" vec3 col = \n",
" clamp(\n",
" mix(\n",
" mix(\n",
" vec3(0.5, 1, 0.5),\n",
" vec3(1,0,0),\n",
" 0.8-uv.y\n",
" ),\n",
" vec3(0.9-length(uv-0.4)),\n",
" length(uv-0.9)\n",
" )*1.3,\n",
" 0.0,1.0\n",
" );\n",
" \n",
"\tif (vGroupID>0.5) \n",
"\t{\n",
" if( map(vPos)< 0.001) \n",
" {\n",
" vec3 normal = calcNormal(vPos);\n",
" col = \n",
" mix(\n",
" col,\n",
" clamp(\n",
" ((vGroupID<1.5)?vec3(0.1,0.5,0.9):\n",
" (vGroupID<2.5)?vec3(0.1,0.9,0.5):\n",
" vec3(1,0.3,0.3)) *\n",
" clamp((dot(normal, vLight)+0.5)*0.7, 0.3, 1.0) *\n",
" max(0.0, 0.7+0.3*normal.y) +\n",
" pow(\n",
" clamp(\n",
" dot(reflect(normalize(vPos - vEye), normal), vLight),\n",
" 0.0, 1.0\n",
" ),\n",
" 50.0\n",
" ) * step(0.5,dot(normal, vLight)),\n",
" 0.0,1.0\n",
" ),\n",
" exp(-pow(length(vPos - vEye),2.0)*0.0002)\n",
" );\n",
" } \n",
" else \n",
" {\n",
" discard;\n",
" }\n",
" }\n",
" fragColor = vec4(col, 1); \n",
"}\n",
"\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"msh = \"\"\"\n",
"#version 330\n",
"\n",
"out vec2 gain; \n",
"uniform float sampleRate; \n",
"\n",
"float TIME;\n",
"\n",
"float hash(float n){return fract(sin(n) * 4379.5453);}\n",
"\n",
"float tone(float t)\n",
"{\n",
" t *= 3.;\n",
" float ti = floor(t);\n",
" float scale[7] = float[](0.0, 3.0, 5.0, 7.0, 10.0, 12.0, -10000.0);\n",
" int id = int(7.*(hash(ti*126.89 + fract(ti*0.964) + floor(ti*1723.23))));\n",
" float f = 6.2831 * fract(440.0*pow(2.0,(scale[id]-10.0)/12.0)*TIME);\n",
" return sin(f+ 2.0*sin(2.0*f + sin(2.0*f))) * exp(-3.0*fract(t));\n",
"}\n",
"\n",
"float sound(float time)\n",
"{\n",
" TIME = time;\n",
" time *= 1.5;\n",
" float a = \n",
" tone(time)+ \n",
" tone(time*0.5)+\n",
" tone(time*0.25);\n",
" return clamp(a*0.3,-1.0,1.0);\n",
"}\n",
"\n",
"void main() {\n",
" float time = float(gl_VertexID) / sampleRate;\n",
" gain = vec2(sound(time+0.01*cos(time*0.05)),sound(time+0.01*sin(time*0.05))) * \n",
" smoothstep(147.0,140.0,time); \n",
"}\n",
"\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"gccPath = r\"C:\\mingw-w64\\x86_64-6.1.0-posix-seh-rt_v5-rev0\\mingw64\\bin\"\n",
"crinklerPath = r\"C:\\Users\\haru\\Dropbox\\demoscene\\tools\"\n",
"libPath = r\"C:\\Users\\haru\\Dropbox\\demoscene\\lib\""
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"playTime =148\n",
"polySize = 1 + 30*6*7*7*2 + 30*21*15"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"var = {\n",
" \"U_RESOLUTION\" : \"resolution\",\n",
" \"U_TIME\" : \"time\",\n",
" \"U_SAMPLERATE\" : \"sampleRate\",\n",
" \"O_GAIN\" : \"gain\"\n",
"}\n",
"\n",
"gfx_init = \"\"\"\n",
" glEnable(GL_DEPTH_TEST);\n",
" \n",
" GLint program = glCreateProgram();\n",
" compile(program, vsh, GL_VERTEX_SHADER);\n",
" compile(program, fsh, GL_FRAGMENT_SHADER);\n",
" glLinkProgram(program);\n",
" glUseProgram(program);\n",
" //glUniform1f(glGetUniformLocation(program, U_ASPECT), (float)XRES/(float)YRES);\n",
" glUniform2f(glGetUniformLocation(program, U_RESOLUTION), (float)XRES, (float)YRES);\n",
"\"\"\"\n",
"\n",
"gfx_do = \"\"\"\n",
" glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);\n",
" glUniform1f(glGetUniformLocation(program, U_TIME), time);\n",
" glDrawArrays(GL_TRIANGLES, 0, 6*{0});\n",
"\"\"\".format(polySize)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import re\n",
"import string\n",
"import subprocess\n",
"import os\n",
"\n",
"def fRead(fn):\n",
" f = open(fn, 'r')\n",
" str = f.read()\n",
" f.close()\n",
" return str\n",
" \n",
"def fWrite(fn, src):\n",
" f = open(fn, 'w')\n",
" f.write(src)\n",
" f.close()\n",
" \n",
"def batch(cmd):\n",
" fn = \"command.bat\"\n",
" fWrite(fn, cmd)\n",
" subprocess.call(\"start \" + fn, shell=True) \n",
"\n",
"def dic2code(d):\n",
" s = \"\"\n",
" for k, v in d.items():\n",
" if isinstance(v, str):\n",
" s = s + '#define {0} \"{1}\" \\n'.format(k, v)\n",
" else: # int, float\n",
" s = s + '#define {0} {1} \\n'.format(k, v)\n",
" return s\n",
" \n",
"def glRename(src):\n",
" for m in re.finditer(\"(gl\\w+)\\(\" , src):\n",
" s = m.group(1)\n",
" f = False\n",
" if re.search(\"Program\", s) != None: f = True\n",
" if re.search(\"Shader\", s) != None: f = True\n",
" if re.search(\"TransformFeedback\", s) != None: f = True\n",
" if re.search(\"Buffer\", s) != None: f = True\n",
" if re.search(\"Uniform\", s) != None: f = True\n",
" if s == \"glReadBuffer\": f = False\n",
" if f: \n",
" src = src.replace(s + \"(\", '((PFN' + s.upper() + 'PROC)wglGetProcAddress(\"' + s + '\"))(')\n",
" return src"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def makeCSrc4debug(gfx_init, gfx_do, vsh, fsh, msh, var):\n",
" conf = {\n",
" \"SND_DURATION\" : playTime,\n",
" \"SAMPLE_RATE\" : 48000,\n",
" \"SND_NUMCHANNELS\" : 2,\n",
" \"XPOS\" : 500,\n",
" \"YPOS\" : 200,\n",
" \"XRES\" : 640,\n",
" \"YRES\" : 480,\n",
" }\n",
" src = \"\"\"\n",
"#include <windows.h>\n",
"#include <GL/gl.h>\n",
"#include <GL/glext.h>\n",
"#include <mmreg.h>\n",
"#include <stdio.h> \n",
"\n",
"\"\"\" + \"\"\"\n",
"static const char *vsh = \"{0}\";\n",
"static const char *fsh = \"{1}\";\n",
"static const char *msh = \"{2}\";\n",
"\n",
"\"\"\".format(\n",
" vsh.replace(\"\\n\",r\"\\n\"),\n",
" fsh.replace(\"\\n\",r\"\\n\"),\n",
" msh.replace(\"\\n\",r\"\\n\")\n",
" ) + \"\"\"\n",
" \n",
"\"\"\" + dic2code(var) + dic2code(conf) + \"\"\"\n",
"\n",
"#define SND_NUMSAMPLES (SND_DURATION*SAMPLE_RATE)\n",
"#define SND_NUMSAMPLESC (SND_NUMSAMPLES*SND_NUMCHANNELS)\n",
"\n",
"void checkShader(GLuint shader){\n",
" GLint compile_status = 0;\n",
" glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);\n",
" if(compile_status != GL_TRUE){\n",
" GLint info_length;\n",
" GLsizei buffer_size;\n",
" glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_length);\n",
" GLchar message[info_length];\n",
" glGetShaderInfoLog(shader, info_length, &buffer_size, message);\n",
" printf( message);\n",
" }\n",
"}\n",
"\n",
"void compile(GLuint program, const char *source, GLenum type){\n",
" GLuint shader = glCreateShader(type);\n",
" glShaderSource(shader, 1, &source, 0);\n",
" glCompileShader(shader);\n",
" checkShader(shader);\n",
" glAttachShader(program, shader);\n",
"}\n",
"\n",
"float samples[SND_NUMSAMPLESC];\n",
"\n",
"int main(){\n",
" HWND hWnd = CreateWindow((LPCSTR)0xC018,\"demoscene\",WS_OVERLAPPEDWINDOW|WS_VISIBLE,XPOS,YPOS,XRES,YRES,0,0,0,0);\n",
" HDC hDC = GetDC(hWnd);\n",
" PIXELFORMATDESCRIPTOR pfd={0,1,PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER,32,0,0,0,0,0,0,0,0,0,0,0,0,0,32,0,0,0,0,0,0,0};\n",
" SetPixelFormat(hDC, ChoosePixelFormat(hDC, &pfd) , &pfd);\n",
" HGLRC hGLrc = wglCreateContext(hDC);\n",
" wglMakeCurrent(hDC, hGLrc);\n",
" \n",
" GLuint programMzk = glCreateProgram();\n",
" compile(programMzk, msh, GL_VERTEX_SHADER);\n",
" const GLchar* outs[] = {O_GAIN};\n",
" glTransformFeedbackVaryings(programMzk, 1, outs, GL_INTERLEAVED_ATTRIBS);\n",
" glLinkProgram(programMzk);\n",
" glUseProgram(programMzk);\n",
" GLuint tmp;\n",
" glGenBuffers(1, &tmp);\n",
" glBindBuffer(GL_ARRAY_BUFFER, tmp);\n",
" glBufferData(GL_ARRAY_BUFFER, SND_NUMSAMPLESC*sizeof(float), 0, GL_STATIC_READ);\n",
" glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, tmp);\n",
" tmp = glGetUniformLocation(programMzk, U_SAMPLERATE);\n",
" glUniform1f(tmp, (float)SAMPLE_RATE);\n",
" glEnable(GL_RASTERIZER_DISCARD);\n",
" glBeginTransformFeedback(GL_POINTS);\n",
" glDrawArrays(GL_POINTS, 0, SND_NUMSAMPLES);\n",
" glEndTransformFeedback();\n",
" glDisable(GL_RASTERIZER_DISCARD);\n",
" glGetBufferSubData(GL_TRANSFORM_FEEDBACK_BUFFER, 0, sizeof(samples), samples);\n",
"\n",
"\"\"\" + gfx_init + \"\"\"\n",
"\n",
" WAVEFORMATEX wave_format = {\n",
" WAVE_FORMAT_IEEE_FLOAT,\n",
" SND_NUMCHANNELS,\n",
" SAMPLE_RATE,\n",
" SAMPLE_RATE*sizeof(float)*SND_NUMCHANNELS,\n",
" sizeof(float)*SND_NUMCHANNELS,\n",
" sizeof(float)*8,\n",
" 0\n",
" };\n",
" WAVEHDR wave_hdr = {(LPSTR)samples, sizeof(samples)};\n",
" HWAVEOUT hWaveOut;\n",
" waveOutOpen(&hWaveOut, WAVE_MAPPER, &wave_format, (DWORD_PTR)hWnd, 0, CALLBACK_WINDOW);\n",
" waveOutPrepareHeader(hWaveOut, &wave_hdr, sizeof(wave_hdr));\n",
" waveOutWrite(hWaveOut, &wave_hdr, sizeof(wave_hdr));\n",
" static MMTIME mmt = { TIME_SAMPLES }; \n",
" MSG msg;\n",
" int done=0;\n",
" int fps = 0;\n",
" int cnt = 0;\n",
" float t0 = 0.0;\n",
" while (!done) {\n",
" while (PeekMessage(&msg, 0, 0, 0, TRUE)) {\n",
" if (msg.message == WM_NCLBUTTONDOWN && msg.wParam == HTCLOSE) done=1;\n",
" DispatchMessage(&msg);\n",
" }\n",
" if (GetAsyncKeyState(VK_ESCAPE)) done = 1;\n",
" if (mmt.u.sample == SND_NUMSAMPLES) done = 1;\n",
" waveOutGetPosition(hWaveOut, &mmt, sizeof(mmt));\n",
" float time = (float)mmt.u.sample / (float)SAMPLE_RATE;\n",
" \n",
"\"\"\" + gfx_do + \"\"\"\n",
"\n",
" SwapBuffers(hDC);\n",
" if (time - t0 > 1.0) {\n",
" fps = cnt;\n",
" cnt = -1;\n",
" t0 = time;\n",
" }\n",
" cnt++;\n",
" printf(\"\\\\r FPS : %d Time : %5.1f\", fps, time);\n",
" }\n",
" \n",
" waveOutReset(hWaveOut);\n",
" waveOutUnprepareHeader(hWaveOut,&wave_hdr,sizeof(WAVEHDR));\n",
" waveOutClose(hWaveOut);\n",
"\n",
" wglMakeCurrent(NULL, NULL);\n",
" wglDeleteContext(hGLrc);\n",
" ReleaseDC(hWnd, hDC);\n",
" PostQuitMessage(0);\n",
" ExitProcess(0);\n",
" return 0;\n",
"}\n",
"\"\"\"\n",
" return glRename(src)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Debug and Compile and Run\n",
"\n",
"src = makeCSrc4debug(gfx_init, gfx_do, vsh, fsh, msh, var)\n",
"fWrite(\"demo.c\", src)\n",
"batch(r\"\"\"\n",
" echo off\n",
" set PATH={0};%PATH%\n",
" gcc demo.c -O3 -Wall -lopengl32 -lgdi32 -lwinmm -o demo.exe\n",
" demo.exe\n",
"\"\"\".format(gccPath))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## --------------------------------------------------------------------------------"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import re\n",
"import string\n",
"\n",
"def glslTrim(src):\n",
" src = re.compile(r'/\\*.*?\\*/', re.DOTALL).sub(\"\", src)\n",
" src = re.sub(r\"//.*\", \"\", src)\n",
" src = re.sub(r\"\\t\", \" \", src)\n",
" src = re.sub(r\" +\", \" \", src)\n",
" src = re.sub(r\" *\\n *\", \"\\n\", src)\n",
" src = re.sub(r\"\\n+\", \"\\n\", src) \n",
" src = re.sub(r\"^\\n\", \"\", src)\n",
" L = src.split(\"\\n\")\n",
" for i in range(len(L)):\n",
" s = L[i]\n",
" if re.search(\"#\", s) != None:\n",
" L[i] = \"\\n\" + L[i] + \"\\n\"\n",
" else:\n",
" s = re.sub(r\" *\\+ *\" ,\"+\", s)\n",
" s = re.sub(r\" *\\- *\" ,\"-\", s)\n",
" s = re.sub(r\" *\\* *\" ,\"*\", s)\n",
" s = re.sub(r\" */ *\" ,\"/\", s)\n",
" s = re.sub(r\" *= *\" ,\"=\", s)\n",
" s = re.sub(r\" *< *\" ,\"<\", s)\n",
" s = re.sub(r\" *> *\" ,\">\", s)\n",
" s = re.sub(r\" *& *\" ,\"&\", s)\n",
" s = re.sub(r\" *\\| *\" ,\"|\", s)\n",
" s = re.sub(r\" *\\( *\" ,\"(\", s)\n",
" s = re.sub(r\" *\\) *\" ,\")\", s)\n",
" s = re.sub(r\" *\\[ *\" ,\"[\", s)\n",
" s = re.sub(r\" *\\] *\" ,\"]\", s)\n",
" s = re.sub(r\" *{ *\" ,\"{\", s)\n",
" s = re.sub(r\" *} *\" ,\"}\", s)\n",
" s = re.sub(r\" *; *\" ,\";\", s)\n",
" s = re.sub(r\" *, *\" ,\",\", s)\n",
" L[i] = s\n",
" src = \"\".join(L)\n",
" for i in range(2):\n",
" for m in re.finditer(r\"(\\W)(\\d+\\.)0(\\W)\" , src):\n",
" src = src.replace(m.group(0), m.group(1)+m.group(2)+m.group(3))\n",
" for m in re.finditer(r\"(\\W)0(\\.\\d+)(\\W)\" , src):\n",
" src = src.replace(m.group(0), m.group(1)+m.group(2)+m.group(3))\n",
" src = src.replace(\"(in \", \"(\")\n",
" src = src.replace(\",in \", \",\")\n",
" src = src.replace(\"(void)\", \"()\")\n",
" src = re.sub(r\"\\n+\", \"\\n\", src)\n",
" src = re.sub(r\"^\\n\", \"\", src)\n",
" return src\n",
"\n",
"def usedChar(src):\n",
" L = []\n",
" for m in re.finditer(r\"(\\W)([a-zA-z]\\w*)\", src):\n",
" if m.group(1) != \".\" and len(m.group(2)) < 3:\n",
" L.append(m.group(2))\n",
" return list(set(L))\n",
"\n",
"def virginChar(L):\n",
" L1 = []\n",
" L2 = []\n",
" for s in L:\n",
" if len(s) == 1:\n",
" L1.append(s)\n",
" else:\n",
" L2.append(s)\n",
" c = string.ascii_lowercase + string.ascii_uppercase\n",
" nameL = [c[i] for i in range(len(c))]\n",
" for s in L1:\n",
" while s in nameL: nameL.remove(s)\n",
" if len(nameL) > 0:\n",
" return nameL[0]\n",
" else:\n",
" c = string.ascii_uppercase\n",
" c = [c[i] for i in range(len(c))]\n",
" s = c[0] + c[0]\n",
" n = 0\n",
" while s in L2:\n",
" n += 1\n",
" s = c[n // len(c)] + c[n % len(c)]\n",
" return s\n",
"\n",
"def replaceChar(fr0m, t0, src):\n",
" for _ in range(2):\n",
" for m in re.finditer(r\"(\\W)\" + fr0m + \"(\\W)\", src):\n",
" if m.group(1) != \".\":\n",
" src = src.replace(m.group(1) + fr0m + m.group(2), m.group(1) + t0 + m.group(2))\n",
" return src\n",
" \n",
"def glslMinify(src, oneCharD={}):\n",
" src = glslTrim(src)\n",
" # 関数、構造体の宣言部分の取得\n",
" headL = []\n",
" for m in re.finditer(r\"(\\w+ \\w+.*?){\", re.sub(r\"[;}]\", \"\\n\", src)):\n",
" if len(list(re.finditer(\"if\" , m.group(1)))) == 0:\n",
" headL.append(m.group(1))\n",
" \n",
" # 関数、構造体のスクリプト部分の取得する為の正規表現を作成\n",
" regL = []\n",
" code = {\"{\" : 1, \"}\" : -1}\n",
" pool = \"\"\n",
" lv = 0\n",
" for m in re.finditer(r\"[{}]\" , src):\n",
" lv += code[m.group()]\n",
" pool = pool + m.group()\n",
" if lv == 0:\n",
" regL.append(\".*?\".join([pool[x] for x in range(len(pool))]))\n",
" pool = \"\"\n",
" \n",
" # 関数、構造体ブロックのリストを作成\n",
" funcBlockL = []\n",
" structBlockL = []\n",
" for (head, reg) in zip(headL, regL):\n",
" if head[0:6] == \"struct\":\n",
" structBlockL.append(re.search(head.replace(\"(\", \"\\(\").replace(\")\", \"\\)\") + reg, src, re.DOTALL).group())\n",
" else:\n",
" funcBlockL.append(re.search(head.replace(\"(\", \"\\(\").replace(\")\", \"\\)\") + reg, src, re.DOTALL).group())\n",
" \n",
" # 関数、構造体の名前を取得\n",
" funcL = []\n",
" structL = []\n",
" for s in headL:\n",
" for m in re.finditer(r\"(\\w+) (\\w+).*\", s):\n",
" if m.group(1) == \"struct\":\n",
" structL.append(m.group(2))\n",
" else:\n",
" funcL.append(m.group(2))\n",
" funcL = list(set(funcL))\n",
" funcL.remove(\"main\")\n",
" \n",
" # グローバル変数の名前を取得\n",
" globalVarL = []\n",
" defineL = []\n",
" uniformL = []\n",
" outL = []\n",
" temp = src\n",
" for s in funcBlockL:\n",
" temp = temp.replace(s, \"\")\n",
" for s in structBlockL:\n",
" temp = temp.replace(s, \"\")\n",
" globalLineL = re.split('[\\n;]', temp)\n",
" while \"\" in globalLineL: globalLineL.remove(\"\")\n",
" for s in globalLineL:\n",
" if s.startswith(\"#version\"):\n",
" while s in globalLineL: globalLineL.remove(s)\n",
" for s in globalLineL:\n",
" if s.startswith(\"#define\"):\n",
" for m in re.finditer(r\"#define (\\w+) (.*)\", s):\n",
" defineL.append((s, m.group(1), m.group(2)))\n",
" else:\n",
" s = re.sub(r\".*?in \", \"in \", s)\n",
" if s.startswith(\"uniform\"):\n",
" s = re.sub(r\"^\\w+ \\w+ \", \"\", s)\n",
" uniformL = uniformL + re.split(r\",\", s)\n",
" elif s.startswith(\"out\"):\n",
" s = re.sub(r\"^\\w+ \\w+ \", \"\", s)\n",
" outL = outL + re.split(r\",\", s)\n",
" elif s.startswith(\"in\"):\n",
" s = re.sub(r\"^\\w+ \\w+ \", \"\", s)\n",
" else:\n",
" s = re.sub(r\"^\\w+ \", \"\", s)\n",
" globalVarL = globalVarL + re.split(r\",\", s)\n",
" \n",
" # 関数、構造体、グローバル変数をキーにした1文字Dicの作成\n",
" oneCharL = list(oneCharD.values())\n",
" for x,y,z in defineL:\n",
" if len(y) == 1:\n",
" oneCharL.append(y)\n",
" for s in globalVarL + structL + funcL:\n",
" if len(s) == 1:\n",
" oneCharL.append(s)\n",
" else:\n",
" if s not in oneCharD:\n",
" char = virginChar(oneCharL)\n",
" oneCharD[s] = char\n",
" oneCharL.append(char)\n",
" uniformD = {}\n",
" outD = {}\n",
" for s in uniformL:\n",
" uniformD[s] = oneCharD[s]\n",
" for s in outL:\n",
" outD[s] = oneCharD[s]\n",
" \n",
" # ブロック毎に置換 \n",
" for s in funcBlockL:\n",
" \n",
" # ローカル変数取得 \n",
" temp = s\n",
" varL = []\n",
" f = s.split(\"{\", 1)\n",
" f[0] = re.sub(r\"^\\w+ \\w+\", \"\", f[0])\n",
" f[0] = f[0].replace(\",\", \",,\")\n",
" for m in re.finditer(\"\\w+ (\\w+)\", f[0]):\n",
" varL.append(m.group(1))\n",
" f[1] = re.sub(r\"return.*?;\", \"\", f[1])\n",
" f[1] = re.sub(r\"const \", \"\", f[1])\n",
" f[1] = re.sub(r\"else if\", \"\", f[1])\n",
" for a in re.split(r'[{};]', f[1]):\n",
" for m in re.finditer(r\"^\\w+ (\\w+)\" , a): \n",
" for i in range(5):\n",
" a = re.sub(r\"\\([^\\(\\)]+\\)\", \"\", a)\n",
" for i in re.sub(r\"^\\w+ \", \"\", a).split(\",\"):\n",
" for m in re.finditer(r\"^\\w+\" , i):\n",
" varL.append(m.group())\n",
" for m in re.finditer(r\"for\\(\\w+ (\\w+)\" , a):\n",
" varL.append(m.group(1))\n",
" varL = list(set(varL))\n",
" \n",
" # ローカル変数の一時的置換\n",
" for i in range(len(varL)):\n",
" temp = replaceChar(varL[i], \"dummy%03d\" % i, temp)\n",
"\n",
" # 関数、構造体、グローバル変数の置換\n",
" for k,v in oneCharD.items():\n",
" temp = replaceChar(k, v, temp)\n",
"\n",
" # 1文字 関数、構造体、グローバル変数、defineの取得\n",
" oneCharL = usedChar(temp)\n",
" for x,y,z in defineL:\n",
" if len(y) == 1:\n",
" oneCharL.append(y)\n",
" \n",
" # ローカル変数の置換\n",
" sz = -1\n",
" for m in re.finditer(r\"dummy(\\d+)\", temp): \n",
" sz = max(int(m.group(1)), sz)\n",
" for i in range(sz + 1):\n",
" char = virginChar(oneCharL)\n",
" temp = replaceChar(\"dummy%03d\" % i, char, temp)\n",
" oneCharL.append(char)\n",
" \n",
" # ソースの置換\n",
" src = src.replace(s, temp) \n",
" for k,v in oneCharD.items():\n",
" src = replaceChar(k, v, src)\n",
" for s,fr0m,t0 in defineL:\n",
" src = src.replace(s, \"\")\n",
" src = replaceChar(fr0m, t0, src)\n",
" src = re.sub(r\"\\n\\n+\", \"\", src)\n",
" return (src, uniformD, outD) "
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [],
"source": [
"vsh, uniformD, outD = glslMinify(vsh,{})\n",
"var[\"U_RESOLUTION\"] = uniformD[var[\"U_RESOLUTION\"]]\n",
"var[\"U_TIME\"] = uniformD[var[\"U_TIME\"]]\n",
"fsh, dummy0, dummy1 = glslMinify(fsh, outD)\n",
"\n",
"msh = glslTrim(msh)\n",
"\n",
"msh, uniformD, outD = glslMinify(msh,{})\n",
"var[\"U_SAMPLERATE\"] = uniformD[var[\"U_SAMPLERATE\"]]\n",
"var[\"O_GAIN\"] = outD[var[\"O_GAIN\"]]"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Debug and Compile and Run\n",
"\n",
"src = makeCSrc4debug(gfx_init, gfx_do, vsh, fsh, msh, var)\n",
"fWrite(\"demo.c\", src)\n",
"batch(r\"\"\"\n",
" echo off\n",
" set PATH={0};%PATH%\n",
" gcc demo.c -O3 -Wall -lopengl32 -lgdi32 -lwinmm -o demo.exe\n",
" demo.exe\n",
" rem exit\n",
"\"\"\".format(gccPath))"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def makeCSrc4release(gfx_init, gfx_do, vsh, fsh, msh, var):\n",
" conf = {\n",
" \"SND_DURATION\" : playTime,\n",
" \"SAMPLE_RATE\" : 48000,\n",
" \"SND_NUMCHANNELS\" : 2,\n",
" \"XRES\" : 1920,\n",
" \"YRES\" : 1080,\n",
" }\n",
" def inlineCompileFunc(src):\n",
" for m in re.finditer(\"compile\\((\\w+), (\\w+), (\\w+)\\);\" , src):\n",
" s = m.group()\n",
" dst = \"\"\"\n",
" tmp = glCreateShader({2});\n",
" glShaderSource(tmp, 1, &{1}, 0);\n",
" glCompileShader(tmp);\n",
" glAttachShader({0}, tmp); \n",
"\"\"\".format(m.group(1),m.group(2),m.group(3))\n",
" src = src.replace(s, dst)\n",
" return src\n",
"\n",
" src =\"\"\"\n",
"#include <windows.h>\n",
"#include <GL/gl.h>\n",
"#include <GL/glext.h>\n",
"#include <mmreg.h>\n",
"\n",
"\"\"\" + \"\"\"\n",
"static const char *vsh = \"{0}\";\n",
"static const char *fsh = \"{1}\";\n",
"static const char *msh = \"{2}\";\n",
"\n",
"\"\"\".format(\n",
" vsh.replace(\"\\n\",r\"\\n\"),\n",
" fsh.replace(\"\\n\",r\"\\n\"),\n",
" msh.replace(\"\\n\",r\"\\n\")\n",
" ) + \"\"\"\n",
" \n",
"\"\"\" + dic2code(var) + dic2code(conf) + \"\"\"\n",
"\n",
"#define SND_NUMSAMPLES (SND_DURATION*SAMPLE_RATE)\n",
"#define SND_NUMSAMPLESC (SND_NUMSAMPLES*SND_NUMCHANNELS)\n",
"\n",
"float samples[SND_NUMSAMPLESC];\n",
"\n",
"void entrypoint(){\n",
" DEVMODE dmScreenSettings={{0},0,0,sizeof(DEVMODE),0,DM_PELSWIDTH|DM_PELSHEIGHT,{0},0,0,0,0,0,{0},0,0,XRES,YRES,{0},0};\n",
" ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN);\n",
" HWND hWnd = CreateWindow((LPCSTR)0xC018,0,WS_POPUP|WS_VISIBLE|WS_MAXIMIZE,0,0,0,0,0,0,0,0);\n",
" ShowCursor(0);\n",
" HDC hDC = GetDC(hWnd);\n",
" PIXELFORMATDESCRIPTOR pfd={0,1,PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER,32,0,0,0,0,0,0,0,0,0,0,0,0,0,32,0,0,0,0,0,0,0};\n",
" SetPixelFormat(hDC, ChoosePixelFormat(hDC, &pfd) , &pfd);\n",
" HGLRC hGLrc = wglCreateContext(hDC);\n",
" wglMakeCurrent(hDC, hGLrc);\n",
" \n",
" GLuint tmp;\n",
" GLuint programMzk = glCreateProgram();\n",
" compile(programMzk, msh, GL_VERTEX_SHADER);\n",
" const GLchar* outs[] = {O_GAIN};\n",
" glTransformFeedbackVaryings(programMzk, 1, outs, GL_INTERLEAVED_ATTRIBS);\n",
" glLinkProgram(programMzk);\n",
" glUseProgram(programMzk);\n",
" glGenBuffers(1, &tmp);\n",
" glBindBuffer(GL_ARRAY_BUFFER, tmp);\n",
" glBufferData(GL_ARRAY_BUFFER, SND_NUMSAMPLESC*sizeof(float), 0, GL_STATIC_READ);\n",
" glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, tmp);\n",
" glUniform1f(glGetUniformLocation(programMzk, U_SAMPLERATE), (float)SAMPLE_RATE);\n",
" glEnable(GL_RASTERIZER_DISCARD);\n",
" glBeginTransformFeedback(GL_POINTS);\n",
" glDrawArrays(GL_POINTS, 0, SND_NUMSAMPLES);\n",
" glEndTransformFeedback();\n",
" glDisable(GL_RASTERIZER_DISCARD);\n",
" glGetBufferSubData(GL_TRANSFORM_FEEDBACK_BUFFER, 0, sizeof(samples), samples);\n",
" \n",
"\"\"\" + gfx_init + \"\"\"\n",
"\n",
" WAVEFORMATEX wave_format = {\n",
" WAVE_FORMAT_IEEE_FLOAT,\n",
" SND_NUMCHANNELS,\n",
" SAMPLE_RATE,\n",
" SAMPLE_RATE*sizeof(float)*SND_NUMCHANNELS,\n",
" sizeof(float)*SND_NUMCHANNELS,\n",
" sizeof(float)*8,\n",
" 0\n",
" };\n",
" WAVEHDR wave_hdr = {(LPSTR)samples, sizeof(samples)};\n",
" HWAVEOUT hWaveOut;\n",
" waveOutOpen(&hWaveOut, WAVE_MAPPER, &wave_format, (DWORD_PTR)hWnd, 0, CALLBACK_WINDOW);\n",
" waveOutPrepareHeader(hWaveOut, &wave_hdr, sizeof(wave_hdr));\n",
" waveOutWrite(hWaveOut, &wave_hdr, sizeof(wave_hdr));\n",
" MMTIME mmt = { TIME_SAMPLES };\n",
" MSG msg;\n",
" do {\n",
" PeekMessage(&msg, 0, 0, 0, TRUE);\n",
" waveOutGetPosition(hWaveOut, &mmt, sizeof(mmt));\n",
" float time = (float)mmt.u.sample / (float)SAMPLE_RATE;\n",
"\"\"\" + gfx_do + \"\"\"\n",
" SwapBuffers(hDC);\n",
" } while (!GetAsyncKeyState(VK_ESCAPE) && (mmt.u.sample < SND_NUMSAMPLES));\n",
"\n",
" ExitProcess(0);\n",
"} \n",
"\"\"\"\n",
" src = inlineCompileFunc(src)\n",
" return glRename(src)\n"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"src = makeCSrc4release(gfx_init, gfx_do, vsh, fsh, msh, var)\n",
"fWrite(\"demo.c\", src)\n",
"cmd = \"echo off\\n\"\n",
"cmd = cmd + \"set PATH={0};{1};%PATH%\\n\".format(gccPath, crinklerPath)\n",
"cmd = cmd + \"gcc -m32 -c demo.c -o demo.o\\n\"\n",
"cmd = cmd + \"crinkler /OUT:demo.exe /LIBPATH:{0} \".format(libPath)\n",
"cmd = cmd +\"/COMPMODE:FAST /ENTRY:entrypoint /RANGE:opengl32 /SUBSYSTEM:WINDOWS /ORDERTRIES:1000 \"\n",
"#cmd = cmd +\"/COMPMODE:SLOW /ENTRY:entrypoint /RANGE:opengl32 /SUBSYSTEM:WINDOWS /ORDERTRIES:1000 \"\n",
"cmd = cmd +\"/PRINT:IMPORTS /PRINT:LABELS kernel32.lib user32.lib gdi32.lib opengl32.lib winmm.lib demo.o \\n\"\n",
"cmd = cmd + \"rem exit\\n\"\n",
"batch(cmd)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6219\n",
"1625\n",
"551\n"
]
}
],
"source": [
"print(len(vsh))\n",
"print(len(fsh))\n",
"print(len(msh))"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
" batch(r\"\"\"\n",
" demo.exe\n",
" exit\n",
" \"\"\")"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"File Size : 3.8k 3882byte\n"
]
}
],
"source": [
"s = os.path.getsize(\"demo.exe\")\n",
"print(\"File Size : {0:.1f}k {1}byte\".format(s/1024, s))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"実行ファイルサイズが4096バイトを超えないこと"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment