Advertisement
Guest User

Untitled

a guest
May 2nd, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.15 KB | None | 0 0
  1.  
  2. -- Docs:
  3. -- http://www.opengl.org/sdk/docs/man4/xhtml/
  4. -- http://christopheremoore.net/opengl-lua-shell/
  5. -- http://christopheremoore.net/opengl-lua-shell-reference/
  6.  
  7. local Font,vsx,vsy,Circuits,u,v,DisplayList,tick,LogoDL,OldMouse,Lifetime
  8.  
  9. local Kernel_Panic_Logo={x=225,y=88,c={128,128,255},{
  10.     x=0,y=50,
  11.     --[[K]]{{1,1,10,1,10,14,23,1,35,1,19,17,32,30,26,36,10,20,10,37,1,37},c={0,255,255}},
  12.     --[[E]]{{40,1,71,1,64,8,48,8,48,15,63,15,66,18,62,22,48,22,48,29,62,29,70,37,40,37}},
  13.     --[[R]]{{77,1,84,1,84,16,88,16,103,1,111,1,96,16,107,16,107,30,100,37,77,37},{84,22,100,22,100,26,95,31,84,31}},
  14.     --[[N]]{{116,1,125,1,125,30,130,30,138,22,138,1,147,1,147,27,137,37,116,37}},
  15.     --[[E]]{{156,1,187,1,179,9,163,9,163,16,175,16,175,22,163,22,163,29,179,29,187,37,156,37}},
  16.     --[[L]]{{193,1,224,1,217,8,202,8,202,37,193,28}},
  17.     },{
  18.     x=30,y=0,
  19.     --[[P]]{{1,1,8,1,8,16,20,16,30,26,19,37,1,37},{8,23,17,23,17,30,8,30},c={0,255,255}},
  20.     --[[A]]{{34,1,40,1,40,16,61,16,61,1,67,1,67,24,54,37,46,37,34,25},{41,22,60,22,52,30,49,30}},
  21.     --[[N]]{{75,1,83,1,83,29,93,29,98,24,98,1,106,1,106,28,97,37,75,37}},
  22.     --[[I]]{{112,1,120,9,120,29,112,37}},
  23.     --[[C]]{{126,1,155,1,162,8,134,8,134,30,159,30,152,37,126,37}},
  24. }}
  25.  
  26. local function CreateList(f,...)
  27.     local name=gl.glGenList()
  28.     assert(type(name)=="number","name is a "..type(name))
  29.     gl.glNewList(name,gl.GL_COMPILE)
  30.     f(...)
  31.     gl.glEndList()
  32.     return name
  33. end
  34.  
  35. local function BeginEnd(m,f,...)
  36.     gl.glBegin(m)
  37.     f(...)
  38.     gl.glEnd()
  39. end
  40.  
  41. local function Scribble(sentence,x,y,s)
  42.     local s=math.min((s or 0.8*sx)/(sentence.x or 1),(s or 0.8*sy)/(sentence.y or 1))
  43.     local x=(x or vsx/2)-s*(sentence.x or 0)/2
  44.     local y=(y or vsy/2)-s*(sentence.y or 0)/2
  45.     for _,word in ipairs(sentence) do
  46.         local x,y=x+s*(word.x or 0),y+s*(word.y or 0)
  47.         for _,letter in ipairs(word) do
  48.             local c=letter.c or word.c or sentence.c
  49.             if c then
  50.                 local c=function(k)
  51.                     local c=(c[k] or 1)
  52.                     return math.abs(c)<=1 and c or c/255
  53.                 end
  54.                 gl.glColor(0.03*c(1),0.03*c(2),0.03*c(3),1)
  55.             else
  56.                 gl.glColor(0,0.03,0.03,1)
  57.             end
  58.             for _,loop in ipairs(letter) do
  59.                 BeginEnd(gl.GL_LINE_LOOP,function()
  60.                     for p=1,#loop-1,2 do
  61.                         gl.glVertex(x+s*loop[p],y+s*loop[p+1])
  62.                     end
  63.                 end)
  64.             end
  65.         end
  66.     end
  67. end
  68.  
  69. local function ScribbleFancy(sentence,x,y,s)
  70.  
  71.     -- Save Logo
  72.     if not LogoDL then
  73.         LogoDL=CreateList(Scribble,sentence,x,y,s)
  74.     end
  75.  
  76.     -- Draw the wobbling logo
  77.     --gl.glBlendFunc("add")
  78.     gl.glEnable(gl.GL_BLEND)
  79.     gl.glBlendFunc(gl.GL_ONE,gl.GL_ONE)
  80.     gl.glLineWidth(1)
  81.     gl.glColor(0,0.03,0.03,1)-- Faint Cyan
  82.     gl.glPushMatrix()
  83.     local cos,sin=math.cos,math.sin
  84.     local theta=((os.clock()/5)%1)*2*math.pi-- Where 5 is the period in second
  85.     for alpha=0,6.28,2*math.pi/64 do
  86.         local a=6*cos(alpha+theta);
  87.         local b=1*sin(alpha+theta);
  88.         local tx=a*cos(theta)-b*sin(theta);
  89.         local ty=a*sin(theta)+b*cos(theta);
  90.         gl.glTranslate(tx,ty,0)
  91.         gl.glCallList(LogoDL)
  92.         gl.glTranslate(-tx,-ty,0)
  93.     end
  94.     gl.glPopMatrix()
  95.     gl.glLineWidth(1)
  96.  
  97.  
  98.     -- Draw the logo tail
  99.     local mx,my=glush.mouse()
  100.     mx,my=vsx*mx,vsy*my
  101.     if OldMouse then
  102.         if mx==OldMouse.omx and my==OldMouse.omy then
  103.             OldMouse.t=1-math.min(10,os.clock()-OldMouse.omc)/10
  104.         else
  105.             OldMouse.t=1
  106.             OldMouse.omx=mx
  107.             OldMouse.omy=my
  108.             OldMouse.omc=os.clock()
  109.         end
  110.     else
  111.         OldMouse={omx=mx,omy=my,omc=os.clock()-60,t=0}
  112.     end
  113.     --gl.glBlendFunc("add")
  114.     gl.glEnable(gl.GL_BLEND)
  115.     gl.glBlendFunc(gl.GL_ONE,gl.GL_ONE)
  116.     local s=math.min((s or 0.8*sx)/(sentence.x or 1),(s or 0.8*sy)/(sentence.y or 1))
  117.     local x=(x or vsx/2)-s*(sentence.x or 0)/2
  118.     local y=(y or vsy/2)-s*(sentence.y or 0)/2
  119.     local rx=x+s*sentence.x/2*(1+cos(((os.clock()/13)%1)*2*math.pi))-- Where 13 is the period in second
  120.     local ry=y+s*sentence.y/2*(1+sin(((os.clock()/17)%1)*2*math.pi))-- Where 17 is the period in second
  121.     for _,word in ipairs(sentence) do
  122.         local x,y=x+s*(word.x or 0),y+s*(word.y or 0)
  123.         for _,letter in ipairs(word) do
  124.             for _,loop in ipairs(letter) do
  125.                 BeginEnd(gl.GL_QUAD_STRIP,function()
  126.                     for p=0,#loop,2 do
  127.                         local x,y=x+s*loop[1+p%#loop],y+s*loop[1+(p+1)%#loop]
  128.                         local c=letter.c or word.c or sentence.c
  129.                         if c then
  130.                             local c=function(k)
  131.                                 local c=(c[k] or 1)
  132.                                 return math.abs(c)<=1 and c or c/255
  133.                             end
  134.                             gl.glColor(0.1*c(1),0.1*c(2),0.1*c(3),1)-- Faint Green
  135.                         else
  136.                             gl.glColor(0,0.1,0,1)-- Faint Green
  137.                         end
  138.                         gl.glVertex(x,y)
  139.                         --local dx,dy=x-mx,y-my-- mouse
  140.                         --local dx,dy=x-rx,y-ry-- clock
  141.                         local t=OldMouse.t
  142.                         local dx,dy=x-mx*t-rx*(1-t),y-my*t-ry*(1-t)--both mouse and clock
  143.                         local d=math.sqrt(dx^2+dy^2)
  144.                         dx,dy=vsy/3*dx/d,vsy/3*dy/d
  145.                         gl.glColor(0,0,0,1)-- Black
  146.                         gl.glVertex(x+dx,y+dy)
  147.                     end
  148.                 end)
  149.             end
  150.         end
  151.     end
  152.     gl.glColor(1,1,1,1)
  153.     gl.glDisable(gl.GL_BLEND)
  154. end
  155.  
  156. local function DrawTitle()
  157.     ScribbleFancy(Kernel_Panic_Logo,vsx*0.5,vsy*0.5,vsx*0.7)
  158. end
  159.  
  160. local function DrawInfo()
  161.     Font=Font or gl.glLoadFont("FreeSansBold.otf", 50, 10, 5)
  162.     Font:Print("Hello World!", vsx*0.5, vsy*0.2, vsx/40, "cvo")
  163. end
  164.  
  165. local function Toss()
  166.     return math.random()>0.5 and 1 or 0
  167. end
  168.  
  169. local function InitializeCells()
  170.     if not(vsx and vsy) then
  171.         return
  172.     end
  173.     local nct=3*math.sqrt(vsy)
  174.     -- nct is the number of circuit tracks vertically.
  175.     -- I made it neither constant nor proportionnal to screen size, but inbetween
  176.     u=2+math.ceil(nct*vsx/math.min(vsx,vsy))
  177.     v=2+math.ceil(nct*vsy/math.min(vsx,vsy))
  178.     if #Circuits<=2 then
  179.         Lifetime=math.ceil(math.min(u,v)*3/2)
  180.         -- Betterize Random
  181.         for k=1,80+os.time()%200 do
  182.             math.random()
  183.         end
  184.         local a={}
  185.         for x=1,u do
  186.             a[x]={}
  187.             for y=1,v+1 do
  188.                 a[x][y]=Toss()
  189.             end
  190.         end
  191.         return a
  192.     else
  193.         -- Initialize cells at 0
  194.         local a={}
  195.         for x=1,u do
  196.             a[x]={}
  197.             for y=1,v+1 do
  198.                 a[x][y]=0
  199.             end
  200.         end
  201.         for b=0,(#Circuits==3 and 2 or 0) do-- with b=0,2 it's squarer
  202.             -- Initialize border cells at random
  203.             for x=1,u do
  204.                 a[x][1+b]=Toss()
  205.                 a[x][v-b]=Toss()
  206.             end
  207.             for y=1,v do
  208.                 a[1+b][y]=Toss()
  209.                 a[u-b][y]=Toss()
  210.             end
  211.         end
  212.         return a
  213.     end
  214. end
  215.  
  216. local function IterateCells(a)
  217.     local b={}
  218.     -- Recopy half of the border cells (frozen)
  219.     for x=1,u do
  220.         b[x]={}
  221.         b[x][1]=a[x][1]
  222.     end
  223.     for y=1,v do
  224.         b[1][y]=a[1][y]
  225.     end
  226.     -- Make non border cells evolve
  227.     for x=2,u-1 do
  228.         for y=2,v-1 do
  229.             local n=a[x+1][y+1]+
  230.                     a[x+1][y+0]+
  231.                     a[x+1][y-1]+
  232.                     a[x-1][y+1]+
  233.                     a[x-1][y+0]+
  234.                     a[x-1][y-1]+
  235.                     a[x+0][y+1]+
  236.                     a[x+0][y-1]
  237.             if n==3 then
  238.                 b[x][y]=1--Birth
  239.             elseif n<=4 then
  240.                 b[x][y]=a[x][y]--Survival
  241.             else
  242.                 b[x][y]=0--Death
  243.             end
  244.         end
  245.     end
  246.     -- Recopy other half of the border cells (frozen)
  247.     for x=1,u do
  248.         b[x][v]=a[x][v]
  249.     end
  250.     for y=1,v do
  251.         b[u][y]=a[u][y]
  252.     end
  253.     return b
  254. end
  255.  
  256.  
  257.  
  258. local function KillFourCells(a)
  259.     -- Copy a to b
  260.     local b={}
  261.     for x=1,u do
  262.         b[x]={}
  263.         for y=1,v do
  264.             b[x][y]=a[x][y]
  265.         end
  266.     end
  267.     -- Remove squares
  268.     for x=3,u-1 do
  269.         for y=3,v-1 do
  270.             if a[x][y]+a[x-1][y]+a[x][y-1]+a[x-1][y-1]==4 then
  271.                 local hasKilled=false
  272.                 for i=x-1,x do
  273.                     for j=y-1,y do
  274.                         if a[i-1][j]+a[i+1][j]+a[i][j-1]+a[i][j+1]==3 then
  275.                             b[i][j]=0
  276.                             hasKilled=true
  277.                         end
  278.                     end
  279.                 end
  280.                 if not hasKilled then
  281.                     b[x][y]=0
  282.                 end
  283.             end
  284.         end
  285.     end
  286.     -- Remove diamonds
  287.     for x=2,u-1 do
  288.         for y=2,v-1 do
  289.             if a[x-1][y]+a[x+1][y]+a[x][y-1]+a[x][y+1]==4 and a[x][y]==0 then
  290.                 b[x][y]=1
  291.             end
  292.         end
  293.     end
  294.     return b
  295. end
  296.  
  297.  
  298. local function DrawCircuit(A,C,T,dx,dy)
  299.     local dx,dy=-1-dx,-1-dy
  300.  
  301.     --gl.glTexture(false)
  302.     gl.glColor(T*C[1],T*C[2],T*C[3],1)
  303.     gl.glLineWidth(1)
  304.  
  305.     local s=(vsx/(u-3)+vsy/(v-3))/2--Scale: distance between tracks
  306.  
  307.     local vias={}
  308.  
  309.     BeginEnd(gl.GL_LINES,function()
  310.  
  311.         -- Draw tracks
  312.         for i=2,u-1 do
  313.             for j=2,v-1 do
  314.                 if A[i][j]~=0 then
  315.                     local line=function(i2,j2)
  316.                         gl.glVertex((i+dx)*s,(j+dy)*s)
  317.                         gl.glVertex((i2+dx)*s,(j2+dy)*s)
  318.                     end
  319.  
  320.                     -- Draw horizontal & vertical tracks
  321.                     if A[i-1][j]~=0 then
  322.                         line(i-1,j)
  323.                     end
  324.                     if A[i+1][j]~=0 then
  325.                         line(i+1,j)
  326.                     end
  327.                     if A[i][j-1]~=0 then
  328.                         line(i,j-1)
  329.                     end
  330.                     if A[i][j+1]~=0 then
  331.                         line(i,j+1)
  332.                     end
  333.  
  334.                     -- Draw diagonal tracks
  335.                     if A[i-1][j-1]~=0 and not (A[i-1][j]~=0 or A[i][j-1]~=0) then
  336.                         line(i-1,j-1)
  337.                     end
  338.                     if A[i-1][j+1]~=0 and not (A[i-1][j]~=0 or A[i][j+1]~=0) then
  339.                         line(i-1,j+1)
  340.                     end
  341.                     if A[i+1][j-1]~=0 and not (A[i+1][j]~=0 or A[i][j-1]~=0) then
  342.                         line(i+1,j-1)
  343.                     end
  344.                     if A[i+1][j+1]~=0 and not (A[i+1][j]~=0 or A[i][j+1]~=0) then
  345.                         line(i+1,j+1)
  346.                     end
  347.  
  348.                 end
  349.             end
  350.         end
  351.  
  352.         -- Draw vias
  353.         local circle=function(i,j)
  354.             local x,y,r=(i+dx)*s,(j+dy)*s,s/3
  355.             gl.glVertex(x+r*math.cos(0),y+r*math.sin(0))
  356.             for a=0,6.2,math.pi/8 do
  357.                 gl.glVertex(x+r*math.cos(a),y+r*math.sin(a))
  358.                 gl.glVertex(x+r*math.cos(a),y+r*math.sin(a))
  359.             end
  360.             gl.glVertex(x+r*math.cos(0),y+r*math.sin(0))
  361.         end
  362.         for i=2,u-1 do
  363.             for j=2,v-1 do
  364.                 if A[i][j]~=0 then
  365.                     if A[i-1][j]+A[i-1][j-1]+A[i-1][j+1]+A[i][j-1]+A[i][j+1]==0
  366.                     or A[i+1][j]+A[i+1][j-1]+A[i+1][j+1]+A[i][j-1]+A[i][j+1]==0
  367.                     or A[i][j-1]+A[i-1][j-1]+A[i+1][j-1]+A[i-1][j]+A[i+1][j]==0
  368.                     or A[i][j+1]+A[i-1][j+1]+A[i+1][j+1]+A[i-1][j]+A[i+1][j]==0 then
  369.                         circle(i,j)
  370.                         vias[1+#vias]={i,j}
  371.                     end
  372.                 end
  373.             end
  374.         end
  375.  
  376.     end)
  377.  
  378.     -- Drill vias
  379.     gl.glColor(0,0,0,1)
  380.     for _,via in ipairs(vias) do
  381.         BeginEnd(gl.GL_TRIANGLE_FAN,function()
  382.             local x,y,r=(via[1]+dx)*s,(via[2]+dy)*s,s/3-1
  383.             gl.glVertex(x,y)
  384.             for a=0,6.3,math.pi/8 do
  385.                 gl.glVertex(x+r*math.cos(a),y+r*math.sin(a))
  386.             end
  387.         end)
  388.     end
  389.  
  390.     gl.glLineWidth(1)
  391.     gl.glColor(1,1,1,1)
  392.  
  393. end
  394.  
  395.  
  396. local function GetNewCircuit()
  397.     local r=math.random()
  398.     local dice=math.random(1,3)
  399.     local c
  400.     if dice==1 then
  401.         c={r,1,0,1}-- Apple green
  402.     elseif dice==2 then
  403.         c={1,r,0,1}-- Orange
  404.     else
  405.         c={1,0,1,1}-- Pink
  406.     end
  407.     return {Matrix=InitializeCells(),Color=c,Age=0,dx=math.random(),dy=math.random()}
  408. end
  409.  
  410.  
  411. local function GrowCircuit()
  412.  
  413.     if not Circuits then
  414.         local N=4
  415.         tick=0
  416.         Circuits={}
  417.         for k=1,N do
  418.             Circuits[k]=GetNewCircuit()
  419.             Circuits[k].Age=Lifetime*2*(1+N-k)/N
  420.         end
  421.     end
  422.  
  423.     -- Next lattice iteration
  424.     if 10*os.clock()>tick then
  425.         tick=tick+1
  426.         local circuit=Circuits[1+tick%#Circuits]
  427.         circuit.Matrix=IterateCells(circuit.Matrix)
  428.         circuit.Age=circuit.Age+1
  429.         if circuit.DisplayList then
  430.             gl.glDeleteLists(circuit.DisplayList,1)
  431.         end
  432.         circuit.DisplayList=CreateList(DrawCircuit,KillFourCells(KillFourCells(circuit.Matrix)),
  433.         circuit.Color,(1-circuit.Age/(2*Lifetime))/2,circuit.dx,circuit.dy)
  434.     end
  435.  
  436.     -- Draw circuits
  437.     for _,circuit in ipairs(Circuits) do
  438.         if circuit.DisplayList then
  439.             gl.glCallList(circuit.DisplayList)
  440.         end
  441.     end
  442.  
  443.     -- Death of old circuit
  444.     if Circuits[1].Age>2*Lifetime then
  445.         gl.glDeleteLists(Circuits[1].DisplayList,1)
  446.         table.insert(Circuits,1+#Circuits,GetNewCircuit())
  447.         table.remove(Circuits,1)
  448.     end
  449.  
  450. end
  451.  
  452. local function PrintContent()
  453.     local a={}
  454.     for k,v in pairs(gl) do
  455.         local t=type(v)
  456.         a[t]=a[t] or {}
  457.         table.insert(a[t],k)
  458.     end
  459.     for t,b in pairs(a) do
  460.         print("")
  461.         print("["..t:upper().."]")
  462.         table.sort(b)
  463.         for _,n in ipairs(b) do
  464.             print(n)
  465.         end
  466.     end
  467. end
  468.  
  469. function glush.display(a,b)
  470.     --PrintContent()
  471.     --glush.exit()
  472.     if vsx and vsy then
  473.         gl.glMatrixMode(gl.GL_PROJECTION)
  474.         gl.glLoadIdentity()
  475.         gl.glOrtho(0,vsx,0,vsy,0,1)
  476.         gl.glMatrixMode(gl.GL_MODELVIEW)
  477.         gl.glLoadIdentity()
  478.         -- Now 0,0 is bottom left and vsx,vsy is top right
  479.  
  480.         --gl.glPushAttrib(gl.glALL_ATTRIB_BITS)
  481.         --gl.glDepthTest(false)
  482.         --gl.glDepthMask(false)
  483.         --gl.glBlending(false)
  484.         --gl.glTexture(false)
  485.         gl.glDisable(gl.GL_DEPTH_TEST)
  486.         gl.glDisable(gl.GL_BLEND)
  487.         gl.glClear(GL_COLOR_BUFFER_BIT)
  488.  
  489.         gl.glColor(0,0,0,1)
  490.         gl.glRect(0,0,vsx,vsy)
  491.         GrowCircuit()
  492.         DrawTitle()
  493.         --DrawInfo()
  494.         --gl.glPopAttrib()
  495.     else
  496.         vsx, vsy = glush.dispSize()
  497.     end
  498. end
  499.  
  500.  
  501. function Shutdown()-- Unused
  502.     if DisplayList then
  503.         gl.glDeleteLists(DisplayList,1)
  504.         DisplayList=nil
  505.     end
  506.     if Font then
  507.         --gl.glDeleteFont(Font)
  508.         --Font=nil
  509.     end
  510.     if LogoDL then
  511.         gl.glDeleteLists(LogoDL,1)
  512.         LogoDL=nil
  513.     end
  514. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement