Guest User

Untitled

a guest
May 21st, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.91 KB | None | 0 0
  1. --Parse ASS file
  2. package.path = package.path .. ";E:\\PROJEKTE\\FLuaG\\examples\\advanced\\karaoke\\?.lua"
  3. require("assparser")("[Hatsune Miku] Monster universe [Original PV].ass")
  4.  
  5. --Load textures
  6. local flying_tex = glGenTextures(3)
  7. local silhouette_tex = glGenTextures(5)
  8. local face_tex = glGenTextures(1)
  9. do
  10.     local image
  11.     for i=1, 3 do
  12.         image = flLoadPNG("textures\\flying" .. i .. ".png")
  13.         glBindTexture(GL_TEXTURE_2D, flying_tex[i])
  14.         glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
  15.         glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
  16.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width, image.height, 0, GL_RGBA, image)
  17.     end
  18.     for i=1, 5 do
  19.         image = flLoadPNG("textures\\silhouette" .. i .. ".png")
  20.         glBindTexture(GL_TEXTURE_2D, silhouette_tex[i])
  21.         glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
  22.         glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
  23.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width, image.height, 0, GL_RGBA, image)
  24.     end
  25.     image = flLoadPNG("textures\\face.png")
  26.     glBindTexture(GL_TEXTURE_2D, face_tex[1])
  27.     glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
  28.     glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
  29.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width, image.height, 0, GL_RGBA, image)
  30. end
  31.  
  32. --Text drawing list
  33. local textlist = glGenLists(1)
  34.  
  35. --Culling for performance
  36. glEnable(GL_CULL_FACE)
  37. glFrontFace(GL_CW)
  38.  
  39. --Light
  40. glLightModel(GL_LIGHT_MODEL_AMBIENT, {0, 0, 0, 1})
  41. glLight(GL_LIGHT0, GL_AMBIENT, {0, 0, 0, 1})
  42. glLight(GL_LIGHT0, GL_DIFFUSE, {1, 1, 1, 1})
  43. glLight(GL_LIGHT0, GL_POSITION, {VIDEO_WIDTH/2, VIDEO_HEIGHT/2, 1500, 1})
  44. glLight(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.8)
  45. glEnable(GL_LIGHT0)
  46. glEnable(GL_COLOR_MATERIAL)
  47. glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
  48.  
  49. --Clear value
  50. glClearDepth(1)
  51. glDepthFunc(GL_LEQUAL)
  52.  
  53. --Help functions
  54. local function myView(width, height, near, far, zoom)
  55.     glMultMatrix({
  56.         2/width, 0, 0, 0,
  57.         0, -2/height, 0, 0,
  58.         0, 0, -2/(far-near), -1/zoom,
  59.         -1, 1, -(far+near)/(far-near), 1
  60.     })
  61. end
  62.  
  63. local function text_render(text, font, text_depth)
  64.     local text_path = font:getpath(text)
  65.     glNormal(0, 0, 1)
  66.     glNewList(textlist, GL_COMPILE_AND_EXECUTE)
  67.     gluDrawPath( text_path )
  68.     glEndList()
  69.     for i, p in ipairs(text_path) do
  70.         if p.type == GLU_MOVE then glBegin(GL_TRIANGLE_STRIP) end
  71.         local nx, ny, nz
  72.         if i == #text_path then
  73.             nx, ny, nz = math.calcnormal(p.x, p.y, 0, p.x, p.y, -text_depth, text_path[i-1].x, text_path[i-1].y, 0)
  74.         else
  75.             nx, ny, nz = math.calcnormal(p.x, p.y, 0, p.x, p.y, -text_depth, text_path[i+1].x, text_path[i+1].y, 0)
  76.         end
  77.         glNormal(nx, ny, nz)
  78.         --Front
  79.         glVertex(p.x, p.y, 0)
  80.         --Back
  81.         glVertex(p.x, p.y, -text_depth)
  82.         if p.type == GLU_ENDLINE then glEnd() end
  83.     end
  84.     glTranslate(0, 0, -text_depth)
  85.     glNormal(0, 0, -1)
  86.     glFrontFace(GL_CCW)
  87.     glCallList(textlist)
  88.     glFrontFace(GL_CW)
  89. end
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102. --Karaoke rendering
  103. local function kara_render(line, fi, ms)
  104.     --Infade
  105.     if ms < line.start_time then
  106.         local pct = 1 - (line.start_time - ms) / (line.infade/2)
  107.         local x_offset = line.height*(875/580)
  108.         glBindTexture(GL_TEXTURE_2D, silhouette_tex[ math.mod(fi, 5)+1 ])
  109.         glEnable(GL_TEXTURE_2D)
  110.         glColor(1, 1, 1)
  111.         glEnable(GL_BLEND)
  112.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  113.         if line.styleref.alignment > 6 then
  114.             glPushMatrix()
  115.             glTranslate(pct * (VIDEO_WIDTH + x_offset), 0, 0)
  116.             glBegin(GL_QUADS)
  117.                 glTexCoord(1, 0); glVertex(-x_offset, line.top)
  118.                 glTexCoord(0, 0); glVertex(0, line.top)
  119.                 glTexCoord(0, 1); glVertex(0, line.bottom)
  120.                 glTexCoord(1, 1); glVertex(-x_offset, line.bottom)
  121.             glEnd()
  122.             glPopMatrix()
  123.         elseif line.styleref.alignment < 4 then
  124.             glPushMatrix()
  125.             glTranslate((1-pct) * (VIDEO_WIDTH + x_offset), 0, 0)
  126.             glBegin(GL_QUADS)
  127.                 glTexCoord(0, 0); glVertex(-x_offset, line.top)
  128.                 glTexCoord(1, 0); glVertex(0, line.top)
  129.                 glTexCoord(1, 1); glVertex(0, line.bottom)
  130.                 glTexCoord(0, 1); glVertex(-x_offset, line.bottom)
  131.             glEnd()
  132.             glPopMatrix()
  133.         end
  134.         glDisable(GL_BLEND)
  135.         glDisable(GL_TEXTURE_2D)
  136.     --Outfade
  137.     elseif ms > line.end_time then
  138.         local pct = 1 - (line.end_time + line.outfade/2 - ms) / (line.outfade/2)
  139.         local x_offset = line.height*1.3
  140.         glBindTexture(GL_TEXTURE_2D, flying_tex[ math.mod(fi, 3)+1 ])
  141.         glEnable(GL_TEXTURE_2D)
  142.         glColor(1, 1, 1)
  143.         glEnable(GL_BLEND)
  144.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  145.         if line.styleref.alignment > 6 then
  146.             glPushMatrix()
  147.             glTranslate(pct * (VIDEO_WIDTH + x_offset), -(line.top + line.height) * (1 - pct ^ 0.25), 0)
  148.             glBegin(GL_QUADS)
  149.                 glTexCoord(0, 0); glVertex(-x_offset, line.top)
  150.                 glTexCoord(1, 0); glVertex(0, line.top)
  151.                 glTexCoord(1, 1); glVertex(0, line.bottom)
  152.                 glTexCoord(0, 1); glVertex(-x_offset, line.bottom)
  153.             glEnd()
  154.             glPopMatrix()
  155.         elseif line.styleref.alignment < 4 then
  156.             glPushMatrix()
  157.             glTranslate((1-pct) * (VIDEO_WIDTH + x_offset), (VIDEO_HEIGHT-line.top) * (1 - pct ^ 0.25), 0)
  158.             glBegin(GL_QUADS)
  159.                 glTexCoord(1, 0); glVertex(-x_offset, line.top)
  160.                 glTexCoord(0, 0); glVertex(0, line.top)
  161.                 glTexCoord(0, 1); glVertex(0, line.bottom)
  162.                 glTexCoord(1, 1); glVertex(-x_offset, line.bottom)
  163.             glEnd()
  164.             glPopMatrix()
  165.         end
  166.         glDisable(GL_BLEND)
  167.         glDisable(GL_TEXTURE_2D)
  168.     --Base
  169.     else
  170.         for si, syl in ipairs(line.syls) do
  171.             --Text properties
  172.             glEnable(GL_LIGHTING)
  173.             glColor(1, 1, 1)
  174.             glPushMatrix()
  175.             glTranslate(syl.x, line.y, 0)
  176.             --Effect
  177.             if ms >= line.start_time + syl.start_time and ms < line.start_time + syl.end_time then
  178.                 local pct = (ms-syl.start_time-line.start_time)/syl.duration
  179.                 glColor(1, 0, 0)
  180.                 glTranslate(syl.width/2, syl.height/2, -10)
  181.                 glRotate(math.random(-5,5), 1, 0, 0)
  182.                 glRotate(math.random(-5,5), 0, 1, 0)
  183.                 glTranslate(-syl.width/2, -syl.height/2, 10)
  184.             end
  185.             --Text drawing
  186.             text_render(syl.text, line.styleref.font, 20)
  187.             glPopMatrix()
  188.             glDisable(GL_LIGHTING)
  189.         end
  190.     end
  191. end
  192.  
  193. --Karaoke initialization
  194. local function karaoke(fi, ms)
  195.     glMatrixMode(GL_PROJECTION)
  196.     glLoadIdentity()
  197.     myView(VIDEO_WIDTH, VIDEO_HEIGHT, -1000, 1000, 2000)
  198.     glMatrixMode(GL_MODELVIEW)
  199.     glLoadIdentity()
  200.     glClear(GL_DEPTH_BUFFER_BIT)
  201.     glEnable(GL_DEPTH_TEST)
  202.     for li, line in ipairs(lines) do
  203.         if line.start_time - line.infade/2 <= ms and line.end_time + line.outfade/2 > ms then
  204.             kara_render(line, fi, ms)
  205.         end
  206.     end
  207.     glDisable(GL_DEPTH_TEST)
  208. end
  209.  
  210. --Deletes original kanjis at top
  211. local o_red, o_green, o_blue, o_height = 88/255, 98/255, 76/255, 1-2*(130/1080)
  212. local function grey_overlay()
  213.     glMatrixMode(GL_PROJECTION)
  214.     glLoadIdentity()
  215.     glMatrixMode(GL_MODELVIEW)
  216.     glLoadIdentity()
  217.     glColor(o_red, o_green, o_blue)
  218.     glRect(-1, 1, 1, o_height)
  219. end
  220.  
  221. --Main loop
  222. function GetFrame(frame_index, frame_ms)
  223.     LoadFrameToContext()
  224.  
  225.     grey_overlay()
  226.     karaoke(frame_index, frame_ms)
  227.  
  228.     SaveFrameFromContext()
  229. end
Add Comment
Please, Sign In to add comment